This is a text-only version of the following page on https://raymii.org: --- Title : Ray-Mon - PHP and Bash server status monitoring Author : Remy van Elst Date : 22-10-2012 URL : https://raymii.org/s/software/Bash_PHP_Server_Status_Monitor.html Format : Markdown/HTML --- ![screenshot][1] <p class="ad"> <b>Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:</b><br><br> <a href="https://leafnode.nl">I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!</a><br><br> <a href="https://github.com/sponsors/RaymiiOrg/">Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.</a><br><br> <a href="https://www.digitalocean.com/?refcode=7435ae6b8212">You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days. </a><br><br> </p> Ray-Mon is a linux server monitoring script written in PHP and Bash, utilizing JSON as data storage. It requires only bash and a webserver on the client side, and only php on the server side. The client currently supports monitoring processes, uptime, updates, amount of users logged in, disk usage, RAM usage and network traffic. #### Features * Ping monitor * History per host * Threshold per monitored item. * Monitors: * Processes (lighttpd, apache, nginx, sshd, munin etc.) * RAM * Disk * Uptime * Users logged on * Updates * Network (RX/TX) #### Download Either _git clone_ the github repo: git clone git://github.com/RaymiiOrg/raymon.git Or download the zipfile from github: <https://github.com/RaymiiOrg/raymon/zipball/master> Or download the zipfile from Raymii.org <https://raymii.org/s/inc/software/raymon-0.0.2.zip> This is the github page: <https://github.com/RaymiiOrg/raymon/> #### Changelog ##### v0.0.2 * Server side now only requires 1 script instead of 2. * Client script creates the json better, if a value is missing the json file doesn't break. * Changed the visual style to a better layout. * Thresholds implemented and configurable. * History per host now implemented. ##### v0.0.1 * Initial release #### Install ##### Client The client.sh script is a bash script which outputs JSON. It requires root access and should be run as root. It also requires a webserver, so that the server can get the json file. Software needed for the script: * bash * awk * grep * ifconfig * package managers supported: apt-get, yum and pacman (debian/ubuntu, centos/RHEL/SL, Arch) Setup a webserver (lighttpd, apache, boa, thttpd, nginx) for the script output. If there is already a webserver running on the server you dont need to install another one. Edit the script: Network interfaces. First one is used for the IP, the second one is used for bandwidth calculations. This is done because openVZ has the "venet0" interface for the bandwidth, and the venet0:0 interface with an IP. If you run bare-metal or KVM or vserver etc. you can set these two to the same value (eth0 eth1 etc). # Network interface for the IP address iface="venet0:0" # network interface for traffic monitoring (RX/TX bytes) iface2="venet0" The IP address of the server, this is used by me when deploying this script via chef or ansible. You can set it, but it is not required. Services are checked by doing a _ps_ to see if the process is running. The last service should be defined without a comma, for valid JSON. The code below monitors "sshd", "lighttpd", "munin-node" and "syslog". SERVICE=lighttpd if ps ax | grep -v grep | grep $SERVICE > /dev/null; then echo -n ""$SERVICE" : "running","; else echo -n ""$SERVICE" : "not running","; fi SERVICE=sshd if ps ax | grep -v grep | grep $SERVICE > /dev/null; then echo -n ""$SERVICE" : "running","; else echo -n ""$SERVICE" : "not running","; fi SERVICE=syslog if ps ax | grep -v grep | grep $SERVICE > /dev/null; then echo -n ""$SERVICE" : "running","; else echo -n ""$SERVICE" : "not running","; fi #LAST SERVICE HAS TO BE WITHOUT , FOR VALID JSON!!! SERVICE=munin-node if ps ax | grep -v grep | grep $SERVICE > /dev/null; then echo -n ""$SERVICE" : "running""; else echo -n ""$SERVICE" : "not running""; fi To add a service, copy the 2 lines and replace the SERVICE=processname with the actual process name: SERVICE=processname if ps ax | grep -v grep | grep $SERVICE > /dev/null; then echo -n ""$SERVICE" : "running","; else echo -n ""$SERVICE" : "not running","; fi And, make sure the last service montiored does not echo a comma at the end, else the JSON is not valid and the php script fails. Now setup a cronjob to execute the script on a set interval and save the JSON to the webserver directory. As root, create the file /etc/cron.d/raymon-client with the following contents: SHELL=/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin */5 * * * * root /root/scripts/client.sh | sed ':a;N;$!ba;s/n//g' > /var/www/stat.json In my case, the client script is in _/root/scripts_ , and my webserver directory is _/var/www_. Change this to your own setup. Also, you might want to change the time interval. */5 executes every 5 minutes. The sed line is in there to remove the newlines, this creates a shorter JSOn file, saves some KB's. The _root_ after the cron time is special for a file in _/etc/cron.d/_ , it tells cron as which user it has to execute the crontab file. When this is setup you should get a stat.json file in the /var/www/ folder containing the status json. If so, the client is setup correctly. ##### Server The status server is a php script which fetches the json files from the clients every 5 minutes, saves them and shows them. It also saves the history, but that is defined below. Requirements: * Webserver with PHP (min. 5.2) and write access to the folder the script is located. Steps: Create a new folder on the webserver and make sure the webserver user (www-data) can write to it. Place the php file "stat.php" in that folder. Edit the host list in the php file to include your clients: The first parameter is the filename the json file is saved to, and the second is the URL where the json file is located. $hostlist=array( 'example1.org.json' => 'http://example1.org/stat.json', 'example2.nl.json' => 'http://example2.nl/stat.json', 'special1.network.json' => 'http://special1.network.eu:8080/stat.json', 'special2.network.json' => 'https://special2.network.eu/stat.json' ); Edit the values for the ping monitor: $pinglist = array( 'github.com', 'google.nl', 'tweakers.net', 'jupiterbroadcasting.com', 'lowendtalk.com', 'lowendbox.com' ); Edit the threshold values: ## Set this to "secure" the history saving. This key has to be given as a parameter to save the history. $historykey = "8A29691737D"; #the below values set the threshold before a value gets shown in bold on the page. # Max updates available $maxupdates = "10"; # Max users concurrently logged in $maxusers = "3"; # Max load. $maxload = "2"; # Max disk usage (in percent) $maxdisk = "75"; # Max RAM usage (in percent) $maxram = "75"; ###### History To save the history you have to setup a cronjob to get the status page with a special "history key". You define this in the stat.php file: ## Set this to "secure" the history saving. This key has to be given as a parameter to save the history. $historykey = "8A29691737D"; And then the cronjob to get it: ## This saves the history every 8 hours. 30 */8 * * * wget -qO /dev/null http://url-to-status.site/status/stat.php?action=save&&key=8A29691737D The cronjob can be on any server which can access the status page, but preferably on the host where the status page is located. [1]: http://i.imgur.com/AccDKyZ.png [2]: https://www.digitalocean.com/?refcode=7435ae6b8212 --- License: All the text on this website is free as in freedom unless stated otherwise. This means you can use it in any way you want, you can copy it, change it the way you like and republish it, as long as you release the (modified) content under the same license to give others the same freedoms you've got and place my name and a link to this site with the article as source. This site uses Google Analytics for statistics and Google Adwords for advertisements. You are tracked and Google knows everything about you. Use an adblocker like ublock-origin if you don't want it. All the code on this website is licensed under the GNU GPL v3 license unless already licensed under a license which does not allows this form of licensing or if another license is stated on that page / in that software: This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. Just to be clear, the information on this website is for meant for educational purposes and you use it at your own risk. I do not take responsibility if you screw something up. Use common sense, do not 'rm -rf /' as root for example. If you have any questions then do not hesitate to contact me. See https://raymii.org/s/static/About.html for details.