shell-腳本集合2

火星人 @ 2014-03-08 , reply:0


shell-腳本集合

shell-腳本集合2

監控之檢查磁碟

  1. $ cat chk_df.sh
  2. #!/bin/bash
  3. #
  4. #check the disk usage via ssh
  5. #variables
  6. let dflimit=90
  7. crondir="crontab"
  8. source CONFIG
  9. #main
  10. #執行ssh 命令
  11. for HOST in $ALLHOSTS ;do
  12. flag_disk_file=$crondir/log/"$HOST".disk
  13. log=$crondir/log/disk_error.log
  14. capacity=$(ssh root@$HOST "df" | grep "/dev/" | sed 's/\%//' | awk '{print $5}')
  15. let flags=0
  16. #判斷ssh命令返回結果
  17. for used in $capacity ;do
  18. if [ $used -ge $dflimit ];then
  19. let flags=1
  20. break
  21. fi
  22. done
  23. #如果磁碟超過限制,則發送報警郵件

  24. if [ "$flags" -eq "1" -a ! -f "$flag_disk_file" ];then
  25. #for mobile in "$MOBILES";do
  26. #echo "$HOST disk will full" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
  27. #done
  28. echo "$HOST disk will full" | mail -s "$HOST disk will full" $MAILS
  29. date '%F %T' >>$log
  30. echo "$HOST disk will full" >> $log
  31. echo "disk_error" >$flag_disk_file
  32. fi
  33. #如果磁碟正常,則發郵件解除報警郵件
  34. if [ "$flags" -eq "0" -a -f "$flag_disk_file" ];then
  35. for mobile in "$MOBILES";do
  36. echo "$HOST disk ok"|/usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
  37. done
  38. echo "$HOST disk ok" | mail -s "$HOST disk ok" $MAILS
  39. date '%F %T' >>$log
  40. echo "$HOST disk ok" >> $log
  41. rm -f $flag_disk_file
  42. fi
  43. done

監控之檢查系統負載

  1. $ cat chk_load.sh
  2. #!/bin/bash
  3. #
  4. #check the system load through ssh
  5. #variables
  6. let loadlimit=3
  7. sh_dir=/root/sh/
  8. crondir=${sh_dir}crontab
  9. log=${crondir}/log/system_load.log
  10. source ${sh_dir}/CONFIG
  11. #main
  12. #執行ssh 命令
  13. for HOST in $ALLHOSTS ;do
  14. LOAD=$(ssh root@$HOST "/bin/cat /proc/loadavg" | awk '{print $1}' | awk -F. '{print $1}')
  15. if [ -z "$LOAD" ];then
  16. echo "$HOST is null"
  17. continue
  18. fi
  19. #判斷ssh命令返回結果
  20. if [ "$LOAD" -lt "$loadlimit" ];then
  21. #echo "$HOST load is OK!"
  22. #如果找到load文件,則發送解除報警郵件
  23. if [ -f $crondir/log/"$HOST".load ];then

  24. #for mobile in $MOBILES;do
  25. # echo "$HOST load ok"|/usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
  26. #done
  27. echo "$HOST load ok" | mail -s "$HOST load ok" $MAILS
  28. #刪除load文件
  29. rm -f $crondir/log/"$HOST".load
  30. date '%F %T' >>$log
  31. echo "$HOST load ok" >> $log
  32. fi
  33. else
  34. #echo "$HOST is overload!"
  35. #不能找到load文件,則發送報警郵件
  36. if [ ! -f $crondir/log/"$HOST".load ];then
  37. #for mobile in $MOBILES;do
  38. # echo ""$HOST" overload"|/usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
  39. #done
  40. echo "$HOST overload" | mail -s "$HOST overload" $MAILS
  41. #生成load文件
  42. echo "overload" >$crondir/log/"$HOST".load
  43. date '%F %T' >>$log
  44. echo "$HOST overload" >> $log

  45. fi
  46. fi
  47. done

