Jump to content

Grabbing all and sending as one


Boxerman

Recommended Posts

Evening all,

 

I'm trying to get a script then will run on a CRON job at some point. it works as you see it, however when more than one server is down it sends a new email for every server, how would i go about getting it to grab all down servers and sending in just 1 email?

 

Thanks guys! (code below).

<?
$hosts = array( 
array ( "hostdescription" => "B1", "hostaddress" => "IP HIDDEN" ),
array ( "hostdescription" => "B2", "hostaddress" => "IP HIDDEN" ),
array ( "hostdescription" => "BB1","hostaddress" => "IP HIDDEN" ), 
array ( "hostdescription" => "BB2", "hostaddress" => "IP HIDDEN" ),
array ( "hostdescription" => "BB3", "hostaddress" => "IP HIDDEN" ),
array ( "hostdescription" => "BBB1", "hostaddress" => "IP HIDDEN" ),
array ( "hostdescription" => "BBB2", "hostaddress" => "IP HIDDEN" ),
array ( "hostdescription" => "BBBB1", "hostaddress" => "IP HIDDEN" ),
array ( "hostdescription" => "BBBB2", "hostaddress" => "IP HIDDEN" ),
array ( "hostdescription" => "BBBB3", "hostaddress" => "IP HIDDEN" ) );  
$pingcount = 1;  
foreach ($hosts as $host){  
$hostdescription = $host["hostdescription"];  
$hostaddress = $host["hostaddress"];  
exec("ping -c $pingcount -w $pingcount $hostaddress", $pingoutput, $Spingstatuscode);  
if($Spingstatuscode === 0){ 
echo("$hostdescription ($hostaddress) is Up <BR>"); }else{ 
echo("$hostdescription ($hostaddress) is Down <BR>"); 
$to = "EMAIL HIDDEN";
$subject = "SERVER DOWN: $hostdescription"; 
$message = "ATTENTION: $hostdescription is currently down please investigate"; 
$headers .= "From: EMAIL HIDDEN\r\n";
$mail_sent = @mail( $to, $subject, $message, $headers );
} }
?>
Link to comment
https://forums.phpfreaks.com/topic/280873-grabbing-all-and-sending-as-one/
Share on other sites

try this code

$description_message = '';	
	foreach ($hosts as $host)
	{
		$hostdescription = $host["hostdescription"];
		$hostaddress = $host["hostaddress"];
		exec("ping -c $pingcount -w $pingcount $hostaddress", $pingoutput, $Spingstatuscode);
		if ($Spingstatuscode === 0)
		{
			echo("$hostdescription ($hostaddress) is Up <BR>");
			$description_message .= "$hostdescription ($hostaddress) is Up <BR>";
		}
		else
		{
			echo("$hostdescription ($hostaddress) is Down <BR>");
			$description_message .= "$hostdescription ($hostaddress) is Down <BR>";
		}		
	}
	$to = "EMAIL HIDDEN";
	$subject = "SERVER DOWN: $hostdescription";
	$message = "ATTENTION: Please check Server Status below <br>$description_message";
	$headers .= "From: EMAIL HIDDEN\r\n";
	$mail_sent = @mail($to, $subject, $message, $headers);

i have concatenated your messages in $description_message variable and added it in mail body and taken out mail code from foreach loop

 

may this will help you

Hi there,

 

Thanks for the response, however from what i can see, this will send the status of all servers.

 

What i would like to get is a way off builiding the list of DOWN (pingstatus != 1) and sending just the down server via email while ignoring the up ones.

 

Thanks again buddy!

 

B-Man

Hi there,

 

Thanks for the response, however from what i can see, this will send the status of all servers.

 

What i would like to get is a way off builiding the list of DOWN (pingstatus != 1) and sending just the down server via email while ignoring the up ones.

 

Thanks again buddy!

 

B-Man

 

OK, then just remove the part where it includes the UP servers.

So would:

<?
$hosts = array( 
array ( "hostdescription" => "B1", "hostaddress" => "IP HIDDEN" ),
array ( "hostdescription" => "B2", "hostaddress" => "IP HIDDEN" ),
array ( "hostdescription" => "BB1","hostaddress" => "IP HIDDEN" ), 
array ( "hostdescription" => "BB2", "hostaddress" => "IP HIDDEN" ),
array ( "hostdescription" => "BB3", "hostaddress" => "IP HIDDEN" ),
array ( "hostdescription" => "BBB1", "hostaddress" => "IP HIDDEN" ),
array ( "hostdescription" => "BBB2", "hostaddress" => "IP HIDDEN" ),
array ( "hostdescription" => "BBBB1", "hostaddress" => "IP HIDDEN" ),
array ( "hostdescription" => "BBBB2", "hostaddress" => "IP HIDDEN" ),
array ( "hostdescription" => "BBBB3", "hostaddress" => "IP HIDDEN" ) );  
$pingcount = 1;  
foreach ($hosts as $host){  
$hostdescription = $host["hostdescription"];  
$hostaddress = $host["hostaddress"];  
exec("ping -c $pingcount -w $pingcount $hostaddress", $pingoutput, $Spingstatuscode);  
if($Spingstatuscode === 1){ 
$to = "EMAIL HIDDEN";
$subject = "SERVER DOWN"; 
$message = "ATTENTION: $hostdescription is currently down please investigate"; 
$headers .= "From: EMAIL HIDDEN\r\n";
$mail_sent = @mail( $to, $subject, $message, $headers );
} 
?>

Work?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.