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
阅读全文
0 0
- openfalcon
- openfalcon
- openfalcon
- openfalcon
- 安装Openfalcon
- OpenFalcon线上初步部署
- openfalcon-mongodb监控插件
- openfalcon-zookeeper监控 "zkmon"
- openfalcon - 再谈数据类型
- openfalcon - 日志报警实验
- openfalcon - agent - fastdfs
- openfalcon监控端口大全
- grafana 2.6 + openfalcon (草版)
- openfalcon 支持query组件
- openfalcon hosts and groups
- openfalcon 监控端口配置
- openfalcon mail provider starttls 版本
- openfalcon 咋就不发送邮件了来
- 织梦Dedecms教程:针对织梦安全问题的解决办法
- GreenDao使用说明(三)多表的使用
- JDK安装与环境变量配置
- 在devicetree节点中通过加status来方便的使能或者disable driver
- Qcom 8976 单独烧录boot.img后wifi打不开
- openfalcon
- iOS软件编程规范
- LibRTMP源代码分析1:概述
- MySQL常用函数整理 | 黄乔国PHP
- Python-re 正则 regex 简介
- html5自定义属性data详解及使用方法
- Python2/3 list set性能测试
- 使用chilkat解压.Z(unix压缩格式)的压缩文件
- HTML 简介—1