CA BD NY
  • Categories

  • Recent Posts

  • RSS MySQL Hacker

  • RSS Apache Hacker

  • RSS MiniCTO

  • Meta

  • Quick and Dirty Monitoring Tool

    Published February 23rd, 2010

    Recently, 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:

    Get a Trackback link

    No Comments Yet

    Be the first to comment!

    Leave a comment

    Comment Policy: First time comments are moderated. Please be patient.