If you have a server which is pretty much, of course you will find it hard to monitor your server load. You can buy software monitoring, but the price is quite expensive. In this article will explain how to create a monitoring server with bash and mysql.

The concept of monitoring script is very simple, server write load conditions and mail queue to the database server (remote or local) you can cultivate the database and display with php propgram.

Here the figure :
Simple Server monitoring

In MySQL Server

Create table and use this mysql query :

CREATE TABLE IF NOT EXISTS `YOURTABLENAME` (
  `id` int(7) NOT NULL AUTO_INCREMENT,
  `hostname` varchar(50) NOT NULL,
  `date` datetime NOT NULL,
  `sload` varchar(6) NOT NULL,
  `mqueue` int(6) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=23 ;

In Your Server

Create directory and create file with name servercek.sh.

mkdir /bashscripts
cd /bashscripts
nano servercek.sh

And paste this code. You can customize the mysql server parameter with your own.

#!/bin/bash

# MySQL Parameter
H=YOURMYSQLHOSTORIP
U=YOURMYSQLUSERNAME
P=YOURMYSQLPASSWORD
D=YOURDATABASENAME

# HOSTNAME INFORMATION
THEHOST=$(hostname)

# Get Server Load Information
LOAD=$(cat /proc/loadavg)
CURRENTLOAD=${LOAD:0:4}

# Get Current Date
CURDATE=$(date +"%Y-%m-%d %H:%M:%S")

# Check Total of Mail QUEUE
QUEUE=$(/usr/sbin/exim -bpc)

#Update Current Load
mysql -u $U -p$P -D $D -h $H -e "UPDATE currentload SET date='$CURDATE', sload='$CURRENTLOAD', mqueue='$QUEUE' WHERE hostname='$THEHOST'"

Create new cronjob every 30 second to run scripts

crontab -e
* * * * * sh /bashscripts/servercek.sh > /dev/null 2>&1
* * * * * sleep 30; sh /bashscripts/servercek.sh > /dev/null 2>&1

Here the example monitoring with php

Visual Server Monitoring Example

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>