使用Python编写Prometheus监控的方法
2018-10-15 16:15
1966 查看
要使用python编写Prometheus监控,需要你先开启Prometheus集群。可以参考https://www.geek-share.com/detail/2750590103.html 安装。在python中实现服务器端。在Prometheus中配置请求网址,Prometheus会定期向该网址发起申请获取你想要返回的数据。
使用Python和Flask编写Prometheus监控
Installation
pip install flask pip install prometheus_client
Metrics
Prometheus提供4种类型Metrics:Counter
, Gauge
, Summary
和Histogram
Counter
Counter可以增长,并且在程序重启的时候会被重设为0,常被用于任务个数,总处理时间,错误个数等只增不减的指标。
import prometheus_client from prometheus_client import Counter from prometheus_client.core import CollectorRegistry from flask import Response, Flask app = Flask(__name__) requests_total = Counter("request_count", "Total request cout of the host") @app.route("/metrics") def requests_count(): requests_total.inc() # requests_total.inc(2) return Response(prometheus_client.generate_latest(requests_total), mimetype="text/plain") @app.route('/') def index(): requests_total.inc() return "Hello World" if __name__ == "__main__": app.run(host="0.0.0.0")
运行该脚本,访问youhost:5000/metrics
# HELP request_count Total request cout of the host # TYPE request_count counter request_count 3.0
Gauge
Gauge与Counter类似,唯一不同的是Gauge数值可以减少,常被用于温度、利用率等指标。
import random import prometheus_client from prometheus_client import Gauge from flask import Response, Flask app = Flask(__name__) random_value = Gauge("random_value", "Random value of the request") @app.route("/metrics") def r_value(): random_value.set(random.randint(0, 10)) return Response(prometheus_client.generate_latest(random_value), mimetype="text/plain") if __name__ == "__main__": app.run(host="0.0.0.0")
运行该脚本,访问youhost:5000/metrics
# HELP random_value Random value of the request # TYPE random_value gauge random_value 3.0
Summary/Histogram
Summary/Histogram概念比较复杂,一般exporter很难用到,暂且不说。
LABELS
使用labels来区分metric的特征
from prometheus_client import Counter c = Counter('requests_total', 'HTTP requests total', ['method', 'clientip']) c.labels('get', '127.0.0.1').inc() c.labels('post', '192.168.0.1').inc(3) c.labels(method="get", clientip="192.168.0.1").inc()
使用Python和asyncio编写Prometheus监控
from prometheus_client import Counter, Gauge from prometheus_client.core import CollectorRegistry REGISTRY = CollectorRegistry(auto_describe=False) requests_total = Counter("request_count", "Total request cout of the host", registry=REGISTRY) random_value = Gauge("random_value", "Random value of the request", registry=REGISTRY)
import prometheus_client from prometheus_client import Counter,Gauge from prometheus_client.core import CollectorRegistry from aiohttp import web import aiohttp import asyncio import uvloop import random,logging,time,datetime asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) routes = web.RouteTableDef() # metrics包含 requests_total = Counter("request_count", "Total request cout of the host") # 数值只增 random_value = Gauge("random_value", "Random value of the request") # 数值可大可小 @routes.get('/metrics') async def metrics(request): requests_total.inc() # 计数器自增 # requests_total.inc(2) data = prometheus_client.generate_latest(requests_total) return web.Response(body = data,content_type="text/plain") # 将计数器的值返回 @routes.get("/metrics2") async def metrics2(request): random_value.set(random.randint(0, 10)) # 设置值任意值,但是一定要为 整数或者浮点数 return web.Response(body = prometheus_client.generate_latest(random_value),content_type="text/plain") # 将值返回 @routes.get('/') async def hello(request): return web.Response(text="Hello, world") # 使用labels来区分metric的特征 c = Counter('requests_total', 'HTTP requests total', ['method', 'clientip']) # 添加lable的key, c.labels('get', '127.0.0.1').inc() #为不同的label进行统计 c.labels('post', '192.168.0.1').inc(3) #为不同的label进行统计 c.labels(method="get", clientip="192.168.0.1").inc() #为不同的label进行统计 g = Gauge('my_inprogress_requests', 'Description of gauge',['mylabelname']) g.labels(mylabelname='str').set(3.6) #value自己定义,但是一定要为 整数或者浮点数 if __name__ == '__main__': logging.info('server start:%s'% datetime.datetime.now()) app = web.Application(client_max_size=int(2)*1024**2) # 创建app,设置最大接收图片大小为2M app.add_routes(routes) # 添加路由映射 web.run_app(app,host='0.0.0.0',port=2222) # 启动app logging.info('server close:%s'% datetime.datetime.now())
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接
您可能感兴趣的文章:
- 利用Prometheus与Grafana对Mysql服务器的性能监控详解
- Python文件监听工具pyinotify与watchdog实例
- Python实现一个服务器监听多个客户端请求
- 使用python编写监听端
- Python利用pyHook实现监听用户鼠标与键盘事件
- Python从ZabbixAPI获取信息及实现Zabbix-API 监控的方法
- Python实现监控键盘鼠标操作示例【基于pyHook与pythoncom模块】
- Python实现数据可视化看如何监控你的爬虫状态【推荐】
- python脚本监控Tomcat服务器的方法
- python实现内存监控系统
- python实现简易内存监控
- 使用Python监控文件内容变化代码实例
相关文章推荐
- 使用Python和Flask编写Prometheus监控
- 使用python编写udp协议的ping程序方法
- 使用IDLE编写Python程序出现汉字横着显示的解决方法(附删除键空格消除)
- 使用Python编写提取日志中的中文的脚本的方法
- python socket编程入门(编写server实例)+send 与sendall的区别与使用方法
- 使用Python的元类实现AOP监控类方法调用过程
- 使用Python编写提取日志中的中文的脚本的方法
- 使用python编写一个监控使用内存并使用flask模块出图
- zabbix使用自己编写脚本模板和zabbix自带模板两种方法添加对指定进程和端口的监控
- 以前编写LIKE谓词被忽略的使用方法
- 【用Python写爬虫】获取html的方法【一】:使用urllib
- 【用Python写爬虫】获取html的方法【四】:使用urllib下载文件
- Python httplib,smtplib使用方法
- python下使用qrcode方法
- PRTG流量监控基本安装和使用方法
- Python httplib,smtplib使用方法
- 使用Delphi 编写Python Extension
- 【用Python写爬虫】获取html的方法【一】:使用urllib
- PRTG流量监控基本安装和使用方法
- VS2005编写aspx,设计 不能使用的解决方法