Quick and Dirty Monitoring Tool
Published February 23rd, 2010Recently, we needed a simple cron script to record load average, apache server count, existence of lighthttpd and mysql status in a log file every 10 minutes. So following shell script was born:
#!/bin/sh httpd_cnt=`ps auxww | grep '/home/apache/bin/httpd' | wc -l` lighthttpd_cnt=`ps auxww | grep 'lighthttpd' | wc -l` mysql_status=`/usr/bin/mysqladmin -u root -pYOUR_DB_PWD status` load=`/bin/cat /proc/loadavg` now=`date "+%m-%d-%Y %H:%M:%S"` today=`date "+%m-%d-%Y_%H00"` logfile=/var/log/monitor.$today.log lock=/dev/shm/monitor.$today.lock if [ -e $lock ]; then # Already running exit fi; touch $lock; if [ ! -e $logfile ]; then echo "TIME, LOAD AVRG, APACHE COUNT, LIGHTHTTPD COUNT, MYSQL STATUS" > $logfile; fi echo $now, $load, $httpd_cnt, $lighthttpd_cnt, $mysql_status >> $logfile; rm -f $lock;
Once this script was created and save into an executable (r-x) text file, it was scheduled in /etc/crontab to run every 10 minutes as follows:
*/10 * * * * root /root/monitor/monitor.sh
Here is a sample log file created by this script in /var/log:
TIME, LOAD AVRG, APACHE COUNT, LIGHTHTTPD COUNT, MYSQL STATUS 02-23-2010 01:00:01, 0.32 0.19 0.12 1/205 322, 51, 1, Uptime: 316032 Threads: 7 Questions: 30377543 Slow queries: 41 Opens: 1762 Flush tables: 1 Open tables: 512 Queries per second avg: 96.122 02-23-2010 01:10:01, 0.11 0.18 0.13 1/194 702, 40, 1, Uptime: 316632 Threads: 6 Questions: 30402534 Slow queries: 41 Opens: 1762 Flush tables: 1 Open tables: 512 Queries per second avg: 96.019 02-23-2010 01:20:01, 0.15 0.14 0.12 3/230 1093, 77, 1, Uptime: 317232 Threads: 4 Questions: 30425101 Slow queries: 41 Opens: 1762 Flush tables: 1 Open tables: 512 Queries per second avg: 95.908 02-23-2010 01:30:01, 0.13 0.12 0.11 2/221 1466, 68, 1, Uptime: 317832 Threads: 4 Questions: 30449548 Slow queries: 41 Opens: 1762 Flush tables: 1 Open tables: 512 Queries per second avg: 95.804 02-23-2010 01:40:01, 0.26 0.13 0.10 1/200 1782, 46, 1, Uptime: 318432 Threads: 5 Questions: 30475422 Slow queries: 41 Opens: 1762 Flush tables: 1 Open tables: 512 Queries per second avg: 95.705 02-23-2010 01:50:01, 0.58 0.21 0.13 1/218 2142, 63, 1, Uptime: 319032 Threads: 7 Questions: 30501543 Slow queries: 41 Opens: 1762 Flush tables: 1 Open tables: 512 Queries per second avg: 95.607
Leave a comment
Comment Policy: First time comments are moderated. Please be patient.