Файл: wmon_dist/index.php
Строк: 196
<?php
// Include /////////////////////////////////////////////////////////////
include('lib/wmon.php');
$config = include('config.php');
// Fetch data if called by cron ////////////////////////////////////////
if(empty($_SERVER['REMOTE_ADDR']))
{
$top = shell_exec("ps axeo '%C %U %c' --sort -pcpu | head -n 7 | tail -n 6");
$top = trim(str_replace(array("n",'|+'),array('|','|'),preg_replace('/ +/','+',$top)),'+|');
$load = trim(shell_exec("cat /proc/loadavg | cut -d' ' -f1,2,3 --output-delimiter=','"));
$last = explode(',',trim(shell_exec("tail -n 1 ".wmon::config('log')." 2>/dev/null")));
if(!empty($last[4])) $last[4] = (int)trim($last[4],"'");
if(!empty($last[6])) $last[6] = (int)trim($last[6],"'");
$services = wmon::config('services');
$service_status = 0;
$i = 1;
foreach($services as $s)
{
if($s[0] == 'internet')
{
if(!wmon::internet()) $service_status+=$i;
}
elseif(!wmon::check_service($s[1])) $service_status+=$i;
$i = $i*2;
}
$partitions = wmon::partitions();
$partition_status = 0;
$i = 1;
foreach($partitions as $p)
{
$p = explode(' ',$p);
if(!empty($p[5]) && (int)$p[5] > wmon::config('partition_space_alert')) $partition_status=+$i;
$i = $i*2;
}
if(wmon::config('email'))
{
$msg = "";
if(isset($last[4]) && $last[4]!=$service_status)
{
$bits_bef = array_reverse(str_split(decbin($last[4])));
$bits_cur = array_reverse(str_split(decbin($service_status)));
foreach(range(0,count($services)-1) as $i)
{
if(empty($bits_bef[$i]) != empty($bits_cur[$i])){
$msg.= '- service '.$services[$i][0].' is '.(empty($bits_cur[$i]) ? 'UP' : 'DOWN')."n";
}
}
}
if(isset($last[6]) && $last[6]!=$partition_status)
{
$bits_bef = array_reverse(str_split(decbin($last[6])));
$bits_cur = array_reverse(str_split(decbin($partition_status)));
$limit = wmon::config('partition_space_alert');
foreach(range(0,count($partitions)-1) as $i)
{
if(empty($bits_bef[$i]) != empty($bits_cur[$i])){
$p = explode(' ',$partitions[$i]);
$msg.= "- partition {$p[1]} ({$p[0]}) space usage {$p[5]} ".
(empty($bits_cur[$i]) ? "back to OK [space usage < {$limit}%]" : "matches resource limit [space usage > {$limit}%]")."n";
}
}
}
if($msg)
{
include_once('lib/Mustache.php');
$mail = new Mustache;
$mail->msg = $msg;
$mail->date = date('Y-m-d H:i:s');
$mail->host = wmon::config('hostname');
// send mail
mail(wmon::config('email'), 'wmon - alert', $mail->render(file_get_contents(__DIR__.'/templates/email.php')));
}
}
$data = time().",{$load},'{$service_status}','{$top}','{$partition_status}'"."n";
file_put_contents(wmon::config('log'), $data, FILE_APPEND | LOCK_EX);
$file = file(wmon::config('log'),FILE_IGNORE_NEW_LINES);
$lines = count($file);
if($lines>wmon::config('period',2016))
{
$output = array_slice($file, $lines-wmon::config('period',2016), $lines-1);
file_put_contents(wmon::config('log'), implode("n", $output)."n");
}
exit;
}
// Return CSV //////////////////////////////////////////////////////////
if(isset($_GET['csv']))
{
$hosts = wmon::config('external');
if(!empty($_GET['csv']) && !empty($hosts[$_GET['csv']]))
{
header("Content-Type:text/plain");
echo file_get_contents(
'compress.zlib://'.trim($hosts[$_GET['csv']],'/').'/index.php?csv',
false,
stream_context_create(array(
'http' => array(
'protocol_version' => 1.1,
'header' => array(
'Connection: close',
'Accept-Encoding: gzip, deflate'
),
'timeout' => 7
)
)));
exit;
}
header("Content-Type:text/plain");
readfile(wmon::config('log'));
exit;
}
// Web part ////////////////////////////////////////////////////////////
include_once('lib/Mustache.php');
$host = new Mustache;
$host->hostname = wmon::config('hostname');
$host->hostvar = wmon::varname(wmon::config('hostname'));
$host->cpu_model_name = wmon::cpu_model_name();
$host->uptime = wmon::uptime();
$host->cvs = 'index.php?csv';
$host->partitions = array();
foreach(wmon::partitions() as $p)
{
if(!empty($p))
{
$p = explode(' ',$p);
if(wmon::config('partition_space_alert') && $p[5] && (int)$p[5] > wmon::config('partition_space_alert')) $p[]= 'bar-danger';
$host->partitions[] = $p;
}
}
$host->services = array();
foreach(wmon::config('services') as $s)
{
if($s[0] == 'internet')
{
$res = wmon::internet();
$status = $res ? 'on' : 'down';
$stat = $res ? 'success' : 'important blink';
}
else
{
$res = wmon::check_service($s[1]);
$status = $res ? 'on' : 'down';
$stat = $res ? 'success' : 'important blink';
}
$host->services[]= array(
'name' => $s[0],
'service' => $s[1],
'status' => $status,
'class' => $stat.' status',
);
}
$host->top = array();
foreach(explode("n",wmon::top()) as $l)
{
if(trim($l)) $host->top[]= explode(' ',trim($l));
}
if(!empty($_GET['external']))
{
$host->hostname = $_GET['external'];
echo $host->render(file_get_contents('templates/host.php'));
exit;
}
$tpl = new Mustache;
$tpl->center = $host->render(file_get_contents('templates/host.php'));
if(wmon::config('external')) foreach(wmon::config('external') as $host=>$url)
{
$out = file_get_contents(
'compress.zlib://'.trim($url,'/').'/index.php?external='.urlencode($host),
false,
stream_context_create(array(
'http' => array(
'protocol_version' => 1.1,
'header' => array(
'Connection: close',
'Accept-Encoding: gzip, deflate'
),
'timeout' => 7
)
)));
$out = str_replace('index.php?csv','index.php?csv='.$host,$out);
$tpl->center.= $out;
}
echo $tpl->render(file_get_contents('templates/template.php'));