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
342 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 learn how to dynamically set the form action using jQuery.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

627 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