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.
Note: There are a few lines here relating to siteA and SiteB that are logs that have been created by another script. Unless you are following all of the linux backup and system stats documentation on this site, you can remove these lines.
#!/bin/bash logfile=/home/YourUserName/public_html/sites/default/files/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` SiteABACKUP=`tail -n5 /home/YourUserName/backups/SiteA.log` SiteBBACKUP=`tail -n5 /home/YourUserName/backups/SiteB.log` cat <<- _EOF_ > $logfileServer stats Server statistics for $HOSTNAME
Updated on $(date +"%x %r %Z") by $USER
Logged in users:
$WHOProcessor stats:
$MPSTATVirtual memory stats:
$VMSTATTop twenty memory hog applications:
$PS_MEMTop twenty CPU hogging applications:
$PS_CPUFree memory:
$FREEProcessor information:
$PROCINFOEstablished connections:
$NETSTATErrors
In the mail logs:
$MAILLOGSMySQL logs
$MYSQLLOGSApache2 logs
$APACHE2LOGSIcecast2 logs
$ICECASTLOGSSite backups
SiteA
$SiteABACKUPSiteB
$SiteBBACKUP
Return to the index
_EOF_
echo "$(date +%Y%m%d)
" >> /home/YourUserName/public_html/sites/default/files/logs/index.html