設(shè)計(jì)好看的美食網(wǎng)站有哪些百度廣告投放收費(fèi)標(biāo)準(zhǔn)
💫《博主主頁》:
? ? ? ? ? ?🔎?CSDN主頁__奈斯DB
? ? ? ? ? ?🔎?IF Club社區(qū)主頁__奈斯、
🔥《擅長領(lǐng)域》:擅長阿里云AnalyticDB for MySQL(分布式數(shù)據(jù)倉庫)、Oracle、MySQL、Linux、prometheus監(jiān)控;并對(duì)SQLserver、NoSQL(MongoDB)有了解
💖如果覺得文章對(duì)你有所幫助,歡迎點(diǎn)贊收藏加關(guān)注💖
?
? ? 如期而至,今天繼續(xù)給大家分享監(jiān)控系列的文章——?使用Prometheus+Grafana打造專業(yè)的postgreSQL監(jiān)控系統(tǒng) 。Prometheus抓指標(biāo)?🔍?+ Grafana畫大屏?📈?+ Alertmanager拍電報(bào)?📢,跟著三步走,打造PostgreSQL監(jiān)控天網(wǎng)🚀。
? ? ? ? ? ? ? ? ?
特別說明💥:
📌 開源儀表盤引用
- 本文采用的postgreSQL數(shù)據(jù)庫監(jiān)控儀表盤直接使用了Grafana官網(wǎng)開源項(xiàng)目(Dashboard ID: 9628),非常感謝原作者(Lucas Estienne)的無私分享。關(guān)于Grafana的所有配置步驟均基于該開源儀表盤并驗(yàn)證通過,各位可一鍵導(dǎo)入快速搭建專業(yè)級(jí)的可視化監(jiān)控。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? 原創(chuàng)告警規(guī)則實(shí)現(xiàn)
關(guān)于告警部分為博主獨(dú)立開發(fā)完成,針對(duì)postgreSQL數(shù)據(jù)庫實(shí)現(xiàn)了如下告警:
? postgreSQL 實(shí)例不可用
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
🔗?Prometheus+Grafana監(jiān)控postgreSQL全流程
? ? ?
? ? ? ?
prometheus+Grafana全系列文章(實(shí)時(shí)更新🔥):
序號(hào) 文章 1 【prometheus+Grafana篇】Prometheus與Grafana:深入了解監(jiān)控架構(gòu)與數(shù)據(jù)可視化分析平臺(tái)-CSDN博客 2 【prometheus+Grafana篇】從零開始:Linux 7.6 上二進(jìn)制安裝 Prometheus、Grafana 和 Node Exporter_grafana二進(jìn)制部署-CSDN博客 3 【prometheus+Grafana篇】Prometheus告警規(guī)則參數(shù)全解析 + Alertmanager實(shí)現(xiàn)多平臺(tái)告警(含電子郵件/企業(yè)微信群/飛書群/釘釘群接受方式)_grafana做飛書機(jī)器人報(bào)警-CSDN博客 4 【prometheus+Grafana篇】基于Prometheus+Grafana實(shí)現(xiàn)Linux操作系統(tǒng)的監(jiān)控與可視化-CSDN博客 5 【prometheus+Grafana篇】基于Prometheus+Grafana實(shí)現(xiàn)windows操作系統(tǒng)的監(jiān)控與可視化_grafana 監(jiān)控 windows-CSDN博客 6 【prometheus+Grafana篇】基于Prometheus+Grafana實(shí)現(xiàn)Oracle數(shù)據(jù)庫的監(jiān)控與可視化-CSDN博客 7 【prometheus+Grafana篇】基于Prometheus+Grafana實(shí)現(xiàn)MySQL數(shù)據(jù)庫的監(jiān)控與可視化-CSDN博客 8 【prometheus+Grafana篇】基于Prometheus+Grafana實(shí)現(xiàn)MongoDB數(shù)據(jù)庫的監(jiān)控與可視化-CSDN博客 9 【prometheus+Grafana篇】基于Prometheus+Grafana實(shí)現(xiàn)Redis數(shù)據(jù)庫的監(jiān)控與可視化-CSDN博客
? ? ? ? ? ? ? ? ? ? ? ?
目錄
一、在postgresql 5432實(shí)例上的操作
二、安裝postgres_exporter(postgres_exporter:是prometheus的一個(gè)插件,抓取和收集postgresql數(shù)據(jù)庫的各種運(yùn)行指標(biāo)用于分析。postgres_exporter插件是GitHub開源組織提供的,通過GitHub官網(wǎng)下載postgres_exporter)
1)下載postgres_exporter安裝包,選擇download,然后點(diǎn)擊“Exporters and integrations”,選擇postgres_exporter
2)跳轉(zhuǎn)到了gitHub,點(diǎn)擊postgres_exporter的版本集,選擇最新的postgres_exporter下載
3)目前postgres_exporter最新版本為0.17.1。
4)解壓二進(jìn)制postgres_exporter包
5)移動(dòng)并重命名postgres_exporter解壓出來的目錄
6)創(chuàng)建prometheus用戶
7)賦權(quán)
8)寫入linux啟動(dòng)服務(wù)項(xiàng)
9)登錄postgres_exporter界面管理,默認(rèn)端口為9187
三、將當(dāng)前postgresql 5432實(shí)例加入到prometheus監(jiān)控,并通過Grafana展示
1)將postgres_exporter的信息加入到prometheus監(jiān)控的配置文件prometheus.yml中
2)配置告警規(guī)則文件
3)檢查配置文件
4)prometheus.yml文件添加了信息,所以重啟prometheus進(jìn)程或者重新加載配置文件(二選一)
5)在prometheus查看是否可以看到監(jiān)控信息
6)登錄Grafana界面管理,將當(dāng)前主機(jī)的監(jiān)控信息做展示
一、下載一個(gè)儀表盤:Grafana官網(wǎng)“http://www.grafana.com”—Dashboards,然后選擇一個(gè)下載量高的自己喜歡的postgres_exporter(postgresql監(jiān)控)
二、將儀表盤添加到Grafana上:Dashboards—New—Import—點(diǎn)擊“Upload dashboard JSON file”
三、儀表盤展示:Dashboards—查看到已經(jīng)定義好的儀表盤,并且數(shù)據(jù)源是prometheus監(jiān)控,點(diǎn)擊進(jìn)去
? ? ? ? ? ? ? ? ? ? ? ??
監(jiān)控postgreSQL信息如下:
主機(jī)名 | IP地址 | 配置 | 系統(tǒng) | 描述 |
postgresql | 10.2.4.98 | 4C 8G | Red Hat Linux 7.6 | 安裝postgres_exporter對(duì)這臺(tái)服務(wù)器進(jìn)行監(jiān)控 |
postgres_exporter:默認(rèn)端口9187。抓取和收集postgresql數(shù)據(jù)庫的各種運(yùn)行指標(biāo)用于分析
? ? ? ? ? ? ? ? ??
一、在postgresql 5432實(shí)例上的操作
1)創(chuàng)建專屬監(jiān)控用戶(不建議使用postgres用戶,postgres用戶權(quán)限太高)
SQL> CREATE USER postgres_exporter WITH PASSWORD 'nX5sA4iC';-- 授予連接權(quán)限 GRANT CONNECT ON DATABASE postgres TO postgres_exporter;-- 授予訪問必要視圖的權(quán)限 GRANT USAGE ON SCHEMA public TO postgres_exporter; GRANT SELECT ON pg_stat_activity TO postgres_exporter; GRANT SELECT ON pg_stat_database TO postgres_exporter; GRANT SELECT ON pg_stat_user_tables TO postgres_exporter; GRANT SELECT ON pg_stat_user_indexes TO postgres_exporter; GRANT SELECT ON pg_statio_user_tables TO postgres_exporter; GRANT SELECT ON pg_statio_user_indexes TO postgres_exporter; GRANT SELECT ON pg_stat_bgwriter TO postgres_exporter; GRANT SELECT ON pg_stat_wal_receiver TO postgres_exporter;
? ? ? ??
二、安裝postgres_exporter(postgres_exporter:是prometheus的一個(gè)插件,抓取和收集postgresql數(shù)據(jù)庫的各種運(yùn)行指標(biāo)用于分析。postgres_exporter插件是GitHub開源組織提供的,通過GitHub官網(wǎng)下載postgres_exporter)
1)下載postgres_exporter安裝包,選擇download,然后點(diǎn)擊“Exporters and integrations”,選擇postgres_exporter
?
?
?
? ? ? ? ?
2)跳轉(zhuǎn)到了gitHub,點(diǎn)擊postgres_exporter的版本集,選擇最新的postgres_exporter下載
? ? ? ? ? ? ? ? ? ??
PS:也可以直接在gitHub官網(wǎng)下載
一、通過gitHub(官網(wǎng)地址:https://github.com/)下載postgres_exporter安裝包,在gitHub搜索“postgres_exporter”。因?yàn)槭情_源,所以大家都是有時(shí)間才會(huì)開發(fā),所以選擇一個(gè)最近更新的
? ? ? ??
二、點(diǎn)擊postgres_exporter的版本集,選擇最新的postgres_exporter下載
?? ? ? ? ? ? ? ? ? ?
3)目前postgres_exporter最新版本為0.17.1。
386:這是 Intel 80386 處理器架構(gòu),也被稱為 x86。它是早期的32位處理器架構(gòu)。
amd64:也稱為 x86-64 或 x64,這是一種64位的處理器架構(gòu),由 AMD 公司推出。它是基于 x86 架構(gòu)的64位擴(kuò)展,現(xiàn)在廣泛用于桌面和服務(wù)器系統(tǒng)。
arm64:這是 ARMv8-A 的64位處理器架構(gòu),主要應(yīng)用于 ARM 架構(gòu)的64位處理器,包括用于服務(wù)器、移動(dòng)設(shè)備和嵌入式系統(tǒng)的處理器。
armv5 和 armv6:這兩者都屬于 ARM 架構(gòu)的32位處理器架構(gòu),分別對(duì)應(yīng)于較舊的 ARM 處理器。它們通常用于嵌入式系統(tǒng)和一些較老的移動(dòng)設(shè)備。
............
?? ? ? ? ? ? ? ??
4)解壓二進(jìn)制postgres_exporter包
[root@postgresql ~]# tar -zxvf postgres_exporter-0.17.1.linux-amd64.tar.gz
? ? ? ? ? ? ??
5)移動(dòng)并重命名postgres_exporter解壓出來的目錄
[root@postgresql ~]# mkdir -p /opt/prometheus [root@postgresql ~]# mv postgres_exporter-0.17.1.linux-amd64 /opt/prometheus/ [root@postgresql ~]# mv /opt/prometheus/postgres_exporter-0.17.1.linux-amd64 /opt/prometheus/postgres_exporter
? ? ? ??
6)創(chuàng)建prometheus用戶
[root@postgresql ~]# groupadd prometheus [root@postgresql ~]# useradd -g prometheus -M -s /usr/sbin/nologin prometheus ---創(chuàng)建一個(gè)名為prometheus的系統(tǒng)賬號(hào),該賬號(hào)沒有家目錄并且不能登錄Shell。這種設(shè)置適用于需要運(yùn)行服務(wù)或任務(wù)而不需要用戶交互的情況。 -M:不為用戶創(chuàng)建家目錄。對(duì)于系統(tǒng)服務(wù)賬號(hào)來說,通常不需要家目錄,因此使用 -M 可以跳過家目錄的創(chuàng)建步驟。 -s /usr/sbin/nologin:指定用戶登錄時(shí)使用的Shell。將用戶prometheus的登錄Shell設(shè)置為/usr/sbin/nologin。nologin Shell的作用是阻止用戶登錄系統(tǒng),但允許該用戶擁有有效的系統(tǒng)賬號(hào)。這通常用于服務(wù)賬號(hào),因?yàn)樗鼈儾恍枰换ナ絊hell訪問,僅用于執(zhí)行特定服務(wù)或任務(wù)。[root@postgresql ~]# passwd prometheus [root@postgresql ~]# id prometheus
? ? ? ? ? ??
7)賦權(quán)
[root@postgresql ~]# chown -R prometheus:prometheus /opt/prometheus/postgres_exporter
? ? ? ? ?
8)寫入linux啟動(dòng)服務(wù)項(xiàng)
[root@postgresql ~]# cd /usr/lib/systemd/system [root@postgresql ~]# vi postgres5432_exporter.service [Unit] Description=postgres_exporter After=network.target[Service] Type=simple User=prometheus Group=prometheus Restart=on-failure Environment="DATA_SOURCE_NAME=postgresql://postgres_exporter:nX5sA4iC@10.2.4.98:5432/postgres?sslmode=disable" ExecStart=/opt/prometheus/postgres_exporter/postgres_exporter --web.listen-address=0.0.0.0:9187[Install] WantedBy=multi-user.target [root@postgresql ~]# systemctl daemon-reload [root@postgresql ~]# systemctl start postgres5432_exporter.service [root@postgresql ~]# systemctl enable postgres5432_exporter.service [root@postgresql ~]# systemctl status postgres5432_exporter.service
###postgres_exporter不同于其他數(shù)據(jù)庫,其他數(shù)據(jù)庫連接數(shù)據(jù)庫的用戶、密碼、IP等信息都可以在exporter插件中指定,但postgresql的exporter插件就不行,需要定義環(huán)境變量指定
###--web.listen-address=<address>:<port>:指定服務(wù)端口。Postgresql的抓取默認(rèn)通過9187端口訪問postgres_exporter管理界面,可以修改成其他端口用于訪問postgres_exporter管理界面(如果沒有修改端口的需求可以不設(shè)置這個(gè)參數(shù))。此參數(shù)只能在postgres_exporter命令啟動(dòng)時(shí)指定,不可以在配置文件prometheus.yml中指定。如果服務(wù)器上有多個(gè)實(shí)例,比如還有5433實(shí)例,那么需要再寫一個(gè)linux啟動(dòng)項(xiàng),并且通過DATA_SOURCE_NAME變量指定postgresql數(shù)據(jù)源
? ? ? ? ??
9)登錄postgres_exporter界面管理,默認(rèn)端口為9187
http://10.2.4.98:9187/metrics
? ? ? ? ? ? ?
三、將當(dāng)前postgresql 5432實(shí)例加入到prometheus監(jiān)控,并通過Grafana展示
注意:如下操作是在安裝了prometheus和Grafana的主機(jī)上進(jìn)行操作
? ? ? ? ? ?
1)將postgres_exporter的信息加入到prometheus監(jiān)控的配置文件prometheus.yml中
[root@prometheus ~]# vi /opt/prometheus/prometheus.yml 在scrape_configs下面新增如下內(nèi)容:# postgres_exporter配置- job_name: "postgreSQL監(jiān)控" ###job_name 用來唯一標(biāo)識(shí)一個(gè)監(jiān)控任務(wù)。在同一個(gè) prometheus.yml 文件中,不同的 scrape_config可以有不同的job_name,以便 Prometheus 能夠區(qū)分和管理不同的監(jiān)控目標(biāo)和配置,用來將不同的監(jiān)控目標(biāo)分組。單位為監(jiān)控postgreSQL數(shù)據(jù)庫設(shè)置一個(gè)獨(dú)立的job_namescrape_interval: 15s ###指定這個(gè)job_name每隔多久從每個(gè)目標(biāo)(如 Exporter、應(yīng)用端點(diǎn))拉取一次指標(biāo)數(shù)據(jù)。優(yōu)先級(jí):會(huì)覆蓋全局的 global:scrape_interval(如果存在)。file_sd_configs:- files:- /data/prometheus/conf.d/postgreSQL_targets.json # postgreSQL需要監(jiān)控的實(shí)例都單獨(dú)寫在了mysqld_targets.json文件中,是為了避免prometheus.yml內(nèi)容過多,看起來更簡潔[root@prometheus ~]# vi /data/prometheus/conf.d/postgreSQL_targets.json ###寫成linux或者M(jìn)ySQL通用的targets、labels的話,Prometheus可以獲取到數(shù)據(jù),但grafna獲取不到數(shù)據(jù)。如下是deepseek推薦的寫法,因?yàn)槭褂玫?628 json模版沒有告訴怎么定義格式 [{"targets": ["10.2.4.98:9187"],"labels": {"instance": "10.2.4.98:9187","business_name": "postgreSQL數(shù)據(jù)庫主庫(IP:10.2.4.98,端口:5432)"}} ]
? ? ? ? ??
2)配置告警規(guī)則文件
? ? 除了如下告警規(guī)則之外,還需要單獨(dú)配置一個(gè)Targets目標(biāo)不可達(dá)(up)的相關(guān)規(guī)則,因?yàn)閡p 是一個(gè)布爾值指標(biāo),表示 Prometheus 是否能夠成功地 scrape(抓取)到指定的目標(biāo)(targets)數(shù)據(jù),用于判斷目標(biāo)node-exporter相關(guān)進(jìn)程是否在目標(biāo)主機(jī)存活。關(guān)于up的規(guī)則文件參考:【prometheus+Grafana篇】從零開始:Linux 7.6 上二進(jìn)制安裝 Prometheus、Grafana 和 Node Exporter_grafana二進(jìn)制部署-CSDN博客
[root@prometheus ~]# cd /opt/prometheus/rules/ ###在prometheus.yml文件中定義了告警規(guī)則文件rule_files參數(shù) [root@prometheus rules]# vi postgreSQL_targets.yml
? ? ? ? ?
3)檢查配置文件
[root@prometheus ~]# cd /opt/prometheus/ [root@prometheus ~]# ./promtool check config prometheus.yml ###用于檢查 Prometheus 配置文件(prometheus.yml)的語法和格式是否正確。
執(zhí)行這條命令會(huì)執(zhí)行以下操作:
- 驗(yàn)證配置文件的語法:檢查 prometheus.yml 文件是否符合 Prometheus 配置格式規(guī)范。
- 檢測潛在的錯(cuò)誤或警告:如果配置文件中存在拼寫錯(cuò)誤、格式問題、無效的配置項(xiàng)等問題,它會(huì)提供相關(guān)的錯(cuò)誤信息或警告。比如告警規(guī)則寫的不對(duì),就會(huì)有提示
- 輸出有用的診斷信息:如果配置文件存在問題,promtool 會(huì)輸出詳細(xì)的錯(cuò)誤信息。
? ? ? ? ? ? ? ?
4)prometheus.yml文件添加了信息,所以重啟prometheus進(jìn)程或者重新加載配置文件(二選一)
#重載:前提是在prometheus.service啟動(dòng)服務(wù)項(xiàng)中加了--web.enable-lifecycle參數(shù):--web.enable-lifecycle:###啟用Prometheus的生命周期接口,允許通過HTTP請(qǐng)求來動(dòng)態(tài)重新加載配置等操作。這對(duì)于在運(yùn)行時(shí)更新配置或執(zhí)行其他管理操作非常有用,當(dāng)修改了prometheus的配置后,可以通過curl命令來重新加載配置文件,而不需要重啟prometheus(推薦方式)
[root@prometheus ~]# curl -X POST http://localhost:9090/-/reload
#重啟
[root@prometheus ~]# systemctl restart prometheus.service
?? ? ? ? ??
5)在prometheus查看是否可以看到監(jiān)控信息
網(wǎng)址:http://110.120.100.21:9090
菜單欄:Status—Targets
? ? ? ? ? ? ? ??
6)登錄Grafana界面管理,將當(dāng)前主機(jī)的監(jiān)控信息做展示
地址:http://110.120.100.21:3000
默認(rèn)用戶:admin
默認(rèn)密碼:admin
? ? ? ??
? ? ? ?
一、下載一個(gè)儀表盤:Grafana官網(wǎng)“http://www.grafana.com”—Dashboards,然后選擇一個(gè)下載量高的自己喜歡的postgres_exporter(postgresql監(jiān)控)
注意:不需要再增加數(shù)據(jù)源了。因?yàn)樵诘谝淮未罱╬rometheus+Grafana的時(shí)候就已經(jīng)加好了,在添加數(shù)據(jù)源時(shí),有個(gè)配置項(xiàng)是Connection,這里添加的是prometheus的地址,指的是prometheus監(jiān)控服務(wù)器的地址,而不是node-exporter的地址
? ? ? ? ? ?
?以9628為例(PostgreSQL Database),新增了多個(gè)指標(biāo),比較全面
? ? ? ??
二、將儀表盤添加到Grafana上:Dashboards—New—Import—點(diǎn)擊“Upload dashboard JSON file”
Name:定義名稱,最好定義為主機(jī)ip加用途
Prometheus:選擇prometheus源
....
然后import
? ? ? ? ? ? ? ? ? ? ??
三、儀表盤展示:Dashboards—查看到已經(jīng)定義好的儀表盤,并且數(shù)據(jù)源是prometheus監(jiān)控,點(diǎn)擊進(jìn)去
儀表盤Name:postgresql數(shù)據(jù)庫監(jiān)控
數(shù)據(jù)源Name:prometheus監(jiān)控
instance:這里其實(shí)就是當(dāng)時(shí)已經(jīng)在配置prometheus.yml時(shí),定義的instance,也就是說Grafana會(huì)自動(dòng)識(shí)別到
? ? 完結(jié),撒花。如果你覺得這篇文章有用,歡迎分享給你的隊(duì)友/老板/隔壁工位的同事——畢竟,獨(dú)樂樂不如眾樂樂,獨(dú) debug 不如眾 debug。