1.Exporter
只要任何遵循Prometheus数据格式 ,可对其提供监控指标的程序都可以称为Exporter。在Prometheus社区中提供了丰富多样的Exporter供选择,如前面用到的node_exporter。
这些Exporter不仅类型丰富,功能上也很强大,通过合理的使用可以极大的方便我们的运维监控工作。除此之外,Prometheus还提供了支持多种开发语言的Clinet Libraries,用于满足Exporter的定制化开发需求。
2. Exporter运行方式
2.1 独立运行
前面使用过的node_exporter为例,由于操作系统本身并不直接支持Prometheus,因此,只能通过一个独立运行的程序,从操作系统提供的相关接口将系统的状态参数转换为可供Prometheus读取的监控指标。
2.2 应用集成
由于Prometheus项目的火热,目前有部分开源产品直接在代码层面使用Prometheus的Client Library,提供了在监控上的直接支持,如kubernetes、ETCD等产品。
这类产品自身提供对应的metrics接口,Prometheus可通过接口直接获取相关的系统指标数据。这种方式打破了监控的界限,应用程序本身做为一个Exporter提供功能。
3. 常用的Exporter
https://prometheus.io/docs/instrumenting/exporters
4. 自定义Exporter
虽然Prometheus社区提供了丰富多样的Exporter给用户使用,但由于各家公司的环境都有自身的特点,有时候可能无法在现有资源中找到合适的工具。对此,我们可以利用Prometheus的Clinet Libraries,开发符合实际需要的自定义Exporter。
Client Libraries支持的语言版本非常丰富,除了官方提供了Go、Java or Scala、Python和Ruby几种外,还有很多第三方开发的其他语言版本。
比如python
本次将调用到的Linux的命令如下 ,用于获取系统的TIME_WAIT连接数量
$ netstat -an |grep TIME_WAIT |wc -l
36
$ netstat -an |grep TIME_WAIT |wc -l
36
使用pip安装python的prometheus-client库
$ pip install prometheus-client
$ pip install prometheus-client
在Python开发中引入prometheus-client和commands库,command库用于执行Linux系统命令。
from prometheus_client import Gauge
import commands
from prometheus_client import Gauge
import commands
定义一个Gauge指标,名称为time_wait_count,并添加标签type。
time_wait_count = Gauge('time_wait_count', 'time_wait count of system',['type'])
time_wait_count = Gauge('time_wait_count', 'time_wait count of system',['type'])
定义执行函数,函数调用上面的Linux命令,用于获取相关的指标信息
python
def get_time_wait_count():
number=commands.getoutput('netstat -an |grep TIME_WAIT |wc -l')
time_wait_count.labels('Linux').set(int(number))
def get_time_wait_count():
number=commands.getoutput('netstat -an |grep TIME_WAIT |wc -l')
time_wait_count.labels('Linux').set(int(number))
现在,我们可以通过执行get_time_wait_count函数获取到time_wait_count的指标value,但要做为一个exporter运行,我们还得支持http协议。
此处,可以用到prometheus_client的start_http_server模块,该模块支持做为http服务启动。 完整的代码如下:
from prometheus_client import start_http_server,Gauge
import commands
time_wait_count = Gauge('time_wait_count', 'time_wait count of system',['type'])
def get_time_wait_count():
number=commands.getoutput('netstat -an |grep TIME_WAIT |wc -l')
time_wait_count.labels('Linux').set(int(number))
if __name__ == '__main__':
# Start up the server to expose the metrics.
start_http_server(8090)
# Generate some requests.
while True:
get_time_wait_count()
from prometheus_client import start_http_server,Gauge
import commands
time_wait_count = Gauge('time_wait_count', 'time_wait count of system',['type'])
def get_time_wait_count():
number=commands.getoutput('netstat -an |grep TIME_WAIT |wc -l')
time_wait_count.labels('Linux').set(int(number))
if __name__ == '__main__':
# Start up the server to expose the metrics.
start_http_server(8090)
# Generate some requests.
while True:
get_time_wait_count()
python mytest_exporter.py
python mytest_exporter.py