Marius Burkard
2016-07-10 e1ceb050e19c7574bca146a8da7047ee4ff456b5
commit | author | age
dead5c 1 <?php
V 2
3 /*
996bad 4     Copyright (c) 2007-2011, Till Brehm, projektfarm Gmbh and Oliver Vogel www.muv.com
M 5     All rights reserved.
dead5c 6
996bad 7     Redistribution and use in source and binary forms, with or without modification,
M 8     are permitted provided that the following conditions are met:
dead5c 9
V 10  * Redistributions of source code must retain the above copyright notice,
996bad 11     this list of conditions and the following disclaimer.
dead5c 12  * Redistributions in binary form must reproduce the above copyright notice,
996bad 13     this list of conditions and the following disclaimer in the documentation
M 14     and/or other materials provided with the distribution.
dead5c 15  * Neither the name of ISPConfig nor the names of its contributors
996bad 16     may be used to endorse or promote products derived from this software without
M 17     specific prior written permission.
dead5c 18
996bad 19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
M 20     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22     IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23     INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27     NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
dead5c 29  */
V 30
31 class monitor_tools {
32
33     //** Get distribution identifier
34     //** IMPORTANT!
35     //   This is the same code as in install/lib/install.lib.php
36     //   So if you change it here, you also have to change it in there!
37     //   Please do not forget to remove the swriteln(); - lines here at this file
3b8b9e 38     function get_distname() {
dead5c 39
3b8b9e 40     $distname = '';
TB 41     $distver = '';
42     $distid = '';
43     $distbaseid = '';
dead5c 44
3b8b9e 45     //** Debian or Ubuntu
TB 46     if(file_exists('/etc/debian_version')) {
47         
48         // Check if this is Ubuntu and not Debian
49         if (strstr(trim(file_get_contents('/etc/issue')), 'Ubuntu') || (is_file('/etc/os-release') && stristr(file_get_contents('/etc/os-release'), 'Ubuntu'))) {
50             
51             $issue = file_get_contents('/etc/issue');
52             
53             // Use content of /etc/issue file
54             if(strstr($issue,'Ubuntu')) {
55                 if (strstr(trim($issue), 'LTS')) {
f35123 56                     $lts=" LTS";
T 57                 } else {
58                     $lts="";
59                 }
60
d281bb 61                 $distname = 'Ubuntu';
C 62                 $distid = 'debian40';
63                 $distbaseid = 'debian';
b1a6a5 64                 $ver = explode(' ', $issue);
d281bb 65                 $ver = array_filter($ver);
C 66                 $ver = next($ver);
b1a6a5 67                 $mainver = explode('.', $ver);
d281bb 68                 $mainver = array_filter($mainver);
C 69                 $mainver = current($mainver).'.'.next($mainver);
3b8b9e 70             // Use content of /etc/os-release file
TB 71             } else {
72                 $os_release = file_get_contents('/etc/os-release');
73                 if (strstr(trim($os_release), 'LTS')) {
74                     $lts = " LTS";
75                 } else {
76                     $lts = "";
d281bb 77                 }
3b8b9e 78                 
TB 79                 $distname = 'Ubuntu';
dead5c 80                 $distid = 'debian40';
V 81                 $distbaseid = 'debian';
3b8b9e 82
TB 83                 preg_match("/.*VERSION=\"(.*)\".*/ui", $os_release, $ver);
84                 $ver = str_replace("LTS", "", $ver[1]);
85                 $ver = explode(" ", $ver, 2);
86                 $ver = reset($ver);
87                 $mainver = $ver;
dead5c 88             }
3b8b9e 89             switch ($mainver){
TB 90             case "16.04":
91                 $relname = "(Xenial Xerus)";
92                 $distconfid = 'ubuntu1604';
93                 break;
94             case "15.10":
95                 $relname = "(Wily Werewolf)";
96                 break;
97             case "15.04":
98                 $relname = "(Vivid Vervet)";
99                 break;
100             case "14.10":
101                 $relname = "(Utopic Unicorn)";
102                 break;
103             case "14.04":
104                 $relname = "(Trusty Tahr)";
105                 break;
106             case "13.10":
107                 $relname = "(Saucy Salamander)";
108                 break;
109             case "13.04":
110                 $relname = "(Raring Ringtail)";
111                 break;
112             case "12.10":
113                 $relname = "(Quantal Quetzal)";
114                 break;
115             case "12.04":
116                 $relname = "(Precise Pangolin)";
117                 break;
118             case "11.10":
119                 $relname = "(Oneiric Ocelot)";
120                 break;
121             case "11.14":
122                 $relname = "(Natty Narwhal)";
123                 break;
124             case "10.10":
125                 $relname = "(Maverick Meerkat)";
126                 break;
127             case "10.04":
128                 $relname = "(Lucid Lynx)";
129                 break;
130             case "9.10":
131                 $relname = "(Karmic Koala)";
132                 break;
133             case "9.04":
134                 $relname = "(Jaunty Jackpole)";
135                 break;
136             case "8.10":
137                 $relname = "(Intrepid Ibex)";
138                 break;
139             case "8.04":
140                 $relname = "(Hardy Heron)";
141                 break;
142             case "7.10":
143                 $relname = "(Gutsy Gibbon)";
144                 break;
145             case "7.04":
146                 $relname = "(Feisty Fawn)";
147                 break;
148             case "6.10":
149                 $relname = "(Edgy Eft)";
150                 break;
151             case "6.06":
152                 $relname = "(Dapper Drake)";
153                 break;
154             case "5.10":
155                 $relname = "(Breezy Badger)";
156                 break;
157             case "5.04":
158                 $relname = "(Hoary Hedgehog)";
159                 break;
160             case "4.10":
161                 $relname = "(Warty Warthog)";
162                 break;
163             default:
164                 $relname = "UNKNOWN";
dead5c 165             }
3b8b9e 166             $distver = $ver.$lts." ".$relname;
TB 167         } elseif(trim(file_get_contents('/etc/debian_version')) == '4.0') {
168             $distname = 'Debian';
169             $distver = '4.0';
170             $distid = 'debian40';
171             $distbaseid = 'debian';
172         } elseif(strstr(trim(file_get_contents('/etc/debian_version')), '5.0')) {
173             $distname = 'Debian';
174             $distver = 'Lenny';
175             $distid = 'debian40';
176             $distbaseid = 'debian';
177         } elseif(strstr(trim(file_get_contents('/etc/debian_version')), '6.0') || trim(file_get_contents('/etc/debian_version')) == 'squeeze/sid') {
178             $distname = 'Debian';
179             $distver = 'Squeeze/Sid';
180             $distid = 'debian60';
181             $distbaseid = 'debian';
182         } elseif(strstr(trim(file_get_contents('/etc/debian_version')), '7.0') || substr(trim(file_get_contents('/etc/debian_version')),0,2) == '7.' || trim(file_get_contents('/etc/debian_version')) == 'wheezy/sid') {
183             $distname = 'Debian';
184             $distver = 'Wheezy/Sid';
185             $distid = 'debian60';
186             $distbaseid = 'debian';
187         } elseif(strstr(trim(file_get_contents('/etc/debian_version')), '8') || substr(trim(file_get_contents('/etc/debian_version')),0,1) == '8') {
188             $distname = 'Debian';
189             $distver = 'Jessie';
190             $distid = 'debian60';
191             $distbaseid = 'debian';
eab0cf 192         } elseif(strstr(trim(file_get_contents('/etc/debian_version')), '/sid')) {
TB 193             $distname = 'Debian';
194             $distver = 'Testing';
195             $distid = 'debian60';
196             $distconfid = 'debiantesting';
197             $distbaseid = 'debian';
dead5c 198         } else {
3b8b9e 199             $distname = 'Debian';
TB 200             $distver = 'Unknown';
201             $distid = 'debian40';
202             $distbaseid = 'debian';
dead5c 203         }
3b8b9e 204     }
dead5c 205
3b8b9e 206     //** OpenSuSE
TB 207     elseif(file_exists('/etc/SuSE-release')) {
208         if(stristr(file_get_contents('/etc/SuSE-release'), '11.0')) {
209             $distname = 'openSUSE';
210             $distver = '11.0';
211             $distid = 'opensuse110';
212             $distbaseid = 'opensuse';
213         } elseif(stristr(file_get_contents('/etc/SuSE-release'), '11.1')) {
214             $distname = 'openSUSE';
215             $distver = '11.1';
216             $distid = 'opensuse110';
217             $distbaseid = 'opensuse';
218         } elseif(stristr(file_get_contents('/etc/SuSE-release'), '11.2')) {
219             $distname = 'openSUSE';
220             $distver = '11.2';
221             $distid = 'opensuse112';
222             $distbaseid = 'opensuse';
223         }  else {
224             $distname = 'openSUSE';
225             $distver = 'Unknown';
226             $distid = 'opensuse112';
227             $distbaseid = 'opensuse';
228         }
229     }
230
231
232     //** Redhat
233     elseif(file_exists('/etc/redhat-release')) {
234
235         $content = file_get_contents('/etc/redhat-release');
236
237         if(stristr($content, 'Fedora release 9 (Sulphur)')) {
238             $distname = 'Fedora';
239             $distver = '9';
240             $distid = 'fedora9';
241             $distbaseid = 'fedora';
242         } elseif(stristr($content, 'Fedora release 10 (Cambridge)')) {
243             $distname = 'Fedora';
244             $distver = '10';
245             $distid = 'fedora9';
246             $distbaseid = 'fedora';
247         } elseif(stristr($content, 'Fedora release 10')) {
248             $distname = 'Fedora';
249             $distver = '11';
250             $distid = 'fedora9';
251             $distbaseid = 'fedora';
252         } elseif(stristr($content, 'CentOS release 5.2 (Final)')) {
253             $distname = 'CentOS';
254             $distver = '5.2';
255             $distid = 'centos52';
256             $distbaseid = 'fedora';
257         } elseif(stristr($content, 'CentOS release 5.3 (Final)')) {
258             $distname = 'CentOS';
259             $distver = '5.3';
260             $distid = 'centos53';
261             $distbaseid = 'fedora';
262         } elseif(stristr($content, 'CentOS release 5')) {
263             $distname = 'CentOS';
264             $distver = 'Unknown';
265             $distid = 'centos53';
266             $distbaseid = 'fedora';
267         } elseif(stristr($content, 'CentOS Linux release 6')) {
268             $distname = 'CentOS';
269             $distver = 'Unknown';
270             $distid = 'centos53';
271             $distbaseid = 'fedora';
272         } elseif(stristr($content, 'CentOS Linux release 7.2')) {
273             $distname = 'CentOS';
274             $distver = 'Unknown';
275             $distid = 'centos70';
276             $distconfid = 'centos72';
277             $distbaseid = 'fedora';
278         } elseif(stristr($content, 'CentOS Linux release 7')) {
279             $distname = 'CentOS';
280             $distver = 'Unknown';
281             $distid = 'centos70';
282             $distbaseid = 'fedora';
283         } else {
284             $distname = 'Redhat';
285             $distver = 'Unknown';
286             $distid = 'fedora9';
287             $distbaseid = 'fedora';
288         }
289     }
290
291     //** Gentoo
292     elseif(file_exists('/etc/gentoo-release')) {
293
294         $content = file_get_contents('/etc/gentoo-release');
295
296         preg_match_all('/([0-9]{1,2})/', $content, $version);
297         $distname = 'Gentoo';
298         $distver = $version[0][0].$version[0][1];
299         $distid = 'gentoo';
300         $distbaseid = 'gentoo';
301
302     } else {
303         die('Unrecognized GNU/Linux distribution');
304     }
305     
306     // Set $distconfid to distid, if no different id for the config is defined
307     if(!isset($distconfid)) $distconfid = $distid;
308
309     return array('name' => $distname, 'version' => $distver, 'id' => $distid, 'confid' => $distconfid, 'baseid' => $distbaseid);
dead5c 310     }
V 311
b1a6a5 312     // this function remains in the tools class, because it is used by cron AND rescue
dead5c 313     public function monitorServices() {
V 314         global $app;
315         global $conf;
316
317         /** the id of the server as int */
b1a6a5 318
MC 319
dead5c 320         $server_id = intval($conf['server_id']);
V 321
eed6b3 322         /**  get the "active" Services of the server from the DB */
cc7a82 323         $services = $app->db->queryOneRecord('SELECT * FROM server WHERE server_id = ?', $server_id);
eed6b3 324         /*
V 325          * If the DB is down, we have to set the db to "yes".
326          * If we don't do this, then the monitor will NOT monitor, that the db is down and so the
327          * rescue-module can not try to rescue the db
328          */
329         if ($services == null) {
330             $services['db_server'] = 1;
331         }
dead5c 332
V 333         /* The type of the Monitor-data */
334         $type = 'services';
335
336         /** the State of the monitoring */
337         /* ok, if ALL active services are running,
338          * error, if not
339          * There is no other state!
340          */
341         $state = 'ok';
342
343         /* Monitor Webserver */
344         $data['webserver'] = -1; // unknown - not needed
345         if ($services['web_server'] == 1) {
346             if ($this->_checkTcp('localhost', 80)) {
347                 $data['webserver'] = 1;
348             } else {
349                 $data['webserver'] = 0;
350                 $state = 'error'; // because service is down
351             }
352         }
353
354         /* Monitor FTP-Server */
355         $data['ftpserver'] = -1; // unknown - not needed
356         if ($services['file_server'] == 1) {
357             if ($this->_checkFtp('localhost', 21)) {
358                 $data['ftpserver'] = 1;
359             } else {
360                 $data['ftpserver'] = 0;
361                 $state = 'error'; // because service is down
362             }
363         }
364
365         /* Monitor SMTP-Server */
366         $data['smtpserver'] = -1; // unknown - not needed
367         if ($services['mail_server'] == 1) {
368             if ($this->_checkTcp('localhost', 25)) {
369                 $data['smtpserver'] = 1;
370             } else {
371                 $data['smtpserver'] = 0;
372                 $state = 'error'; // because service is down
373             }
374         }
375
376         /* Monitor POP3-Server */
377         $data['pop3server'] = -1; // unknown - not needed
378         if ($services['mail_server'] == 1) {
379             if ($this->_checkTcp('localhost', 110)) {
380                 $data['pop3server'] = 1;
381             } else {
382                 $data['pop3server'] = 0;
383                 $state = 'error'; // because service is down
384             }
385         }
386
387         /* Monitor IMAP-Server */
388         $data['imapserver'] = -1; // unknown - not needed
389         if ($services['mail_server'] == 1) {
390             if ($this->_checkTcp('localhost', 143)) {
391                 $data['imapserver'] = 1;
392             } else {
393                 $data['imapserver'] = 0;
394                 $state = 'error'; // because service is down
395             }
396         }
397
398         /* Monitor BIND-Server */
399         $data['bindserver'] = -1; // unknown - not needed
400         if ($services['dns_server'] == 1) {
dfcd55 401             if ($this->_checkUdp('localhost', 53)) {
dead5c 402                 $data['bindserver'] = 1;
V 403             } else {
404                 $data['bindserver'] = 0;
405                 $state = 'error'; // because service is down
406             }
407         }
408
409         /* Monitor MySQL Server */
410         $data['mysqlserver'] = -1; // unknown - not needed
411         if ($services['db_server'] == 1) {
412             if ($this->_checkTcp('localhost', 3306)) {
413                 $data['mysqlserver'] = 1;
414             } else {
415                 $data['mysqlserver'] = 0;
416                 $state = 'error'; // because service is down
417             }
418         }
e7ae4e 419 /*
b1a6a5 420         $data['mongodbserver'] = -1;
MC 421         if ($this->_checkTcp('localhost', 27017)) {
422             $data['mongodbserver'] = 1;
423         } else {
424             $data['mongodbserver'] = 0;
e7ae4e 425 */
b1a6a5 426             //$state = 'error'; // because service is down
MC 427             /* TODO!!! check if this is a mongodbserver at all, otherwise it will always throw an error state!!! */
e7ae4e 428 //        }
def897 429
dead5c 430         /*
V 431          * Return the Result
432          */
433         $res['server_id'] = $server_id;
434         $res['type'] = $type;
435         $res['data'] = $data;
436         $res['state'] = $state;
437         return $res;
438     }
b1a6a5 439
dead5c 440     public function _getLogData($log) {
V 441         global $conf;
442
443         $dist = '';
444         $logfile = '';
445
446         if (@is_file('/etc/debian_version')) {
447             $dist = 'debian';
448         } elseif (@is_file('/etc/redhat-release')) {
449             $dist = 'redhat';
450         } elseif (@is_file('/etc/SuSE-release')) {
451             $dist = 'suse';
452         } elseif (@is_file('/etc/gentoo-release')) {
453             $dist = 'gentoo';
454         }
455
456         switch ($log) {
b1a6a5 457         case 'log_mail':
MC 458             if ($dist == 'debian') {
459                 $logfile = '/var/log/mail.log';
460             } elseif ($dist == 'redhat') {
461                 $logfile = '/var/log/maillog';
462             } elseif ($dist == 'suse') {
463                 $logfile = '/var/log/mail.info';
464             } elseif ($dist == 'gentoo') {
465                 $logfile = '/var/log/maillog';
466             }
467             break;
468         case 'log_mail_warn':
469             if ($dist == 'debian') {
470                 $logfile = '/var/log/mail.warn';
471             } elseif ($dist == 'redhat') {
472                 $logfile = '/var/log/maillog';
473             } elseif ($dist == 'suse') {
474                 $logfile = '/var/log/mail.warn';
475             } elseif ($dist == 'gentoo') {
476                 $logfile = '/var/log/maillog';
477             }
478             break;
479         case 'log_mail_err':
480             if ($dist == 'debian') {
481                 $logfile = '/var/log/mail.err';
482             } elseif ($dist == 'redhat') {
483                 $logfile = '/var/log/maillog';
484             } elseif ($dist == 'suse') {
485                 $logfile = '/var/log/mail.err';
486             } elseif ($dist == 'gentoo') {
487                 $logfile = '/var/log/maillog';
488             }
489             break;
490         case 'log_messages':
491             if ($dist == 'debian') {
492                 $logfile = '/var/log/syslog';
493             } elseif ($dist == 'redhat') {
494                 $logfile = '/var/log/messages';
495             } elseif ($dist == 'suse') {
496                 $logfile = '/var/log/messages';
497             } elseif ($dist == 'gentoo') {
498                 $logfile = '/var/log/messages';
499             }
500             break;
501         case 'log_ispc_cron':
502             if ($dist == 'debian') {
503                 $logfile = $conf['ispconfig_log_dir'] . '/cron.log';
504             } elseif ($dist == 'redhat') {
505                 $logfile = $conf['ispconfig_log_dir'] . '/cron.log';
506             } elseif ($dist == 'suse') {
507                 $logfile = $conf['ispconfig_log_dir'] . '/cron.log';
508             } elseif ($dist == 'gentoo') {
509                 $logfile = '/var/log/cron';
510             }
511             break;
512         case 'log_freshclam':
513             if ($dist == 'debian') {
514                 $logfile = '/var/log/clamav/freshclam.log';
515             } elseif ($dist == 'redhat') {
516                 $logfile = (is_file('/var/log/clamav/freshclam.log') ? '/var/log/clamav/freshclam.log' : '/var/log/freshclam.log');
517             } elseif ($dist == 'suse') {
518                 $logfile = '/var/log/freshclam.log';
519             } elseif ($dist == 'gentoo') {
520                 $logfile = '/var/log/clamav/freshclam.log';
521             }
522             break;
523         case 'log_clamav':
524             if ($dist == 'debian') {
525                 $logfile = '/var/log/clamav/clamav.log';
526             } elseif ($dist == 'redhat') {
527                 $logfile = (is_file('/var/log/clamav/clamd.log') ? '/var/log/clamav/clamd.log' : '/var/log/maillog');
528             } elseif ($dist == 'suse') {
529                 $logfile = '/var/log/clamd.log';
530             } elseif ($dist == 'gentoo') {
531                 $logfile = '/var/log/clamav/clamd.log';
532             }
533             break;
534         case 'log_fail2ban':
535             if ($dist == 'debian') {
536                 $logfile = '/var/log/fail2ban.log';
537             } elseif ($dist == 'redhat') {
538                 $logfile = '/var/log/fail2ban.log';
539             } elseif ($dist == 'suse') {
540                 $logfile = '/var/log/fail2ban.log';
541             } elseif ($dist == 'gentoo') {
542                 $logfile = '/var/log/fail2ban.log';
543             }
544             break;
545         case 'log_mongodb':
546             $logfile = '/var/log/mongodb/mongodb.log';
547             break;
548         case 'log_ispconfig':
549             if ($dist == 'debian') {
550                 $logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
551             } elseif ($dist == 'redhat') {
552                 $logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
553             } elseif ($dist == 'suse') {
554                 $logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
555             } elseif ($dist == 'gentoo') {
556                 $logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
557             }
558             break;
559         default:
560             $logfile = '';
561             break;
dead5c 562         }
V 563
564         // Getting the logfile content
565         if ($logfile != '') {
566             $logfile = escapeshellcmd($logfile);
567             if (stristr($logfile, ';') or substr($logfile, 0, 9) != '/var/log/' or stristr($logfile, '..')) {
568                 $log = 'Logfile path error.';
569             } else {
570                 $log = '';
571                 if (is_readable($logfile)) {
572                     $fd = popen('tail -n 100 ' . $logfile, 'r');
573                     if ($fd) {
574                         while (!feof($fd)) {
575                             $log .= fgets($fd, 4096);
576                             $n++;
577                             if ($n > 1000)
578                                 break;
579                         }
580                         fclose($fd);
581                     }
582                 } else {
583                     $log = 'Unable to read ' . $logfile;
584                 }
585             }
586         }
587
588         return $log;
589     }
590
591     private function _checkTcp($host, $port) {
e5c5af 592         /* Try to open a connection */
dead5c 593         $fp = @fsockopen($host, $port, $errno, $errstr, 2);
V 594
595         if ($fp) {
8bbcc1 596             /*
e5c5af 597              * We got a connection, this means, everything is O.K.
V 598              * But maybe we are able to do more deep testing?
8bbcc1 599              */
e5c5af 600             if ($port == 80) {
V 601                 /*
602                  * Port 80 means, testing APACHE
603                  * So we can do a deepter test and try to get data over this connection.
604                  * (if apache hangs, we get a connection but a timeout by trying to GET the data!)
605                  */
8cf78b 606                 // fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
T 607                 $out = "GET / HTTP/1.1\r\n";
608                 $out .= "Host: localhost\r\n";
609                 $out .= "User-Agent: Mozilla/5.0 (ISPConfig monitor)\r\n";
610                 $out .= "Accept: application/xml,application/xhtml+xml,text/html\r\n";
611                 $out .= "Connection: Close\r\n\r\n";
612                 fwrite($fp, $out);
e5c5af 613                 stream_set_timeout($fp, 5); // Timeout after 5 seconds
V 614                 $res = fread($fp, 10);  // try to get 10 bytes (enough to test!)
615                 $info = stream_get_meta_data($fp);
616                 if ($info['timed_out']) {
617                     return false; // Apache was not able to send data over this connection
618                 }
eed6b3 619             }
e5c5af 620
V 621             /* The connection is no longer needed */
622             fclose($fp);
623             /* We are able to establish a connection */
624             return true;
dead5c 625         } else {
e5c5af 626             /* We are NOT able to establish a connection */
V 627             return false;
dead5c 628         }
V 629     }
630
631     private function _checkUdp($host, $port) {
632
633         $fp = @fsockopen('udp://' . $host, $port, $errno, $errstr, 2);
634
635         if ($fp) {
636             fclose($fp);
637             return true;
638         } else {
639             return false;
640         }
641     }
642
643     private function _checkFtp($host, $port) {
644
645         $conn_id = @ftp_connect($host, $port);
646
647         if ($conn_id) {
648             @ftp_close($conn_id);
649             return true;
650         } else {
651             return false;
652         }
653     }
eed6b3 654
e5c5af 655     /**
V 656      * Set the state to the given level (or higher, but not lesser).
657      * * If the actual state is critical and you call the method with ok,
658      *   then the state is critical.
659      *
660      * * If the actual state is critical and you call the method with error,
661      *   then the state is error.
662      */
5de2af 663     public function _setState($oldState, $newState) {
e5c5af 664         /*
V 665          * Calculate the weight of the old state
666          */
667         switch ($oldState) {
b1a6a5 668         case 'no_state': $oldInt = 0;
MC 669             break;
670         case 'ok': $oldInt = 1;
671             break;
672         case 'unknown': $oldInt = 2;
673             break;
674         case 'info': $oldInt = 3;
675             break;
676         case 'warning': $oldInt = 4;
677             break;
678         case 'critical': $oldInt = 5;
679             break;
680         case 'error': $oldInt = 6;
681             break;
e5c5af 682         }
V 683         /*
684          * Calculate the weight of the new state
685          */
686         switch ($newState) {
b1a6a5 687         case 'no_state': $newInt = 0;
MC 688             break;
689         case 'ok': $newInt = 1;
690             break;
691         case 'unknown': $newInt = 2;
692             break;
693         case 'info': $newInt = 3;
694             break;
695         case 'warning': $newInt = 4;
696             break;
697         case 'critical': $newInt = 5;
698             break;
699         case 'error': $newInt = 6;
700             break;
e5c5af 701         }
V 702
703         /*
704          * Set to the higher level
705          */
706         if ($newInt > $oldInt) {
707             return $newState;
708         } else {
709             return $oldState;
710         }
711     }
dead5c 712
5de2af 713     /**
M 714      * Deletes Records older than 4 minutes.
715      * The monitor writes new data every 5 minutes or longer (4 hour, 1 day).
716      * So if i delete all Date older than 4 minutes i can be sure, that all old data
717      * are deleted...
718      */
719     public function delOldRecords($type, $serverId) {
720         global $app;
721
722         // $now = time();
723         // $old = $now - (4 * 60); // 4 minutes
6a3742 724         $old = 240; //seconds
5de2af 725
M 726         /*
727          * ATTENTION if i do NOT pay attention of the server id, i delete all data (of the type)
728          * of ALL servers. This means, if i have a multiserver-environment and a server has a
729          * time not synced with the others (for example, all server has 11:00 and ONE server has
730          * 10:45) then the actual data of this server (with the time-stamp 10:45) get lost
731          * even though it is the NEWEST data of this server. To avoid this i HAVE to include
732          * the server-id!
733          */
8081df 734         $sql = 'DELETE FROM `monitor_data` WHERE `type` = ? AND `created` < UNIX_TIMESTAMP() - ? AND `server_id` = ?';
cc7a82 735         $app->dbmaster->query($sql, $type, $old, $serverId);
dead5c 736     }
V 737
a20430 738     public function send_notification_email($template, $placeholders, $recipients) {
FS 739         global $conf;
740
741         if(!is_array($recipients) || count($recipients) < 1) return false;
742         if(!is_array($placeholders)) $placeholders = array();
743
744         if(file_exists($conf['rootpath'].'/conf-custom/mail/' . $template . '_'.$conf['language'].'.txt')) {
745             $lines = file($conf['rootpath'].'/conf-custom/mail/' . $template . '_'.$conf['language'].'.txt');
746         } elseif(file_exists($conf['rootpath'].'/conf-custom/mail/' . $template . '_en.txt')) {
747             $lines = file($conf['rootpath'].'/conf-custom/mail/' . $template . '_en.txt');
748         } elseif(file_exists($conf['rootpath'].'/conf/mail/' . $template . '_'.$conf['language'].'.txt')) {
749             $lines = file($conf['rootpath'].'/conf/mail/' . $template . '_'.$conf['language'].'.txt');
750         } else {
751             $lines = file($conf['rootpath'].'/conf/mail/' . $template . '_en.txt');
752         }
753
754         //* get mail headers, subject and body
755         $mailHeaders = '';
756         $mailBody = '';
757         $mailSubject = '';
758         $inHeader = true;
759         for($l = 0; $l < count($lines); $l++) {
d8faa4 760             if(trim($lines[$l]) == '') {
a20430 761                 $inHeader = false;
FS 762                 continue;
763             }
764             if($inHeader == true) {
765                 $parts = explode(':', $lines[$l], 2);
b31c14 766                 if(strtolower($parts[0]) == 'subject') {
MB 767                     $mailSubject = trim($parts[1]);
768                     continue;
769                 }
a20430 770                 unset($parts);
FS 771                 $mailHeaders .= trim($lines[$l]) . "\n";
772             } else {
773                 $mailBody .= trim($lines[$l]) . "\n";
774             }
775         }
776         $mailBody = trim($mailBody);
777
778         //* Replace placeholders
779         $mailHeaders = strtr($mailHeaders, $placeholders);
780         $mailSubject = strtr($mailSubject, $placeholders);
781         $mailBody = strtr($mailBody, $placeholders);
782
783         for($r = 0; $r < count($recipients); $r++) {
784             mail($recipients[$r], $mailSubject, $mailBody, $mailHeaders);
785         }
786
787         unset($mailSubject);
788         unset($mailHeaders);
789         unset($mailBody);
790         unset($lines);
791
792         return true;
793     }
5de2af 794
dead5c 795 }
V 796
2332b2 797 ?>