Guest User

for_LOR

a guest
Apr 28th, 2015
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.12 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ### basic variables ###
  4.  
  5. pidfile=/var/run/check_info.pid
  6. _pid=$(cat ${pidfile} 2> /dev/null)
  7. dev_mnt_home=vda1
  8. io_temp=/tmp/io.tmp
  9. db_temp=/tmp/db.tmp
  10. used_home_temp=/tmp/home.tmp
  11. access_log=/root/access.log
  12.  
  13. touch ${used_home_temp}
  14. touch ${io_temp}
  15. touch ${db_temp}
  16.  
  17. ### check and alert variable ###
  18. _before=$(cat ${used_home_temp} | tail -3600 | head -1 | cut -dM -f1)
  19. _after=$(cat ${used_home_temp} | tail -3600 | tail -1 | cut -dM -f1)
  20. _mail=a.posadsky@hotmail.com
  21. pidfile_alert=/var/run/check_info_alert.pid
  22. _pid_alert=$(cat ${pidfile_alert} 2> /dev/null)
  23.  
  24. ### mysql variables ###
  25.  
  26. DBuser=root
  27. DBpassword=ai1Aimei
  28. DBname=
  29. DBhost=localhost
  30.  
  31. colinfo () {
  32.  
  33. if [ -e "$pidfile" ]; then
  34.         if [ -e /proc/${_pid} ]; then
  35.                 echo "Daemon alredy started. PID=${_pid}"
  36.                 exit 0
  37.         fi
  38. fi
  39.  
  40. cd /
  41.  
  42. echo "ooookaaaaaay..."
  43.  
  44. exec > /root/112233 #debug
  45. exec 2> /root/112233 #debug
  46. exec < /dev/null
  47.  
  48. (
  49. trap "{ rm -r $pidfile; exit 255; }"  TERM INT EXIT
  50.  
  51. while [ 1 ]
  52.         do
  53.  
  54.                 #Disk I/O
  55.                 echo "`date +%H:%M:%S` `cat /proc/diskstats | \
  56.                grep ${dev_mnt_home} | awk {'print $12'}`" >> ${io_temp}
  57.  
  58.                 #circle mode for tmp-files (debug needed)
  59. #               grep `date +%H:%M:%S -d "5 seconds ago"` /tmp/io.tmp && \
  60. #               sed '1,/`date +%H:%M:%S -d "5 seconds ago`/ d' < /tmp/io.tmp
  61.  
  62.                 #mysql_threads
  63.                 echo "`date +%H:%M:%S` `echo "SHOW STATUS LIKE 'Threads_running';" | \
  64.                mysql -u${DBuser} -p${DBpassword} | tail -n1 | \
  65.                awk {'print $2'}`" >> ${db_temp}
  66.  
  67.                 #used_home
  68.  
  69.                 if [ "$dev_mnt_home" ]; then
  70.                         df -h -BM /dev/${dev_mnt_home} | awk {'print $4'} | tail -1 >> ${used_home_temp}
  71.                 fi
  72.  
  73.                 echo $BASHPID > $pidfile
  74.                 sleep 1
  75.         done
  76.  
  77. exit 0
  78.  
  79. )&
  80.  
  81. }
  82.  
  83. stop () {
  84.  
  85. if [ -e "$pidfile" ]; then
  86.         kill ${_pid} && \
  87.         kill ${_pid_alert} && \
  88.         echo "Daemon successfully stopped" || \
  89.         echo "Daemon not stopped"
  90. else
  91.         echo "Daemon not started"
  92. fi
  93. }
  94.  
  95. check_info () {
  96.  
  97. if [ -e "$pidfile" ]; then
  98.  
  99.         top_sites=$(awk {'print $7'} ${access_log} | \
  100.         sort -n -k2 | uniq -c | \
  101.         sort -nr | head -n10 | \
  102.         awk {'print $2'})
  103.         echo -e "############\n#Top Sites:#\n############\n$top_sites\n\n"
  104.         sleep 2
  105.  
  106.         for _top in ${top_sites}
  107.                 do
  108.                         top_ip=$(awk {'print $6,$7'} ${access_log} | \
  109.                         grep "$_top" | sort -n | \
  110.                         uniq -c | sort -nr | \
  111.                         head -n10 | awk {'print $2'})
  112.                         echo -e "#############################\n#Top IP's for ${_top}:#\
  113.                        \n#############################\n${top_ip}\n\n"
  114.                         sleep 2
  115.                 done
  116.  
  117. echo -e "#####################\n#\
  118. Max. Mysql Threads:#\n#####################\n`\
  119. awk {'print $2'} /tmp/db.tmp | tail -n300 | sort -unk1 | tail -n1`\n\n"
  120.  
  121. sleep 2
  122.  
  123. echo -e "######################################\n#\
  124. Load Average (I/O) /home \
  125. (/dev/${dev_mnt_home})#\n######################################\n\
  126. `awk '{sum+=$2} END { print sum/NR }' /tmp/io.tmp`\n\n"
  127.  
  128. _delta=`echo $((($_before-$_after))) 2> /dev/null`
  129.  
  130. if [ "$_before" -gt "$_after" ]; then
  131.         echo -e "##############\n#/home usage:#\n##############
  132.        \nBefore:${_before}M After:${_after}M. Added ${_delta}M"
  133.  
  134. elif [ "$_before" -lt "$_after" ]; then
  135.         echo -e "##############\n#/home usage:#\n##############
  136.        \nBefore:${_before}M After:${_after}M. Deleted ${_delta}M"
  137.  
  138. else
  139.         echo -e "##############\n#/home usage:#\n##############
  140.        \nBefore:${_before}M After:${_after}M."
  141.  
  142. fi
  143.  
  144. else
  145.         echo "Daemon not started. Run daemon first"
  146. fi
  147. }
  148.  
  149. alert () {
  150.  
  151. exec > /dev/null
  152. exec 2> /dev/null
  153. exec < /dev/null
  154.  
  155. cd /
  156. (
  157. #send_alert=`mail -s "$0 Critical Alert" ${_mail}`
  158. while [ 1 ]
  159.         do
  160.                 _delta=`echo $((($_before-$_after))) 2> /dev/null`
  161.  
  162.                 if [ "$_delta" -ge 5000 ]; then
  163.                         echo -e "Диск заюзан более чем на 5ГБ за час, всё плохо" | mail -s "$0: всё очень плохо" ${_mail}
  164.                 fi
  165.  
  166.                 if [ "`awk {'print $2'} /tmp/db.tmp | tail -n600 | sort -unk1 | tail -n1`" -ge 100 ]; then
  167.                         echo -e "Количество тредов mysql привысило 100 за 10 минут, всё плохо" | mail -s "$0: critical alert" ${_mail}
  168.                 fi
  169.  
  170.                 echo $BASHPID > $pidfile_alert
  171.                 sleep 120
  172.         done
  173. )&
  174.  
  175. }
  176.  
  177. case $1 in
  178.  
  179. start)
  180.         colinfo;
  181.         alert;
  182. ;;
  183.  
  184. stop)
  185.         stop;
  186. ;;
  187.  
  188. status)
  189.         if [ -e "$pidfile" ]; then
  190.                 echo "Daemon started"
  191.         else
  192.                 echo "Daemon stopped"
  193.         fi
  194. ;;
  195.  
  196. check)
  197.         check_info;
  198. ;;
  199.  
  200. alert)
  201. alert;
  202. ;;
  203.  
  204. *)
  205.         echo "Usage:$0 start/stop/status/check"
  206. ;;
  207.  
  208. esac
Advertisement
Add Comment
Please, Sign In to add comment