Solved

How to read stats from a remote server

Posted on 2011-09-02
8
326 Views
Last Modified: 2012-05-12
I have a "central" ubuntu server which should read the load stats of several remote ubuntu/debian servers automatically every minute or so. Can it be done in PHP? What would be the best way to accomplish this?

Thanks
Jay
0
Comment
Question by:jiiins2
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Accepted Solution

by:
boon86 earned 350 total points
ID: 36476782
This shows uptime and load average for 1 server called monitor.php
please place this to other server:

http://myserver1.com/monitor.php
http://myserver2.com/monitor.php
http://myserver3.com/monitor.php
http://myserver4.com/monitor.php

<?php

$uptime = trim(exec("cat /proc/uptime")); 
/*D*/print_r($uptime);
echo "<br/>";
$uptime = explode(" ", $uptime);
$idletime=$uptime[1];
$uptime=$uptime[0];


$day=86400;
$days=floor($uptime/$day);
echo "Up: $days days<br/>";
$utdelta=$uptime-($days*$day);

$hour=3600;
$hours=floor($utdelta/$hour);
echo "$hours hours<br/>";
$utdelta-=$hours*$hour;

$minute=60;
$minutes=floor($utdelta/$minute);
echo "$minutes minutes<br/>";
$utdelta-=round($minutes*$minute,2);

echo "$utdelta seconds<br/>";

echo '<b>Load Average</b><br/>';
echo '<div id="load">';
echo round((1-($idletime/$uptime))*100,3);
echo "%";
echo '</div>';
?>

Open in new window

This file to place at your central server:

http://mycentralubuntuserver.com/status.php

<?php
include('simple_html_dom.php');
//server 1
$html = file_get_html('http://myserver1.com/monitor.php');
foreach($html->find('div[id=load]') as $key => $info)
{
echo 'Load: '.$info->plaintext."<br />\n";
}
//server 2
$html = file_get_html('http://myserver2.com/monitor.php');
foreach($html->find('div[id=load]') as $key => $info)
{
echo 'Load: '.$info->plaintext."<br />\n";
}
//server 3
$html = file_get_html('http://myserver3.com/monitor.php');
foreach($html->find('div[id=load]') as $key => $info)
{
echo 'Load: '.$info->plaintext."<br />\n";
}
//server 4
$html = file_get_html('http://myserver4.com/monitor.php');
foreach($html->find('div[id=load]') as $key => $info)
{
echo 'Load: '.$info->plaintext."<br />\n";
}

?>

Open in new window


i included: simple_html_dom.php
                 complete.zip

so that you can refer, i tested my code before i post it here.

hope that help


simple-html-dom.php
complete.zip
0
 
LVL 3

Expert Comment

by:dbauermann
ID: 36476842
You can try to install Zabbix (www.zabbix.com).

This software collect a lot of information about servers, not only load stats...
0
 

Author Comment

by:jiiins2
ID: 36476892
@boon86

Thanks, very interesting. The main stats I'd need to retrive are the current cpu load and mem usage. Would your script provide those as well?
0
 
LVL 7

Expert Comment

by:boon86
ID: 36477666
current output is like this:

2312931.92 2298002.62
Up: 26 days
18 hours
28 minutes
51.9199999999 seconds
Load Average
0.645%

Open in new window


for ram you might use:

echo system('free');

my code above its execute uptime command so you will get server load as well.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:jiiins2
ID: 36479421
What about getting the current load instead of the average?
0
 
LVL 25

Expert Comment

by:madunix
ID: 36479442
I use Cacti , MRTG and Nagios, to monitor servers.

Mylist:
http://www.groundworkopensource.com GroundWork Monitor Enterprise
http://www.hyperic.com Hyperic HQ
http://www.icinga.org Icinga
http://www.nagios.com Nagios
http://www.n-able.com N-central
http://www.op5.com op5 Monitor
http://www.open-itcockpit.com openITCockpit
http://www.opennms.org OpenNMS
http://www.hp.com/de/bto HP OpenView
http://www.bmc.com BMC PATROL ProactiveNet Performance Management
http://www.shinken-monitoring.org Shinken
http://www.ibm.com/software/de/tivoli IBM Tivoli
http://www.ca.com/de CA Unicenter
http://www.whatsupgold.com WhatsUp Gold
http://www.zabbix.com Zabbix
http://www.zenoss.com Zenoss Enterprise
http://oss.oetiker.ch/mrtg/ mrtg
0
 

Author Comment

by:jiiins2
ID: 36479464
Anything fred and extremely easy to use?
0
 
LVL 25

Assisted Solution

by:madunix
madunix earned 150 total points
ID: 36479616
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to count occurrences of each item in an array.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

21 Experts available now in Live!

Get 1:1 Help Now