Solved

How do I take PHP Net_Ping response and have it display something based on what I get back?

Posted on 2010-11-14
8
339 Views
Last Modified: 2012-05-10
Hey all I realize I should be learning this on my own but I'm hoping to get a little jump start help from someone... I have server monitoring setup but have been trying to make a little graphical web page of my own.

So I've started using this php-pear module called Net_Ping. using a webpage like the following...

<?php

// include class
require ("Net/Ping.php");

// create object
$ping = Net_Ping::factory();

// ping host and display response
if(!PEAR::isError($ping))
{
   $response = $ping->ping('www.google.com');
   print_r($response);
}

?>

Open in new window


It pings Google and then gives a response like the following

Net_Ping_Result Object ( [_icmp_sequence] => Array ( [1] => 79.8 [2] => 79.8 [3] => 80 ) [_target_ip] => 74.125.91.105 [_bytes_per_request] => 64 [_bytes_total] => 192 [_ttl] => 54 [_raw_data] => Array ( [0] => PING www.l.google.com (74.125.91.105) 56(84) bytes of data. [1] => 64 bytes from qy-in-f105.1e100.net (74.125.91.105): icmp_seq=1 ttl=54 time=79.8 ms [2] => 64 bytes from qy-in-f105.1e100.net (74.125.91.105): icmp_seq=2 ttl=54 time=79.8 ms [3] => 64 bytes from qy-in-f105.1e100.net (74.125.91.105): icmp_seq=3 ttl=54 time=80.0 ms [4] => [5] => --- www.l.google.com ping statistics --- [6] => 3 packets transmitted, 3 received, 0% packet loss, time 1999ms [7] => rtt min/avg/max/mdev = 79.805/79.903/80.066/0.258 ms ) [_sysname] => linux [_round_trip] => Array ( [min] => 79.805 [avg] => 79.903 [max] => 80.066 [stddev] => 0.258 ) [_transmitted] => 3 [_received] => 3 [_loss] => 0 ) 

Open in new window


So my plan is to have an image displaying for when the target is able to be pinged. And when it has a loss or can not be reached for a certain time or amount of lost packets then I would like it to display a different image. You know, just the normal Green up - Red down type thing.

The other monitors do this but I can't make them display in a webpage the way I want to go up on our 42" monitor I have at work.

So how do I have PHP read the returned information and then do something according... You don't have to give me the code but if you could at least point me in the right direction of something to read.  A small code snippet/sample would be great too but like I said not necessary. I'm hoping to have this run multiple targets on one page but I'm guessing it won't be hard I'll just have to run multiple instances of hte code on the same page and then just position each response how I want on the page.

Thanks again in advance!!
0
Comment
Question by:keith_opswat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 3

Expert Comment

by:Prograministrator
ID: 34132991
Hello,

you can trying something like :

if($result->getgetReceived() > 0) {
// place your code here
}

Open in new window


and there're getMin(), getMax(), getAvg(), getStddev(),
getTransmitted(), and getLoss() methods, could help with your works.
0
 
LVL 3

Accepted Solution

by:
Prograministrator earned 500 total points
ID: 34132999
Oops typo,

there's duplicated "get",

here is the right one :

if($result->getReceived() > 0) {
// place your code here
}

Open in new window



0
 
LVL 4

Author Comment

by:keith_opswat
ID: 34133002
That's great! And from that I see how I can make it display something from different parts of the output except from packets Received. So that's very useful I appreciate it.

! You're at least getting a portion of the points!! I'm just curious how do I make it not show the output from the ping?
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 3

Expert Comment

by:Prograministrator
ID: 34133053
Just comment or remove this part of code :
print_r($response);

Open in new window

0
 
LVL 4

Author Comment

by:keith_opswat
ID: 34133132
Ok... So I have this

<?php

// include class
require ("Net/Ping.php");

// create object
$ping = Net_Ping::factory();

// ping host and display response
if(!PEAR::isError($ping))
{
   $response = $ping->ping('www.notarealwebaddress.com');
}

if($result->getReceived() > 0) {
// place your code here

Print "The server appears to be down...";

}

?>

Open in new window


But when I run it I get absolutely nothing. Before I added the new line it would at least show me the Print amongst the other stuff. What did I do wrong?
0
 
LVL 3

Expert Comment

by:Prograministrator
ID: 34133149
ooh Sorry,

just replace $result with $response and it will work.
0
 
LVL 3

Expert Comment

by:Prograministrator
ID: 34133182
and be attention that www.notarealwebaddress.com not response, so the value of $response->getRecieved is 0, and it won't print any thing.

when $response->getRecieved 's value greater than 0 you should print that the server is found.

else if its value equal to 0 you should to print that the server isn't found or down.

Best regrads...
0
 
LVL 4

Author Closing Comment

by:keith_opswat
ID: 34133222
Thanks, I wasn't paying attention and didn't notice it was greater than 0. So it's working well now. Thanks!!
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question