promethues+alertmanager+grafana监控docker容器和报警—基于手动配置和文件自动发现—详细文档

promethues+alertmanager+grafana监控docker容器和报警—基于手动配置和文件自动发现—详细文档

相关配套软件包网盘下载链接如下:
网盘地址: https://url28.ctfile.com/f/37115828-589234295-b4e3f2?p=4907
访问密码: 4907

本人会经常更新运维相关技术文档,如有兴趣,可以关注我博客,欢迎互动分享

–promethues支持两种添加被监控端:1)静态配置(手动配置), 2)服务发现(动态发现需要监控的target实例(基于文件和基于consul))
node节点安装cadvisor: (手动到各个节点安装)
机器:192.168.10.92 alertmanager、promethues、cadvisor (被监控端)
机器:192.168.10.90 cadvisor (被监控端) 10.90根据情况进行添加或去除
1.安装cadvisor-docker安装,配置认证和加参数(被监控宿主机安装2台)
准备要监控的系统服务:
#yum -y install docker
]# cat /etc/docker/daemon.json
{
“graph”: “/data/docker”,
“insecure-registries”:[“https://b9pmyelo.mirror.aliyuncs.com”]
}
#docker 运行一些容器服务(以被监控使用)
安装cadvisor(此处用docker安装):
#docker pull google/cadvisor:latest
#docker images |grep cadvisor
google/cadvisor latest eb1210707573 3 years ago 69.6MB
运行cadvisor容器: (cadvisor是采集docker容器映射到宿主机上的信息,需要cadvisor挂载宿主机上相关用到docker的目录)
#docker run -d
–volume=/:/rootfs:ro
–volume=/var/run:/var/run:ro
–volume=/sys:/sys:ro
–volume=/data/docker/:/var/lib/docker:ro
–volume=/dev/disk/:/dev/disk:ro
–publish=8080:8080
–detach=true
–name=cadvisor
google/cadvisor:latest #回车
#docker ps |grep cadvisor
2dac88e42016 google/cadvisor:latest “/usr/bin/cadvisor -…” About a minute ago Up About a minute 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp cadvisor
查看cadvisor暴露的采集数据接口: http://192.168.10.90:8080/metrics http://192.168.10.92:8080/metrics
在这里插入图片描述
在这里插入图片描述
2.二进制安装alertmanager: (192.168.10.92)
[root@nacos-nfs ~]# ls alertmanager-0.24.0.linux-amd64.tar.gz
alertmanager-0.24.0.linux-amd64.tar.gz
[root@nacos-nfs ~]# tar -zxf alertmanager-0.24.0.linux-amd64.tar.gz
[root@nacos-nfs ~]# mv alertmanager-0.24.0.linux-amd64 /data/alertmanager
[root@nacos-nfs ~]# cd /data/alertmanager/
[root@nacos-nfs alertmanager]# ls
alertmanager alertmanager.yml amtool LICENSE NOTICE
[root@nacos-nfs alertmanager]# cp alertmanager.yml alertmanager.yml.bak
[root@nacos-nfs alertmanager]# vim alertmanager.yml #使用qq邮箱,有时163邮箱不靠谱,(使用自带默认模板,不配置模板就是默认使用自带模板)
global:
resolve_timeout: 1m
#配置邮箱服务器:
smtp_smarthost: ‘smtp.qq.com:25’
#配置发件人:
smtp_from: ‘1036981484@qq.com’
smtp_auth_username: ‘1036981484@qq.com’
#配置发件人的授权密码:
smtp_auth_password: ‘fwbxnmbfnrpvbedi’
smtp_require_tls: false
#配置路由树:
route:
group_by: [‘alertname’] #根据告警规则组名进行分组,默认这里就是用alertname就可以了,可以精确到每一个告警规则,alertname的取值就是promethues中rules中自定义的告警规则的名称,根据触发情况取值会有所变动
group_wait: 10s #分组内第一个告警等待时间,10s内如有第二个告警会合并一个告警
group_interval: 10s #发送新告警间隔时间
repeat_interval: 10s #重复告警间隔发送时间
receiver: ‘mail’ #发送给哪个接收人,定义一个名字,具体接收人是谁,可以在下面的该名字下定义
receivers:

  • name: ‘mail’
    email_configs:
    • to: ‘1441107787@qq.com’
      send_resolved: true #设置恢复时候也提醒恢复信息,默认没有配置,则恢复时候不会发送提示信息

[root@nacos-nfs alertmanager]# ./alertmanager --config.file=./alertmanager.yml #指定配置文件启动
交给systemed管理服务:
[root@nacos-nfs alertmanager]# vim /usr/lib/systemd/system/alertmanager.service
[Unit]
Description=alertmanager daemon
[Service]
Restart=on-failure
ExecStart=/data/alertmanager/alertmanager --config.file=/data/alertmanager/alertmanager.yml
[Install]
WantedBy=multi-user.target
[root@nacos-nfs alertmanager]# systemctl daemon-reload
[root@nacos-nfs alertmanager]# systemctl enable alertmanager
[root@nacos-nfs alertmanager]# systemctl start alertmanager
[root@nacos-nfs alertmanager]# systemctl status alertmanager
● alertmanager.service - alertmanager daemon
Loaded: loaded (/usr/lib/systemd/system/alertmanager.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2022-04-10 17:43:02 CST; 4s ago
[root@nacos-nfs alertmanager]# netstat -anput |grep 9093
tcp6 0 0 :::9093 ::😗 LISTEN 30260/alertmanager
访问web界面如下:http://192.168.10.92:9093/
在这里插入图片描述
3.二进制安装promethues并测试告警
[root@nacos-nfs ~]# ls
prometheus-2.35.0-rc0.linux-amd64.tar.gz
[root@nacos-nfs ~]# tar -zxf prometheus-2.35.0-rc0.linux-amd64.tar.gz
[root@nacos-nfs ~]# ls
prometheus-2.35.0-rc0.linux-amd64 prometheus-2.35.0-rc0.linux-amd64.tar.gz
[root@nacos-nfs ~]# mv prometheus-2.35.0-rc0.linux-amd64 /data/prometheus
[root@nacos-nfs ~]# cd /data/prometheus/
[root@nacos-nfs prometheus]# ls
console_libraries consoles LICENSE NOTICE prometheus prometheus.yml promtool
[root@nacos-nfs prometheus]# cp prometheus.yml prometheus.yml.bak
[root@nacos-nfs prometheus]# vim prometheus.yml
global:
scrape_interval: 15s # 采集数据时间间隔
evaluation_interval: 15s # 评估的告警规则的时间间隔,每多少时间评估一次告警规则
alerting:
alertmanagers:
- static_configs:
- targets:
- 192.168.10.92:9093 #当安装了alertmanager,需要告警时可以指定alertmanager的ip和端口,若不用告警则可注释该行.
#下面配置告警规则引用的文件:
rule_files:

  • “rules/*.yml” #告警相关规则配置,不用可注释,rules是在promethues安装目录中创建一个rules目录
    #- “second_rules.yml” #告警相关规则配置,不用可注释
    scrape_configs: #下面是被监控端的相关配置

  • job_name: “docker” #每一个job_name可以认为是一个分组,一个分组可以在targets里定义多个监控的主机,相当于一个分组可以包含一批机器
    static_configs:

    • targets: [“192.168.10.92:8080”] #docker机器上安装cadvisor的地址和端口
      #targets: 是被监控端, targets中的[“”,“”,“”]为instance: 即每个被监控端称为实例
      #job: 具有相同目标的实例集合称为作业
      [root@nacos-nfs prometheus]# mkdir /data/prometheus/rules
      [root@nacos-nfs prometheus]# vim /data/prometheus/rules/docker.yml #配置系统服务状态告警触发条件和告警规则
      groups:
  • name: docker.rules #告警规则组名称
    rules:
    #docker所在容器宿主机实例down,主要以运行的cadvisor服务是否down为指标

    • alert: DockerInstanceDown #自定义的告警规则名称,触发报警时该值就作为alertmanager.yml中定义的alertname的取值
      expr: up{job=“docker”} == 0 #这种就是在promethues中语句试验出来的报警语句,下面类似
      for: 0m
      labels:
      severity: critical
      annotations:
      summary: “Docker Instance down”
      description: “容器实例: 【{{ $labels.instance }}】has been down for more than 1 minute”

    #容器消失或kill:-不太理解,感觉不准确,有待研究:

    • alert: ContainerKilled
      expr: time() - container_last_seen{name!=“”} > 60
      for: 1m
      labels:
      severity: critical
      annotations:
      summary: “A Container has disappeared”
      description: “Container Name 【{{ $labels.name }}】 on 主机【{{ $labels.instance }}】 has disappeared”

    #指定的容器挂掉或不存在则报警,需要指定监控的具体容器名,因为它也不能判断宿主机上都有哪些容器,例如:下面就具体指定了tomcat容器:

    • alert: ContainerAbsent-指定的容器如果挂掉或不存在了则报警
      expr: absent(container_last_seen{name=“tomcat”,instance=“192.168.10.92:8080”})
      #absent(container_last_seen{name=“tomcat”}) ,这行函数表示给定的容器不存在则报警,参考链接: https://blog.csdn.net/weixin_45444133/article/details/115823156
      #上面表达式也可模糊匹配,写成: absent(container_last_seen{name=~“tomcat.+”}) 或: absent(container_last_seen{name=~“tomcat.+”})

      for: 1m
      labels:
      severity: critical
      annotations:
      summary: “各容器如果挂掉或不存在了则报警”
      description: “容器名:{{ $labels.name }} 在宿主机{{ $labels.instance }}上挂掉或不存在了”

    #容器cup使用率高,大于80% 1分钟内每秒的使用率,rate()函数是: 一段时间内每秒的增长率(积累量/时间,得出每秒的变化率)
    #下面cpu表达式,也可用成: sum(irate(container_cpu_usage_seconds_total{image!=“”}[1m])) without (cpu) 都是表示1min变化率,表达式可以不同,只要在promethues中调试好就行
    #without (cpu)是表示不区分cpu,如: cpu001,cpu002,…

    • alert: ContainerCpuUsage
      expr: (sum by(instance, name) (rate(container_cpu_usage_seconds_total{name!=“”}[1m])) * 100) > 80
      for: 1m
      labels:
      severity: warning
      annotations:
      summary: “Container CPU usaged above 80%”
      description: “Container Name 【{{ $labels.name }}】 on 主机【{{ $labels.instance }}】 CPU usage is above 80%, Current Value: {{ $value }}”
      #容器内存使用率高: 各个容器内存占用各自容器设置最大限制总内存的比例,(容器已经使用的/限制容器的最大使用内存),>80%
    • alert: ContainerMemoryUsage-各容器占用各自最大限制值比例
      expr: (sum(container_memory_working_set_bytes{name!=“”}) by (instance,name) / sum(container_spec_memory_limit_bytes{name!=“”}) by (instance,name) * 100 != +Inf) > 80
      for: 1m
      labels:
      severity: warning
      annotations:
      summary: “Container Memory usaged 占用限制容器最大内存值 above 80%”
      description: “Container Name 【{{ $labels.name }}】 on 主机【{{ $labels.instance }}】 Memory usage占用限制容器最大内存比例 is above 80%,Current Value: {{ $value }}”

    #容器内存使用率高: 所有容器已使用的内存总和占用宿主机总内存的比例,(所有容器已经使用的总和/宿主机的总内存),>80%

    • alert: ContainerMemoryUsageAll-all容器已使用内存总和占用宿主机总内存比例
      expr: sum (container_memory_working_set_bytes{name!=“”}) by (instance)/sum (machine_memory_bytes) by (instance) *100 > 80
      for: 1m
      labels:
      severity: warning
      annotations:
      summary: “all容器内存总和 占用宿主机总内存 above 80%”
      description: “所有容器内存总和 占用宿主机【{{ $labels.instance }}】总内存比例 is above 80%, Current Value: {{ $value }}”
      #注意: 只有上面表达式中加入了 by (instance),即by (标签名),下面{{ $labels.instance }} 才能调用到相关的某个主机实例的值

    #容器网络入口(接收)流量速率高(单位:字节/秒),每秒网卡接收多少字节数据,下面除以两个1024转换为M单位,by (标签):按主机实例,容器名,网卡接口划分,划分后,下面才能调用标签的值

    • alert: Netowork-receiveRate-各容器网络接收流量速率(流入速率)
      expr: sum(rate(container_network_receive_bytes_total{image!=“”}[1m])) by (instance,name,interface)/1024/1024 > 100
      #上面也可使用类似表达式: sum(rate(container_network_receive_bytes_total{image!=“”}[1m])) without (interface)
      for: 1m
      labels:
      severity: warning
      annotations:
      summary: “容器入口(接收)流量过高”
      description: "容器名{{ $labels.name }} 的网卡接口{{ $labels.interface}} 在实例主机{{ $labels.instance }} 每秒入口流量过高,大于100M,当前值为:{{ $value }}M "

    #容器网络出口(传输)流量速率高(单位:字节/秒),每秒网卡流出(传输)多少字节,下面除以两个1024转换为M单位,by (标签):按主机实例,容器名,网卡接口划分,划分后,下面才能调用标签的值

    • alert: Netowork-trasmitRate-各容器网络传输流量速率(流出速率)
      expr: sum(rate(container_network_transmit_bytes_total{image!=“”}[1m])) by (instance,name,interface)/1024/1024 > 100
      #上面也可使用类似表达式: sum(rate(container_network_transmit_bytes_total{image!=“”}[1m])) without (interface)
      for: 1m
      labels:
      severity: warning
      annotations:
      summary: “容器出口(传输)流量过高”
      description: "容器名{{ $labels.name }} 的网卡接口{{ $labels.interface}} 在实例主机{{ $labels.instance }} 每秒出口(传输)流量过高,大于100M,当前值为:{{ $value }}M "

    #容器文件系统磁盘读取速率高(单位:字节/秒),每秒读取多少字节,下面除以两个1024转换为M单位,by (标签):按主机实例,容器名,网卡接口划分,划分后,下面才能调用标签的值

    • alert: Disk-Read-Rate-各容器文件系统磁盘读取速率
      expr: sum(rate(container_fs_reads_bytes_total{image!=“”}[1m])) by (instance,name,device)/1024/1024 > 50
      #上面也可使用类似表达式: sum(rate(container_fs_reads_bytes_total{image!=“”}[1m])) without (device)
      for: 1m
      labels:
      severity: warning
      annotations:
      summary: “容器文件系统磁盘读取速率过高”
      description: "容器名{{ $labels.name }} 的磁盘分区{{ $labels.device}} 在实例主机{{ $labels.instance }} 每秒读取速率过高,大于50M,当前值为:{{ $value }}M "

    #容器文件系统磁盘写入速率高(单位:字节/秒),每秒写入多少字节,下面除以两个1024转换为M单位,by (标签):按主机实例,容器名,网卡接口划分,划分后,下面才能调用标签的值

    • alert: Disk-Write-Rate-各容器文件系统磁盘写入速率
      expr: sum(rate(container_fs_writes_bytes_total{image!=“”}[1m])) by (instance,name,device)/1024/1024 > 50
      #上面也可使用类似表达式: sum(rate(container_fs_writes_bytes_total{image!=“”}[1m])) without (device)
      for: 1m
      labels:
      severity: warning
      annotations:
      summary: “容器文件系统磁盘写入速率过高”
      description: "容器名{{ $labels.name }} 的磁盘分区{{ $labels.device}} 在实例主机{{ $labels.instance }} 每秒写入速率过高,大于50M,当前值为:{{ $value }}M "

    #运行容器的数量高(单位:个),实例宿主机上容器数量大于100个报警

    • alert: ContainerNumbers
      expr: count(container_last_seen{image!=“”}) by (instance) > 100
      for: 1m
      labels:
      severity: warning
      annotations:
      summary: “实例宿主机上运行容器数量过多”
      description: “实例宿主机{{ $labels.instance }}上运行的容器数量过多,大于100个,当前值为:{{ $value }}个”

#注释:
#absent()函数:取布尔值,先获取一个瞬时向量作为参数,然后判断这个瞬时向量是否有值,如果该向量存在值,则返回空向量,如果该向量没有值,则返回不带标签名称的时间序列,并返回值为1,这对于在给定度量标准名称和标签组合不存在时间序列时发出警报非常有用.
#without (标签名) ,不区分标签,如: without (interface) 不区分网卡
#by (instance,name) by (标签名) 是根据标签名分类,此处是根据实例主机和容器名区分,name是该项目的标签名,根据实际情况选择相应的标签名,可以手动在prometheus中测试查看标签名
#内存注释:
#container_spec_memory_limit_bytes: 容器的内存使用量限制,当k8s中limit限制值,或docker run -m xxM 容器时候对容器内存的限制值,即容器的最大能达到内存
#container_memory_usage_bytes: 容器使用的内存,包含了cache,如filesystem cache,当存在mem pressure的时候能够被回收
#container_memory_working_set_bytes: 容器使用的内存(更准确),更能体现出mem usage,oom killer也是根据container_memory_working_set_bytes来决定是否oom kill的
#!= +Inf:过滤正无穷的数据,如果容器没有定义限制内存,测container_spec_memory_limit_bytes值是0,则:已使用内存/总内存的比值就是正无穷: +Inf
#container_memory_working_set_bytes 容器使用内存 更能体现出mem usage,也是oom killer指标(建议使用)
#machine_memory_bytes 当前主机的内存总量
#参考链接: https://blog.csdn.net/qq_31555951/article/details/108984036
#https://blog.csdn.net/u010918487/article/details/106190764

#关于docker监控指标和报警,参考的链接:
#https://www.prometheus.wang/exporter/use-prometheus-monitor-container.html
#https://blog.csdn.net/u013288190/article/details/116793085?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-0-116793085-blog-82941402.pc_relevant_antiscanv2&spm=1001.2101.3001.4242.1&utm_relevant_index=2
#https://blog.csdn.net/yjph83/article/details/84909318
#all规则: https://awesome-prometheus-alerts.grep.to/rules
#主机监控详细:https://blog.csdn.net/weixin_43815140/article/details/119730821
#主机监控和docker监控详细: https://blog.csdn.net/wangshui898/article/details/120321640?spm=1001.2101.3001.6650.5&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-5-120321640-blog-122500343.pc_relevant_without_ctrlist_v2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-5-120321640-blog-122500343.pc_relevant_without_ctrlist_v2&utm_relevant_index=9

#关于docker stats 容器名 得到的容器参数指标详解:
#docker stats --no-stream elasticsearch
#CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
#255d25fefcf6 elasticsearch 0.75% 871.8MiB / 3.702GiB 23.00% 6.42GB / 4.34GB 7.98GB / 15.2GB 81
#CONTAINER ID:容器ID NAME:容器名称 CPU%:容器正在使用的主机CPU的百分比
#MEM USAGE / LIMIT:容器正在使用的总内存,以及允许使用的总内存量 MEM%:容器正在使用的主机内存的百分比
#NET I/O:容器通过其网络接口发送和接收的数据量 BLOCK I/O:容器已从主机上的块设备读取和写入的数据量
#PIDS:容器创建的进程或线程数
#参考链接地址: https://www.cnblogs.com/scholars-xian/p/11928595.html
[root@nacos-nfs prometheus]# ./promtool check config ./prometheus.yml #检查配置文件是否正确
Checking ./prometheus.yml
SUCCESS: ./prometheus.yml is valid prometheus config file syntax
[root@nacos-nfs prometheus]# /data/prometheus/prometheus --config.file=/data/prometheus/prometheus.yml #指定配置文件启动
交给systemed管理:
[root@nacos-nfs prometheus]# vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus daemon
[Service]
Restart=on-failure
ExecStart=/data/prometheus/prometheus --config.file=/data/prometheus/prometheus.yml
[Install]
WantedBy=multi-user.target
[root@nacos-nfs prometheus]# systemctl daemon-reload
[root@nacos-nfs prometheus]# systemctl enable prometheus
[root@nacos-nfs prometheus]# systemctl start prometheus
[root@nacos-nfs prometheus]# systemctl status prometheus
● prometheus.service - Prometheus daemon
Loaded: loaded (/usr/lib/systemd/system/prometheus.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2022-04-09 23:35:06 CST; 6s ago
[root@nacos-nfs prometheus]# netstat -anput |grep 9090|grep LISTEN
tcp6 0 0 :::9090 ::😗 LISTEN 28970/prometheus
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
测试告警:测试几个报警例子演示即可(配置告警规则都测试过)
将cpu、内存、磁盘写入速率、网络每秒流出速率的阀值设置为0,则报警如下:
[root@nacos-nfs rules]# vim docker.yml
[root@nacos-nfs rules]# systemctl restart prometheus
在这里插入图片描述
在这里插入图片描述
查看发送邮件情况:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
当把参数调整为正常值,恢复报警时发送恢复邮件:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
注意:promethues可以配置手动配置主机,也可基于文件发现主机:(根据需要使用)
[root@prometheus ~]# vim /data/prometheus/prometheus.yml #配置文件解释,静态配置和基于文件发现(类似监控主机配置)
#1).静态配置内容案例:
global:
scrape_interval: 15s # 采集数据时间间隔
evaluation_interval: 15s # 评估的告警规则的时间间隔,每多少时间评估一次告警规则
alerting:
alertmanagers:
- static_configs:
- targets:
- 192.168.10.92:9093 #当安装了alertmanager,需要告警时可以指定alertmanager的ip和端口,若不用告警则可注释该行.
#下面配置告警规则引用的文件:
rule_files:

  • “rules/*.yml” #告警相关规则配置,不用可注释,rules是在promethues安装目录中创建一个rules目录
    #- “second_rules.yml” #告警相关规则配置,不用可注释
    scrape_configs: #下面是被监控端的相关配置
  • job_name: “docker” #每一个job_name可以认为是一个分组,一个分组可以在targets里定义多个监控的主机,相当于一个分组可以包含一批机器
    static_configs:
    • targets: [“192.168.10.92:8080”] #docker机器上安装cadvisor的地址和端口
      #targets: 是被监控端, targets中的[“”,“”,“”]为instance: 即每个被监控端称为实例
      #job: 具有相同目标的实例集合称为作业

#2) 基于文件发现
#file_sd_configs: 基于文件发现的方式,定义被监控的容器
global:
scrape_interval: 15s # 采集数据时间间隔
evaluation_interval: 15s # 评估的告警规则的时间间隔,每多少时间评估一次告警规则
alerting:
alertmanagers:
- static_configs:
- targets:
- 192.168.10.92:9093 #当安装了alertmanager,需要告警时可以指定alertmanager的ip和端口,若不用告警则可注释该行.
#下面配置告警规则引用的文件:
rule_files:

  • “rules/*.yml” #告警相关规则配置,不用可注释,rules是在promethues安装目录中创建一个rules目录
    #- “second_rules.yml” #告警相关规则配置,不用可注释
    scrape_configs: #下面是被监控端的相关配置
# metrics_path defaults to '/metrics'
  • job_name: “docker” #每一个job_name可以认为是一个分组,一个分组可以在targets里定义多个监控的主机,相当于一个分组可以包含一批机器
    file_sd_configs:
    • files: [‘/data/prometheus/sd_config/docker_sd_config.yaml’] #指定具体文件路径
      refresh_interval: 5s
      #refresh_interval:指定刷新发现文件的间隔
      [root@prometheus ~]# mkdir /data/prometheus/sd_config
      [root@prometheus ~]# vim /data/prometheus/sd_config/docker_sd_config.yaml
  • targets:
    • 192.168.10.92:8080
      然后启动promethues即可
      效果一样:
      在这里插入图片描述
      在这里插入图片描述
      4.二进制安装grafana:界面展示
      [root@nacos-nfs ~]# ls grafana-enterprise-8.4.5.linux-amd64.tar.gz
      grafana-enterprise-8.4.5.linux-amd64.tar.gz
      [root@nacos-nfs ~]# tar -zxf grafana-enterprise-8.4.5.linux-amd64.tar.gz
      [root@nacos-nfs ~]# mv grafana-8.4.5/ /data/grafana
      [root@nacos-nfs ~]# cd /data/grafana/
      [root@nacos-nfs grafana]# ls
      bin conf LICENSE NOTICE.md plugins-bundled public README.md scripts VERSION
      [root@nacos-nfs grafana]# ls bin/
      grafana-cli grafana-cli.md5 grafana-server grafana-server.md5
      [root@nacos-nfs grafana]# /data/grafana/bin/grafana-server #即可启动grafana
      交给systemed管理启动:
      [root@nacos-nfs grafana]# vim /usr/lib/systemd/system/grafana.service
      [Unit]
      Description=grafana daemon
      [Service]
      Restart=on-failure
      ExecStart=/data/grafana/bin/grafana-server -homepath=/data/grafana #指定安装目录启动
      [Install]
      WantedBy=multi-user.target
      [root@nacos-nfs grafana]# systemctl start grafana
      [root@nacos-nfs grafana]# systemctl status grafana
      ● grafana.service - grafana daemon
      Loaded: loaded (/usr/lib/systemd/system/grafana.service; disabled; vendor preset: disabled)
      Active: active (running) since Sat 2022-04-09 20:14:52 CST; 11s ago
      [root@nacos-nfs grafana]# systemctl enable grafana
      [root@nacos-nfs grafana]# netstat -anput |grep 3000
      tcp6 0 0 :::3000 ::😗 LISTEN 28647/grafana-serve
      访问grafana: http://192.168.10.92:3000 默认用户名和密码都是admin
      在这里插入图片描述
      在这里插入图片描述
      添加promethues数据源:
      在这里插入图片描述
      在这里插入图片描述
      在这里插入图片描述
      在这里插入图片描述
      导入监控docker容器的相关监控仪表盘,或者选择相应的id输入导入:
      在这里插入图片描述
      在这里插入图片描述
      在这里插入图片描述
      在这里插入图片描述
      在这里插入图片描述

补充:邮件报警时候,自定义报警模板:(其他不变,只修改下面变化部分):
[root@nacos-nfs ~]# vim /data/alertmanager/alertmanager.yml #修改alertmanager配置文件
global:
resolve_timeout: 1m
#配置邮箱服务器:
smtp_smarthost: ‘smtp.qq.com:25’
#配置发件人:
smtp_from: ‘1036981484@qq.com’
smtp_auth_username: ‘1036981484@qq.com’
#配置发件人的授权密码:
smtp_auth_password: ‘fwbxnmbfnrpvbedi’
smtp_require_tls: false
#添加下面:
templates:

  • ‘/data/alertmanager/templates/*.tmpl’ #定义模板路径
    #配置路由树:
    route:
    group_by: [‘alertname’] #根据告警规则组名进行分组,默认这里就是用alertname就可以了,可以精确到每一个告警规则,alertname的取值就是promethues中rules中自定义的告警规则的名称,根据触发情况取值会有所变动
    group_wait: 10s #分组内第一个告警等待时间,10s内如有第二个告警会合并一个告警
    group_interval: 10s #发送新告警间隔时间
    repeat_interval: 10s #重复告警间隔发送时间
    receiver: ‘mail’ #发送给哪个接收人,定义一个名字,具体接收人是谁,可以在下面的该名字下定义
    receivers:
    • name: ‘mail’
      email_configs:
      • to: ‘1441107787@qq.com’
        send_resolved: true #设置恢复时候也提醒恢复信息
        #添加下面:
        html: ‘{{ template “email.template.tmpl” . }}’ #配置调用哪个模板

[root@nacos-nfs ~]# mkdir /data/alertmanager/templates #创建自定义报警模板路径
[root@nacos-nfs ~]# vim /data/alertmanager/templates/email.template.tmpl #编辑自定义报警模板
{{ define “email.template.tmpl” }}
{{- if gt (len .Alerts.Firing) 0 -}}{{ range.Alerts }}
告警名称: {{ .Labels.alertname }}

实例名: {{ .Labels.instance }}

摘要: {{ .Annotations.summary }}

详情: {{ .Annotations.description }}

级别: {{ .Labels.severity }}

开始时间: {{ (.StartsAt.Add 28800e9).Format “2006-01-02 15:04:05” }}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++

{{ end }}{{ end -}}
{{- if gt (len .Alerts.Resolved) 0 -}}{{ range.Alerts }}
Resolved-告警恢复了。

告警名称: {{ .Labels.alertname }}

实例名: {{ .Labels.instance }}

摘要: {{ .Annotations.summary }}

详情: {{ .Annotations.description }}

级别: {{ .Labels.severity }}

开始时间: {{ (.StartsAt.Add 28800e9).Format “2006-01-02 15:04:05” }}

恢复时间: {{ (.EndsAt.Add 28800e9).Format “2006-01-02 15:04:05” }}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++

{{ end }}{{ end -}}
{{- end }}
当告警时候,模板内容如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
当恢复报警时,模板内容如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维实战帮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值