自定义监控(阿里云&zabbix)
目前阿里云对25端口有限制,无法在阿里云上搭建邮件服务器发送告警邮件,如果需要邮件通知,可以采取下面方法:
1.开同阿里云企业邮箱
2.使用阿里云监控告警
3.第三方zabbix自定义脚本监控
一、阿里云自定义监控
阿里云提供自定义监控SDK,这有助于我们定制化的根据自身业务来做监控,下面我就根据业务需求来介绍一个简单的自定义监控配置。
阿里提供了2个版本的自定义监控接口:
自定义监控SDK(python版) :cms_post.py
自定义监控SDK(bash版) :cms_post.sh
下载地址: http://help.aliyun.com/knowledge_detail.htm?knowledgeId=5974901
使用shell版本做演示
监控mysql主从状态:
1.创建自定义监控项
脚本post方法说明
1.命名空间中的字符串,用户aliuid
2.监控项名称,用户创建监控项时填写的名称
3.监控项值,用户上报到云监控的业务数据
4.字段信息,结合监控项,表示具体业务字段意义。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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= "[email protected]"
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脚本来上数据 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
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监控方式
在被监控服务器上需要先安装zabbix-sender 来给zabbix-server提交发送数据
1
2
|
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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#!/bin/bash mysql_user= 'root'
mysql_pass= '123.com'
mail= "[email protected]"
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 键值
监控项目
触发器
监控图表
本文转自 KaliArch 51CTO博客,原文链接:http://blog.51cto.com/kaliarch/1923429,如需转载请自行联系原作者