Browse Source

Report downtime in restore mail

Bernhard Fröhlich 10 months ago
parent
commit
24857bb9d7
1 changed files with 8 additions and 4 deletions
  1. 8
    4
      lib/PhpMonitor/Daemon.php

+ 8
- 4
lib/PhpMonitor/Daemon.php View File

@@ -20,6 +20,7 @@ class Daemon extends \Core_Daemon
20 20
 
21 21
         foreach($this->checks as $key => $value)
22 22
         {
23
+            $this->checks[$key]['downsince'] = time();
23 24
             $this->checks[$key]['failures'] = 0;
24 25
             $this->checks[$key]['status'] = 'unknown';
25 26
         }
@@ -51,6 +52,7 @@ class Daemon extends \Core_Daemon
51 52
             if($this->checks[$checkname]['status'] == 'pending' && $this->checks[$checkname]['failures'] > Config::get('maxfailures', 3))
52 53
             {
53 54
                 $this->checks[$checkname]['status'] = 'down';
55
+                $this->checks[$checkname]['downsince'] = time();
54 56
                 $this->sendFailureNotification($checkname);
55 57
             }
56 58
 
@@ -63,6 +65,7 @@ class Daemon extends \Core_Daemon
63 65
 
64 66
             $this->checks[$checkname]['status'] = 'up';
65 67
             $this->checks[$checkname]['failures'] = 0;
68
+            $this->checks[$checkname]['downsince'] = 0;
66 69
             $this->log(sprintf('check %s (%s) success (%d ms)', $checkname, $url, $check->getTime()));
67 70
         }
68 71
     }
@@ -118,7 +121,7 @@ class Daemon extends \Core_Daemon
118 121
             $mail->addAddress($addr['address'], $addr['name']);
119 122
 
120 123
         $mail->Subject = 'Check '.$checkname.' failed';
121
-        $mail->Body = sprintf("Check: %s\nURL: %s\nDate: %s\nStatus: %s:\nFailures: %s\n",
124
+        $mail->Body = sprintf("Check: %s\nURL: %s\nDate: %s\nStatus: %s\nFailures: %s\n",
122 125
             $checkname, $this->checks[$checkname]['url'], date(DATE_RFC850),
123 126
             $this->checks[$checkname]['status'], $this->checks[$checkname]['failures']);
124 127
 
@@ -127,7 +130,8 @@ class Daemon extends \Core_Daemon
127 130
 
128 131
     protected function sendRestoredNotification($checkname)
129 132
     {
130
-        $this->log('Send restore notification for check '.$checkname);
133
+        $downtime = time()-$this->checks[$checkname]['downsince'];
134
+        $this->log('Send restore notification for check '.$checkname.' (downtime '.$downtime.' seconds)');
131 135
 
132 136
         $mail = new PHPMailer(true);
133 137
         $mail->isSMTP();
@@ -148,9 +152,9 @@ class Daemon extends \Core_Daemon
148 152
             $mail->addAddress($addr['address'], $addr['name']);
149 153
 
150 154
         $mail->Subject = 'Restored check '.$checkname;
151
-        $mail->Body = sprintf("Check: %s\nURL: %s\nDate: %s\nStatus: %s:\n",
155
+        $mail->Body = sprintf("Check: %s\nURL: %s\nDate: %s\nStatus: %s\nDowntime: %d min %d sec\n",
152 156
             $checkname, $this->checks[$checkname]['url'], date(DATE_RFC850),
153
-            $this->checks[$checkname]['status']);
157
+            $this->checks[$checkname]['status'], $downtime / 60, $downtime % 60);
154 158
 
155 159
         return $mail->send();
156 160
     }

Loading…
Cancel
Save