Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ### basic variables ###
- pidfile=/var/run/check_info.pid
- _pid=$(cat ${pidfile} 2> /dev/null)
- dev_mnt_home=vda1
- io_temp=/tmp/io.tmp
- db_temp=/tmp/db.tmp
- used_home_temp=/tmp/home.tmp
- access_log=/root/access.log
- touch ${used_home_temp}
- touch ${io_temp}
- touch ${db_temp}
- ### check and alert variable ###
- _before=$(cat ${used_home_temp} | tail -3600 | head -1 | cut -dM -f1)
- _after=$(cat ${used_home_temp} | tail -3600 | tail -1 | cut -dM -f1)
- _mail=a.posadsky@hotmail.com
- pidfile_alert=/var/run/check_info_alert.pid
- _pid_alert=$(cat ${pidfile_alert} 2> /dev/null)
- ### mysql variables ###
- DBuser=root
- DBpassword=ai1Aimei
- DBname=
- DBhost=localhost
- colinfo () {
- if [ -e "$pidfile" ]; then
- if [ -e /proc/${_pid} ]; then
- echo "Daemon alredy started. PID=${_pid}"
- exit 0
- fi
- fi
- cd /
- echo "ooookaaaaaay..."
- exec > /root/112233 #debug
- exec 2> /root/112233 #debug
- exec < /dev/null
- (
- trap "{ rm -r $pidfile; exit 255; }" TERM INT EXIT
- while [ 1 ]
- do
- #Disk I/O
- echo "`date +%H:%M:%S` `cat /proc/diskstats | \
- grep ${dev_mnt_home} | awk {'print $12'}`" >> ${io_temp}
- #circle mode for tmp-files (debug needed)
- # grep `date +%H:%M:%S -d "5 seconds ago"` /tmp/io.tmp && \
- # sed '1,/`date +%H:%M:%S -d "5 seconds ago`/ d' < /tmp/io.tmp
- #mysql_threads
- echo "`date +%H:%M:%S` `echo "SHOW STATUS LIKE 'Threads_running';" | \
- mysql -u${DBuser} -p${DBpassword} | tail -n1 | \
- awk {'print $2'}`" >> ${db_temp}
- #used_home
- if [ "$dev_mnt_home" ]; then
- df -h -BM /dev/${dev_mnt_home} | awk {'print $4'} | tail -1 >> ${used_home_temp}
- fi
- echo $BASHPID > $pidfile
- sleep 1
- done
- exit 0
- )&
- }
- stop () {
- if [ -e "$pidfile" ]; then
- kill ${_pid} && \
- kill ${_pid_alert} && \
- echo "Daemon successfully stopped" || \
- echo "Daemon not stopped"
- else
- echo "Daemon not started"
- fi
- }
- check_info () {
- if [ -e "$pidfile" ]; then
- top_sites=$(awk {'print $7'} ${access_log} | \
- sort -n -k2 | uniq -c | \
- sort -nr | head -n10 | \
- awk {'print $2'})
- echo -e "############\n#Top Sites:#\n############\n$top_sites\n\n"
- sleep 2
- for _top in ${top_sites}
- do
- top_ip=$(awk {'print $6,$7'} ${access_log} | \
- grep "$_top" | sort -n | \
- uniq -c | sort -nr | \
- head -n10 | awk {'print $2'})
- echo -e "#############################\n#Top IP's for ${_top}:#\
- \n#############################\n${top_ip}\n\n"
- sleep 2
- done
- echo -e "#####################\n#\
- Max. Mysql Threads:#\n#####################\n`\
- awk {'print $2'} /tmp/db.tmp | tail -n300 | sort -unk1 | tail -n1`\n\n"
- sleep 2
- echo -e "######################################\n#\
- Load Average (I/O) /home \
- (/dev/${dev_mnt_home})#\n######################################\n\
- `awk '{sum+=$2} END { print sum/NR }' /tmp/io.tmp`\n\n"
- _delta=`echo $((($_before-$_after))) 2> /dev/null`
- if [ "$_before" -gt "$_after" ]; then
- echo -e "##############\n#/home usage:#\n##############
- \nBefore:${_before}M After:${_after}M. Added ${_delta}M"
- elif [ "$_before" -lt "$_after" ]; then
- echo -e "##############\n#/home usage:#\n##############
- \nBefore:${_before}M After:${_after}M. Deleted ${_delta}M"
- else
- echo -e "##############\n#/home usage:#\n##############
- \nBefore:${_before}M After:${_after}M."
- fi
- else
- echo "Daemon not started. Run daemon first"
- fi
- }
- alert () {
- exec > /dev/null
- exec 2> /dev/null
- exec < /dev/null
- cd /
- (
- #send_alert=`mail -s "$0 Critical Alert" ${_mail}`
- while [ 1 ]
- do
- _delta=`echo $((($_before-$_after))) 2> /dev/null`
- if [ "$_delta" -ge 5000 ]; then
- echo -e "Диск заюзан более чем на 5ГБ за час, всё плохо" | mail -s "$0: всё очень плохо" ${_mail}
- fi
- if [ "`awk {'print $2'} /tmp/db.tmp | tail -n600 | sort -unk1 | tail -n1`" -ge 100 ]; then
- echo -e "Количество тредов mysql привысило 100 за 10 минут, всё плохо" | mail -s "$0: critical alert" ${_mail}
- fi
- echo $BASHPID > $pidfile_alert
- sleep 120
- done
- )&
- }
- case $1 in
- start)
- colinfo;
- alert;
- ;;
- stop)
- stop;
- ;;
- status)
- if [ -e "$pidfile" ]; then
- echo "Daemon started"
- else
- echo "Daemon stopped"
- fi
- ;;
- check)
- check_info;
- ;;
- alert)
- alert;
- ;;
- *)
- echo "Usage:$0 start/stop/status/check"
- ;;
- esac
Advertisement
Add Comment
Please, Sign In to add comment