前言

准备软件包:

  • prometheus:prometheus-2.22.2.linux-amd64.tar.gz
  • grafana安装包:grafana-enterprise-7.3.4-1.x86_64.rpm
  • node_report:node_exporter-1.0.1.linux-amd64.tar.gz
  • mysqld-exporter:mysqld_exporter-0.12.1.linux-amd64.tar.gz
  • alertmanager:alertmanager-0.21.0.linux-amd64.tar.gz
  • 钉钉告警插件:prometheus-webhook-dingtalk-1.4.0.linux-amd64.tar.gz

可以使用kill -HUP <prometheus's pid>的方式热加载prometheus的配置。

本文档只是简单的初步配置,不涉及高可用和微服务、容器云、日志监控。

配置server单节点

  • server节点:172.50.13.101
  • 部署位置:/usr/local/prometheus/(建议部署在/usr/local/prometheus/prometheus/目录下)
  • 配置文件:/usr/local/prometheus/prometheus.yml(建议放在/usr/local/prometheus/prometheus/目录下)
  • 监听端口:19090
  • 数据存储位置:/home/prometheus/data/(建议存放于/home/data/prometheus/目录下)
  • 数据保留时间:15天

下载prometheus压缩包并解压,二进制文件可直接运行。

prometheus.service

将prometheus设置为service,并开机自启

  1. vim /usr/lib/systemd/system/prometheus.service
  2. 文件内容见本文底部 -> 附录代码 -> prometheus.service
  3. 加载service:systemctl daemon-reload
  4. 启动:systemctl start prometheus
  5. 设置自启:systemctl enable prometheus

配置node节点

  • node节点:需要被监控的服务器
  • 部署位置:/usr/local/prometheus/node_exporter/
  • 监听端口:18080
  1. 下载node_exporter压缩包到目标服务器的部署位置
  2. vim /usr/lib/systemd/system/node_exporter.service
  3. 文件内容见本文底部 -> 附录代码 -> node_exporter.service
  4. 加载service文件:systemctl daemon-reload
  5. 启动:systemctl start node_exporter
  6. 设置自启:systemctl enable node_exporter

配置mysql监控

在数据库中添加exporter账户

  • 登录需要被监控的数据库
  • 创建用户exporter:CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'XXXXXXXX' WITH MAX_USER_CONNECTIONS 3;
  • 授权:GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
  • 刷新:flush privileges;

修改mysql_exporter的配置

添加/usr/local/prometheus/mysql_exporter/.my.cnf文件

[client]
user=exporter
password=xxxxxxxx

添加service

见附录代码中的mysql_exporter.service

相关参考资料

配置grafana

安装grafana

下载rpm安装包后直接yum安装。

添加prometheus数据源

进入grafana的web界面,添加prometheus的数据源。

导入模板

mysql主题的ID:7362

node主题ID:13105

踩坑

  • 问题1:添加mysql的exporter后,prometheus的web页面能看到mysql的监控数据,但是grafana里面添加不了prometheus的数据源

    • 解决:

      • 试试其它mysql dashboard的ID
      • 自己创建panel。
    • 原因:并没有适配Prometheus
  • 问题2:grafana中的MySQL仪表盘有部分没有数据:
    • 解决:更改node_exporter和mysql_exporter的instance为一致

官方Dashboard资源

监控告警

  • 告警服务在172.50.13.102
  • 部署位置:/usr/local/prometheus/alertmanager/
  • 数据存储路径:/home/data/prometheus/alertmanager/
  • 监听端口:18081

配置alertmanager

./alertmanager --storage.path=/home/data/prometheus/alertmanager/ --web.listen-address=:18081 --config.file=/usr/local/prometheus/alertmanager/alertmanager.yml --data.retention=120h --web.external-url=http://172.50.13.102:18081 &

  • storage.path:数据存储路径
  • web.listen-address:监听端口
  • config.file:配置文件路径
  • data.retention:数据存储保留时长
  • web.external-url:web访问的url

配置prometheus告警规则

  1. 关联alertmanager:见prometheus.yml中的alerting
  2. 指定告警规则文件的路径:见prometheus.yml中的rule_files

配置钉钉告警机器人

/usr/local/prometheus/prometheus-webhook-dingtalk/prometheus-webhook-dingtalk --ding.profile=webhook1=https://oapi.dingtalk.com/robot/send?access_token=钉钉token &

附录代码

prometheus.yml

# my global config
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: ['172.50.13.102:18081'] # Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
- "alertrules/*_rules.yml" # A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus' # metrics_path defaults to '/metrics'
# scheme defaults to 'http'. static_configs:
- targets: ['localhost:19090'] - job_name: '非生产'
file_sd_configs:
- files: ['/usr/local/prometheus/sd_configs/noGroup*.yml']
refresh_interval: 10s - job_name: '生产mysql'
file_sd_configs:
- files: ['/usr/local/prometheus/sd_configs/mysql/product*.yml']
refresh_interval: 10s - job_name: '非生产mysql'
file_sd_configs:
- files: ['/usr/local/prometheus/sd_configs/mysql/noproduct*.yml']
refresh_interval: 10s - job_name: '生产服务器'
file_sd_configs:
- files: ['/usr/local/prometheus/sd_configs/product*.yml']
refresh_interval: 10s
- job_name: '物理机'
file_sd_configs:
- files: ['/usr/local/prometheus/sd_configs/wuli*.yml']
refresh_interval: 10s

