龙空技术网

Prometheus+Grafana+钉钉部署一个单机的MySQL监控告警系统

GreatSQL社区 685

前言:

如今我们对“curlforjava”大致比较关切,大家都想要学习一些“curlforjava”的相关内容。那么小编同时在网络上网罗了一些对于“curlforjava””的相关知识,希望你们能喜欢,同学们快快来了解一下吧!

一、Prometheus二、exporter2.1 node_exporter2.2 mysqld_exporter三、grafana3.1 部署3.2 配置数据源3.3 配置监控模板四、alertmanager4.1 配置alertmanager服务4.2 配置dingding告警4.3 配置 rule五、总结一、Prometheus

# 1.下载wget  2.解压tar xvpf prometheus-2.35.0.linux-amd64.tar.gz -C /usr/local# 3.建软链ln -s /usr/local/prometheus-2.35.0.linux-amd64 /usr/local/prometheus# 4.建用户和目录并开权限groupadd prometheususeradd prometheus -g prometheus -s /sbin/nologinmkdir -p /data/prometheuschown prometheus.prometheus /data/prometheus -Rchown prometheus.prometheus /usr/local/prometheus/ -R# 5.配置启动echo '[Unit]Description=prometheusAfter=network.target[Service]Type=simpleUser=prometheusExecStart=/usr/local/prometheus/prometheus \          --config.file=/usr/local/prometheus/prometheus.yml \          --storage.tsdb.path=/data/prometheus \          --web.console.templates=/usr/local/prometheus/consoles \          --web.console.libraries=/usr/local/prometheus/console_librariesExecReload=/bin/kill -HUP $MAINPIDRestart=on-failureRestartSec=60s[Install]WantedBy=multi-user.target' > /usr/lib/systemd/system/prometheus.service# 6.添加prometheus.yml配置echo 'global:  scrape_interval: 15s  scrape_timeout: 10s  evaluation_interval: 15salerting:  alertmanagers:  - static_configs:    - targets:      - localhost:9093    scheme: http    timeout: 10srule_files:- /usr/local/prometheus/rules.d/*.rulesscrape_configs:- job_name: prometheus  honor_timestamps: true  scrape_interval: 5s  scrape_timeout: 5s  metrics_path: /metrics  scheme: http  static_configs:  - targets:    - localhost:9090- job_name: node-exporter  honor_timestamps: true  scrape_interval: 5s  scrape_timeout: 5s  metrics_path: /metrics  scheme: http  static_configs:  - targets:    - localhost:9100- job_name: mysqld-exporter  honor_timestamps: true  scrape_interval: 5s  scrape_timeout: 5s  metrics_path: /metrics  scheme: http  static_configs:  - targets:    - localhost:9104' > /usr/local/prometheus/prometheus.yml# 7.启动systemctl enable prometheus.servicesystemctl start prometheus.service# 8.确认开启[root@mgr2 prometheus]# netstat -nltp|grep prometheustcp6       0      0 :::9090                 :::*                    LISTEN      11028/prometheus

9.浏览器访问出现prometheus的管理后台

以上单机版的prometheus服务端就部署完成了,接下来我们部署下node_exportmysqld_export 来采集系统和 MySQL 的监控数据。

二、exporter

exporter 是客户端采集模块,除了系统模块 node_exporter 之外,每个应用都有自己相应的模块,比如 MySQL 的 mysqld_exporter

建立一个 exporter 统一管理目录

mkdir -p /usr/local/prometheus_exporterchown prometheus.prometheus /usr/local/prometheus_exporter -R
2.1 node_exporter

用来监控系统指标的 exporter 包括内存、CPU、磁盘空间、磁盘IO、网络等一系列指标数据。

