auth->check_module_permissions('monitor');
/* Change the Server if needed */
if (isset($_GET['server'])){
$server = explode('|', $_GET['server'], 2);
$_SESSION['monitor']['server_id'] = $server[0];
$_SESSION['monitor']['server_name'] = $server[1];
}
/*
* Loading the template
*/
$app->uses('tpl');
$app->tpl->newTemplate("form.tpl.htm");
$app->tpl->setInclude('content_tpl','templates/show_sys_state.htm');
/*
* setting the content
*/
if ($_GET['state'] == 'server')
{
$output = _getServerState($_SESSION['monitor']['server_id'], $_SESSION['monitor']['server_name'], true);
$title = "Server State";
$stateType = 'server';
}
else
{
$output = _getSysState();
$title = "System State";
$stateType = 'system';
}
$app->tpl->setVar("state_data",$output);
$app->tpl->setVar("state_type",$stateType);
$app->tpl->setVar("title",$title);
$app->tpl->setVar("description",$description);
/*
Creating the array with the refresh intervals
Attention: the core-moule ist triggered every 5 minutes,
so reload every 2 minutes is impossible!
*/
$refresh = (isset($_GET["refresh"]))?intval($_GET["refresh"]):0;
$refresh_values = array('0' => '- '.$app->lng("No Refresh").' -','5' => '5 '.$app->lng("minutes"),'10' => '10 '.$app->lng("minutes"),'15' => '15 '.$app->lng("minutes"),'30' => '30 '.$app->lng("minutes"),'60' => '60 '.$app->lng("minutes"));
$tmp = '';
foreach($refresh_values as $key => $val) {
if($key == $refresh) {
$tmp .= "";
} else {
$tmp .= "";
}
}
$app->tpl->setVar("refresh",$tmp);
/*
* doing the output
*/
$app->tpl_defaults();
$app->tpl->pparse();
function _getSysState(){
global $app;
/*
* Get all Servers and calculate the state of them
*/
$html = '';
$servers = $app->db->queryAllRecords("SELECT server_id, server_name FROM server order by server_name");
foreach ($servers as $server)
{
$html .= _getServerState($server['server_id'], $server['server_name'], false);
}
return $html;
}
/*
* Calculates the State of ONE Server
*/
function _getServerState($serverId, $serverName, $showAll)
{
global $app;
/* The State of the server */
$serverState = 'ok';
/** The Number of several infos, warnings, errors, ... */
$count = array('unknown' => 0, 'info' => 0, 'warning' => 0, 'critical' => 0, 'error' => 0);
/** The messages */
$messages = array();
/** The Result of the function */
$res = '';
/*
* get all monitoring-data from the server als process then
* (count them and set the server-state)
*/
$records = $app->db->queryAllRecords("SELECT DISTINCT type FROM monitor_data WHERE server_id = " . $serverId);
foreach($records as $record){
_processDbState($record['type'], $serverId, &$serverState, &$messages);
}
$res .= '
';
$res .= '
';
$res .= '
';
$res .= 'Server: ' . $serverName . ' ';
$res .= 'State: ' . $serverState . ' ';
// $res .= sizeof($messages['ok']) . ' ok | ';
$res .= sizeof($messages['unknown']) . ' unknown | ';
$res .= sizeof($messages['info']) . ' info | ';
$res .= sizeof($messages['warning']) . ' warning | ';
$res .= sizeof($messages['critical']) . ' critical | ';
$res .= sizeof($messages['error']) . ' error ';
$res .= ' ';
if ($showAll){
/*
* if we have to show all, then we do it...
*/
/*
* Show all messages
*/
foreach($messages as $key => $state){
/*
* There is no need, to show the "ok" - messages
*/
// if ($key != 'ok')
{
$res .= $key . ': ';
foreach ($state as $msg)
{
$res .= $msg . ' ';
}
$res .= ' ';
}
}
}
else
{
/*
* if not, we only show a link to the server...
*/
$res .= " More information...";
}
$res .= '
';
$res .= '
';
$res .= '
';
if ($showAll){
/*
* Show some state-info
*/
//$res .= showServerLoad();
//$res .= ' '. showDiskUsage();
//$res .= ' '.showServices();
}
return $res;
}
/*
* gets the state from the db and process it
*/
function _processDbState($type, $serverId, &$serverState, &$messages)
{
global $app;
/*
* Always the NEWEST record of each monitoring is responsible for the
* state
*/
// get the State from the DB
$record = $app->db->queryOneRecord("SELECT state FROM monitor_data WHERE type = '" . $type . "' and server_id = " . $serverId . " order by created desc");
// change the new state to the highest state
$serverState = _setState($serverState, $record['state']);
// count the states
$count[$record['state']]+= 1;
/*
* The message depands on the type and the state
*/
if ($type == 'cpu_info'){
/* this type has no state */
}
if ($type == 'disk_usage'){
switch ($record['state']) {
case 'ok':
$messages['ok'][] = 'The state of your Hard-Disk space is ok ' .
"[more...]";
break;
case 'info':
$messages['info'][] = 'Your Hard-Disk space is going full ' .
"[more...]";
break;
case 'warning':
$messages['warning'][] = 'Your Hard-Disk is nearly full ' .
"[more...]";
break;
case 'critical':
$messages['critical'][] = 'Your Hard-Disk is very full '.
"[more...]";
break;
case 'error':
$messages['error'][] = 'Your Hard-Disk has no more space left ' .
"[more...]";
break;
default:
$messages['unknown'][] = 'Hard-Disk: ??? ' .
"[more...]";
break;
}
}
if ($type == 'mem_usage'){
/* this type has no state */
}
if ($type == 'server_load'){
switch ($record['state']) {
case 'ok':
$messages['ok'][] = 'Your Server load is ok ' .
"[more...]";
break;
case 'info':
$messages['info'][] = 'Your Server in under heavy load ' .
"[more...]";
break;
case 'warning':
$messages['warning'][] = 'Your Server in under high load ' .
"[more...]";
break;
case 'critical':
$messages['critical'][] = 'Your Server in under higher load ' .
"[more...]";
break;
case 'error':
$messages['error'][] = 'Your Server in under highest load ' .
"[more...]";
break;
default:
$messages['unknown'][] = 'Server Load: ??? ' .
"[more...]";
break;
}
}
if ($type == 'services'){
switch ($record['state']) {
case 'ok':
$messages['ok'][] = 'All needed Services are online ' .
"[more...]";
break;
case 'error':
$messages['error'][] = 'One or more needed Services are offline ' .
"[more...]";
break;
default:
$messages['unknown'][] = 'services:??? ' .
"[more...]";
break;
}
}
if ($type == 'system_update'){
switch ($record['state']) {
case 'ok':
$messages['ok'][] = 'Your System is up to date. ' .
"[more...]";
break;
case 'warning':
$messages['warning'][] = 'One or more Components needs a update ' .
"[more...]";
break;
case 'no_state':
/*
* not debian and not Ubuntu, so the state could not be monitored...
*/
break;
default:
$messages['unknown'][] = 'System-Update:??? ' .
"[more...]";
break;
}
}
if ($type == 'raid_state'){
switch ($record['state']) {
case 'ok':
$messages['ok'][] = 'Your RAID is ok ' .
"[more...]";
break;
case 'info':
$messages['info'][] = 'Your RAID is in RESYNC mode ' .
"[more...]";
break;
case 'critical':
$messages['critical'][] = 'Your RAID has one FAULT disk. Replace as soon as possible! '.
"[more...]";
break;
case 'error':
$messages['error'][] = 'Your RAID is not working anymore ' .
"[more...]";
break;
case 'no_state':
/*
* mdadm is not installed or the RAID is not supported...
*/
break;
default:
$messages['unknown'][] = 'RAID state: ??? ' .
"[more...]";
break;
}
}
if ($type == 'mailq'){
switch ($record['state']) {
case 'ok':
$messages['ok'][] = 'Your Mailq load is ok ' .
"[more...]";
break;
case 'info':
$messages['info'][] = 'Your Mailq in under heavy load ' .
"[more...]";
break;
case 'warning':
$messages['warning'][] = 'Your Mailq in under high load ' .
"[more...]";
break;
case 'critical':
$messages['critical'][] = 'Your Mailq in under higher load ' .
"[more...]";
break;
case 'error':
$messages['error'][] = 'Your Mailq in under highest load ' .
"[more...]";
break;
default:
$messages['unknown'][] = 'Mailq: ??? ' .
"[more...]";
break;
}
}
if ($type == 'log_clamav'){
/* this type has no state */
}
if ($type == 'log_freshclam'){
switch ($record['state']) {
case 'ok':
$messages['ok'][] = 'Your Virus-protection is ok ' .
"[more...]";
break;
case 'warning':
$messages['warning'][] = 'Your Virus-protection is OUTDATED! ' .
"[more...]";
break;
default:
$messages['unknown'][] = 'Freshclam: ???! ' .
"[more...]";
break;
}
}
if ($type == 'log_ispconfig'){
/* this type has no state */
}
if ($type == 'log_mail'){
/* this type has no state */
}
if ($type == 'log_mail_err'){
/* this type has no state */
}
if ($type == 'log_mail_warn'){
/* this type has no state */
}
if ($type == 'log_messages'){
/* this type has no state */
}
if ($type == 'rkhunter'){
/* this type has no state */
}
}
/*
* Set the state to the given level (or higher, but not lesser).
* * If the actual state is critical and you call the method with ok,
* then the state is critical.
*
* * If the actual state is critical and you call the method with error,
* then the state is error.
*/
function _setState($oldState, $newState)
{
/*
* Calculate the weight of the old state
*/
switch ($oldState) {
case 'no_state': $oldInt = 0;
break;
case 'ok': $oldInt = 1;
break;
case 'unknown': $oldInt = 2;
break;
case 'info': $oldInt = 3;
break;
case 'warning': $oldInt = 4;
break;
case 'critical': $oldInt = 5;
break;
case 'error': $oldInt = 6;
break;
}
/*
* Calculate the weight of the new state
*/
switch ($newState) {
case 'no_state': $newInt = 0 ;
break;
case 'ok': $newInt = 1 ;
break;
case 'unknown': $newInt = 2 ;
break;
case 'info': $newInt = 3 ;
break;
case 'warning': $newInt = 4 ;
break;
case 'critical': $newInt = 5 ;
break;
case 'error': $newInt = 6 ;
break;
}
/*
* Set to the higher level
*/
if ($newInt > $oldInt){
return $newState;
}
else
{
return $oldState;
}
}
?>