有这么个需求,一个api可以获得当前设备的在线总数,如果少于某个数值,那么就得报警。

想来想去,接入falcon还是非常不错的。

首先我们需要一个脚本,可以获取api返回的json串,把数值挖出来,画图然后报警。

脚本

#!/usr/bin/env python
#coding: utf8

import sys
import json
import requests
import time
import urllib2, base64

# 获取数据
r = requests.get('http://xx/ss')
r2 = json.loads(r.text)
value = r2['online']['total']


# 准备上报数据
p = []
endpoint = "10.0.2.90"
metric = "yw"
key = "onlinetotal"
timestamp = int(time.time())
step = 60
vtype = "GAUGE"
tags = ''

i = {
        'Metric' :'%s.%s'%(metric,key),
        'Endpoint': endpoint,
        'Timestamp': timestamp,
        'Step': step,
        'value': value,
        'CounterType': vtype,
        'TAGS': tags
        }

p.append(i)

print json.dumps(p, sort_keys=True,indent = 4)

sys.exit(0)
# 上报
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

falcon 的设置

  1. cron, 一分钟运行一次,记得 修改/etc/crontab,把mailto改成空,要不很有可能造成FD不够用,因为都是一个个小邮件
  2. 模板,需要添加yw.onlinetotal的报警
  3. 报警
  4. 绑定到host
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 ""