# 1.下载解压wget  xvpf node_exporter-0.18.0.linux-amd64.tar.gzcd node_exporter-0.18.0.linux-amd64/mv node_exporter /usr/local/prometheus_exporter/chown prometheus:prometheus /usr/local/prometheus_exporter/ -R# 2.配置启动服务echo '[Unit]Description=node_exporterAfter=network.target[Service]Type=simpleUser=prometheusExecStart=/usr/local/prometheus_exporter/node_exporterRestart=on-failureRestartSec=60s[Install]WantedBy=multi-user.target' > /usr/lib/systemd/system/node_exporter.service# 5.启动systemctl enable node_exporter.servicesystemctl start node_exporter.service# 6.确认开启[root@mgr2 node_exporter]# netstat -nltp|grep node_exportertcp6       0      0 :::9100                 :::*                    LISTEN      15654/node_exporter# 7.确认采集到数据[root@mgr2 prometheus]# curl  TYPE node_cpu_seconds_total counternode_cpu_seconds_total{cpu="0",mode="idle"} 273849.94node_cpu_seconds_total{cpu="0",mode="iowait"} 607.22node_cpu_seconds_total{cpu="0",mode="irq"} 0node_cpu_seconds_total{cpu="0",mode="nice"} 84.82node_cpu_seconds_total{cpu="0",mode="softirq"} 3.35node_cpu_seconds_total{cpu="0",mode="steal"} 0node_cpu_seconds_total{cpu="0",mode="system"} 5026.1node_cpu_seconds_total{cpu="0",mode="user"} 3723.54# HELP node_disk_io_now The number of I/Os currently in progress.# TYPE node_disk_io_now gaugenode_disk_io_now{device="dm-0"} 0node_disk_io_now{device="dm-1"} 0
2.2 mysqld_exporter

监控 MySQL 的 exporter ,包括连接数、同步状态,InnoDB状态、响应状态等。

# 下载解压wget  xvpf mysqld_exporter-0.11.0.linux-amd64.tar.gzcd mysqld_exporter-0.11.0.linux-amd64mv mysqld_exporter /usr/local/prometheus_exporter/chown prometheus:prometheus /usr/local/prometheus_exporter/ -R# 3.创建监控用的账户权限,数据库是8.0版本CREATE USER 'mysqlmonitor'@'127.0.0.1' IDENTIFIED BY 'mc.2022' WITH MAX_USER_CONNECTIONS 3;GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysqlmonitor'@'127.0.0.1';ALTER USER 'mysqlmonitor'@'127.0.0.1' IDENTIFIED WITH mysql_native_password BY 'mc.2022';flush privileges;# 4.配置启动服务vi /usr/lib/systemd/system/mysqld_exporter.service[Unit]Description=mysqld_exporterAfter=network.target[Service]Type=simpleUser=prometheusEnvironment='DATA_SOURCE_NAME=mysqlmonitor:mc.2022@tcp(127.0.0.1:3306)'ExecStart=/usr/local/prometheus_exporter/mysqld_exporter \          --config.my-cnf='/data/GreatSQL/my.cnf' \          --collect.engine_innodb_status \          --collect.slave_status \          --web.listen-address=:9104 \          --web.telemetry-path=/metricsRestart=on-failureRestartSec=60s[Install]WantedBy=multi-user.target# 5.启动systemctl enable mysqld_exporter.servicesystemctl start mysqld_exporter.service# 6.确认开启[root@mgr2 prometheus]# netstat -nltp|grep mysqld_exporttcp6       0      0 :::9104                 :::*                    LISTEN      14639/mysqld_export# 7.确认采集到数据[root@mgr2 prometheus]# curl  TYPE mysql_up gaugemysql_up 1......
三、grafana

通过 grafana 我们可以将采集到的数据通过可视化的方式展现出来,对采集的数据进行展示和分类等。

grafana 的数据源既可以是 prometheus 也可以是zabbix、ES等、这是一个提供多种数据接口的数据展示软件。

3.1 部署

