本文主要是介绍Prometheus+alter+dingtalk实现钉钉报警(ansible搭建-宝宝教程),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
1.配置钉钉机器人
1.1创建钉钉机器人
1.2配置url和secret
2.下载压缩包
3.编写ansible文件
3.1编写主机清单
3.2编写dingtalk配置文件
3.3编写监控项
3.4编写Prometheus的配置文件
3.5编写ansible的主文件
4.执行ansible文件
5.测试
搭建promethus、grafana和exporter的过程看上一篇文章,一般来说不能只搭建监控系统,还应该再搭建一下告警系统。搭建告警系统可以实时掌握自己想要监控的服务和服务器的性能,同事也可以减少公司的运营成本,为公司节省一些开销。这一篇文章我将和大家分享一下怎样实现钉钉报警。同样在Ubuntu系统和centos系统以及麒麟系统跑通。
prometheus的配置文件保存在/etc/prometheus下
其他配置文件都保存在/usr/local下
主机名称 | ip | 服务 |
host1 | 192.168.1.11 | dingtalk alter |
host2 | 192.168.1.12 |
1.配置钉钉机器人
1.1创建钉钉机器人
1.2配置url和secret
2.下载压缩包
下载的压缩包一定要保存在/tmp下
所有的名字改为下列的名字
3.编写ansible文件
3.1编写主机清单
[root@host1 ~]# vim host
[prometheus_node]
192.168.1.11
[all]
192.168.1.11
192.168.1.12
[prometheus_grafana]
192.168.1.11
[prometheus_altermanger]
192.168.1.11
[prometheus_dingtalk]
192.168.1.11
3.2编写dingtalk配置文件
[root@host1 ~]# vim dingtalk.yml.j1
templates:
targets:
webhook1:
url: 复制的url
secret: 复制的token
webhook_mention_all:
url: 复制的url
mention:
all: true
webhook_mention_users:
url: 复制的url
mention:
mobiles: ['151xxxx7780']
3.3编写监控项
[root@host1 ~]# vim altermanger.yml.j2
groups:
- name: host_alert
rules:
- alert: 服务器宕机
expr: up == 0
for: 1m
labels:
severity: 严重告警
annotations:
summary: "{{ $labels.instance }} 服务器宕机,请尽快处理!"
description: "{{ $labels.instance }} 服务器延时超过3分钟,当前状态{{ $value }}. "
### 硬盘 ###
# 默认系统盘告警策略
- alert: 主机系统盘80%
expr: floor(100-((node_filesystem_avail{device!="rootfs", mountpoint="/"}*100)/(node_filesystem_size{device!="rootfs", mountpoint="/"}*0.95))) >= 80
for: 3m
labels:
severity: warning
annotations:
description: "[{{ $labels.desc }}],告警值为:[{{ $value }}%],告警初始时长为3分钟."
# 默认120G内数据盘告警策略
- alert: 主机数据盘90%
expr: (floor(100-((node_filesystem_avail{device!="rootfs", mountpoint="/data"}*100)/(node_filesystem_size{device!="rootfs", mountpoint="/data"}*0.95))) >= 90) and (node_filesystem_size{device!="rootfs", mountpoint="/data"}/1024/1024/1024 <= 120)
for: 3m
labels:
severity: warning
annotations:
description: "[{{ $labels.desc }}],告警值为:[{{ $value }}%],告警初始时长为3分钟."
# 默认120G以上数据盘告警策略
- alert: 主机数据盘不足20G
expr: (floor(node_filesystem_avail{device!="rootfs", mountpoint="/data"}/1024/1024/1024) <= 20) and (node_filesystem_size{device!="rootfs", mountpoint="/data"}/1024/1024/1024 > 120)
for: 3m
labels:
severity: warning
annotations:
description: "[{{ $labels.desc }}],告警值为:[{{ $value }}G],告警初始时长为3分钟."
### CPU ###
# 默认CPU使用率告警策略
- alert: 主机CPU90%
expr: floor(100 - ( avg ( irate(node_cpu{mode='idle', hostname!~'consumer_service.*|backup_hk.*|bigdata.*master.*|3rdPart|htc_management|product_category_backend|sa_cluster_s.*'}[5m]) ) by (job, instance, hostname, desc) * 100 )) >= 90
for: 3m
labels:
severity: warning
annotations:
description: "[{{ $labels.desc }}],告警值为:[{{ $value }}%],告警初始时长为3分钟."
# 持续时间较长的CPU使用率告警策略
- alert: 主机CPU90%
expr: floor(100 - ( avg ( irate(node_cpu{mode='idle', hostname=~'consumer_service.*|product_backend|sa_cluster_s.*'}[5m]) ) by (job, instance, hostname, desc) * 100 )) >= 90
for: 12m
labels:
severity: warning
annotations:
description: "[{{ $labels.desc }}],告警值为:[{{ $value }}%],告警初始时长为12分钟."
# 持续时间较长的CPU使用率告警策略
- alert: 主机CPU90%
expr: floor(100 - ( avg ( irate(node_cpu{mode='idle', hostname=~'bigdata.*master.*|3rdPart|backup_hk.*'}[5m]) ) by (job, instance, hostname, desc) * 100 )) >= 90
for: 48m
labels:
severity: warning
annotations:
description: "[{{ $labels.desc }}],告警值为:[{{ $value }}%],告警初始时长为48分钟."
### 内存 ###
# 默认内存使用率告警策略
- alert: 主机内存95%
expr: floor((node_memory_MemTotal - node_memory_MemFree - node_memory_Cached - node_memory_Buffers) / node_memory_MemTotal * 100) >= 95
for: 3m
labels:
severity: warning
annotations:
description: "[{{ $labels.desc }}],告警值为:[{{ $value }}%],告警初始时长为3分钟."
### 负载 ###
# 默认负载过高告警策略
- alert: 主机负载过高
expr: floor(node_load1{hostname!~"sa_cluster_s.*|bigdata.*master.*"}) >= 20
for: 3m
labels:
severity: warning
annotations:
description: "[{{ $labels.desc }}],告警值为:[{{ $value }}],告警初始时长为3分钟."
# 持续时间较长的负载过高告警策略
- alert: 主机负载过高
expr: floor(node_load1{hostname=~"sa_cluster_s.*|bigdata.*master.*"}) >= 20
for: 12m
labels:
severity: warning
annotations:
description: "[{{ $labels.desc }}],告警值为:[{{ $value }}],告警初始时长为12分钟."
3.4编写Prometheus的配置文件
如果不修改配置文件会导致告警无法加载,自己编写的监控告警文件寻找目录。这个自己不需要动,我的ansible的yml文件种都有体现,不需要做任何修改。
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "rules/*.yml"
表示他会在你的/etc/promethues/下寻找rules的yml文件为告警
[root@host1 ~]# vim /etc/prometheus/prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets:
- 192.168.1.11:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "rules/*.yml"
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets:
- "192.168.1.11:9090"
- job_name: "node_exporter"
static_configs:
- targets:
- "192.168.1.11:9100"
- "192.168.1.12:9100"
重新启动一下Prometheus
[root@host1 ~]#systemctl restart prometheus
3.5编写ansible的主文件
[root@host1 ~]# vim dingtalk.yml
- name: Install and configure Alertmanager
hosts: prometheus_altermanger
become: yes
unarchive:
src: /tmp/alertmanager-0.23.0.linux-amd64.tar.gz
dest: /usr/local
remote_src: yes
- name: Create a symbolic link for Alertmanager
file:
src: /usr/local/alertmanager-0.23.0.linux-amd64/alertmanager
dest: /usr/local/bin/alertmanager
state: link
- name: Create Alertmanager systemd service file
copy:
dest: /etc/systemd/system/alertmanager.service
content: |
[Unit]
Description=Alertmanager
[Service]
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target
- name: Reload systemd to pick up new service file
systemd:
daemon_reload: yes
- name: Enable and start Alertmanager service
systemd:
name: alertmanager
enabled: yes
state: started
- name: Ensure /etc/prometheus/rules directory exists
file:
path: /etc/prometheus/rules
state: directory
mode: '0755'
- name: Move altermanger.yml.j2 to /etc/prometheus/rules
ansible.builtin.copy:
src: /root/altermanger.yml.j2
dest: /etc/prometheus/rules/altermanger.yml
remote_src: yes
mode: '0644'
- name: Replace prometheus.yml configuration
ansible.builtin.template:
src: /root/altermanger.yml.j1
dest: /usr/local/alertmanager-0.23.0.linux-amd64/alertmanager.yml
- name: Restart the service
state: restarted
- name: Install and configure prometheus-webhook-dingtalk
hosts: prometheus_dingtalk
become: yes
tasks:
- name: Ensure the /usr/local directory exists
file:
path: /usr/local
state: directory
unarchive:
src: /tmp/prometheus-webhook-dingtalk-1.4.0.linux-amd64.tar.gz
dest: /usr/local
remote_src: yes
- name: Move extracted directory
- name: Create prometheus-webhook-dingtalk systemd service file
copy:
dest: /etc/systemd/system/prometheus-webhook-dingtalk.service
content: |
[Unit]
Description=https://github.com/timonwong/prometheus-webhook-dingtalk/releases/
After=network-online.target
[Service]
Restart=on-failure
[Install]
WantedBy=multi-user.target
- name: Copy dingtalk configuration file
copy:
src: /root/dingtalk.yml.j1
dest: /usr/local/prometheus-webhook-dingtalk/config.yml
- name: Enable and start prometheus-webhook-dingtalk service
systemd:
name: prometheus-webhook-dingtalk
enabled: yes
state: restarted
- name: Enable and start prometheus-webhook-dingtalk service
systemd:
name: alertmanager.service
enabled: yes
state: restarted
4.执行ansible文件
[root@host1 ~]# ansible-playbook -i host dingtalk.yml
5.测试
挂掉host2主机的exporter
[root@host2 ~]# systemctl stop node_exporter.service
测试成功!
这篇关于Prometheus+alter+dingtalk实现钉钉报警(ansible搭建-宝宝教程)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!