alertmanager.yml

global:
resolve_timeout: 5m route:
group_by: [alertname]
group_wait: 10s
group_interval: 10s
repeat_interval: 1h
receiver: webhook
receivers:
- name: webhook
webhook_configs:
- url: 'http://172.50.13.102:8060/dingtalk/webhook1/send'
send_resolved: true

prometheus.service

[Unit]
Description=https://prometheus.io
Documentation=https://prometheus.io [Service]
Restart=on-failure
ExecStart=/usr/local/prometheus/prometheus/ \
--storage.tsdb.path=/home/prometheus/data/ \
--config.file=/usr/local/prometheus/prometheus.yml \
--web.listen-address=:19090 \
--storage.tsdb.retention=15d [Install]
WantedBy=multi-user.target

node_exporter.service

[Unit]
Description=https://prometheus.io [Service]
Restart=on-failure
ExecStart=/usr/local/prometheus/node_exporter/node_exporter --web.listen-address 0.0.0.0:18080 [Install]
WantedBy=multi-user.target

mysql_exporter.service

[Unit]
Description=https://prometheus.io [Service]
Restart=on-failure
ExecStart=/usr/local/prometheus/mysqld_exporter/mysqld_exporter \
--web.listen-address 0.0.0.0:9104 \
--config.my-cnf=/usr/local/prometheus/mysqld_exporter/.my.cnf [Install]
WantedBy=multi-user.target

alertmanager.service

告警规则-内存、硬盘、CPU

groups:
- name: mem
rules:
- alert : mem
expr: (1 - (node_memory_MemAvailable_bytes / (node_memory_MemTotal_bytes)))* 100 > 95
for: 5m
labels:
severity: critical
annotations:
description: "{{ $labels.job }} {{ $labels.instance }} 节点的内存使用率超过95%已持续5分钟!"
summary: "{{ $labels.instance }} 内存使用率超标! " - name: disk
rules:
- alert : disk
expr: (node_filesystem_size_bytes{fstype=~"ext.*|xfs",mountpoint !~".*pod.*"}-node_filesystem_free_bytes{fstype=~"ext.*|xfs",mountpoint !~".*pod.*"}) *100/(node_filesystem_avail_bytes {fstype=~"ext.*|xfs",mountpoint !~".*pod.*"}+(node_filesystem_size_bytes{fstype=~"ext.*|xfs",mountpoint !~".*pod.*"}-node_filesystem_free_bytes{fstype=~"ext.*|xfs",mountpoint !~".*pod.*"})) > 95
for: 5m
labels:
severity: warning
annotations:
description: "{{ $labels.job }} {{ $labels.instance }} 节点的硬盘使用率超过95%已持续5分钟!"
summary: "{{ $labels.instance }} 硬盘空间使用率已超过95%! " - name: cpu
rules:
- alert : cpu
expr: ((1- sum(increase(node_cpu_seconds_total{mode="idle"}[5m])) by (instance)/sum(increase(node_cpu_seconds_total[5m])) by (instance)) * 100) > 70
for: 5m
labels:
severity: warning
annotations:
description: "{{ $labels.job }} {{ $labels.instance }} 节点的CPU使用率超过70%已持续5分钟!"
summary: "{{ $labels.instance }} CPU使用率已超过70! "

告警规则-主机存活

groups:
- name: UP
rules:
- alert : node
expr: up == 0
for: 1m
labels:
severity: critical
annotations:
description: "{{ $labels.job }} {{ $labels.instance }} 节点断联已超过1分钟!"
summary: "{{ $labels.instance }} down "

