Skip to content

Commit 1d3fca8

Browse files
committed
added check services and sen alert functions
1 parent 687fc33 commit 1d3fca8

File tree

6 files changed

+155
-11
lines changed

6 files changed

+155
-11
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,27 @@ cd /opt
77
wget http://github.com/ircf/php-monitoring/archive/master.zip
88
gunzip master.zip
99
cd php-monitoring
10-
ln -s cron /etc/cron.d/php-monitoring
1110
```
1211

13-
# Configure
12+
# Configure services and alert
1413
```
1514
cd /opt/php-monitoring
1615
cp config.inc.php-dist config.inc.php
1716
nano config.inc.php
1817
# Enter the service names, IP, alert method, and save file
1918
```
19+
20+
# Configure cron
21+
```
22+
ln -s cron /etc/cron.d/php-monitoring
23+
```
24+
25+
# View services statuses
26+
Services statuses can be viewed from CLI :
27+
```
28+
php /opt/php-monitoring/index.php
29+
```
30+
Or if you have a web server installed, from a web browser :
31+
```
32+
http://example.tld/php-monitoring/index.php
33+
```

config.inc.php-dist

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
*/
55

66
$config = array(
7+
'timeout' => 2,
78
'services' => array(
8-
// TODO
9+
// 'www.example.tld:80',
10+
// 'pop.example.tld:110',
11+
// 'smtp.example.tld:25',
912
),
1013
'alert' => array(
11-
// TODO
14+
// 'subject' => 'PHP monitoring alert',
15+
// 'to' => '[email protected]',
1216
),
1317
);

cron

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
# PHP monitoring cron script
33
# To install this script please see README.md
44
#
5-
* * * * * /bin/php /opt/php-monitoring/cron.php &> /var/log/php-monitoring.log
5+
* * * * * /bin/php /opt/php-monitoring/cron.php

cron.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
* PHP monitoring cron script
44
*/
55

6+
require_once('functions.php');
7+
68
$p = new PHPMonitoring();
9+
710
try{
811
$p->init();
912
$p->run();
1013
}catch(Exception $e){
11-
$p->log($e);
14+
$p->error($e->getMessage());
1215
}

functions.php

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,120 @@
55

66
class PHPMonitoring {
77

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+
*/
815
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();
1025
}
1126

27+
/**
28+
* Check all services statuses
29+
*/
1230
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;
1481
}
1582

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);
1891
}
1992

93+
/**
94+
* Send a mail alert
95+
*/
2096
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'];
22123
}
23124
}

index.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* PHP Monitoring index page : show services statuses
4+
*/
5+
6+
require_once('functions.php');
7+
8+
header('content-type:text/plain');
9+
10+
$p = new PHPMonitoring();
11+
try{
12+
$p->init();
13+
ob_start();
14+
foreach ($p->getServices() as $service){
15+
echo $p->printService($service);
16+
ob_flush();
17+
flush();
18+
}
19+
}catch(Exception $e){
20+
$p->error($e->getMessage());
21+
echo $e->getMessage();
22+
}

0 commit comments

Comments
 (0)