我是靠谱客的博主 明亮过客,这篇文章主要介绍openfalcon 添加监控远程tcp端口异常情况监控代码,现在分享给大家,希望可以做个参考。

监控代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/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 = "10.0.2.90" 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("./ip.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 = "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
复制代码
1
2
3
4
5
www.baidu.com 80 百度 www.sina.com.cn 80 新浪
  • 添加template
    这里写图片描述

  • 绑定到host
    这里写图片描述

  • 别忘了添加endpoint
    这里写图片描述

  • 看到指标已经上报
    这里写图片描述

  • 看到已经收到了报警右键
    这里写图片描述

  • 修改报警设置

复制代码
1
2
3
如果value0,说明端口一切正常 如果value11,说明不通,或者超市 如果value111,这个是我们自己定义的,说明有了其他异常

结果就是如下的截图:

这里写图片描述

最后

以上就是明亮过客最近收集整理的关于openfalcon 添加监控远程tcp端口异常情况监控代码的全部内容,更多相关openfalcon内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部