Boxerman Posted August 6, 2013 Share Posted August 6, 2013 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 More sharing options...
PravinS Posted August 6, 2013 Share Posted August 6, 2013 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 https://forums.phpfreaks.com/topic/280873-grabbing-all-and-sending-as-one/#findComment-1443615 Share on other sites More sharing options...
Boxerman Posted August 6, 2013 Author Share Posted August 6, 2013 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 https://forums.phpfreaks.com/topic/280873-grabbing-all-and-sending-as-one/#findComment-1443689 Share on other sites More sharing options...
AbraCadaver Posted August 6, 2013 Share Posted August 6, 2013 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 https://forums.phpfreaks.com/topic/280873-grabbing-all-and-sending-as-one/#findComment-1443690 Share on other sites More sharing options...
Boxerman Posted August 6, 2013 Author Share Posted August 6, 2013 But this will still send multiple emails? right? as its in a loop? Link to comment https://forums.phpfreaks.com/topic/280873-grabbing-all-and-sending-as-one/#findComment-1443695 Share on other sites More sharing options...
AbraCadaver Posted August 6, 2013 Share Posted August 6, 2013 But this will still send multiple emails? right? as its in a loop? If you notice, it builds the message in a loop and then just sends 1 email after the loop. Link to comment https://forums.phpfreaks.com/topic/280873-grabbing-all-and-sending-as-one/#findComment-1443697 Share on other sites More sharing options...
Boxerman Posted August 6, 2013 Author Share Posted August 6, 2013 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? Link to comment https://forums.phpfreaks.com/topic/280873-grabbing-all-and-sending-as-one/#findComment-1443705 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.