System statistics at a glance.

It’s important to see how servers are doing regularly to enable you to to take a proactive approach to resolving problems.

But, when your using a Linux server with no GUI, it can be easy to become complacent. After all, they so rairly go down and it can take time to type all the commands required to get the information you need.

That’s where Bash scripting comes in. Creating a script to provide you with memory, CPU, disk and bandwidth usage is very useful. Having this available to you on a day to day basis is even better again.

I’ve been looking for something that would give me a tailored report of what’s happening on a server that I run but nothing gave me what I needed. Everything was either too complicated, too process intensive, too graphical or had too many dependencies. So, after looking around for a while, I decided that it would take less time to throw something together my self.

As I say with all these scripts. This is only one way of doing things. It’s not necessarily the best way, but it works none the less.

The first script obtains all the information I need and simply writes it to a text file. This can be sent by email or just stored somewhere for analysis.

#!/bin/bash
logfile=/YourDirectory/output.log
echo Logged in users: >> $logfile
w >> $logfile
echo >> $logfile
echo Processor stats: >> $logfile
mpstat >> $logfile
echo >> $logfile
echo Virtual memory stats: >> $logfile
vmstat >> $logfile
echo >> $logfile
echo Top twenty memory hog applications: >> $logfile
ps -A -o pid,pcpu,pmem,start_time,state,time,comm | perl -e ‘($_ = join “”,<>) =~ s/(t)/ /g; print;’ |sort -g -k 3 -r | head -20 >> logfile
echo >> $logfile
echo Top twenty CPU hogging applications: >> $logfile
ps -A -o pid,pcpu,pmem,start_time,state,time,comm | perl -e ‘($_ = join “”,<>) =~ s/(t)/ /g; print;’ | sort -g -k 2 -r | head -10 >> $logfile
echo >> $logfile
echo Free memory: >> $logfile
free -m >> $logfile
echo >> $logfile
echo Processor information: >> $logfile
procinfo >> $logfile
echo >> $logfile
echo Established connections: >> $logfile
netstat -na |grep -i esta |grep -v 127.0.0.1 |sort -n -t. -k2 >> $logfile

This next script though is where it gets interesting. I’ve created a virtual host and all the files are written to a it’s directory. There’s an index and a separate html file for each day of the week. When the script is called, it populates the html file for the day with the data I want and it adds a line to the index to point to the new file that has just been created. So, instead of getting email every day from a server that is ordinarily very reliable, Touch Wood, I can visit this page every so often to check that everything is ok.

You’ll notice that in this script, I’ve also included checks for mail, Apache2, MySQL and Icecast errors as these are the most important services running on this server.

#!/bin/bash
logfile=/YourDirectory/logs/$(date +%Y%m%d).html
WHO=`w`
MPSTAT=`mpstat`
VMSTAT=`vmstat`
PS_MEM=`ps -A -o pid,pcpu,pmem,start_time,state,time,comm | perl -e ‘($_ = join “”,<>) =~ s/(t)/ /g; print;’ |sort -g -k 3 -r | head -20`
PS_CPU=`ps -A -o pid,pcpu,pmem,start_time,state,time,comm | perl -e ‘($_ = join “”,<>) =~ s/(t)/ /g; print;’ | sort -g -k 2 -r | head -10`
FREE=`free -m`
PROCINFO=`procinfo`
NETSTAT=`netstat -na |grep -i esta |grep -v 127.0.0.1 |sort -n -t. -k2`
APACHE2LOGS=`tail /var/log/apache2/error.log`
MYSQLLOGS=`tail /var/log/mysql.err`
MAILLOGS=`tail /var/log/mail.err`
ICECASTLOGS=`tail /var/log/icecast2/error.log`
cat <<- _EOF_ > $logfile

Server stats

Server statistics for $HOSTNAME

Updated on $(date +”%x %r %Z”) by $USER

Logged in users:

$WHO

Processor stats:

$MPSTAT

Virtual memory stats:

$VMSTAT

Top twenty memory hog applications:

$PS_MEM

Top twenty CPU hogging applications:

$PS_CPU

Free memory:

$FREE

Processor information:

$PROCINFO

Established connections:

$NETSTAT

Errors

In the mail logs:

$MAILLOGS

MySQL logs

$MYSQLLOGS

Apache2 logs

$APACHE2LOGS

Icecast2 logs

$ICECASTLOGS

Return to the index


_EOF_
echo “$(date +%Y%m%d)” >> /YourDirectory/logs/index.html