Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


PHP+SSH server uptime monitor
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

PHP+SSH server uptime monitor

Hi All,

I have been googling but didnt found what I'm looking for. Maybe my keywords not good enough,but I have tried many.

What I'm looking for is that a php script for monitoring remote server uptime by utilising SSH. Using password or key file. No installing agents on the remote server.

If you have suggestion or links for any existing script or tutorial,would be appreciated.

Thanks.

Comments

  • edited December 2013
    <?php $uptime = file_get_contents('/proc/uptime'); echo $uptime; ?>

    2 numbers: uptime in seconds, idle time (total cores)

    No SSH ;)

    Thanked by 1Shamli
  • Thanks @sergiodelatorre
    but i'm looking to get uptime of remote server,not local.

  • Not exactly what you want, but might worth a shot

    http://bash.cyberciti.biz/monitoring/get-system-information-in-html-format/

    Note: I've never tested it

    Thanked by 1Shamli
  • thanks @jebat_ks
    already have a look at that...but its bash script.....

  • edited December 2013
    <?php $uptime=shell_exec("ssh user@host 'uptime' ") ?>

    Assuming passwordsless ssh login

    Thanked by 1Shamli
  • thanks @sergiodelatorre
    great....by passwordless,it's using key file...but,where should I put this key file...? and how about if there are multiple key file for multiple host...?

  • Jebat and Shamli is like Malaysian name to me. ;)

  • edited December 2013

    Good question

    You should generate the key with

    su - user -c "ssh-keygen -t rsa"

    Where user is the user executing the php script. It could be apache (mod_php or php-cgi), the user configured in the php-fpm pool...

    I've never done this before, try it and tell us

    PS: You can specify the key path with -i option, but you must be careful with permissions

    Thanked by 1Shamli
  • @budi1413 I'm malaysian...not sure about Jebat......=)

    @sergiodelatorre generating the key is not an issue for me...I'm usually use putty to generate them....but,for executing the php script to authenticate ssh connection using key file,where do I need to place them(the private key)....?

  • edited December 2013

    SSH keys are stored in /home/user/.ssh

    If you run su - user, the private key should be saved under the relative user .ssh directory

    The public key must be added to /home/user/.ssh/authorized_keys

  • edited December 2013

    Only one question, why you don't put the first php script in the remote server? It's easier and safer

  • @sergiodelatorre I dont want to have any agent or php or apache or anything except ssh running on the remote server(for this monitoring purpose)...is it possible to have ssh key file location in the script..? or append the key in the script..?

  • edited December 2013

    Another solution: Cronjob in the remote server (5min), creating a static html file and sending to localhost (or wherever). It's safer

    Thanked by 1Shamli
  • @sergiodelatorre a good idea....I can use the link provided by @jebat_ks...

  • @Shamli just curious I'm a big fan of uptime monitoring scripts and services..And so without offering my own opinion wanted to see what you are planning to do for notifications- possible SMS alerts and so forth. Have you thought beyond just the script yet about these things?

  • Hi @natestamm that's also in my head...but not for now....i'm looking for this kind of monitoring to avoid having any overhead on the remote server,or to avoid issue if there are any problem with the agent on the remote server.....

    for now,i'm looking to make this ssh check into existing monitoring script....if there are none available....for notifications,later,maybe....=)

  • This kind of thing really isn't hard to make, I'm using exactly that setup for my monitoring, just make sure you restrict your remote SSH users to only the commands necessary for monitoring. I moved on to running a bash script locally and outputting to a file then transferring the file to the monitoring server otherwise it can take some time to poll the servers.

    You can look into observium all it requires is SNMPd running on your remote servers and eliminates the need to A. write your own script, B. mess with key based authentication and such and it provides nifty graphs.

  • drserverdrserver Member, Host Rep

    Why dont you run uptime on remote server then passed variable via wget postdata to central php script ?

  • ShamliShamli Member
    edited December 2013

    @nunim great idea...but i'm looking for something simple to setup on new server....create user->set key based auth->monitor from remote.....

    @Spirit looking at them....

    @drserver if possible,i dont want to have anything run on the remote server....all from central server...

  • mikhomikho Member, Host Rep

    Not sure if it was said in one of the posts above, I admit to not reading them properly. :)

    My suggestion would be a cronjob on the monitoring server executing a script that fetches the information about the remote server and outputs to file on the monitoring server. Then process needed information on the monitoring server.
    No need to let php handle the ssh connection, could end up with unresponsive site if not handled correct.

Sign In or Register to comment.