Solved

How to read stats from a remote server

Posted on 2011-09-02
8
333 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

816 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