Location: PHPKode > projects > Monyet > scripts/collectalert.php
<?php

//not yet fully functional

require '/www/htdocs/ajaxmonyet/includes/startup.php';

$agent = new Agent();
$ping = new Ping();
$servmondef = new ServiceMon();
$snmpmon = new SNMPMon();
$automate = new Automate();
$sms = new SMS();
$user = new User();
$systemstat = new SystemStat();
$array_agent = $agent->select('status="Active"');
$field_id = $agent->getField('id');
$field_description = $agent->getField('description');
$field_priority = $agent->getField('priority');
$sms_alert = '';

$debug = '';
//if host seems down for long time, send alert only in immediate hour
$minute = date("i");
$flag = '';
$bflag = '';

for($i=0;$i<sizeof($array_agent);$i++){
	$priority = $array_agent[$i]->$field_priority;
	if($priority!='no'){
		$st='';
		$ft='';
		if($priority[0]=='p'){
			$ping->selectByID($priority);
			$st = $ping->getStartTime();
			$ft = $ping->getFinishTime();
		}else
		if($priority[0]=='s'){
			$servmondef->selectByID($priority);
			$st = $servmondef->getStartTime();
			$ft = $servmondef->getFinishTime();
		
		}
		else
		if($priority[0]=='m'){
			$snmpmon->selectByID($priority);
			$st = $snmpmon->getStartTime();
			$ft = $snmpmon->getFinishTime();
		}
		//process only in defined window time
		$tmpdate = date('G:i:s');
		$dowerun = false;
		if($st!=$ft){
			//agent with defined window time goes here
			//only for in window time
			if((strtotime($tmpdate)>strtotime($st))&&(strtotime($tmpdate)<strtotime($ft))){
					  $dowerun = true;
				$debug .= $priority." masuk window time\n";
			}
		}else{
			$dowerun = true;
			$debug .= $priority." tidak punya window time\n";
		}
		if($dowerun){		
			$holder = HolderFactory::create($priority);
			$flag = $holder->getStatus($priority);
			$bflag = $holder->getStatusBeforeRecent($priority);
			$execute = false;
			$debug .= $priority." bflag = ".$bflag.", flag = ".$flag."\n";
	
			//be wise, inform that host up again
			if((($bflag=='red')||($bflag=='gray'))&&(($flag=='yellow')||($bflag=='green'))){
				$sms_alert .= $array_agent[$i]->$field_description.' [-1]. ';
				$debug .= $priority." up lagi\n";
			}
	
			//here you are alert collector
			if($minute<5){
				//if under immediate hour, resend alert
				if($flag=='red'){
					if($bflag==$flag){
						$debug .= $priority." masih critical\n";
						$sms_alert .= $array_agent[$i]->$field_description.' [2]. ('.$holder->getRecent().') ';
					}else{
						$debug .= $priority." critical\n";
						$sms_alert .= $array_agent[$i]->$field_description.' [1]. ('.$holder->getRecent().') ';
					}
					//only execute automation once
					if($bflag!=$flag){
						$execute = true;
					}
				}
				else
				if($flag=='gray'){
					$debug .= $priority." down\n";
					$sms_alert .= $array_agent[$i]->$field_description.' [0]. ';
					if($bflag!=$flag){
						$execute = true;
					}
				}
			}
			else{
				//if outside immediate hour.. check status before recent
			   //if status != status before recent.. send alert
				if(($flag=='red')&&($bflag!=$flag)){
					$sms_alert .= $array_agent[$i]->$field_description.' [1]. ('.$holder->getRecent().') ';
					$debug .= $priority." critical\n";
					$execute = true;
				}
				else
				if(($flag=='gray')&&($bflag!=$flag)){
					$sms_alert .= $array_agent[$i]->$field_description.' [0]. ';
					$debug .= $priority." down\n";
					$execute = true;
				}else
				if((($flag=='gray')||($flag=="red"))&&(($flag=='gray')||($flag=="red"))){
					$debug .= $priority." down lama dalam satu jam ini\n";
				
				}
			}
			//execute automate script if any
			if($execute){
				$temp = $automate->select('caller = "'.$priority.'"');
				if(sizeof($temp)>0){
					$debug .= $priority." execute script ".REPO."/".$temp[0]->script." \n";
					system("perl ".REPO."/".$temp[0]->script);
				}
			}
		}
	}
}
//echo $sms_alert;
if($sms_alert!=''){
	//get handset for user definition [simplified]
	$tempuser = $user->select(1);
	for($i=0;$i<sizeof($tempuser);$i++){
		$sms->add(array('','NOW()',$sms_alert,'false',$tempuser[$i]->handset));
	}
}
echo $debug;
?>
Return current item: Monyet