|
5 | 5 |
|
6 | 6 | class PHPMonitoring { |
7 | 7 |
|
| 8 | + const CONFIG_FILE = 'config.inc.php'; |
| 9 | + const ERROR_LOG = '/var/log/php-monitoring.log'; |
| 10 | + var $config; |
| 11 | + |
| 12 | + /** |
| 13 | + * Initialize PHP settings, load config file and reset results |
| 14 | + */ |
8 | 15 | function init(){ |
9 | | - // TODO load config file |
| 16 | + ini_set('log_errors', 1); |
| 17 | + ini_set('error_log', self::ERROR_LOG); |
| 18 | + global $config; |
| 19 | + if (!file_exists(self::CONFIG_FILE)){ |
| 20 | + throw new Exception('config file not found'); |
| 21 | + } |
| 22 | + require_once self::CONFIG_FILE; |
| 23 | + $this->config = $config; |
| 24 | + $this->config['results'] = array(); |
10 | 25 | } |
11 | 26 |
|
| 27 | + /** |
| 28 | + * Check all services statuses |
| 29 | + */ |
12 | 30 | function run(){ |
13 | | - // TODO check services |
| 31 | + if ( |
| 32 | + !isset($this->config['services']) |
| 33 | + || !is_array($this->config['services']) |
| 34 | + || empty($this->config['services']) |
| 35 | + ){ |
| 36 | + throw new Exception('services not configured'); |
| 37 | + } |
| 38 | + $alert = false; |
| 39 | + foreach ($this->config['services'] as $service){ |
| 40 | + $alert = $alert || !$this->checkService($service); |
| 41 | + } |
| 42 | + if ($alert){ |
| 43 | + $this->alert(); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Check a service status |
| 49 | + */ |
| 50 | + function checkService($service){ |
| 51 | + list($ip, $port) = explode(':', $service); |
| 52 | + $this->config['results'][$service] = array(); |
| 53 | + $this->config['results'][$service]['result'] = @fsockopen( |
| 54 | + $ip, |
| 55 | + $port, |
| 56 | + $this->config['results'][$service]['errno'], |
| 57 | + $this->config['results'][$service]['errstr'], |
| 58 | + $this->config['timeout'] |
| 59 | + ); |
| 60 | + if (!$this->config['results'][$service]['result']){ |
| 61 | + $this->error("$service is down"); |
| 62 | + } |
| 63 | + return $this->config['results'][$service]['result']; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Check and print a service status |
| 68 | + */ |
| 69 | + function printService($service){ |
| 70 | + if (!isset($this->config['results'][$service])){ |
| 71 | + $this->checkService($service); |
| 72 | + } |
| 73 | + $result = "{$service} -> "; |
| 74 | + if ($this->config['results'][$service]['result']){ |
| 75 | + $result .= "OK"; |
| 76 | + }else{ |
| 77 | + $result .= "ERROR {$this->config['results'][$service]['errno']} ({$this->config['results'][$service]['errstr']})"; |
| 78 | + } |
| 79 | + $result .= "\r\n"; |
| 80 | + return $result; |
14 | 81 | } |
15 | 82 |
|
16 | | - function log(){ |
17 | | - // TODO log message |
| 83 | + /** |
| 84 | + * Message logging |
| 85 | + */ |
| 86 | + function info($msg){ |
| 87 | + error_log(date('Y-m-d H:i:s') . ' [INFO] ' . $msg); |
| 88 | + } |
| 89 | + function error($msg){ |
| 90 | + error_log(date('Y-m-d H:i:s') . ' [ERROR] ' . $msg); |
18 | 91 | } |
19 | 92 |
|
| 93 | + /** |
| 94 | + * Send a mail alert |
| 95 | + */ |
20 | 96 | function alert(){ |
21 | | - // TODO send alert |
| 97 | + if ( |
| 98 | + !isset($this->config['alert']) |
| 99 | + || !is_array($this->config['alert']) |
| 100 | + || empty($this->config['alert']) |
| 101 | + ){ |
| 102 | + throw new Exception('alert not configured'); |
| 103 | + } |
| 104 | + foreach ($p->getServices() as $service){ |
| 105 | + $body .= $p->printService($service); |
| 106 | + } |
| 107 | + $result = mail( |
| 108 | + $this->config['alert']['to'], |
| 109 | + $this->config['alert']['subject'], |
| 110 | + $body |
| 111 | + ); |
| 112 | + if (!$result){ |
| 113 | + $this->error('could not send alert'); |
| 114 | + } |
| 115 | + return $result; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Get services from config |
| 120 | + */ |
| 121 | + function getServices(){ |
| 122 | + return $this->config['services']; |
22 | 123 | } |
23 | 124 | } |
0 commit comments