#!/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,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 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

更多相关文章

  1. Linux系统监控命令详解
  2. Linux curl 命令模拟 POST/GET 请求
  3. Linux基础-常用命令
  4. Linux中iptables基础命令
  5. linux基础(三)----linux命令系统学习----安装和登录命令
  6. shell用户配置详解及bash命令参数
  7. Linux网络和进程管理命令
  8. Linux最常用的基础命令 上篇
  9. 【linux学习笔记】vim命令小结

随机推荐

  1. centos LVM(逻辑卷管理)
  2. Android(安卓)NDK开发之旅(5):Android(安
  3. Unity3D之坐标系的转换
  4. 数据结构之哈希表
  5. 基于业务和平台理解数字营销概念
  6. 打卡学习
  7. Plotly中4种文本类型设置详解
  8. 裸辞,杀回一线!
  9. Ansible 之 自动化部署redis主从(单机)
  10. 小鹿又熬肝写了一份 Vue 2.0 核心原理!