Remote Monitoring System with Minimum Coding

By | August 23, 2017

This is the example how to organize remote monitoring of processes running on Linux devices.
Let us create simple script which executes ps command, saves results in html file and uploads the file in http server. 2 arguments are required to run the script: the fist one is html file name without extension and the second one is http server IP address or fqdn.

#!/bin/bash
if [ $# -ne 2 ]; then
# 2 arguments are required
# file name
# server IP or fqdn
   exit -1
fi
echo "<html><title>process monitoring</title><body>" > "/tmp/$1.htm"
datetime=$(date)
echo "Local time: $datetime<br><pre>" >> "/tmp/$1.htm"
ps -eo pid,ppid,%cpu,rss,vsz,comm,user >> "/tmp/$1.htm"
echo "</pre></body></html>" >> "/tmp/$1.htm"
curl -i -X POST --form filedata=@/tmp/$1.htm --form userid=qwerty \
https://$2/monitoring/mon.php > /dev/null 2>&1
rm -f "/tmp/$1.htm"

Save script as ps_mon.sh file and make it executable:

chmod 755 ps_mon.sh

In other words the script is simple http client, which usually is not blocked by firewall.
Remote monitoring

Now we add script into cron scheduler with stat interval 5 minute.
Open crontab file as using command crontab -e and add the following line in the end:

*/5 * * * * /projects/ps_mon.sh process ladydebug.com

Where the first argument process is name of html file on the server side where data should be saved, the second argument ladydebug.com is server name.

You may check in log file that the script is started periodically. On Ubuntu check /var/log/syslog file, on Centos /var/log/cron file.


# tail -f /var/log/syslog | grep ps_mon
Aug 22 20:40:01 smike19-VirtualBox CRON[18396]: (root) CMD (/Alex/projects/ps_mon.sh process ladydebug.com)
Aug 22 20:45:01 smike19-VirtualBox CRON[18433]: (root) CMD (/Alex/projects/ps_mon.sh process ladydebug.com)
Aug 22 20:50:01 smike19-VirtualBox CRON[18456]: (root) CMD (/Alex/projects/ps_mon.sh process ladydebug.com)

Now open the Web browser you check the list of processes running on your Linux device. The URL is

http://ladydebug.com//monitoring/process.htm

Remote process browsing

If you want to monitor something else for example system disk space usage create similar script for df command, specify the first argument (file name) as df and the URL will be:

http://ladydebug.com//monitoring/df.htm


Pay attention the server adds short extension htm to html file.

You may use ladydebug.com for remote monitoring, specifying your unique html file name. However if you want to configure your own server for collecting monitored data here is the server side php script:


<?php
if ( isset( $_POST['userid'] ) ) {
   $curdir=getcwd();
   $newfile=$curdir."/".basename($_FILES['filedata']['name']);
   echo $newfile;
   echo "\n";
   echo $_FILES['filedata']['size'];
   $tmp_file=$_FILES['filedata']['tmp_name'];
   echo $tmp_file;
   if(move_uploaded_file($tmp_file, $newfile))
   {
      echo "file moved";
   }
   else
   {
      echo "failed";
   }
}
else {
   echo "Not found";
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *