Prometheus的架构与基本使用方式

grafana基本参数的解释可以参考:https://blog.csdn.net/weixin_39540280/article/details/115364887?spm=1001.2014.3001.5501

 

node-exporter

下载node-exporter,运行./node_exporter

本地查询:curl localhost:9100/metrics | grep -i node_cpu | head

/home/tarena/app/node_exporter/

Chmod  777  node_exporter_start.sh

daemonize -c /home/tarena/app/node_exporter/ /home/tarena/app/node_exporter/node_exporter_start.sh

 

我们可以利用 Prometheus 的 static_configs 来拉取 node_exporter 的数据。+

打开 prometheus.yml 文件, 在 scrape_configs 中添加如下配置:

- job_name: "node"

    static_configs:

      - targets: ["127.0.0.1:9100"]

 

自定义的node-exporter:

  1. 自身是HTTP服务器,可以响应从外发的HTTPGET请求
  2. 自身需要运行在后台,可以定期触发抓取本地的监控数据
  3. 返回peometheus_sever内容需要符合metrics类型(key-value)
  4. Key-value à Prometheus(TS) values(float int) return string*

 

Cpu时间计算分为:user time/sys time/nice time/idle time/irq/softirq等

 用户使用时间/系统\内核使用时间/nice值分配/空闲时间/硬中断时间/软中断时间

空闲cpu使用时间:

sum(increase(node_cpu_seconds_total {mode="idle"}[1m])) by (instance)

总cpu时间:

sum(increase(node_cpu_seconds_total [1m])) by (instance)

使用率:

1 - (sum(increase(node_cpu_seconds_total {mode="idle"}[1m])) by (instance) /sum(increase(node_cpu_seconds_total [1m])) by (instance))

 

Pushgateway

 

chmod 777 /home/tarena/app/pushgateway/pushgateway_start.sh

daemonize -c /home/tarena/app/pushgateway/ /home/tarena/app/pushgateway/pushgateway_start.sh

 

pushgateway_start.sh

/home/tarena/app/pushgateway/pushgateway

 

#!/usr/bin/env python3

from prometheus_client import CollectorRegistry, Gauge, push_to_gateway

registry = CollectorRegistry()

g = Gauge('test_metrics', '描述信息',['label_name'], registry=registry)

g.labels('label1').set(2)

push_to_gateway('localhost:9091', job='ping_status', registry=registry)

 

prometheus

安装

tar –xzvf prometheus-2.6.1.linux-amd64.tar.gz

cp –r prometheus-2.6.1.linux-amd64 /usr/local

 

运行

  ./prometheus

  127.0.0.1::9090

 

global: 

  scrape_interval: 15s  采样数据时间间隔

  evaluation_interval: 15s 监控数据评估频率

 

程序放入后台: screen / daemonize

git clone git://github.com/bmc/daemonize.git

cd daemonize

sh configure && make && sudo make install

chmod 777 up.sh

daemonize -c /home/tarena/app/prometheus/ /home/tarena/app/prometheus/up.sh

 

up.sh

/home/tarena/app/prometheus/prometheus --config.file="/home/tarena/app/prometheus/prometheus.yml" --web.listen-address="0.0.0.0:9090" --web.read-timeout=5m --web.max-connections=10 --storage.tsdb.retention=15d --storage.tsdb.path="data/" --query.max-concurrency=20 --query.timeout=2m

--web.read-timeout=5m 防止太多空闲链接,占用资源

 

 

查询是否启动成功:

ps -ef | grep prometheus | grep -v gluster

 

Prometheus计算公式

increase函数:该函数配合counter数据类型使用,获取区间向量中的第一个和最后一个样本并返回其增长量。如果除以一定时间就可以获取该时间内的平均增长率

1

increase(node_cpu[2m]) / 120 #主机节点最近两分钟内的平均CPU使用率

 

rate函数:该函数配合counter类型数据使用,取counter在这个时间段中的平均每秒增量。比如监控网络接受字节数的情况,在9:10到9:20期间累计量增加了1000bytes,加入rate([1m])函数后就会使用1000除以60秒,计算出数据大约为16bytes。

1

rate(node_cpu[2m])  #直接计算区间向量在时间窗口内平均增长速率

 

sum函数:在实际工作中CPU大多是多核的,而node_cpu会将每个核的数据都单独显示出来,我们其实不会关注每个核的单独情况,而是关心总的CPU情况。使用sum()函数进行求和后可以得出一条总的数据,但sum()是将所有机器的数据都进行了求和,所以还要再使用by (instance)或者by (cluster_name)就可以取出单个服务器或者一组服务器的CPU数据。上面的公式可以进化为:

1

sum( increase(node_cpu[1m]) )

 

count函数:该函数用于进行一些模糊判断,比如有100台服务器在监控,想实现当CPU使用率大于80%的机器达到N台就进行报警就可以使用它

1

count(count_netstat_wait_connections > 200)

 

topk函数:该函数可以从大量数据中取出排行前N的数值,N可以自定义。比如监控了100台服务器的320个CPU,用这个函数就可以查看当前负载较高的那几个,用于报警

1

topk(3,count_netstat_wait_connections)  #Gauge类型

2

topk(3,,rate(node_network_receive_bytes[20m]))  #Counter类型

 

predict_linear函数:对曲线变化速率进行计算,起到一定的预测作用。比如当前这1个小时的磁盘可用率急剧下降,这种情况可能导致磁盘很快被写满,这时可以使用该函数,用当前1小时的数据去预测未来几个小时的状态,实现提前告警

 

predict_linear( node_filesystem_free_bytes{mountpoint="/"}[1h],4*3600 ) < 0   #如果未来4时后磁盘使用率为负数就会报警

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值