# 1.通过rpm安装wget  install grafana-enterprise-8.5.3-1.x86_64.rpm# 2.备份先原来的配置mv /etc/grafana/grafana.ini /etc/grafana/grafana.ini.`date +"%Y-%m-%d"`.bak# 3.创建目录mkdir -p /data/grafanamkdir -p /data/logs/grafanamkdir -p /usr/local/grafana/pluginschown grafana.grafana /data/grafanachown grafana.grafana /data/logs/grafanachown grafana.grafana /usr/local/grafana/plugins # 4.拷贝模板文件进行替换echo 'app_mode = production[paths]data = /data/grafanatemp_data_lifetime = 24hlogs = /data/logs/grafanaplugins = /usr/local/grafana/plugins[server]protocol = httphttp_port = 3000domain = gkhtroot_url =  = true[database]log_queries =[remote_cache][session]provider = file[dataproxy][analytics]reporting_enabled = falsecheck_for_updates = false[security]admin_user = adminadmin_password = adminsecret_key = SW2YcwTIb9zpOOhoPsMm[snapshots][dashboards]versions_to_keep = 10[users]default_theme = dark[auth][auth.anonymous]enabled = trueorg_role = Viewer[auth.github][auth.google][auth.generic_oauth][auth.grafana_com][auth.proxy][auth.basic][auth.ldap][smtp][emails][log]mode = console filelevel = info[log.console][log.file]log_rotate = truedaily_rotate = truemax_days = 30[log.syslog][alerting]enabled = trueexecute_alerts = true[explore][metrics]enabled = trueinterval_seconds = 10[metrics.graphite][tracing.jaeger][grafana_com]url = [external_image_storage][external_image_storage.s3][external_image_storage.webdav][external_image_storage.gcs][external_image_storage.azure_blob][external_image_storage.local][rendering][enterprise][panels]' > /etc/grafana/grafana.inichown grafana.grafana /etc/grafana/grafana.ini# 5.开启systemctl enable grafana-server.servicesystemctl start grafana-server.service# 6.查看开启状态[root@mgr2 opt]# netstat -nltp|grep grafanatcp6       0      0 :::3000                 :::*                    LISTEN      23647/grafana-serve

7.浏览器访问

账户密码都是 admin 登陆后先改下管理员密码,这里演示就跳过,下面是主界面

3.2 配置数据源

1.设置,Data sources

2.Add data source

3.输入Prometheus

4.添加数据源信息

5.测试连接

6.返回数据源

3.3 配置监控模板

模板库:

node_exporter面板

选择数据源 Prometheus ,输入 exporter,选择活跃度高的。

点击面板后,查看右侧id值

1.选择导入 Import

2.输入id值,然后load

3.输入名称,选择数据源,点击导入

4.查看监控数据

5.保存面板

mysqld_exporter面板

还是像之前步骤一样,搜索 mysql 关键字,找到面板,拷贝id 7362,然后导入报存。

查看面板数据

设置,选择 Browse

可以看到添加的2个模板

点击后可以看到完整数据

以上部署后已经有了展示模块,我们配置下告警模块

四、alertmanager

alertmanager是普米的告警模块,可配置各种告警规则并将告警内容发送到微信、钉钉、邮箱等。

4.1 配置alertmanager服务

# 1.下载wget  # 2.解压并拷贝文件tar xvpf alertmanager-0.17.0.linux-amd64.tar.gzln -s /usr/local/alertmanager-0.17.0.linux-amd64 /usr/local/alertmanager# 3.创建数据目录并赋权mkdir -p /data/alertmanagerchown prometheus:prometheus /data/alertmanager -Rchown prometheus:prometheus /usr/local/alertmanager -R# 4.配置启动脚本echo '[Unit]Description=alertmanagerAfter=network.target[Service]Type=simpleUser=prometheusExecStart=/usr/local/alertmanager/alertmanager \          --config.file=/usr/local/alertmanager/alertmanager.yml \          --storage.path=/data/alertmanager \          --data.retention=120hRestart=on-failureRestartSec=60s[Install]WantedBy=multi-user.target' > /usr/lib/systemd/system/alertmanager.service# 5.启动systemctl enable alertmanager.servicesystemctl start alertmanager.service# 6.查看开启情况[root@mgr2 alertmanager]# netstat -nltp|grep alertmanagertcp6       0      0 :::9093                 :::*                    LISTEN      30369/alertmanagertcp6       0      0 :::9094                 :::*                    LISTEN      30369/alertmanager
4.2 配置dingding告警4.2.1 创建dingding告警机器人

