openfalcon - centos 5.5 + python 2.4 running falcon agent

前端之家收集整理的这篇文章主要介绍了openfalcon - centos 5.5 + python 2.4 running falcon agent前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#!/usr/bin/python
#coding: utf-8

import os,sys,re
import simplejson as json
#import request as requests
import time
import urllib2,base64
from socket import *
import commands

def 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"}' % connection


print "开始 "

# 准备上报数据
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 i

p = []
#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,project=%s"%(host,projectname)
        value = checkTcpPort(host,port)
        p.append(zuzhuangData(tags,value))

#print json.dumps(p,sort_keys=True,indent = 4)

#print p

#print p

file_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.gz
pip==1.0 
wget https://pypi.python.org/packages/25/57/0d42cf5307d79913a082c5c4397d46f3793bc35e1138a694136d6e31be99/pip-1.1.tar.gz --no-check-certificate
simplejson
  • 别名引入
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

猜你在找的CentOS相关文章