Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
360 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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…
Suggested Courses

782 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