| | |
| | | <?php |
| | | /* |
| | | Copyright (c) 2007-2008, Till Brehm, projektfarm Gmbh and Oliver Vogel www.muv.com |
| | | Copyright (c) 2007-2010, Till Brehm, projektfarm Gmbh and Oliver Vogel www.muv.com |
| | | All rights reserved. |
| | | |
| | | Redistribution and use in source and binary forms, with or without modification, |
| | |
| | | */ |
| | | |
| | | class monitor_core_module { |
| | | /* TODO: this should be a config - var instead of a "constant" */ |
| | | var $interval = 5; // do the monitoring every 5 minutes |
| | | |
| | | var $module_name = 'monitor_core_module'; |
| | |
| | | Do the monitor every n minutes and write the result in the db |
| | | */ |
| | | $min = date('i'); |
| | | if (($min % $this->interval) == 0) |
| | | { |
| | | if (($min % $this->interval) == 0) { |
| | | $this->doMonitor(); |
| | | } |
| | | } |
| | |
| | | // } // end switch |
| | | } // end function |
| | | |
| | | //** Get distribution identifier |
| | | //** IMPORTANT! |
| | | // This is the same code as in /install/install.php |
| | | // So if you change it here, you also have to change it in /install/install.php! |
| | | // Please do not forget to remove the swriteln(); - lines here at this file |
| | | function get_distname() { |
| | | |
| | | $distname = ''; |
| | | $distver = ''; |
| | | $distid = ''; |
| | | $distbaseid = ''; |
| | | |
| | | //** Debian or Ubuntu |
| | | if(file_exists('/etc/debian_version')) { |
| | | |
| | | if(trim(file_get_contents('/etc/debian_version')) == '4.0') { |
| | | $distname = 'Debian'; |
| | | $distver = '4.0'; |
| | | $distid = 'debian40'; |
| | | $distbaseid = 'debian'; |
| | | } elseif(strstr(trim(file_get_contents('/etc/debian_version')),'5.0')) { |
| | | $distname = 'Debian'; |
| | | $distver = 'Lenny'; |
| | | $distid = 'debian40'; |
| | | $distbaseid = 'debian'; |
| | | } elseif(strstr(trim(file_get_contents('/etc/debian_version')),'6.0') || trim(file_get_contents('/etc/debian_version')) == 'squeeze/sid') { |
| | | $distname = 'Debian'; |
| | | $distver = 'Squeeze/Sid'; |
| | | $distid = 'debian40'; |
| | | $distbaseid = 'debian'; |
| | | } else { |
| | | $distname = 'Debian'; |
| | | $distver = 'Unknown'; |
| | | $distid = 'debian40'; |
| | | $distbaseid = 'debian'; |
| | | } |
| | | } |
| | | |
| | | //** OpenSuSE |
| | | elseif(file_exists("/etc/SuSE-release")) { |
| | | if(stristr(file_get_contents('/etc/SuSE-release'),'11.0')) { |
| | | $distname = 'openSUSE'; |
| | | $distver = '11.0'; |
| | | $distid = 'opensuse110'; |
| | | $distbaseid = 'opensuse'; |
| | | } elseif(stristr(file_get_contents('/etc/SuSE-release'),'11.1')) { |
| | | $distname = 'openSUSE'; |
| | | $distver = '11.1'; |
| | | $distid = 'opensuse110'; |
| | | $distbaseid = 'opensuse'; |
| | | } elseif(stristr(file_get_contents('/etc/SuSE-release'),'11.2')) { |
| | | $distname = 'openSUSE'; |
| | | $distver = '11.1'; |
| | | $distid = 'opensuse110'; |
| | | $distbaseid = 'opensuse'; |
| | | } else { |
| | | $distname = 'openSUSE'; |
| | | $distver = 'Unknown'; |
| | | $distid = 'opensuse110'; |
| | | $distbaseid = 'opensuse'; |
| | | } |
| | | } |
| | | |
| | | |
| | | //** Redhat |
| | | elseif(file_exists("/etc/redhat-release")) { |
| | | |
| | | $content = file_get_contents('/etc/redhat-release'); |
| | | |
| | | if(stristr($content,'Fedora release 9 (Sulphur)')) { |
| | | $distname = 'Fedora'; |
| | | $distver = '9'; |
| | | $distid = 'fedora9'; |
| | | $distbaseid = 'fedora'; |
| | | } elseif(stristr($content,'Fedora release 10 (Cambridge)')) { |
| | | $distname = 'Fedora'; |
| | | $distver = '10'; |
| | | $distid = 'fedora9'; |
| | | $distbaseid = 'fedora'; |
| | | } elseif(stristr($content,'Fedora release 10')) { |
| | | $distname = 'Fedora'; |
| | | $distver = '11'; |
| | | $distid = 'fedora9'; |
| | | $distbaseid = 'fedora'; |
| | | } elseif(stristr($content,'CentOS release 5.2 (Final)')) { |
| | | $distname = 'CentOS'; |
| | | $distver = '5.2'; |
| | | $distid = 'centos52'; |
| | | $distbaseid = 'fedora'; |
| | | } elseif(stristr($content,'CentOS release 5.3 (Final)')) { |
| | | $distname = 'CentOS'; |
| | | $distver = '5.3'; |
| | | $distid = 'centos53'; |
| | | $distbaseid = 'fedora'; |
| | | } else { |
| | | $distname = 'Redhat'; |
| | | $distver = 'Unknown'; |
| | | $distid = 'fedora9'; |
| | | $distbaseid = 'fedora'; |
| | | } |
| | | } |
| | | |
| | | //** Gentoo |
| | | elseif(file_exists("/etc/gentoo-release")) { |
| | | |
| | | $content = file_get_contents('/etc/gentoo-release'); |
| | | |
| | | preg_match_all('/([0-9]{1,2})/', $content, $version); |
| | | $distname = 'Gentoo'; |
| | | $distver = $version[0][0].$version[0][1]; |
| | | $distid = 'gentoo'; |
| | | $distbaseid = 'gentoo'; |
| | | |
| | | } else { |
| | | die('unrecognized linux distribution'); |
| | | } |
| | | |
| | | return array('name' => $distname, 'version' => $distver, 'id' => $distid, 'baseid' => $distbaseid); |
| | | } |
| | | |
| | | |
| | | /* |
| | | This method is called every n minutes, when the module ist loaded. |
| | | The method then does a system-monitoring |
| | | */ |
| | | // TODO: what monitoring is done should be a config-var |
| | | function doMonitor() |
| | | { |
| | | function doMonitor() { |
| | | /* Calls the single Monitoring steps */ |
| | | $this->monitorServer(); |
| | | $this->monitorOSVer(); |
| | | $this->monitorDiskUsage(); |
| | | $this->monitorMemUsage(); |
| | | $this->monitorCpu(); |
| | |
| | | $tmpUser = explode(" ", trim($tmp[2])); |
| | | $data['user_online'] = intval($tmpUser[0]); |
| | | |
| | | /* Old Load Average Code |
| | | $loadTmp = explode(":" , trim($tmp[3])); |
| | | $load = explode(",", $loadTmp[1]); |
| | | $data['load_1'] = floatval(trim($load[0])); |
| | | $data['load_5'] = floatval(trim($load[1])); |
| | | $data['load_15'] = floatval(trim($load[2])); */ |
| | | |
| | | //* New Load Average code to fix "always zero" bug in non-english distros. NEEDS TESTING |
| | | $loadTmp = shell_exec("cat /proc/loadavg | cut -f1-3 -d' '"); |
| | | $load = explode(" ", $loadTmp); |
| | |
| | | $this->_delOldRecords($type, 10); |
| | | } |
| | | |
| | | function monitorOsVer() { |
| | | global $app; |
| | | global $conf; |
| | | |
| | | /* the id of the server as int */ |
| | | $server_id = intval($conf["server_id"]); |
| | | |
| | | /** The type of the data */ |
| | | $type = 'os_info'; |
| | | |
| | | /* |
| | | Fetch the data into a array |
| | | */ |
| | | $dist = $this->get_distname(); |
| | | |
| | | $data['name'] = $dist['name']; |
| | | $data['version'] = $dist['version']; |
| | | |
| | | /* the OS has no state. It is, what it is */ |
| | | $state = 'no_state'; |
| | | |
| | | /* |
| | | Insert the data into the database |
| | | */ |
| | | $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " . |
| | | "VALUES (". |
| | | $server_id . ", " . |
| | | "'" . $app->dbmaster->quote($type) . "', " . |
| | | time() . ", " . |
| | | "'" . $app->dbmaster->quote(serialize($data)) . "', " . |
| | | "'" . $state . "'" . |
| | | ")"; |
| | | $app->dbmaster->query($sql); |
| | | |
| | | /* The new data is written, now we can delete the old one */ |
| | | $this->_delOldRecords($type, 10); |
| | | } |
| | | |
| | | |
| | | function monitorDiskUsage() { |
| | | global $app; |
| | | global $conf; |
| | |
| | | * ignore the first line, process the rest |
| | | */ |
| | | for($i=1; $i <= sizeof($df); $i++){ |
| | | if ($df[$i] != '') |
| | | { |
| | | if ($df[$i] != '') { |
| | | /* |
| | | * Make a array of the data |
| | | */ |
| | |
| | | } |
| | | |
| | | |
| | | function monitorMemUsage() |
| | | { |
| | | function monitorMemUsage() { |
| | | global $app; |
| | | global $conf; |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | function monitorCpu() |
| | | { |
| | | function monitorCpu() { |
| | | global $app; |
| | | global $conf; |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | function monitorServices() |
| | | { |
| | | function monitorServices() { |
| | | global $app; |
| | | global $conf; |
| | | |
| | |
| | | |
| | | /* Monitor Webserver */ |
| | | $data['webserver'] = -1; // unknown - not needed |
| | | if ($services['web_server'] == 1) |
| | | { |
| | | if ($services['web_server'] == 1) { |
| | | if($this->_checkTcp('localhost', 80)) { |
| | | $data['webserver'] = 1; |
| | | } else { |
| | |
| | | |
| | | /* Monitor FTP-Server */ |
| | | $data['ftpserver'] = -1; // unknown - not needed |
| | | if ($services['file_server'] == 1) |
| | | { |
| | | if ($services['file_server'] == 1) { |
| | | if($this->_checkFtp('localhost', 21)) { |
| | | $data['ftpserver'] = 1; |
| | | } else { |
| | |
| | | |
| | | /* Monitor SMTP-Server */ |
| | | $data['smtpserver'] = -1; // unknown - not needed |
| | | if ($services['mail_server'] == 1) |
| | | { |
| | | if ($services['mail_server'] == 1) { |
| | | if($this->_checkTcp('localhost', 25)) { |
| | | $data['smtpserver'] = 1; |
| | | } else { |
| | |
| | | |
| | | /* Monitor POP3-Server */ |
| | | $data['pop3server'] = -1; // unknown - not needed |
| | | if ($services['mail_server'] == 1) |
| | | { |
| | | if ($services['mail_server'] == 1) { |
| | | if($this->_checkTcp('localhost', 110)) { |
| | | $data['pop3server'] = 1; |
| | | } else { |
| | |
| | | |
| | | /* Monitor IMAP-Server */ |
| | | $data['imapserver'] = -1; // unknown - not needed |
| | | if ($services['mail_server'] == 1) |
| | | { |
| | | if ($services['mail_server'] == 1) { |
| | | if($this->_checkTcp('localhost', 143)) { |
| | | $data['imapserver'] = 1; |
| | | } else { |
| | |
| | | |
| | | /* Monitor BIND-Server */ |
| | | $data['bindserver'] = -1; // unknown - not needed |
| | | if ($services['dns_server'] == 1) |
| | | { |
| | | if ($services['dns_server'] == 1) { |
| | | if($this->_checkTcp('localhost', 53)) { |
| | | $data['bindserver'] = 1; |
| | | } else { |
| | |
| | | |
| | | /* Monitor MYSQL-Server */ |
| | | $data['mysqlserver'] = -1; // unknown - not needed |
| | | if ($services['db_server'] == 1) |
| | | { |
| | | if ($services['db_server'] == 1) { |
| | | if($this->_checkTcp('localhost', 3306)) { |
| | | $data['mysqlserver'] = 1; |
| | | } else { |
| | |
| | | * if there is any output, then there is a needed update |
| | | */ |
| | | $aptData = shell_exec('apt-get -s -qq dist-upgrade'); |
| | | if ($aptData == '') |
| | | { |
| | | if ($aptData == '') { |
| | | /* There is nothing to update! */ |
| | | $state = 'ok'; |
| | | } |
| | | else |
| | | { |
| | | else { |
| | | /* There is something to update! */ |
| | | $state = 'warning'; |
| | | } |
| | |
| | | |
| | | // In keeping with gentoo's rsync policy, don't update to frequently (every four hours - taken from http://www.gentoo.org/doc/en/source_mirrors.xml) |
| | | $do_update = true; |
| | | if (file_exists('/usr/portage/metadata/timestamp.chk')) |
| | | { |
| | | if (file_exists('/usr/portage/metadata/timestamp.chk')) { |
| | | $datetime = file_get_contents('/usr/portage/metadata/timestamp.chk'); |
| | | $datetime = trim($datetime); |
| | | |
| | | $dstamp = strtotime($datetime); |
| | | if ($dstamp) |
| | | { |
| | | if ($dstamp) { |
| | | $checkat = $dstamp + 14400; // + 4hours |
| | | if (mktime() < $checkat) { |
| | | $do_update = false; |
| | |
| | | * if there is any output, then there is a needed update |
| | | */ |
| | | $emergeData = shell_exec('emerge -puDNt --color n --nospinner --quiet world'); |
| | | if ($emergeData == '') |
| | | { |
| | | if ($emergeData == '') { |
| | | /* There is nothing to update! */ |
| | | $state = 'ok'; |
| | | } |
| | | else |
| | | { |
| | | else { |
| | | /* There is something to update! */ |
| | | $state = 'warning'; |
| | | } |
| | |
| | | /* fetch the next line */ |
| | | $line = $tmp[$i]; |
| | | |
| | | if ((strpos($line, '[U_]') !== false) || (strpos($line, '[_U]') !== false)) |
| | | { |
| | | if ((strpos($line, '[U_]') !== false) || (strpos($line, '[_U]') !== false)) { |
| | | /* One Disk is not working. |
| | | * if the next line starts with "[>" or "[=" then |
| | | * recovery (resync) is in state and the state is |
| | |
| | | if ((strpos($nextLine, '[>') === false) && (strpos($nextLine, '[=') === false)) { |
| | | $state = $this->_setState($state, 'critical'); |
| | | } |
| | | else |
| | | { |
| | | else { |
| | | $state = $this->_setState($state, 'info'); |
| | | } |
| | | } |
| | | if (strpos($line, '[__]') !== false) |
| | | { |
| | | if (strpos($line, '[__]') !== false) { |
| | | /* both Disk are not working */ |
| | | $state = $this->_setState($state, 'error'); |
| | | } |
| | | if (strpos($line, '[UU]') !== false) |
| | | { |
| | | if (strpos($line, '[UU]') !== false) { |
| | | /* The disks are OK. |
| | | * if the next line starts with "[>" or "[=" then |
| | | * recovery (resync) is in state and the state is |
| | |
| | | if ((strpos($nextLine, '[>') === false) && (strpos($nextLine, '[=') === false)) { |
| | | $state = $this->_setState($state, 'ok'); |
| | | } |
| | | else |
| | | { |
| | | else { |
| | | $state = $this->_setState($state, 'info'); |
| | | } |
| | | } |
| | |
| | | $this->_delOldRecords($type, 10); |
| | | } |
| | | |
| | | function monitorMailLog() |
| | | { |
| | | function monitorMailLog() { |
| | | global $app; |
| | | global $conf; |
| | | |
| | |
| | | $this->_delOldRecords($type, 10); |
| | | } |
| | | |
| | | function monitorMailWarnLog() |
| | | { |
| | | function monitorMailWarnLog() { |
| | | global $app; |
| | | global $conf; |
| | | |
| | |
| | | $this->_delOldRecords($type, 10); |
| | | } |
| | | |
| | | function monitorMailErrLog() |
| | | { |
| | | function monitorMailErrLog() { |
| | | global $app; |
| | | global $conf; |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | function monitorMessagesLog() |
| | | { |
| | | function monitorMessagesLog() { |
| | | global $app; |
| | | global $conf; |
| | | |
| | |
| | | $this->_delOldRecords($type, 10); |
| | | } |
| | | |
| | | function monitorISPCCronLog() |
| | | { |
| | | function monitorISPCCronLog() { |
| | | global $app; |
| | | global $conf; |
| | | |
| | |
| | | $this->_delOldRecords($type, 10); |
| | | } |
| | | |
| | | function monitorFreshClamLog() |
| | | { |
| | | function monitorFreshClamLog() { |
| | | global $app; |
| | | global $conf; |
| | | |
| | |
| | | |
| | | $tmp = explode("\n", $data); |
| | | $lastLog = array(); |
| | | if ($tmp[sizeof($tmp)-1] == "") |
| | | { |
| | | if ($tmp[sizeof($tmp)-1] == "") { |
| | | /* the log ends with an empty line remove this */ |
| | | array_pop($tmp); |
| | | } |
| | | if (strpos($tmp[sizeof($tmp)-1], "-------------") !== false) |
| | | { |
| | | if (strpos($tmp[sizeof($tmp)-1], "-------------") !== false) { |
| | | /* the log ends with "-----..." remove this */ |
| | | array_pop($tmp); |
| | | } |
| | |
| | | /* no delimiter found, so add this to the last-log */ |
| | | $lastLog[] = $tmp[$i]; |
| | | } |
| | | else |
| | | { |
| | | else { |
| | | /* delimiter found, so there is no more line left! */ |
| | | break; |
| | | } |
| | |
| | | $this->_delOldRecords($type, 10); |
| | | } |
| | | |
| | | function monitorClamAvLog() |
| | | { |
| | | function monitorClamAvLog() { |
| | | global $app; |
| | | global $conf; |
| | | |
| | |
| | | $this->_delOldRecords($type, 10); |
| | | } |
| | | |
| | | function monitorIspConfigLog() |
| | | { |
| | | function monitorIspConfigLog() { |
| | | global $app; |
| | | global $conf; |
| | | |
| | |
| | | if(stristr($logfile, ';') or substr($logfile,0,9) != '/var/log/' or stristr($logfile, '..')) { |
| | | $log = 'Logfile path error.'; |
| | | } |
| | | else |
| | | { |
| | | else { |
| | | $log = ''; |
| | | if(is_readable($logfile)) { |
| | | if($fd = popen("tail -n 100 $logfile", 'r')) { |
| | |
| | | * * If the actual state is critical and you call the method with error, |
| | | * then the state is error. |
| | | */ |
| | | function _setState($oldState, $newState) |
| | | { |
| | | function _setState($oldState, $newState) { |
| | | /* |
| | | * Calculate the weight of the old state |
| | | */ |
| | |
| | | if ($newInt > $oldInt){ |
| | | return $newState; |
| | | } |
| | | else |
| | | { |
| | | else { |
| | | return $oldState; |
| | | } |
| | | } |
| | |
| | | } |
| | | return $res; |
| | | } |
| | | |
| | | |
| | | } // end class |
| | | |
| | | } |
| | | ?> |