監控之檢查網路(ping)

  1. $ cat chk_ping.sh
  2. #!/bin/bash
  3. #
  4. #Use the ping command to check network
  5. #
  6. #$ ping -f -c 10 10.0.100.1
  7. #PING 10.0.100.1 (10.0.100.1) 56(84) bytes of data.
  8. #.........E
  9. #--- 10.0.100.1 ping statistics ---
  10. #10 packets transmitted, 0 received, 3 errors, 100% packet loss, time 120ms
  11. #, pipe 3
  12. #$ ping -f -c 10 10.0.100.71
  13. #PING 10.0.100.71 (10.0.100.71) 56(84) bytes of data.
  14. #
  15. #--- 10.0.100.71 ping statistics ---
  16. #10 packets transmitted, 10 received, 0% packet loss, time 0ms
  17. #rtt min/avg/max/mdev = 0.003/0.009/0.056/0.015 ms, ipg/ewma 0.029/0.019 ms
  18. #
  19. #$ echo $?
  20. #1 #返回值1
  21. #
  22. #variables
  23. sh_dir=/root/sh/
  24. crondir

    =${sh_dir}crontab

  25. source ${sh_dir}/CONFIG
  26. DELAY_LIMIT=100
  27. LOSS_LIMIT=20
  28. HOSTS="$ALLHOSTS $WEBHOSTS"
  29. #main
  30. #每個ip
  31. for HOST in $HOSTS ;do
  32. flag_ping_file="$crondir/log/$HOST.ping"
  33. flag_ping_fatal="$crondir/log/$HOST.ping.fatal"
  34. error_log="$crondir/log/ping_error.log"
  35. #返回值0,定義delay 與 loss 變數
  36. if ping -f -c 50 $HOST >$crondir/log/ping.log;then
  37. grep "rtt min" $crondir/log/ping.log >$crondir/log/ping.delay
  38. grep "packet loss" $crondir/log/ping.log >$crondir/log/ping.loss
  39. delay=`awk -F= '{print $2}' $crondir/log/ping.delay | awk -F/ '{print $2}'| awk -F. '{print $1}'`

  40. loss=`awk '{print $6}' $crondir/log/ping.loss | awk -F% '{print $1}'`
  41. #判斷delay 或者loss 大於規定限制,則發送報警郵件
  42. if [ "$delay" -ge "$DELAY_LIMIT" -o "$loss" -ge "$LOSS_LIMIT" ];then
  43. if [ ! -f $flag_ping_file ];then
  44. #for mobile in $MOBILES;do
  45. #echo "Monitor to $HOST delay $delay'ms;$loss% packet loss" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
  46. #done
  47. echo "Monitor to $HOST delay $delay'ms;$loss% packet loss" | mail -s "Monitor to $HOST delay $delay'ms;$loss% packet loss" $MAILS
  48. date '%F %T' >>$error_log
  49. cat $crondir/log/ping.log >>$error_log
  50. #生成用於判斷的flag文件
  51. echo "$HOST network error" >$flag_ping_file
  52. fi
  53. fi
  54. #判斷delay 或者loss 小於規定限制,則發送解除報警郵件
  55. if [ "$delay" -lt "$DELAY_LIMIT" -a "$loss" -lt "$LOSS_LIMIT" -a -f $flag_ping_file ];then
  56. echo "$HOST network ok" | mail -s "$HOST network ok" $MAILS
  57. rm -f $flag_ping_file
  58. if [ -f $flag_ping_fatal ];then

  59. #for mobile in $MOBILES;do
  60. #echo "$HOST network ok"|/usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
  61. #done
  62. #刪除用於判斷的flag文件
  63. rm -f $flag_ping_fatal
  64. fi
  65. fi
  66. #
  67. else
  68. #返回值1,則直接判斷為失敗,發送報警郵件
  69. if [ ! -f $flag_ping_fatal ];then
  70. #for mobile in $MOBILES;do
  71. #echo "Monitor to $HOST fatal;100% packet loss" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
  72. #done
  73. echo "Monitor to $HOST fatal;100% packet loss" | mail -s "Monitor to $HOST fatal;100% packet loss" $MAILS
  74. date '%F %T' >>$error_log
  75. cat $crondir/log/ping.log >>$error_log
  76. echo "$HOST network fatal" >$flag_ping_fatal
  77. echo "$HOST network error" >$flag_ping_file
  78. fi
  79. fi
  80. done

監控之檢查mysql 主從狀態

  1. $ cat chk_mysql_replicate.sh

  2. #!/bin/bash
  3. #
  4. #check the mysql replicate
  5. #
  6. #/usr/local/mysql/bin/mysql -uroot -pdongnan -e 'show slave status\G' -ss | awk '{print $2}' | head -n 13 | tail -n2
  7. #Yes
  8. #Yes
  9. #
  10. #
  11. #variables
  12. let dflimit=90
  13. sh_dir=/root/sh/
  14. crondir=${sh_dir}crontab
  15. source ${sh_dir}/CONFIG
  16. HOSTS="$DB_SLAVE_HOSTS"
  17. #main
  18. for HOST in $HOSTS;do
  19. ssh root@$HOST "/usr/local/mysql/bin/mysql -uroot -pdongnan -e 'show slave status\G' -ss" | awk '{print $2}' | head -n 13 | tail -n2 >"${crondir}/log/chk_mysql_replicate.log"
  20. YN=""
  21. #
  22. while read line;do
  23. YN="$YN $line"
  24. #donevar

    /crontab/log/chk_mysql_replicate.log

  25. done < "${crondir}/log/chk_mysql_replicate.log"
  26. #
  27. if [ "$YN" == " Yes Yes" ];then
  28. echo "$HOST Slave is running!"
  29. if [ -f "${crondir}/log/$HOST.mysql" ];then
  30. #for mobile in $MOBILES;do
  31. #echo ""$HOST"."$i" replicate ok" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
  32. #done
  33. echo "$HOST replicate ok" | mail -s "$HOST replicate ok" $MAILS
  34. rm -f "${crondir}/log/$HOST.mysql"
  35. fi
  36. #
  37. else
  38. echo "$HOST Slave is not running!"
  39. if [ ! -f "${crondir}/log/$HOST.mysql" ];then
  40. #for mobile in $MOBILES;do
  41. #echo ""$HOST"."$i" replicate error" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
  42. #done
  43. echo "$HOST replicate error" | mail -s "$HOST replicate error" $MAILS
  44. echo "replicate error" >"${crondir}/log/$HOST.mysql"
  45. fi

  46. fi
  47. #
  48. done

