Prometheus监控端口

环境依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
依赖:
python3.*
pyyaml==5.1.2
prometheus_client==0.7.1
flask==1.1.1

安装依赖:
1、安装python3.*
2、pip3 install -r requirements.txt

安装路径:
可安装至任意路径
将 export_moniotr_port.py 、 host_port_conf.yaml 放在同级路径

配置文件

export_monitor_port.py

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
71
72
73
74
75
76
# Prometheus monitor server port.
# -*- coding:utf-8 -*-
import socket
import os
import yaml
import prometheus_client
from prometheus_client import Gauge
from prometheus_client.core import CollectorRegistry
from flask import Response, Flask

app = Flask(__name__)

def Getconfigdic():
"""
将 yaml 配置文件中数据格式化为字典
"""
proPath = os.path.dirname(os.path.realpath(__file__))
yamlPath = os.path.join(proPath, "host_port_conf.yaml")
f = open(yamlPath, "r", encoding="utf-8")
sdata = yaml.full_load(f)
f.close()
return sdata

def Exploreport(sertype,ip,port):
"""
检查端口是否存在
"""
try:
tel = socket.socket()
tel.connect((ip, int(port)))
socket.setdefaulttimeout(0.5)
result_dic = {"sertype": sertype, "host": ip, "port": str(port), "status": 1}
return result_dic
except:
result_dic = {"sertype": sertype, "host": ip, "port": str(port), "status": 0}
return result_dic

def Checkport():
"""
Getconfigdic()函数拿到的数据格式
sdic = {'zookeeper': {'host': ['192.168.7.51', '192.168.7.52', '192.168.7.53'], 'port': [2181, 22]},
'harbor': {'host': ['192.168.7.41', '192.168.7.42', '192.168.7.43'], 'port': [9200, 9301]}}
"""
sdic = Getconfigdic()
result_list = []
for sertype in sdic.keys():
iplist = sdic.get(sertype).get("host")
portlist = sdic.get(sertype).get("port")
for ip in iplist:
for port in portlist:
result_dic = Exploreport(sertype, ip, port)
result_list.append(result_dic)
return result_list

@app.route("/metrics")
def ApiResponse():
"""
Checkport() 取出来的数据是这样的
checkport = [{"sertype":"zookeeper","host": "192.168.1.22", "port": "2181", "status": 0},
{"sertype":"zookeeper","host": "192.168.1.23", "port": "2181", "status": 1}]
"""
checkport = Checkport()
# 定义metrics仓库,存放多条数据
REGISTRY = CollectorRegistry(auto_describe=False)
muxStatus = Gauge("server_port_up", "Api response stats is:", ["sertype","host", "port"], registry=REGISTRY)
for datas in checkport:
sertype = "".join(datas.get("sertype"))
host = "".join(datas.get("host"))
port = "".join(datas.get("port"))
status = datas.get("status")
muxStatus.labels(sertype,host, port).set(status)
return Response(prometheus_client.generate_latest(REGISTRY),
mimetype="text/plain")

if __name__ == "__main__":
app.run(host="0.0.0.0", port=31672, debug=True)

host_port_conf.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Prometheus monitor server port config.
zookeeper:
host:
- "192.168.7.51"
- "192.168.7.52"
- "192.168.7.53"
port:
- 2181
elasticsearch:
host:
- "192.168.7.41"
- "192.168.7.42"
- "192.168.7.43"
port:
- 9200
- 9300

requirements.txt

1
2
3
pyyaml==5.1.2
prometheus_client==0.7.1
flask==1.1.1

说明:

1
2
3
4
5
6
7
最外层key为服务名称,自定义服务名称
host:为固定key,不可以变
- "服务器ip"
port:
- 端口
注意:
新增或者删除某项端口监控,不需要重启端口监控服务

prometheus配置

1
2
3
4
5
6
scrape_configs:
- job_name: 'monitor-port'
scrape_interval: 10s
static_configs:
- targets:
- "ip:port"
Donate