基于Prometheus搭建监控平台的更多相关文章

  1. 实战 Prometheus 搭建监控系统

    实战 Prometheus 搭建监控系统 Prometheus 是一款基于时序数据库的开源监控告警系统,说起 Prometheus 则不得不提 SoundCloud,这是一个在线音乐分享的平台,类似于 ...

  2. 自动化运维:使用flask+mysql+highcharts搭建监控平台

    1.前言 本来想做一个比较完善的监控平台,只需要做少许改动就可以直接拿来用,但是在做的过程中发现要实现这个目标所需的工作量太大,而当前的工作中对其需求又不是特别明显.所以就退而求其次,做了一个类似教程 ...

  3. 记录一次基于docker搭建jira平台

    背景:项目需要选择不同测试平台,筛选一个最佳平台 现状:一台机器安装多套环境,虚拟机太繁琐 解决方案:通过Docker模拟安装测环境 1.pull docker 镜像: jira:7.11.1(目前的 ...

  4. SpringBoot整合SpringAdmin搭建监控平台

    在SpringBoot整合Actuator进行健康监控中,胜金讲述了通过Actuator进行健康监控,但是学习API并根据API开发前端需要花费相当大的精力,本次胜金就写一下通过SpringAdmin ...

  5. 基于Prometheus搭建SpringCloud全方位立体监控体系

    前提 最近公司在联合运维做一套全方位监控的系统,应用集群的技术栈是SpringCloud体系.虽然本人没有参与具体基础架构的研发,但是从应用引入的包和一些资料的查阅大致推算出具体的实现方案,这里做一次 ...

  6. 基于Prometheus和Grafana的监控平台 - 环境搭建

    相关概念 微服务中的监控分根据作用领域分为三大类,Logging,Tracing,Metrics. Logging - 用于记录离散的事件.例如,应用程序的调试信息或错误信息.它是我们诊断问题的依据. ...

  7. 理解OpenShift(7):基于 Prometheus 的集群监控

    理解OpenShift(1):网络之 Router 和 Route 理解OpenShift(2):网络之 DNS(域名服务) 理解OpenShift(3):网络之 SDN 理解OpenShift(4) ...

  8. inux监控平台搭建-监控项

    linux监控平台搭建-监控项 本人运维某生态互联网监控平台服务.过程中遇见了很多问题.目前互联网有很多的开源监控工具:nagios.zabbix.falcon.cacti...各有优点.这里不讲解监 ...

  9. JMeter+Grafana+Influxdb搭建可视化性能测试监控平台(使用了docker)

    [运行自定义镜像搭建监控平台] 继上一篇的帖子 ,上一篇已经展示了如何自定义docker镜像,大家操作就行 或者 用我已经自定义好了的镜像,直接pull就行 下面我简单介绍pull下来后如何使用 拉取 ...

  10. Prometheus + Grafana 监控系统搭

    本文主要介绍基于Prometheus + Grafana 监控Linux服务器. 一.Prometheus 概述(略) 与其他监控系统对比 1 Prometheus vs. Zabbix Zabbix ...

随机推荐

  1. 2020-12-21:redis中,rpop和brpop的区别?

    福哥答案2020-12-21:[答案来自此链接:](http://bbs.xiangxueketang.cn/question/806)Redis Rpop 命令用于移除列表的最后一个元素,返回值为移 ...

  2. 2022-03-16:给你一个整数 n ,表示有 n 个专家从 0 到 n - 1 编号。 另外给一个下标从 0 开始的二维整数数组 meetings , 其中 meetings[i] = [xi,

    2022-03-16:给你一个整数 n ,表示有 n 个专家从 0 到 n - 1 编号. 另外给一个下标从 0 开始的二维整数数组 meetings , 其中 meetings[i] = [xi, ...

  3. 2021-10-15:单词拆分。给定一个非空字符串 s 和一个包含非空单词的列表 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。说明:拆分时可以重复使用字典中的单词。你

    2021-10-15:单词拆分.给定一个非空字符串 s 和一个包含非空单词的列表 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词.说明:拆分时可以重复使用字典中的单词.你 ...

  4. Prompt learning 教学[技巧篇]:通过增加示例、引导词、特殊符号指令等方式让chatgpt输出更好的答案

    Prompt learning 教学[技巧篇]:通过增加示例.引导词.特殊符号指令等方式让chatgpt输出更好的答案 技巧1:To Do and Not To Do 在问答场景里,为了让 AI 回答 ...

  5. 补充:C语言枚举类型

    1.枚举类型 1.枚举数据类型是C语言中一种构造数据类型,可以让数据更加简洁,更易读,对于只有几个特定的数据,可以使用枚举类型 2.枚举对应英文enumeration,简写为enum 3.枚举是一组常 ...

  6. ENVI手动地理配准栅格图像的方法

      本文介绍在ENVI软件中,手动划定地面控制点从而实现栅格图像相互间地理配准的方法:其中,所用软件版本为ENVI Classic 5.3 (64-bit).   首先,在软件中同时打开两景需要进行地 ...

  7. Java 新的生态,Solon v2.3.2 发布

    Solon 是什么框架? 一个,Java 新的生态型应用开发框架.它从零开始构建,有自己的标准规范与开放生态(全球第二级别的生态).与其他框架相比,它解决了两个重要的痛点:启动慢,费资源. 解决痛点? ...

  8. 二维数组初始化vector, 以及类型转换问题

    //二维数组的初始化1 vector<vector<float>> _box_parm(class_row_num, vector<float>(class_col ...

  9. 关于linq Where中的”或者“运算只查询出来满足一种条件的数据的问题,本质是IEnumerable和IQuerable之间的区别

    如下代码所示,其中的"query"返回值类型为IQuerable var query = _deviceRepository.GetAll().AsNoTracking() .Wh ...

  10. 5. Mybatis获取参数值的两种方式

    ‍ MyBatis 获取参数值的两种方式:​${} 和 #{}​ ${}的本质就是字符串拼接,#{}的本质就是占位符赋值 ${}使用字符串拼接的方式拼接 sql,若为字符串类型或日期类型的字段进行赋值 ...