前言:
最近一直在做监控方面的东东,一些基本的东西基本到处都有资料也就不多说了。但是,让监控阿里云的数据库真是把我难住了。研究了许久的阿里云api,虽然代码写出来了,但是遇到一个坑,所以转换了思路,分别用redis和mysqladmin连接数据库,获取连接数和请求数,但是却获取不到实例的CPU使用率。又只好回头研究阿里云的api。花了几天终于踩完所有坑,达到自己想要的效果,具体实现过程如下:
正文:
主要添加了以下三条自定义key,第一条通过redis_cli客户端连接redis获取统计数据,第二条通过mysqladmin连接mysql获取统计数据,第三条就是坑我好几天的,通过云监控获取实例信息的key。阿里云的RDS默认是5分钟获取一条监控数据,部分重要的数据库可以设置为1分钟获取一次。不过这个是要收费的。最开始以为都是60秒获取一次监控数据,所以从云监控获取数据时,时间间隔是60秒,就莫名奇妙的出现,有的服务器能获取数据,有的服务器不能获取数据。所以最后的解决思路是,把时间间隔调大,获取好几条数据,然后取最后一条数据就可以了。
1 2 3 4 | #zabbix_agentd.conf UserParameter=redis.get[*], /usr/local/sbin/redis_get .sh $1 | grep -Po '$2:\K[.\d]+' UserParameter=rds.get[*],mysqladmin -h $1 -P 3306 -u db_username extended-status -p 'password' | grep -Po '$2[^\d]+\K\d+' UserParameter=rds.get_from_cms[*], /usr/local/sbin/database_api .py $1 $2 $3 | grep -Po 'Average":\K[.\d]+' | tail -1 |
为了自已想象中的安全,我把redis服务器的密码单独放在某个文件中,然后通过自己写的脚本读取redis的连接地址,连接密码,和实例名称,这样在增加item的时候只用添加一个实例名称就好,下面脚本会自己通过实例名称获取redis地址和密码。
1 2 3 4 5 6 | #!/bin/bash #/usr/local/sbin/redis_get.sh shadow= /usr/local/sbin/redis_shadow address=` awk -vn=$1 'n ~ $NF{print $1}' $shadow` password=` awk -vn=$1 'n ~ $NF{print $2}' $shadow` redis-cli -h $address -a $password info |
保存redis的信息放在一个单独的文件中,有新增的redis实例就直接修改这个配置文件就可。
1 2 | #/usr/local/sbin/redis_shadow redis连接地址 连接密码 实例名称 |
上面的比较简单。但是有个问题。总共60台服务器,每个服务器有3条需要监控的点,所以就得创建180个item,因为redis和rds里面不能放zabbix客户端,所以我就直接通过监控服务器获取60台数据库的监控信息。我真的手动添加了180个item。。。。真是傻的可爱~所以后面监控cpu使用率我就用zabbix的api来批量添加item,顿时爽炸了~不过在此之前,先看看如何通过阿里云的api获取数据库的cpu使用率把。
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 | #!/usr/local/bin/python from aliyunsdkcore import client from aliyunsdkcms.request.v20160318 import QueryMetricListRequest #用的云监控的api import time import sys import json if len(sys.argv) != 4: print '\tusage: python database_api.py <your_PrijectName> <your_Metric> <your_instanceId> ' print '\thelp from help.aliyun.com/document_detail/28619.html\n' exit () clt = client.AcsClient( 'your_access_key' , 'your_key_password' , 'region_id' ) #得先创建access_key哟,region_id可以看region_id的列表,比如华北1是什么id呀之类的。 request = QueryMetricListRequest.QueryMetricListRequest() request.set_accept_format( 'json' ) request.set_Project(sys.argv[1]) #此处设置Project用来定义你是redis还是ecs还是rds,具体看查看文档:https://help.aliyun.com/document_detail/28619.html?spm=5176.doc28615.6.627.vlxOtm request.set_Metric(sys.argv[2]) #此处就是你要获取值的监控项,也可以查看上面的文档 start_time = time .strftime( '%Y-%m-%d %H:%M:%S' , time .localtime( time . time ()-350)) #设置一个时间为当前时间的前350秒处 timestamp_start = int( time .mktime( time .strptime(start_time, "%Y-%m-%d %H:%M:%S" ))) * 1000 request.set_StartTime(timestamp_start) request.set_Dimensions( "{'instanceId':'%s'}" %sys.argv[3]) #此处要设置实例id。 request.set_Period( '60' ) result = clt.do_action(request) print result #最后得到的是一个json格式的字符串,然后在自定义的key中通过grep获取到对应的值 #result2 = json.loads(result) #print float(result2['Datapoints'][-1]['Average']) |
1 2 3 4 | /usr/local/sbin/database_api .py $1 $2 $3 | grep -Po 'Average":\K[.\d]+' | tail -1 单独把自定义key的后半部分解释一下。在创建item时,database_api.py会通过key获取到三个值 分别是Project,Metric和实例 id 。然后通过 grep 提取json字符串中Average对应的值,因为有可能获取 很多值,所以用 tail 获取最后一个值。 |
注:主要的坑就是上面的哪个start_time,如果获取不到值就再设置大点就好。下面就是批量增加item的代码。大致参考的是别人的。其中需要注意 的一点就是我是手动获取applicationid的,可以连接上zabbix后通过 zapi.application.get({"filter":{'name':["APPLICATION_NAME"]}})[0]['applicationid'] 获取application的id。其中APPLICATION_NAME就是你的application的名字咯。如果并不需要放入某个application,可以删掉其中的applications那一项。
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #!/usr/bin/python #-*- coding:utf8 -*- import sys from zabbix_api import ZabbixAPI server = "http://YourZabbixServerAddress" username = "Admin" password = "password" zapi = ZabbixAPI(server = server, path = "", log_level = 0 ) zapi.login(username, password) def get_hostinfo(): #主要用来获取host的id,接口的id,其中HOST_NAME就是你想往哪个host里面增添item。 host_info = zapi.host.get({ "selectInterfaces" :[ "interfaceid" ], "filter" :{ "host" :[ "HOST_NAME" ]}}) hostid = host_info[ 0 ][ 'hostid' ] interfaceid = host_info[ 0 ][ 'interfaces' ][ 0 ][ 'interfaceid' ] return (hostid,interfaceid) def create_item(name,key): a = get_hostinfo() hostid = a[ 0 ] interfaceid = a[ 1 ] create_item = zapi.item.create( { "name" :name, "key_" :key, "hostid" :hostid, "type" : 0 , "value_type" : 0 , #0是float,3是整数 "interfaceid" :interfaceid, "date_type" : 0 , "delay" : 60 , "history" : 7 , "trends" : 90 , "status" : 0 , "applications" :[ #如果不放入某个applications可以删掉此项 "2311" ] } ) return "item create success" if __name__ = = "__main__" : arg1 = 'CpuUsage' with open ( 'conf' ) as f: #从conf文件中读取实例id和实例名称。 for i in f: rds = i.split() key = "rds.get_from_cms[acs_rds,%s,%s]" % (arg1,rds[ 0 ]) name = "%s-%s" % (rds[ 1 ],arg1) print key,name result = create_item(name,key) #调用添加函数。 print result |
1 2 | #conf 实例 id 实例名称 |
本文出自 “机制小风风” 博客,请务必保留此出处http://xiaofengfeng.blog.51cto.com/8193303/1906412
联系客服