前言

准备软件包:

  • 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. docker常用命令之帮助启动类命令和镜像命令

    docker安装之后,启动时会报如下错误: Job for docker.service failed because start of the service was attempted too o ...

  2. Typora使用方法

    Typora使用方法 常见快捷键 无序列表:- + 空格 有序列表:1. + 空格 引用:> + 空格 标题:ctrl + 数字 表格:ctrl + t 选中一整行:ctrl + l 选中单词: ...

  3. Vue 路由router

    简单案例: App.vue是核心组件,其中的<router-link>相当于a标签,to相当于href,export是暴露函数,这样某组件才能被其他组件识别到 代码: <templa ...

  4. SpringMVC 解决中文乱码问题以及前后端Json格式数据交互的测试

    1.今日遇到的报错: 跳转网页出现404原因: 1.检查project structure里面的webapp路径是否正确: 2.检查project structure里的artifaccts里的WEB ...

  5. 封装vue基于element的select多选时启用鼠标悬停折叠文字以tooltip显示具体所选值

    相信很多公司的前端开发人员都会选择使用vue+element-ui的形式来开发公司的管理后台系统,基于element-ui很丰富的组件生态,我们可以很快速的开发管理后台系统的页面(管理后台系统的页面也 ...

  6. C++面试八股文:C++中指针、引用、解引用和取地址有什么不同?

    某日小二参加XXX科技公司的C++工程师开发岗位2面: 面试官:指针.引用.解引用和取地址是C++中的重要概念,请问它们在语法和语义上有所不同? 小二:指针是一个变量,它存储了一个内存地址. 小二:引 ...

  7. .NET的基元类型包括哪些?Unmanaged和Blittable类型又是什么?

    在讨论.NET的类型系统的时候,我们经常提到"基元类型(Primitive Type)"的概念,我发现很多人并没有真正理解基元类型就究竟包含哪些(比如很多人觉得字符串是基元类型). ...

  8. 【LeetCode】Find Pivot Index #724 Rust Solution

    给定一个整数类型的数组 nums,请编写一个能够返回数组 "中心索引" 的方法.我们是这样定义数组 中心索引 的:数组中心索引的左侧所有元素相加的和等于右侧所有元素相加的和.如果数 ...

  9. 常用的Java Enum JdbcType

    常用的Java Enum JdbcType ARRAY BIGINT BINARY BIT BLOB BOOLEAN CHAR CLOB CURSOR DATE DECIMAL DOUBLE FLOA ...

  10. .NET周报 【6月第4期 2023-06-25】

    国内文章 如何在long-running task中调用async方法 https://www.cnblogs.com/eventhorizon/p/17497359.html long-runnin ...