赞
踩
前提环境:已经安装和配置好prometheus server
所有组件对应的版本:
prometheus-2.44.0
mysqld_exporter-0.14.0
grafana-enterprise-9.1.2-1.x86_64.rpm
alertmanager-0.25.0
prometheus-webhook-dingtalk-2.1.0
mysql_exporter是用来收集MysQL或者Mariadb数据库相关指标的,mysql_exporter需要连接到数据库并有相关权限。既可以用二进制安装部署,也可以通过容器形式部署,但为了数据收集的准确性,推荐二进制安装。
安装包下载地址: https://github.com/prometheus/mysqld_exporter/releases
同样物理机上下载,然后上传到服务器,本次安装使用的版本为:mysqld_exporter-0.14.0.linux-amd64
根据服务器情况选择安装目录,上传安装包。
[root@jd-mysql mysqld_exporter]# mkdir /mysqld_exporter
[root@jd-mysql mysqld_exporter]# cd /mysqld_exporter/
[root@jd-mysql mysqld_exporter]# ls
mysqld_exporter-0.14.0.linux-amd64.tar.gz
[root@jd-mysql mysqld_exporter]# tar -xvzf mysqld_exporter-0.14.0.linux-amd64.tar.gz
[root@jd-mysql mysqld_exporter]# cp mysqld_exporter-0.14.0.linux-amd64/* .
[root@jd-mysql mysqld_exporter]# rm -rf mysqld_exporter-0.14.0.linux-amd64
[root@jd-mysql mysqld_exporter]# ls
LICENSE mysqld_exporter mysqld_exporter-0.14.0.linux-amd64.tar.gz NOTICE
.my.cnf
,填入如下内容后保存[root@jd-mysql mysqld_exporter]# cat .my.cnf
[client]
user = exporter
password = Jd123456
host = 192.168.1.150
port = 3306
[root@jd-mysql mysqld_exporter]# mysql -uroot -pJiangda123#
root@(none) 11:06 mysql>create user 'exporter'@'%' identified by 'Jd123456';
Query OK, 0 rows affected (1.02 sec)
root@(none) 11:07 mysql>GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'%' WITH MAX_USER_CONNECTIONS 3;
Query OK, 0 rows affected, 1 warning (0.00 sec)
root@(none) 11:07 mysql>flush privileges;
Query OK, 0 rows affected (0.00 sec)
root@(none) 11:07 mysql>exit
Bye
mysqld_exporter.service
配置文件vim /usr/lib/systemd/system/mysqld_exporter.service
:wq
[Unit]
Description=https://prometheus.io
[Service]
Restart=on-failure
ExecStart=/mysqld_exporter/mysqld_exporter --config.my-cnf=/mysqld_exporter/.my.cnf --web.listen-address=:9104
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start mysqld_exporter.service
systemctl status mysqld_exporter.service
systemctl enable mysqld_exporter.service
curl localhost:9104/metrics
prometheus.yml
添加以下内容,并执行:wq
,点击回车保存
- job_name: "mysql"
static_configs:
- targets: ["192.168.1.150:9104"]
labels:
instance: mysqld-exporter
systemctl restart prometheus
curl -XPOST localhost:9090/-/reload
打开prometheus,搜索mysql,可以看到出现了很多的参数指标
打开targets,可以看到节点状态已经UP
至此mysqld-exporter插件安装完毕
根据服务器情况选择安装目录,上传安装包。
[root@nfs yum.repos.d]# mkdir /grafana
[root@nfs yum.repos.d]# cd /grafana/
[root@nfs grafana]# ls
grafana-enterprise-9.1.2-1.x86_64.rpm
[root@nfs grafana]# yum install grafana-enterprise-9.1.2-1.x86_64.rpm -y
安装好后,
[root@nfs grafana]# service grafana-server start
Starting grafana-server (via systemctl): [ 确定 ]
[root@nfs grafana]# systemctl enable grafana-server
Created symlink from /etc/systemd/system/multi-user.target.wants/grafana-server.service to /usr/lib/systemd/system/grafana-server.service.
[root@nfs grafana]# netstat -anplut|grep grafana
tcp 0 0 192.168.1.130:56948 34.120.177.193:443 ESTABLISHED 8775/grafana-server
tcp 0 0 192.168.1.130:48842 185.199.110.133:443 ESTABLISHED 8775/grafana-server
tcp6 0 0 :::3000 :::* LISTEN 8775/grafana-server
访问http://192.168.1.130:3000/
默认的用户名和密码是
用户名admin
密码admin
配置数据源
成功效果:
1、服务端口规划:
服务 | 端口 |
---|---|
Prometheus | 9090 |
Node_exporter | 8090 |
Mysqld_exporter | 9194 |
Alertanager | 9093 |
Webhook-dingtalk | 8060 |
2.架构图:
3、钉钉创建机器人保存Webhook地址:
用电脑版钉钉,打开设置,点击机器人,再点击添加机器人,进来之后选择最后一个“自定义”,再添加,再输入相关信息,
总共需要保存两项,我们后续会用到:(注意每个机器人不是一样的,看自己的)
webhook地址
https://oapi.dingtalk.com/robot/send?access_token=4e70f2f1a85822394c15671281605fd22f4c0ee9fb80e122297d7b332704a03b
加签后的秘钥
SECc9dc9daf3dcd865b522587455eb076693368bdcbb443478d9542d640f896d2e8
4.顺便校准一下所有机器的时间(不清楚到底是不是影响结果的因素,顺便校准吧)
根据服务器情况选择安装目录,上传安装包。
[root@nfs prometheus]# mkdir /prom_alert [root@nfs prometheus]# cd /prom_alert/ [root@nfs prom_alert]# ls alertmanager-0.25.0.linux-amd64.tar.gz [root@nfs prom_alert]# tar xf alertmanager-0.25.0.linux-amd64.tar.gz [root@nfs prom_alert]# ls alertmanager-0.25.0.linux-amd64 alertmanager-0.25.0.linux-amd64.tar.gz [root@nfs prom_alert]# mv alertmanager-0.25.0.linux-amd64 alertmanager [root@nfs prom_alert]# cd alertmanager [root@nfs alertmanager]# ls alertmanager alertmanager.yml amtool LICENSE NOTICE [root@nfs alertmanager]# cp alertmanager.yml alertmanager.yml.bak [root@nfs alertmanager]# vim alertmanager.yml [root@nfs alertmanager]# pwd /prom_alert/alertmanager
使用systemd管理,启动 && 开机自启
[root@nfs system]# vim /usr/lib/systemd/system/alertmanager.service [Unit] Description=alertmanager Documentation=https://prometheus.io/ After=network.target StartLimitIntervalSec=0 [Service] Type=simple #User=prometheus ExecStart=/prom_alert/alertmanager/alertmanager --storage.path=/prom_alert/alertmanager/data --config.file=/prom_alert/alertmanager/alertmanager.yml --web.external-url=http://192.168.*.* Restart=always RestartSec=1 [Install] WantedBy=multi-user.target
[root@nfs system]# systemctl daemon-reload
[root@nfs system]# systemctl start alertmanager
[root@nfs system]# systemctl enable alertmanager
[root@nfs system]# systemctl status alertmanager
查看端口9093
[root@nfs system]# netstat -anplut|grep alert
tcp6 0 0 :::9093 :::* LISTEN 13175/alertmanager
tcp6 0 0 :::9094 :::* LISTEN 13175/alertmanager
udp6 0 0 :::9094 :::* 13175/alertmanager
验证 浏览器访问alertmanager管理页面
浏览器访问 http://IP:9093,显示下图表示无误~
根据服务器情况选择安装目录,上传安装包。
[root@nfs prom_alert]# ls
alertmanager prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz
alertmanager-0.25.0.linux-amd64.tar.gz
[root@nfs prom_alert]# tar xf prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz
[root@nfs prom_alert]# mv prometheus-webhook-dingtalk-2.1.0.linux-amd64 prometheus-webhook-dingtalk
[root@nfs prom_alert]# cd prometheus-webhook-dingtalk
[root@nfs webhook-dingtalk]# ls
config.example.yml contrib LICENSE prometheus-webhook-dingtalk
[root@nfs webhook-dingtalk]# cp config.example.yml config.yml
[root@nfs alertmanager]# pwd
/prom_alert/alertmanager
使用systemd管理,启动 && 开机自启
[root@nfs prometheus-webhook-dingtalk]# vim /usr/lib/systemd/system/dingtalk.service
[Unit]
Description=dingtalk
Documentation=https://github.com/timonwong/prometheus-webhook-dingtalk/
After=network.target
[Service]
Restart=on-failure
WorkingDirectory=/prom_alert/prometheus-webhook-dingtalk
ExecStart=/prom_alert/prometheus-webhook-dingtalk/prometheus-webhook-dingtalk --config.file=/prom_alert/prometheus-webhook-dingtalk/config.yml
[Install]
WantedBy=multi-user.target
[root@nfs prometheus-webhook-dingtalk]# systemctl daemon-reload
[root@nfs prometheus-webhook-dingtalk]# systemctl start dingtalk
[root@nfs prometheus-webhook-dingtalk]# systemctl enable dingtalk
[root@nfs prometheus-webhook-dingtalk]# systemctl status dingtalk
查看端口8060
[root@nfs prometheus-webhook-dingtalk]# netstat -anplut|grep 8060
tcp6 0 0 :::8060 :::* LISTEN 14291/prometheus-we
1、 只改以下三处即可,如下图:
加签秘钥、webhook地址是咱们在钉钉创建机器人时获取的!
vim /prom_alert/prometheus-webhook-dingtalk/config.yml
2、添加钉钉报警模板(注意复制完整,别少这少那,多这多那!因为我就没注意犯了错,浪费了一些时间!)
[root@nfs prometheus-webhook-dingtalk]# vim template.tmpl {{ define "__subject" }} [{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ end }} {{ define "__alert_list" }}{{ range . }} --- {{ if .Labels.owner }}@{{ .Labels.owner }}{{ end }} **告警主题**: {{ .Annotations.summary }} **告警类型**: {{ .Labels.alertname }} **告警级别**: {{ .Labels.severity }} **告警主机**: {{ .Labels.instance }} **告警信息**: {{ index .Annotations "description" }} **告警时间**: {{ dateInZone "2006.01.02 15:04:05" (.StartsAt) "Asia/Shanghai" }} {{ end }}{{ end }} {{ define "__resolved_list" }}{{ range . }} --- {{ if .Labels.owner }}@{{ .Labels.owner }}{{ end }} **告警主题**: {{ .Annotations.summary }} **告警类型**: {{ .Labels.alertname }} **告警级别**: {{ .Labels.severity }} **告警主机**: {{ .Labels.instance }} **告警信息**: {{ index .Annotations "description" }} **告警时间**: {{ dateInZone "2006.01.02 15:04:05" (.StartsAt) "Asia/Shanghai" }} **恢复时间**: {{ dateInZone "2006.01.02 15:04:05" (.EndsAt) "Asia/Shanghai" }} {{ end }}{{ end }} {{ define "default.title" }} {{ template "__subject" . }} {{ end }} {{ define "default.content" }} {{ if gt (len .Alerts.Firing) 0 }} **====侦测到{{ .Alerts.Firing | len }}个故障====** {{ template "__alert_list" .Alerts.Firing }} --- {{ end }} {{ if gt (len .Alerts.Resolved) 0 }} **====恢复{{ .Alerts.Resolved | len }}个故障====** {{ template "__resolved_list" .Alerts.Resolved }} {{ end }} {{ end }} {{ define "ding.link.title" }}{{ template "default.title" . }}{{ end }} {{ define "ding.link.content" }}{{ template "default.content" . }}{{ end }} {{ template "default.title" . }} {{ template "default.content" . }}
3、刷新服务
systemctl restart dingtalk
systemctl status dingtalk
1、修改配置
[root@nfs prometheus-webhook-dingtalk]# vim /prom_alert/alertmanager/alertmanager.yml route: group_by: ['dingding'] group_wait: 30s group_interval: 5m repeat_interval: 1h receiver: 'dingding.webhook1' routes: - receiver: 'dingding.webhook1' match_re: alertname: ".*" receivers: - name: 'dingding.webhook1' webhook_configs: - url: 'http://localhost:8060/dingtalk/webhook1/send' send_resolved: true inhibit_rules: - source_match: severity: 'critical' target_match: severity: 'warning' equal: ['alertname', 'dev', 'instance']
2、重启
systemctl restart alertmanager
systemctl status alertmanager
1、修改prometheus配置
[root@nfs alertmanager]# vim /prom/prometheus/prometheus.yml
2、添加node_exporter告警规则
[root@nfs prom_alert]# mkdir rule [root@nfs prom_alert]# cd rule/ [root@nfs rule]# vim node_exporter.yml groups: - name: 服务器资源监控 rules: - alert: 内存使用率过高 expr: 100 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100 > 80 for: 3m labels: severity: 严重告警 annotations: summary: "{{ $labels.instance }} 内存使用率过高, 请尽快处理!" description: "{{ $labels.instance }}内存使用率超过80%,当前使用率{{ $value }}%." - alert: 服务器宕机 expr: up == 0 for: 1s labels: severity: 严重告警 annotations: summary: "{{$labels.instance}} 服务器宕机, 请尽快处理!" description: "{{$labels.instance}} 服务器延时超过3分钟,当前状态{{ $value }}. " - alert: CPU高负荷 expr: 100 - (avg by (instance,job)(irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 90 for: 5m labels: severity: 严重告警 annotations: summary: "{{$labels.instance}} CPU使用率过高,请尽快处理!" description: "{{$labels.instance}} CPU使用大于90%,当前使用率{{ $value }}%. " - alert: 磁盘IO性能 expr: avg(irate(node_disk_io_time_seconds_total[1m])) by(instance,job)* 100 > 90 for: 5m labels: severity: 严重告警 annotations: summary: "{{$labels.instance}} 流入磁盘IO使用率过高,请尽快处理!" description: "{{$labels.instance}} 流入磁盘IO大于90%,当前使用率{{ $value }}%." - alert: 网络流入 expr: ((sum(rate (node_network_receive_bytes_total{device!~'tap.*|veth.*|br.*|docker.*|virbr*|lo*'}[5m])) by (instance,job)) / 100) > 102400 for: 5m labels: severity: 严重告警 annotations: summary: "{{$labels.instance}} 流入网络带宽过高,请尽快处理!" description: "{{$labels.instance}} 流入网络带宽持续5分钟高于100M. RX带宽使用量{{$value}}." - alert: 网络流出 expr: ((sum(rate (node_network_transmit_bytes_total{device!~'tap.*|veth.*|br.*|docker.*|virbr*|lo*'}[5m])) by (instance,job)) / 100) > 102400 for: 5m labels: severity: 严重告警 annotations: summary: "{{$labels.instance}} 流出网络带宽过高,请尽快处理!" description: "{{$labels.instance}} 流出网络带宽持续5分钟高于100M. RX带宽使用量{$value}}." - alert: TCP连接数 expr: node_netstat_Tcp_CurrEstab > 10000 for: 2m labels: severity: 严重告警 annotations: summary: " TCP_ESTABLISHED过高!" description: "{{$labels.instance}} TCP_ESTABLISHED大于100%,当前使用率{{ $value }}%." - alert: 磁盘容量 expr: 100-(node_filesystem_free_bytes{fstype=~"ext4|xfs"}/node_filesystem_size_bytes {fstype=~"ext4|xfs"}*100) > 90 for: 1m labels: severity: 严重告警 annotations: summary: "{{$labels.mountpoint}} 磁盘分区使用率过高,请尽快处理!" description: "{{$labels.instance}} 磁盘分区使用大于90%,当前使用率{{ $value }}%."
3、检查Prometheus的配置文件并重启
检查配置文件很有必要,减少一些不必要的麻烦:promtool check config prometheus.yml
[root@nfs prom_alert]# cd /prom/prometheus
[root@nfs prom_alert]# ./promtool check config prometheus.yml
Checking prometheus.yml
SUCCESS: 1 rule files found
SUCCESS: prometheus.yml is valid prometheus config file syntax
Checking /prom_alert/rule/node_exporter.yml
SUCCESS: 8 rules found
[root@nfs prom_alert]# systemctl restart prometheus
[root@nfs prom_alert]# systemctl status prometheus
4、访问Prometheus Web页面可以查看到添加的规则,如下图:
1、故意将exporter机器关机,模拟服务器宕机
过了一会儿
2、Prometheus 管理页面可以看到告警信息如下图:
3、Prometheus会将告警信息发送给Alertmanager,所以说Alertmanager页面可以看到告警信息如下图:
4、此时会发送到钉钉机器人告警,如下图所示:
多回顾,捋清楚那些复制粘贴的内容都是为什么这么做的
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。