本文共 1658 字,大约阅读时间需要 5 分钟。
#!/usr/bin/env python
#coding: utf-8import os, sys, re
import jsonimport requestsimport timeimport urllib2, base64from socket import *def checkTcpPort(host,port):
result = inttry:s = socket(AF_INET, SOCK_STREAM)s.settimeout(1)code = s.connect_ex((host,port))#print codes.close()result = codeexcept Exception, e:result = 111return resultdef uploadToAgent(p):
method = "POST"handler = urllib2.HTTPHandler()opener = urllib2.build_opener(handler)url = ""request = urllib2.Request(url, data=json.dumps(p))request.add_header('Content-Type','application/json')request.get_method = lambda: methodtry:connection = opener.open(request)except urllib2.HTTPError,e:connection = eif connection.code == 200: print connection.read()else: print '{"err":1,"msg":"%s"}' % connection
print "开始 "
def zuzhuangData(tags = '', value = ''):
endpoint = "10.0.2.90"metric = "userdefine"key = "remotetcpcheck"timestamp = int(time.time())step = 60vtype = "GAUGE"i = { 'Metric' :'%s.%s'%(metric,key), 'Endpoint': endpoint, 'Timestamp': timestamp, 'Step': step, 'value': value, 'CounterType': vtype, 'TAGS': tags }return i
p = []
with open("./ip.txt") as f:for line in f:results = re.findall("(\S+)",line)print resultshost = results[0]port = int(results[1])description = results[2]tags = "host=%s,port=%s,description=%s"%(host,port,description)value = checkTcpPort(host,port)p.append(zuzhuangData(tags,value))print json.dumps(p, sort_keys=True,indent = 4)
uploadToAgent(p)
ips.txt
www.baidu.com 80 百度www.sina.com.cn 80 新浪转载地址:
转载于:https://blog.51cto.com/dadloveu/2069494