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
331 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 3

Expert Comment

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

Open in new window

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Author Comment

by:keith_opswat
Comment Utility
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
Comment Utility
ooh Sorry,

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

Expert Comment

by:Prograministrator
Comment Utility
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
Comment Utility
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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now