docker-compose安装prometheus告警系统

news/2024/5/18 22:53:21 标签: docker, prometheus, 运维

docker-compose安装Prometheus

  • 一、概述
  • 一、docker-compose
  • 二、配置文件

一、概述

本文只有监控与告警的安装、告警发送、发送模版的配置。没有数据展示监控数据UI工具

dockercompose_3">一、docker-compose

1)docker-compose.yaml

version: '3.0'
services:
  #1.prometheus
  prometheus:
    image: prom/prometheus
    container_name: prometheus
    restart: always
    volumes:
      - $PWD/config/prometheus.yml:/etc/prometheus/prometheus.yml
      - $PWD/config/rules:/etc/prometheus/rules
      - $PWD/config/targets:/etc/prometheus/targets
    command:
      - --web.enable-lifecycle
      - --config.file=/etc/prometheus/prometheus.yml
    ports:
      - 9090:9090
    networks:
      - prometheus
    environment:
      - TZ=Asia/Shanghai

  #2.alertmanager
  alertmanager:
    image: prom/alertmanager
    container_name: alertmanager
    restart: always
    volumes:
      - $PWD/config/alertmanager.yml:/etc/alertmanager/alertmanager.yml
      - $PWD/config/template:/alertmanager/template
    ports:
      - 9093:9093
    networks:
      - prometheus
    environment:
      - TZ=Asia/Shanghai

networks:
  prometheus:
    driver: bridge

启动后prometheus访问地址:http://192.168.1.1:9090/graph?g0.expr=&g0.tab=1&g0.stacked=0&g0.range_input=1h
在这里插入图片描述
alertmanager访问地址:http://192.168.1.1:9093/#/alerts
在这里插入图片描述

二、配置文件

1)prometheus.yml

global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).
  
# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  - "rules/*"
# - "second_rules.yml"