# Nginx 分割日誌腳本

  1. $ cat nginx_log.sh
  2. #!/bin/bash
  3. #split log
  4. #
  5. #Nginx
  6. #Signal Action
  7. #TERM, INT Terminate the server immediately
  8. #QUIT Stop the server
  9. #HUP Configuration changes, start new workers, graceful stop of old workers
  10. #USR1 Reopen log files
  11. #USR2 Upgrade the server executable
  12. #WINCH Graceful Stop (parent process advise the children to exit)
  13. #variables
  14. log_dir=/data/logs/yum/
  15. log_date=$(date '%F')
  16. nginx_pid=/usr/local/nginx/logs/nginx.pid
  17. keep_days=30
  18. #old_log
  19. find "$log_dir" -name "*\.log" -type f -mtime ${keep_days} -exec rm -rf {} \;
  20. #new_log
  21. if [ -e ${log_dir}access-${log_date}.log ];then
  22. echo "Already exists" && exit 1
  23. else
  24. /bin/mv ${log_dir}access.log ${log_dir}access-${log_date}.log && /bin/kill -USR1 $(cat $nginx_pid)
  25. fi

# php 文件發布腳本

  1. cat rsync_www.sh
  2. #!/bin/bash
  3. #
  4. #/usr/bin/rsync -avz --password-file=/root/rsync_pw /var/www/test/test rsync@10.0.100.82::www/test
  5. #echo "rsync_password" > rsync_pw && chmod 600 rsync_pw
  6. #variables
  7. #HOSTS="10.0.100.82 10.0.100.81 10.0.100.83"
  8. HOSTS="10.0.100.82"
  9. MOD=www
  10. MAILADD="root@localhost"
  11. DATE=`date %F_%T`
  12. src_dir=/var/www/html/
  13. rsync_pw=/root/sh/rsync_pw
  14. rsync_user=dongnan
  15. rsync_file_dir=/var/www/rsync/
  16. log_dir

    =/var/www/rsync/log/

  17. regular_replace=/data/
  18. #USERNAME=`logname`
  19. if [ -n "$SUDO_USER" ];then
  20. USERNAME=`echo $SUDO_USER`
  21. else
  22. USERNAME="root"
  23. fi
  24. #function
  25. rsync_www(){
  26. for host in $HOSTS;do
  27. #if /usr/bin/rsync -avz ${src_dir}$1 $host::${MOD}/$1;then
  28. if /usr/bin/rsync -avz --password-file="$rsync_pw" ${src_dir}$1 ${rsync_user}@${host}::${MOD}/$1;then
  29. echo "$DATE ${src_dir}$1 rsync to $host ok by $USERNAME" >> ${log_dir}rsync.ok
  30. else
  31. echo "$DATE ${src_dir}$1 rsync to $host error by $USERNAME" >> ${log_dir}rsync.error
  32. echo "$DATE ${src_dir}$1 rsync to $host error by $USERNAME."| mail -s "rsync error" $MAILADD
  33. fi
  34. done
  35. }
  36. #main
  37. #刪除空白或空行
  38. sed -ri 's/^[ \t] //g;/^$/d' ${rsync_file_dir}rsync_file.list

  39. #
  40. sed -e 's#/var/www/html/##g' ${rsync_file_dir}rsync_file.list > ${log_dir}rsync.tmp
  41. while read line;do
  42. if [ ! -d "$src_dir$line" -a ! -f "$src_dir$line" ];then
  43. echo "$DATE ${src_dir}$line is't existence by $USERNAME!!!" | tee -a ${log_dir}rsync.error | mail -s "rsync error" $MAILADD
  44. else
  45. rsync_www $line
  46. fi
  47. done < ${log_dir}rsync.tmp

#

#






[火星人 via ] shell-腳本集合2已經有244次圍觀

http://www.coctec.com/docs/linux/show-post-45973.html