• 0

Monitoring 'uptime' of more than one Server in PHP


Question

Hey Guys,

So I know how to display the 'uptime' command for one server, that's easy as pie. But what if I wanted to display uptime, load averages etc for more than one server, any way of integrating some kind of SSH into PHP or extracting the data some other way? (all servers running CentOS 5.5)

Thanks

Chris Pressland

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Okay, so I can pretty much get everything there fine, I'd:

<?php
$connection = ssh2_connect('192.168.20.222', 2200);

if (ssh2_auth_password($connection, 'root', 'examplepass'))
?>

But then what would I add to do a 'uptime' command? I can see commands for basically every other form of communication, but not this.

Link to comment
Share on other sites

  • 0

Hey DaveLegg,

As you may have noticed in some of my previous threads the past few days on here, I'm a php n00b and I keep making trivial mistakes.

$connection = ssh2_connect('192.168.20.10', 2200);
if (ssh2_auth_password($connection, 'root', 'secret'))
$stream = ssh2_exec($connection, 'uptime');

Is what I have so far. Now, If I wanted this to connect to say 4 servers:

192.168.20.10

192.168.20.8

192.168.20.174

192.168.20.7

run uptime

then output as this

20.10 Stats: (output)

20.8 Stats: (output)

20.174 Stats: (output)

20.7 Stats: (output)

How'd I do it? lol... I suck. But I really am trying.

Link to comment
Share on other sites

  • 0

If you don't feel like coding everything for yourself and/or want something nice and fancy: http://www.status2k.com/ or http://serverpulsehq.com/.

Both are developed by the same individual.

None of my servers have Public IPs, and the data they serve is 100% Confidential, so everything is home grown i'm afraid.

Link to comment
Share on other sites

  • 0

$servers = array(
    array('ip'=>'192.168.20.10','user'=>'root','pass'=>'secret'),
    array('ip'=>'192.168.20.8','user'=>'root','pass'=>'secret'),
    array('ip'=>'192.168.20.174','user'=>'root','pass'=>'secret'),
    array('ip'=>'192.168.20.7','user'=>'root','pass'=>'secret'))

foreach($servers as $server)
{
    $connection = ssh2_connect($server['ip'], 2200);
    if (ssh2_auth_password($connection, $server['user'], $server['pass']))
    {
        $server['uptime'] = stream_get_contents(ssh2_exec($connection, 'uptime'));
    }
}

You should then have the uptimes stored in the $servers array. NOTE: Haven't tested this at all, its just straight from the top of my head, so may require a bit of tweaking.

Link to comment
Share on other sites

  • 0

K, got it all tweaked, i'm fairly sure it's connecting properly etc, however what do I do to actually get it to output the information into a web browser.

What code would I need for that?

Link to comment
Share on other sites

  • 0

You could simply replace:

$server['uptime'] = stream_get_contents(ssh2_exec($connection, 'uptime'));

with:

echo $server['ip'].' '.stream_get_contents(ssh2_exec($connection, 'uptime'));

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.