使用Redis监控的方式,监控MySQL服务器
- #! /bin/bash
- #告警条件:
- #磁盘使用率超过70%
- #内存使用率超过50%
- #CPU使用率超过700%(服务器是24核心的)
- #空闲线程小于50
- #出现错误或者警告日志
- disk_target=70
- mem_target=50
- cpu_target=700
- idle_target=50
- port=3306
- password='root'
- #设置错误日志的位置
- alert_file=/home/lihuilin/mysql/data/mysql.51vv.err
- alert_file_bak=$alert_file.bak
- error_msg=''
- serverip='127_0_0_1'
- alert_url='http://IP:port/servlet?to=dba&title=MySQLError('$serverip')&content='
- #设置监控日志的位置
- logfile=/home/lihuilin/monitor_mysql.log
- pid=$(ps -ef | grep -w mysqld | grep -v grep | awk '{print $2}')
- if [ "$pid" == "" ] ; then
- echo "[ERROR]MySQL is shutdown." >> $logfile
- error_msg=$error_msg"+MySQL_is_shutdown"
- else
- topcontent=$(top -b -p $pid -n 1 | grep $pid | awk '/mysqld/{print $0}')
- cpuusage=$(echo $topcontent | awk '{print $9}')
- memusage=$(echo $topcontent | awk '{print $10}')
- diskusage=$(df -h | grep % | awk 'NR!=1' | tr -s ' ' | cut -d ' ' -f 2-6 | awk '{ sub(/%/,"",$4); if($4>'$disk_target') {print $0} }')
- idleThread=$(mysql -uroot -p$password -P$port -e 'show full processlist\G' | grep 'State: $' | wc -l)
- alert_content=$(diff alertfilealert_file_bak | grep -E 'ERROR|Warning' | sed 's/\./_/g' | sed 's/:/_/g'| sed 's/ /_/g' | sed 's/-/_/g')
- echo $(date) >> $logfile
- if [ "$alert_content" != "" ] ; then
- echo "[ERROR]$alert_content" >> $logfile
- error_msg=$error_msg"+errorlog#"
- error_msg=$error_msg$alert_content
- fi
- if [ $(echo "scale=2; memusage>mem_target" | bc) -eq 1 ] ; then
- echo "[ERROR]Memory usage:$memusage" >> $logfile
- error_msg=$error_msg"+memtarget#$mem_target"
- error_msg=$error_msg"+memusage#$memusage"
- else
- echo "[INFO]Memory usage:$memusage" >> $logfile
- fi
- if [ $(echo "scale=2; cpuusage>cpu_target" | bc) -eq 1 ] ; then
- echo "[ERROR]CPU usage:$cpuusage" >> $logfile
- error_msg=$error_msg"+cpu_target#$cpu_target"
- error_msg=$error_msg"+cpu_usage#$cpuusage"
- else
- echo "[INFO]CPU usage:$cpuusage" >> $logfile
- fi
- if [ "$diskusage" != "" ] ; then
- echo "[ERROR]Disk usage:$diskusage" >> $logfile
- error_msg=$error_msg"+diskspace#"$diskusage
- fi
- if [ $idleThread -le $idle_target ] ; then
- echo "[ERROR]Idle thread:$idleThread" >> $logfile
- error_msg=$error_msg"+idle_target#$idle_target"
- error_msg=$error_msg"+idleThread#$idleThread"
- else
- echo "[INFO]Idle thread:$idleThread" >> $logfile
- fi
- fi
- if [ "$error_msg" != '' ] ; then
- error_msg=$(echo $error_msg | sed 's/\./_/g' | sed 's/:/_/g'| sed 's/ /_/g' | sed 's/-/_/g'| sed 's/\///g')
- error_msg=$(echo $error_msg | sed 's/\[/_/g' | sed 's/\]/_/g')
- curl alerturlerror_msg
- fi
- cp -rf alertfilealert_file_bak
下图的这种结果是没有办法使用awk抽取数据的。
- df -h | grep % | awk 'NR!=1' | tr -s ' ' | cut -d ' ' -f 2-6
格式化之后就可以使用awk了。
本文转自ICT时空dbasdk的博客,原文链接:脚本监控MySQL服务器 ,如需转载请自行联系原博主。