Map a text sequence to a Python dictionary push to the OPEN-FALCON monitoring platform

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.