1. 安装python2.7需要的模块
requests
setuptools
2. 准备数据源文本,格式如下:
apay_fail:87
bpay_fail:432
coty_fail:266
dfund_fail:33
fpay_fail:61
...
3. 代码解释
name:push.py
#!/usr/bin/python27 #-*-coding:utf8-*- import requests import time import json ts = int(time.time()) resultlog = ‘/home/work/monitor/logs/result.log‘ #文本转为字典格式的函数 def load_source_dictfile(filename): d1 = {} try: with open(filename,‘r‘) as f1: for line in f1: (key,value) = line.strip(‘\n‘).split(‘:‘) #去掉结尾符‘\n‘,以":"分割字串 d1[key] = float(value) #左为key,右为value,更新字典的d1 f1.close() #记得最后关闭文件 return d1 #将d1作为函数返回值 except IOError as ioerr: print "文件 %s 无法创建" % (filename) result=load_source_dictfile(resultlog) print result #遍历字典,通过变量解包分离key和value for k,v in result.items(): payss = [ { "endpoint":"testpointzcy", "metric":"paygz", "timestamp":ts, "step":60, "value":v, "counterType":"GAUGE", "tags":"paygz=" + k, } ] r = requests.post("http://127.0.0.1:1988/v1/push", data=json.dumps(payss)) print r.text
4. 执行结果
{apay_fail‘: 87.0, ‘bpay_fail‘: 432.0, ‘coty_fail‘: 266.0, ‘dfund_fail‘: 33.0, ‘fpay_fail‘: 61.0,...}
success
...
本文出自 “Zcy.gy” 博客,请务必保留此出处http://1064187464.blog.51cto.com/9108437/1786762
将文本序列映射到python字典push至open-falcon监控平台
原文地址:http://1064187464.blog.51cto.com/9108437/1786762