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
335 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
This article discusses how to create an extensible mechanism for linked drop downs.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

840 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