在运维日常工作中可能出现的场景是:
- 有多套监控系统,基础系统监控一套,Kubernetes容器环境一套等等
- 监控告警各套环境有自己的一套规则和通知方式
- 重复或同类型的监控告警同时出现,我们到底应该关注哪个
目前市面上有一款开源软件叫做PrometheusAlert能够帮助我们解决上面场景中的这些问题。它是开源的运维告警中心消息转发系统,支持主流的监控系统Prometheus、Zabbix,日志系统Graylog2,Graylog3和数据可视化系统Grafana发出的预警消息,支持将收到的这些消息发送到钉钉,微信,飞书,腾讯短信,腾讯电话,阿里云短信,阿里云电话,华为短信,容联云电话等。
虽然它支持四种监控相关应用的接入,但从名称就可以看出它和Prometheus的结合是最好的。下面我就以Prometheus为主来介绍它的安装和使用
在linux系统中部署
# 下载安装包cd /opt/srcwget https://github.com/feiyu563/PrometheusAlert/archive/v3.0.2.zip# 解压移动unzip v3.0.2.zipmv PrometheusAlert-3.0.2/ /opt/app/# 赋权做软链chown dev. /opt/app/PrometheusAlert* -Rchmod 700 /opt/app/PrometheusAlert/example/linux/PrometheusAlertsu - dev -c 'ln -s /opt/app/PrometheusAlert-3.0.2/ /opt/app/PrometheusAlert'
设置开机启动
# vim /etc/systemd/system/PrometheusAlert.service[Unit]Description=PrometheusAlertAfter=network.target[Service]User=devGroup=devRestart=on-failure# 工作目录必须要配置,否则打不开web页面WorkingDirectory=/opt/app/PrometheusAlert/example/linuxExecStart=/opt/app/PrometheusAlert/example/linux/PrometheusAlert[Install]WantedBy=multi-user.target# systemctl start PrometheusAlert# systemctl status PrometheusAlert# systemctl enable PrometheusAlert#启动后可使用浏览器打开以下地址查看:http://127.0.0.1:8080
使用钉钉告警
配置钉钉告警
- 开启钉钉机器人
- 配置PrometheusAlert配置文件app.conf
复制Webhook地址,并填入PrometheusAlert配置文件app.conf中对应配置项即可。钉钉相关配置:
# cd /opt/app/PrometheusAlert/example/linux/conf# vim app.conf#---------------------↓全局配置-----------------------#告警消息标题title=PrometheusAlert#钉钉告警 告警logo图标地址logourl=https://raw.githubusercontent.com/feiyu563/PrometheusAlert/master/doc/alert-center.png#钉钉告警 恢复logo图标地址rlogourl=https://raw.githubusercontent.com/feiyu563/PrometheusAlert/master/doc/alert-center.png#---------------------↓webhook-----------------------#是否开启钉钉告警通道,可同时开始多个通道0为关闭,1为开启open-dingding=1#默认钉钉机器人地址ddurl=https://oapi.dingtalk.com/robot/send?access_token=xxxxx#是否开启 @所有人(0为关闭,1为开启)dd_isatall=1
- 页面上点击测试
Prometheus接入配置
Prometheus支持两种配置,任选其一或者两者搭配均可。
- 通过Prometheus Rules方式
首先需要在Alertmanager配置Webhook,可参考如下模板:
global: resolve_timeout: 5mroute: group_by: ['instance'] group_wait: 10m group_interval: 10s repeat_interval: 10m receiver: 'web.hook.prometheusalert'receivers:- name: 'web.hook.prometheusalert' webhook_configs: - url: 'http://[prometheusalert_url]:8080/prometheus/alert'
Prometheus Server 的告警rules配置,可参考如下模板:
groups: name: node_alert rules: alert: 主机CPU告警 expr: node_load1 > 1 labels: name: prometheusalertcenter level: 3 #告警级别,告警级别定义 0 信息,1 警告,2 一般严重,3 严重,4 灾难 annotations: description: "{{ $labels.instance }} CPU load占用过高" #告警信息 mobile: 15888888881,15888888882,15888888883 #告警发送目标手机号(需要设置电话和短信告警级别) ddurl: "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" #支持添加多个钉钉机器人告警,用,号分割即可,如果留空或者未填写,则默认发送到配置文件中填写的钉钉器人地址 wxurl: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxx-xxxxxx-xxxxxx-xxxxxx,https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxx-xxxx-xxxxxxx-xxxxx" #支持添加多个企业微信机器人告警,用,号分割即可,如果留空或者未填写,则默认发送到配置文件中填写的企业微信机器人地址
实测告警结果:
- 通过Prometheus AlertManager router方式
针对/prometheus/router AlertManager router指定接收端接口
,该接口可在url中直接指定告警的接收端,目前支持三个参数,分别是:wxurl,ddurl,phone(phone用于短信和电话告警)
在 Prometheus Alertmanager 中配置Webhook,可参考如下模板:
global: resolve_timeout: 5mroute: group_by: ['instance'] group_wait: 10m group_interval: 10s repeat_interval: 10m receiver: 'web.hook.prometheusalert'receivers:- name: 'web.hook.prometheusalert' webhook_configs: - url: 'http://[prometheusalert_url]:8080/prometheus/router?wxurl=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxx&ddurl=https://oapi.dingtalk.com/robot/send?access_token=xxxxx&phone=15888888881'
- 针对Prometheus增加了告警级别,并且支持按照不同级别发送消息到不同目标对象
- prometheus增加配置
配置了cpu报警不同的告警级别,如高级别的产生将不会在低级别的报警中出现,不同级别的报警是报到不同的钉钉上的
# cd /opt/app/prometheus/rules# vim prometheusalert.yml - alert: cpu使用率过高 expr: 100-avg(irate(node_cpu_seconds_total{mode="idle"}[5m])) by(instance)*100 > 5 for: 3s labels: level: 1 severity: warning annotations: summary: "Instance {{ $labels.instance }} cpu使用率过高" description: "{{ $labels.instance }} of job {{$labels.job}}cpu使用率超过5%,当前使用率[{{ $value }}]." ddurl: "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxx" - alert: cpu使用率过高 expr: 100-avg(irate(node_cpu_seconds_total{mode="idle"}[5m])) by(instance)*100 > 30 for: 3s labels: level: 2 severity: critical annotations: summary: "Instance {{ $labels.instance }} cpu使用率过高" description: "{{ $labels.instance }} of job {{$labels.job}}cpu使用率超过30%,当前使用率[{{ $value }}]." ddurl: "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx"
- alertmanager配置增加
# cd /opt/app/alertmanager# vim alertmanager.ymlroute: receiver: 'web.hook.prometheusalert'receivers:- name: 'web.hook.prometheusalert' webhook_configs: - url: 'http://192.168.106.227:8080/prometheus/alert' # 如果同时收到critical级别的报警将会抑制warning级别的报警,共同点是alertname和instance一样inhibit_rules: - source_match: severity: 'critical' target_match: severity: 'warning' equal: ['alertname', 'instance']
通过上述两项配置就可以做到报警的分级
- 按照日期发送告警到不同号码,并且已经加入告警失败或者被告警人未接听电话后转联系默认联系人(defaultphone)
- 另外 PrometheusAlert 同时支持按照日期发送告警到不同号码,并且已经加入告警失败或者被告警人未接听电话后转联系默认联系人(defaultphone),只需新建user.csv文件,并将文件放到程序运行目录下即可自动加载,同时该文件也是电话回调接口所必需文件(如回调接口返回非0状态,则会在此文件中寻找下一号码进行拨打,如开启回调功能,请务必创建该文件) ps:目前grafana/graylog的电话和短信告警依赖于该文件,prometheus电话和短信告警优先从rules的Mobile字段读取,如未配置号码,则会从user.csv中读取,如user.csv中也未配置,则会直接发送到defaultphone 关于user.csv中值班时间切换问题,默认每日上午10点进行切换 csv文件格式如下:
2019年4月10日,15888888881,小张,15999999999,备用联系人小陈,15999999998,备用联系人小赵
2019年4月11日,15888888882,小李,15999999999,备用联系人小陈,15999999998,备用联系人小赵
2019年4月12日,15888888883,小王,15999999999,备用联系人小陈,15999999998,备用联系人小赵
2019年4月13日,15888888884,小宋,15999999999,备用联系人小陈,15999999998,备用联系人小赵