1. Install the required modules for python2.7
Requests
Setuptools
2. Prepare the data source text in the following format:
apay_fail:87
bpay_fail:432
coty_fail:266
Dfund_fail:33
fpay_fail:61
...
3. Code Interpretation
name:push.py
#!/usr/bin/python27#-*-coding:utf8-*-import requestsimport timeimport jsonts = 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 ', with ":" Split string d1[key] = float (value) #左为key, right is value, Update dictionary for D1 f1.close () #记得最后关闭文件     &NBsp; return d1 #将d1作为函数返回值 except IOError as ioerr: print "File %s could not create " % (filename) result=load_source_dictfile (resultlog) print result# Traversal dictionary, Separating key and Valuefor k,v in result.items (): payss = [ with variable unpacking { "endpoint": "Testpointzcy", " Metric ":" Paygz ", " timestamp ":ts, "Step":60, "Value":v, "CounterType": "GAUGE", &Nbsp; "tags": "paygz=" + k, } ] r = Requests.post ("Http://127.0.0.1:1988/v1/push", data=json.dumps (Payss)) print R.text
4. Implementation results
{apay_fail ': 87.0, ' bpay_fail ': 432.0, ' coty_fail ': 266.0, ' dfund_fail ': 33.0, ' fpay_fail ': 61.0,...}
Success
...
This article is from the "zcy.gy" blog, make sure to keep this source http://1064187464.blog.51cto.com/9108437/1841042
Map a text sequence to a Python dictionary push to the OPEN-FALCON monitoring platform