aboutsummaryrefslogtreecommitdiffstats
path: root/security/swatchdog/files/patch-lib_Swatchdog_Throttle.pm
blob: d7a29fba98b571983ccc2509197639bde6f2a1ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
--- lib/Swatchdog/Throttle.pm.orig	2015-09-29 16:39:26 UTC
+++ lib/Swatchdog/Throttle.pm
@@ -95,6 +95,7 @@ sub throttle {
 	      @_
 	     );
 
+  my @delay = split(/:/, "0:$opts{DELAY}");
   my @dmyhms;
   my $key;
   my $cur_rec;
@@ -134,30 +135,61 @@ sub throttle {
     $rec->{FIRST} = [ @dmyhms ];
     $rec->{LAST} = [ @dmyhms ];
     $rec->{HOLD_DHMS} = $opts{HOLD_DHMS} if defined $opts{HOLD_DHMS};
-    $rec->{COUNT} = 1;
+    $rec->{COUNT} = 0;
     $LogRecords{$key} = $rec;
-    return $msg;
-  } else {
-    $cur_rec = $LogRecords{$key};
-    $cur_rec->{COUNT}++;
-    if (defined $opts{THRESHOLD} and $cur_rec->{COUNT} == $opts{THRESHOLD}) {
-      ## threshold exceeded ##
-      chomp $msg;
-      $msg = "$msg (threshold $opts{THRESHOLD} exceeded)";
-      $cur_rec->{COUNT} = 0;
-    } elsif (defined $opts{HOLD_DHMS} 
-	     and past_hold_time($cur_rec->{LAST},
-				\@dmyhms, $opts{HOLD_DHMS})) {
+  }
+
+  ## Get current record ##
+  $cur_rec = $LogRecords{$key};
+  $cur_rec->{COUNT}++;
+
+  ## delay only ##
+  if( defined $opts{DELAY} and not defined $opts{THRESHOLD} ) {
+    if( past_hold_time($cur_rec->{LAST}, [ @dmyhms ], [ @delay ]) ) {
       ## hold time exceeded ##
       chomp $msg;
       $msg = "$msg (seen $cur_rec->{COUNT} times)";
-      $cur_rec->{COUNT} = 0;
+      $cur_rec->{COUNT} = 1;
       $cur_rec->{LAST} = [ @dmyhms ];
     } else {
       $msg = '';
     }
-    $LogRecords{$key} = $cur_rec if exists($LogRecords{$key});  ## save any new values ##
+
+  ## threshold only ##
+  } elsif( defined $opts{THRESHOLD} and not defined $opts{DELAY} ) {
+    if( $cur_rec->{COUNT} == $opts{THRESHOLD}) {
+      ## threshold exceeded ##
+      chomp $msg;
+      $msg = "$msg (threshold $opts{THRESHOLD} exceeded)";
+      $cur_rec->{COUNT} = 0;
+    } else {
+      $msg = '';
+    }
+
+  ## threshold AND delay ##
+  } elsif( defined $opts{THRESHOLD} and defined $opts{DELAY} ) {
+    if( not past_hold_time($cur_rec->{LAST}, [ @dmyhms ], [ @delay ]) ) {
+      if( $cur_rec->{COUNT} == $opts{THRESHOLD} ) {
+        ## threshold exceeded during delay ##
+       chomp $msg;
+       $msg = "$msg (threshold $opts{THRESHOLD} exceeded during delay $opts{DELAY})";
+
+       ## TODO: Tenir compte du parametre repeat ici ##
+       $cur_rec->{COUNT} = 0;
+       $cur_rec->{LAST} = [ @dmyhms ];
+      } else {
+        $msg = '';
+      }
+    } else {
+      $cur_rec->{COUNT} = 1;
+      $cur_rec->{LAST} = [ @dmyhms ];
+      $msg = '';
+    }
   }
+
+  ## save any new values ##
+  $LogRecords{$key} = $cur_rec if exists($LogRecords{$key});
+
   return $msg;
 }