cron配置

* * * * * cd /data/openfalcon/agent/userdefine && ./tcp.check 1>>/tmp/1.txt 2>&1

脚本

#!/usr/bin/env python
#coding: utf-8

import os, sys, re
import json
import requests
import time
import urllib2, base64
from socket import *

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.0.88"
    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]
        tags = "project=ops,"
        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)

报警策略

each(metric=userdefine.remotetcpcheck project=ops)
all(#3) != 0

mark

Copyright © opschina.org 2017 with zzlyzq@gmail.com all right reserved,powered by Gitbook该文件修订时间: 2017-07-11 11:32:48

results matching ""

    No results matching ""