openfalcon

来源:互联网 发布:php微信答题 源码 编辑:程序博客网 时间:2024/06/08 15:53
#!/usr/bin/python#coding: utf-8import os, sys, reimport simplejson as json#import request as requestsimport timeimport urllib2, base64from socket import *import commandsdef checkTcpPort(host,port):    result = int    try:        s = socket(AF_INET, SOCK_STREAM)        s.settimeout(1)        code = s.connect_ex((host,port))        #print code        s.close()        result = code    except Exception, e:        result = 111    return result# 上报def uploadToAgent(p):    method = "POST"    handler = urllib2.HTTPHandler()    opener = urllib2.build_opener(handler)    url = "http://127.0.0.1:1988/v1/push"    request = urllib2.Request(url, data=json.dumps(p))    request.add_header('Content-Type','application/json')    request.get_method = lambda: method    try:        connection = opener.open(request)    except urllib2.HTTPError,e:        connection = e    if connection.code == 200:        print connection.read()    else:        print '{"err":1,"msg":"%s"}' % connectionprint "开始 "# 准备上报数据def zuzhuangData(tags = '', value = ''):    endpoint = "192.168.78.140"    metric = "userdefine"    key = "remotetcpcheck"    timestamp = int(time.time())    step = 60    vtype = "GAUGE"    i = {            'Metric' :'%s.%s'%(metric,key),            'Endpoint': endpoint,            'Timestamp': timestamp,            'Step': step,            'value': value,            'CounterType': vtype,            'TAGS': tags            }    return ip = []#with open("./tcp.txt") as f:#    for line in f:#        results = re.findall("(\S+)",line)#        print results#        host = results[0]#        port = int(results[1])#   description = results[2]#   projectname = results[3]#        tags = "project=ops,"#        tags += "host=%s,port=%s,description=%s,project=%s"%(host,port,description,projectname)#        value = checkTcpPort(host,port)#        p.append(zuzhuangData(tags,value))for line in open("tcp.txt"):    results = re.findall("(\S+)",line)    if len(results) != 4:        pass    else:        print results        host = results[0]        port = int(results[1])        description = results[2]        projectname = results[3]        tags = "host=%s,port=%s,description=%s,project=%s"%(host,port,description,projectname)        value = checkTcpPort(host,port)        p.append(zuzhuangData(tags,value))#print json.dumps(p, sort_keys=True,indent = 4)#print p#print pfile_object = open('data.json', 'w')file_object.write(json.dumps(p, sort_keys=True,indent=4))file_object.close()#print commands.getoutput("""curl -H "Content-Type: application/json" -X POST -d "%s" http://127.0.0.1:1988/api/login -vvv"""%p)print commands.getoutput(""" curl -X POST -H "Content-Type: application/json" -d @./data.json http://127.0.0.1:1988/v1/push """)os.remove("./data.json")#uploadToAgent(p)

面临的最大问题

就是python2.4下面模块不全,只能东拼西凑,加上执行系统命令了

解决办法

  • 可以通过下载下面的tar.gz,然后 python setup.py install
setuptools==1.4 wget https://github.com/pypa/setuptools/archive/1.4.tar.gzpip==1.0 wget https://pypi.python.org/packages/25/57/0d42cf5307d79913a082c5c4397d46f3793bc35e1138a694136d6e31be99/pip-1.1.tar.gz --no-check-certificatesimplejson
  • 别名引入
import simplejson as json
  • 调用系统命令执行curl,post数据到数据接收服务
curl -X POST -H "Content-Type: application/json" -d @./data.json http://127.0.0.1:1988/v1/push
原创粉丝点击