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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<blockquote> #!/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_ > $logfile <html> <head><title>Server stats</title></head> <body> <h1>Server statistics for $HOSTNAME</h1> <p>Updated on $(date +"%x %r %Z") by $USER</p> <h2> Logged in users:</h2><br /> <pre> $WHO |
Processor stats:
1 |
$MPSTAT |
Virtual memory stats:
1 |
$VMSTAT |
Top twenty memory hog applications:
1 |
$PS_MEM |
Top twenty CPU hogging applications:
1 |
$PS_CPU |
Free memory:
1 |
$FREE |
Processor information:
1 |
$PROCINFO |
Established connections:
1 |
$NETSTAT |
Errors
In the mail logs:
1 |
$MAILLOGS |
MySQL logs
1 |
$MYSQLLOGS |
Apache2 logs
1 |
$APACHE2LOGS |
Icecast2 logs
1 |
$ICECASTLOGS |
Site backups
SiteA
1 |
$SiteABACKUP |
SiteB
1 |
$SiteBBACKUP |
Return to the index