CentOS   发布时间:2022-05-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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

大佬总结

以上是大佬教程为你收集整理的openfalcon - centos 5.5 + python 2.4 running falcon agent全部内容,希望文章能够帮你解决openfalcon - centos 5.5 + python 2.4 running falcon agent所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: