OpenFalcon是一款由小米运维团队从互联网公司的需求出发, 根据多年的运维经验,结合市面上使用的一些运维监控系统的使用经验和反馈,开发的一套企业级、高可用、可扩展的开源监控解决方案。简单了使用一下Open-falcon运维监控,结合使用过的zabbix,cacti,nagios来讲,以为有如下几个优势:python
- 支持用户主动push,能够结合一些业务需求采集数据,同时也支持用户自定义的插件。
- 支持策略模板,模板继承和覆盖,多种告警方式,支持callback调用
- 因为dashboard采用的是rrd数据,结合rrdtool的数据归档策略,能够及时返回历史数据
- 多维度的数据展现,用户能够自定义Screen。
- 有着完善的开源社区,开源社区提供了各类第三方应用的监控插件。目前的插件支持Linux、Windows、Mysql、Redis、Memache、RabbitMQ和交换机监控。
在这里就再也不赘述Open-falcon运维监控系统各类组件,以及如何部署,官方文档写的十分明白,有兴趣的能够去看看官方文档。ios
官方文档地址:http://book.open-falcon.org/
Open-falcon的github地址:https://github.com/open-falcon/git
在使用Open-falcon的过程当中遇到过这么一个问题,就是在发送微信报警的时候,发现只能绑定一个企业微信中的应用,若是监控的机器多起来,各类类型的报警成堆的在一个微信应用中出现。因此面对不一样的报警类型的告警信息须要把他归类在不一样的应用发送,这样面对不一样的问题时即可以更加方便的解决。查看了官方提供的微信网关,研究了一下,官方使用go语言写,对于go语言没怎么接触,因此打算用python写一个微信的接口,可以自定义添加绑定不一样的应用。github
官方的微信接口:https://github.com/Yanjunhui/chatsql
大概思路就是,python从wechat.conf配置文件中提取配置信息,定时获取微信的access_token,而且存入到指定access_token.conf的配置文件中,绑定端口监听Open-falcon发送过来的数据,解析提取关键的信息,判断告警类型,而且从access_token.conf配置文件中获取指定应用access_token,而后发送到指定的url。
企业微信API:https://work.weixin.qq.com/api/docjson
wechat.conf
access_token.conf
def get_accesstoken(): url = 'https://qyapi.weixin.qq.com/' ap = ConfigParser.SafeConfigParser() cp = ConfigParser.SafeConfigParser() ap.read('./config/wechat.conf') config = ap.sections() for i in config: if i != 'http' and i != 'weixin': agentid = ap.get(i, 'AgentId') corpid = ap.get(i, 'CorpID') corpsecret = ap.get(i, 'Secret') token_url = '%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (url, corpid, corpsecret) access_token = json.loads(urllib.urlopen(token_url).read().decode())['access_token'] cp.add_section(i) cp.set(i,"AgentId",agentid) cp.set(i, "access_token", access_token) cp.write(open('./config/access_token.conf','w')) global timer timer = threading.Timer(3600,get_accesstoken) timer.start()
关键代码以下:
#继承threading.Thread类,重写方法,绑定指定端口,接收数据 def __init__(self,port): threading.Thread.__init__(self) self.port = port self.sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) self.sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) self.sock.bind(('0.0.0.0',port)) self.sock.listen(0)
#对于接收到的数据进行url解码 def run(self): while True: data = self.client.recv(BUFSIZE) if(data): string = urllib.unquote(data) self.handle_String(string) #print string else: break
具体用法以下图:
字符串截取代码具体以下:
def handle_String(self,s): a = s.split('&') dict = {} for i in range(len(a)): if len(re.split('\[',a[i]))>1: dict['content']=re.split('content=',a[i])[1] elif len(re.split('tos=',a[i]))>1: dict['tos']=re.split('tos=',a[i])[1] flag = re.split('\[',dict['content'])[5].split('*')[0] touser = dict['tos'] content = dict['content'] obj = wechat.Weixin(flag, content) obj.message(touser) logging.debug('Send to %s Wechat %s %s ' % (touser,flag,content))
关键代码以下:
def message(self,touser): url = 'https://qyapi.weixin.qq.com/' cp = ConfigParser.SafeConfigParser() ap = ConfigParser.SafeConfigParser() cp.read('./config/wechat.conf') ap.read('./config/access_token.conf') config = cp.sections() access_config = ap.sections() agentname = 'test4' agentid = cp.get('test4', 'AgentId') self.token = ap.get('test4','AgentId') #遍历配置项是否有对应告警类型的应用 for i in config: if i !='http' and i!= 'weixin': value = cp.get('weixin',i) if self.flag == value: agentname = i agentid = cp.get(i, 'AgentId') for k in access_config: if k == agentname: self.token = ap.get(k,'access_token') values = { "touser": touser, "msgtype": 'text', "agentid": agentid, "text": {'content':self.content}, "safe": 0 } return self.send_message(url,json.dumps(values))
关键代码以下:
def send_message(self,url,data): send_url = "%s/cgi-bin/message/send?access_token=%s" %(url,self.token) self.response = urllib.urlopen(urllib.Request(url=send_url,data=data)).read() x = json.loads(self.response.decode())['errcode'] if x==0 : logging.debug('Successfully %s ' % (data)) return 'Successfully' else: logging.debug('Faild %s' % (data)) return 'Failed'
修改alarm配置文件中发送告警信息的指定端口为4567,对应程序中的绑定监听的端口
vim openfalcon/alarm/config/cfg.jsonvim
修改内容以下:api
"api": { "im": "http://127.0.0.1:4567", "sms": "http://127.0.0.1:10086/sms", "mail": "http://127.0.0.1:10086/mail", "dashboard": "http://127.0.0.1:8081", "plus_api":"http://127.0.0.1:8080", "plus_api_token": "default-token-used-in-server-side" },
**最后效果图**
由于只在本身的几台虚拟机上运行,暂时还没发现出问题。若是有什么问题但愿你们指正,本身学python不久,本身写这个东西学到了不少,遇到问题就不要怕解决,就怕不思考。
接口的源代码:https://github.com/libuliduobuqiuqiu/openfalcon-wechat微信