Jump to content

Grabbing all and sending as one


Boxerman
Go to solution Solved by 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 );
} }
?>
Edited by Boxerman
Link to comment
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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

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?

Edited by Boxerman
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.