From 3002d924939573344f8ca8f8bc0ce0ce308b60ff Mon Sep 17 00:00:00 2001
From: Matt Taggart <taggart@riseup.net>
Date: Mon, 17 Jun 2019 18:05:07 -0700
Subject: [PATCH] add cron mode for bulk detect that autocloses lists and gives
 us a report

---
 tools/bulkdetect | 51 ++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 45 insertions(+), 6 deletions(-)

diff --git a/tools/bulkdetect b/tools/bulkdetect
index f86d13c..536f361 100755
--- a/tools/bulkdetect
+++ b/tools/bulkdetect
@@ -3,9 +3,27 @@
 # taggart 2019-06
 
 $log='/var/log/sympa/wwsympa.log';
+$close = 5000;
+$warn = 1000;
+
+# if -c flag, run as a cronjob otherwise report only
+if ( $ARGV[0] eq '-c' ) {
+  $cron = 1;
+}
 
 open(LOG, "$log") or die "cannot open log\n";
 
+# load list status into hash
+$sql=qq(mysql -N --batch --database=sympa --execute "select name_list, status_list from list_table;");
+
+open(SQL, "$sql|") or die "cannot get dump of bouncers\n";
+
+while (<SQL>) {
+   chomp;
+   ($name,$status) = split(/\t/);
+   $lists{$name} = $status;
+}
+
 while (<LOG>) {
   chomp;
   if ( m/.*\[user (.*)\] \[list (.*)\] main::do_add\(\) do_add:(.+)/ ) {
@@ -16,23 +34,44 @@ while (<LOG>) {
 }
 
 format Foo =
-@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @>>>>>>
-$owner,                                         $list                                       $count
+@>>>>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  @<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+$count, $list,                                          $status,$owner
 .
 
 # set output format
 $~= 'Foo';
 
 # print header
-$owner = 'Owner adding';
-$list = 'List';
 $count = 'Add';
-write;
+$list = 'List';
+$status = 'Status';
+$owner = 'Owner adding';
+
+if (! $cron ) {
+  write;
+}
 
 foreach $key ( sort { $result{$b} <=> $result{$a} } keys %result) {
   ($owner,$list) = split /:/,$key;
   $count = $result{$key}; 
-  write;
+  $status = $lists{$list};
+ 
+  if ( $cron ) {
+    if ( $status eq 'open' ) {
+      if ( $result{$key} > $close ) {
+        # close
+        print "$list is $count, autoclosing\n";
+        `sympa.pl --close_list=$list\@lists.riseup.net` or die "cannot close $list\n";
+      } elsif ($result{$key} > $warn ) {
+        # warn
+        print "$list is $count, send them a warning\n";
+      } else {
+        last;
+      }
+    }
+  } else {
+    write;
+  }
 }
 
 ## Notes
-- 
GitLab