1.钉钉创建一个群组,取名 告警

2.点击右上角的 设置

3.点击 智能群助手

4.添加机器人

5.点击设置

6.选择自定义

7.点击 添加

8.设置下 机器人安全设置 ,点击完成

9.最后确认信息,点击完成

10.设置后,群消息会弹出欢迎消息

4.2.2 安装钉钉告警插件

# 1.下载wget  2.解压tar xvpf prometheus-webhook-dingtalk-2.0.0.linux-amd64.tar.gzmv prometheus-webhook-dingtalk-2.0.0.linux-amd64 /usr/local/ln -s /usr/local/prometheus-webhook-dingtalk-2.0.0.linux-amd64 /usr/local/prometheus-webhook-dingtalk# 3.配置config.yml# 拷贝个模板文件 # url 和 secret 是我们创建告警机器人的时候出现的 webook 和安全设置的"加签"cp config.example.yml config.yml[root@mgr2 prometheus-webhook-dingtalk]# cat config.ymltemplates:  - contrib/templates/legacy/template.tmpltargets:  webhook1:    url:     secret: SEC000000000000000000000# 4.配置启动服务echo '[Unit]Description=prometheus-webhook-dingtalkAfter=network.target[Service]Type=simpleUser=prometheusExecStart=/usr/local/prometheus-webhook-dingtalk/prometheus-webhook-dingtalk  \          --config.file='/usr/local/prometheus-webhook-dingtalk/config.yml'Restart=on-failureRestartSec=60s[Install]WantedBy=multi-user.target' > /usr/lib/systemd/system/prometheus-webhook-dingtalk.service# 5.赋权chown prometheus.prometheus /usr/local/prometheus-webhook-dingtalk -R# 6.启动systemctl enable prometheus-webhook-dingtalk.servicesystemctl start prometheus-webhook-dingtalk.service# 7.确认开启了[root@mgr2 prometheus-webhook-dingtalk]# ps -aux|grep prometheus-webhook-dingtalkprometh+ 23162  0.0  0.3 716116  5768 ?        Ssl  15:23   0:00 /usr/local/prometheus-webhook-dingtalk/prometheus-webhook-dingtalk --config.file=/usr/local/prometheus-webhook-dingtalk/config.yml              :::*                    LISTEN      15654/node_exporter

以上,钉钉告警模块就完成了

4.3 配置 rule

prometheus.yml 有个rule_files 模块会加载我们的自定义配置信息

# 1.创建目录mkdir -p /usr/local/prometheus/rules.d/# 2.配置告警规则信息[root@mgr2 rules.d]# cat test.rulesgroups:  - name: OsStatsAlert    rules:    - alert: Out of Disk Space      expr: ( 1 - (node_filesystem_avail_bytes{fstype=~"ext[34]|xfs"} / node_filesystem_size_bytes{fstype=~"ext[234]|btrfs|xfs|zfs"}) ) * 100 > 15      for: 1m      labels:        team: node      annotations:        summary: "{{$labels.instance}}: 文件系统空间使用率过高"        description: "{{$labels.instance}}: 文件系统空间使用率超过 15% (当前使用率: {{ $value }})"  - name: MySQLStatsAlert    rules:    - alert: MySQL is down      expr: mysql_up == 0      for: 1m      labels:        severity: critical      annotations:        summary: "Instance {{ $labels.instance }} MySQL is down"        description: "MySQL database is down."# 3.重启systemctl restart prometheussystemctl restart alertmanager

4.关闭MySQL进程,观察告警信息

systemctl stop greatsql@mgr3306.service

5.提示告警信息

6.异常恢复后也会进行告警通知

五、总结

以上基于Prometheus+Grafana+钉钉简单部署了一个告警系统,可以结合实际情况自行进行扩展,在生产上Prometheus一般采用集群方式,防止单点故障,同时也可与consul结合做服务自动发现,减少手动配置环节。

标签: #curlforjava