# remote_write:
#   - url: http://192.168.50.200:8080/prometheus

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ['192.168.1.1:9090']
        labels:
          instance: prometheus

  - job_name: 'node_exporter'
    file_sd_configs:
    - files:
      - targets/*.yml
    

2)alertmanager.yml


global:
  # smtp_smarthost: 'smtp.exmail.qq.com:587'
  # smtp_from: '***'                # 谁发邮件
  # smtp_auth_username: '***'      # 邮箱用户
  # smtp_auth_password: '***'                 # 邮箱密码
  # smtp_require_tls: true
  smtp_smarthost: 'smtp.163.com:465'
  smtp_from: '***'               # 谁发邮件
  smtp_auth_username: '***'       # 邮箱用户
  smtp_auth_password: '***'                   # 邮箱密码
  smtp_require_tls: false

route:
  group_by: ["instance"]            # 分组名
  group_wait: 10s                   # 当收到告警的时候,等待三十秒看是否还有告警,如果有就一起发出去
  group_interval: 10s                # 发送警告间隔时间
  repeat_interval: 1h              # 重复报警的间隔时间
  receiver: mail                    # 全局报警组,这个参数是必选的,和下面报警组名要相同

templates:
  - '/alertmanager/template/*.tmpl'
  
receivers:
- name: 'mail'                      # 报警组名
  email_configs:
  - to: '****'      # 发送给谁,多个逗号隔开
    html: '{{ template "alert.html" . }}'
    headers: { Subject: "[WARN] 监控报警" }
 

3)targets配置,建目标服务器配置文件node_targets.yml

- targets:
  - 192.168.1.*:9100
  labels:
    host: aaa
- targets:
  - 192.168.1.*:9100
  labels:
    host: bbb

4)常用告警规则,以node_exporter 为例node_down.yml

groups:
- name: 实例存活告警规则
  rules:
  - alert: 实例存活告警
    expr: up == 0
    for: 1m
    labels:
      user: prometheus
      severity: error
    annotations:
      summary: "机器 {{ $labels.instance }} 挂了"
      description: "{{ $labels.instance }} 已停止超过1分钟"

- name: CPU报警规则
  rules:
  - alert: CPU使用率告警
    expr: 100 - (avg by (instance)(irate(node_cpu_seconds_total{mode="idle"}[1m]) )) * 100 > 90
    for: 1m
    labels:
      user: prometheus
      severity: warning
    annotations:
      description: "服务器: CPU使用超过90%!(当前值: {{ $value }}%)"
      summary: "机器 {{ $labels.instance }} CPU使用超过90%"

- name: 内存报警规则
  rules:
  - alert: 内存使用率告警
    expr: (node_memory_MemTotal_bytes - (node_memory_MemFree_bytes+node_memory_Buffers_bytes+node_memory_Cached_bytes )) / node_memory_MemTotal_bytes * 100 > 80
    for: 1m
    labels:
      user: prometheus
      severity: warning
    annotations:
      description: "服务器: 内存使用超过80%!(当前值: {{ $value }}%)"
      summary: "机器 {{ $labels.instance }} 内存使用超过80%"

- name: 磁盘报警规则
  rules:
  - alert: 磁盘使用率告警
    expr: (node_filesystem_size_bytes - node_filesystem_avail_bytes) / node_filesystem_size_bytes * 100 > 80
    for: 1m
    labels:
      user: prometheus
      severity: warning
    annotations:
      description: "服务器: 磁盘设备: 使用超过80%!(挂载点: {{ $labels.mountpoint }} 当前值: {{ $value }}%)"
      summary: "机器 {{ $labels.instance }} 磁盘使用超过80%"

5)告警模版 alert.tmpl

{{ define "alert.html" }}
{{ range .Alerts }}
<table border="1">
  <tr>
    <th> 告警通知 </th>
    <th>  厂区prometheus监控告警通知 </th>
  </tr>
  <tr>
    <td> 告警级别 </td>
    <td>{{ .Labels.severity }}</td>
  </tr>

  <tr>
    <td> 告警类型 </td>
    <td>{{ .Labels.alertname }}</td>
  </tr>
  <tr>
    <td> 故障主机 </td>
    <td>{{ .Labels.instance }}</td>
  </tr>

  <tr>
    <td> 告警主题 </td>
    <td>{{ .Annotations.summary }}</td>
  </tr>

  <tr>
    <td> 告警详情 </td>
    <td>{{ .Annotations.description }}</td>
  </tr>

  <tr>
    <td> 触发时间 </td>
    <td>{{ (.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}</td>
  </tr>

</table>
{{ end }}
{{ end }}

http://www.niftyadmin.cn/n/228088.html

相关文章

easyx

普通的画线图什么的 首先我们需要安装一个easyx的图形库&#xff0c;然后把头文件搞出来 #include <stdio.h> #include <easyx.h>//easyx画线啥啥的图形库 #include <graphics.h> #include <math.h> #include <conio.h>//键盘操作的头文件 设…

数字孪生之ThingJS

数字孪生对象层级关系获取对象的方法对象层级关系 获取对象的方法 通过加载事件获取根对象&#xff0c;从而去获取子对象app.on("load", function(ev){var campus ev.campus; // 园区对象集合var buildings campus.buildings; // 建筑对象集合// var building…

typecho 调用 Gravatar 头像的几种思路

Gravatar&#xff08;英语&#xff1a;Globally Recognized Avatar&#xff09;是一项用于提供在全球范围内使用的头像服务。只要你在 Gravatar 的服务器上上传了你自己的头像&#xff0c;你便可以在其他任何支持 Gravatar 的博客、论坛等地方使用它。 一、更换 Gravatar 头像源…

2.mybatis-plus-mapper

1.通用mapper接口方法 前缀 &#xff1a; 查询select&#xff0c;删除delete&#xff0c;添加insert&#xff0c;更新update 1.1.Insert // 插入一条记录 int insert(T entity);参数说明 类型参数名描述Tentity实体对象 1.2.Delete // 根据 entity 条件&#xff0c;删除记录…

JAVA阶段考内容知识点的梳理

前言&#xff1a;这些都是很基本的&#xff0c;还有很多没有写出来&#xff0c;重点在于编程序理解。 目录 第一章概述 课堂总结 相关习题 第二章&#xff1a;语言基础 课堂总结 相关习题 第三章&#xff1a;类和对象 内容总结 相关习题 第四章&#xff1a;类的派生与…

python+vue 在线读书与分享论坛

在线读书网站&#xff0c;是方便用户在线学习&#xff0c;阅读交流的一个平台&#xff0c;喜欢阅读的朋友&#xff0c;能够通过网站&#xff0c;发布一些关于读书信息&#xff0c;并且可以在线进行图书方面的论坛的交流。系统的开发&#xff0c;采用了django框架技术&#xff0…

【嵌入式常用通信知识点详解】:espi、dbus、kcs

文章目录espi 通信详解lpc 总线详解kcs 接口详解kcs 物理接口是什么&#xff1f;espi物理接口是什么&#xff1f;Dbus 通信详解espi 通信详解 ESPI是一种面向系统管理的总线标准&#xff0c;全称为Enhanced Serial Peripheral Interface&#xff0c;它被用于连接计算机系统的各…

网络安全-JDBC反序列化漏洞与RCE

目录环境Black Hat Europe 2019漏洞原理攻击手法mysql-connector-java的分析Mysql协议抓包分析Mysql服务器docker启动抓包&&分析Fake Mysql Server搭建Java反序列化工具ysoserial使用fnmsd师傅的MySQL_Fake_Server使用Y4tacker师傅的脚本JDBC代码项目架构代码复现参考环…