自定義監控(阿里雲&zabbix)python
目前阿里雲對25端口有限制,沒法在阿里雲上搭建郵件服務器發送告警郵件,若是須要郵件通知,能夠採起下面方法:mysql
1.開同阿里雲企業郵箱sql
2.使用阿里雲監控告警shell
3.第三方zabbix自定義腳本監控json
1、阿里雲自定義監控bash
阿里雲提供自定義監控SDK,這有助於咱們定製化的根據自身業務來作監控,下面我就根據業務需求來介紹一個簡單的自定義監控配置。服務器
阿里提供了2個版本的自定義監控接口:curl
自定義監控SDK(python版) :cms_post.pyide
自定義監控SDK(bash版) :cms_post.shpost
下載地址: http://help.aliyun.com/knowledge_detail.htm?knowledgeId=5974901
使用shell版本作演示
監控mysql主從狀態:
1.建立自定義監控項
腳本post方法說明
1.命名空間中的字符串,用戶aliuid
2.監控項名稱,用戶建立監控項時填寫的名稱
3.監控項值,用戶上報到雲監控的業務數據
4.字段信息,結合監控項,表示具體業務字段意義。
mysql主從監控腳本 cat mysql_check.sh #!/bin/bash export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin export HOSTS=$(hostname) mysql_user='root' mysql_pass='123.com' mail="xuel@51idc.com" data=$(date +%Y-%m-%d-%H:%M) netstat -lntup|egrep ":3306"|grep -v grep>/dev/null0 if [ $? -eq 0 ];then Slave_IO=`mysql -u${mysql_user} -p${mysql_pass} -e "show slave status\G"|grep "Slave_IO_Running:"|awk -F": " '{print $2}'` Slave_SQL=`mysql -u${mysql_user} -p${mysql_pass} -e "show slave status\G"|grep "Slave_SQL_Running:"|awk -F": " '{print $2}'` if [ "$Slave_IO" == "Yes" ] && [ "$Slave_SQL" == "Yes" ];then STAT=1 && echo "$data mysql-status is ok">>/var/log/mysql-status.log else STAT=0 && echo "$data mysql-status is error">>/var/log/mysql-status.log fi else STAT=0 && echo "$data mysql-status is error">>/var/log/mysql-status.log fi /scripts/cms_post.sh 1931875513165389 mysql_status $STAT status=$HOSTS 製做定時任務,沒分鐘調用check腳原本上數據
cms_post.sh 阿里雲上報腳本 #!/bin/bash ######################################### #Usage: sh cms_post.sh #Author: CMS Dev Team #Company: Aliyun Inc. #Version: 1.0 ######################################### #parameters instructions # $1: ali_uid, $2: metric_name, $3: metric_value, $4:fields #convert current time to milliseconds if [[ ! "$#" -eq 4 ]];then echo "usage: $0 userId, metricName, value, dimensions" echo "--multiple dimensions like 'ip=111.111.111.111,hostname=cms'" exit 1 fi #biz time timestamp=`date +%s%N | cut -b1-13` #build dimensions json arr=(${4//,/ }) dimensions="{" for i in "${arr[@]}"; do kv=(${i//=/ }) dimensions=${dimensions}'"'${kv[0]}'":"'${kv[1]}'",' done dimensions=${dimensions%,*} dimensions=${dimensions}'}' #build namespace and metrics userId="$1" namespace="acs/custom/$1" metrics='[{"metricName":"'"$2"'","value":'"$3"',"unit":"None","timestamp":'"$timestamp"',"dimensions":'"$dimensions"'}]' url="http://openNaNs.aliyun.com/metrics/put" params="userId=$userId&namespace=$namespace&metrics=$metrics" echo "params: ${params}" curl -v ${url} --data ${params}
添加完成監控後,監控圖表以下:
告警配置
Status填寫變量HOSTNAME 統計週期填寫告警觸發間隔
在被監控服務器上須要先安裝zabbix-sender 來給zabbix-server提交發送數據
rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/6/x86_64/zabbix-release-3.0-1.el6.noarch.rpm yum install zabbix-sender -y
#!/bin/bash mysql_user='root' mysql_pass='123.com' mail="xuel@51idc.com" data=$(date +%Y-%m-%d-%H:%M) netstat -lntup|egrep ":3306"|grep -v grep>/dev/null0 if [ $? -eq 0 ];then Slave_IO=`mysql -u${mysql_user} -p${mysql_pass} -e "show slave status\G"|grep "Slave_IO_Running:"|awk -F": " '{print $2}'` Slave_SQL=`mysql -u${mysql_user} -p${mysql_pass} -e "show slave status\G"|grep "Slave_SQL_Running:"|awk -F": " '{print $2}'` if [ "$Slave_IO" == "Yes" ] && [ "$Slave_SQL" == "Yes" ];then STAT=1 && echo "$data mysql-status is ok">>/var/log/mysql-status.log else STAT=0 && echo "$data mysql-status is error">>/var/log/mysql-status.log fi else STAT=0 && echo "$data mysql-status is error">>/var/log/mysql-status.log fi zabbix_sender -z 192.168.1.234 -s "test-mysql-status" -k mysql -o $STAT
-z zabbix-server的地址
-s 監控項目名稱
-k 鍵值
監控項目
觸發器
監控圖表