Blackbox-Exporter 學習筆記

一句話總結:跟 prometheus 結合,監(jiān)控網(wǎng)頁歌粥。

更準確的描述如下:

基于blackbox_exporter實現(xiàn)對URL狀態(tài)攀操、IP可用性、端口狀態(tài)锡移、TLS證書的過期時間監(jiān)控呕童。

我們監(jiān)控主機的資源用量、容器的運行狀態(tài)淆珊、數(shù)據(jù)庫中間件的運行數(shù)據(jù)夺饲。這些都是支持業(yè)務和服務的基礎設施,通過白盒能夠了解其內(nèi)部的實際運行狀態(tài),通過對監(jiān)控指標的觀察能夠預判可能出現(xiàn)的問題往声,從而對潛在的不確定因素進行優(yōu)化擂找。

而從完整的監(jiān)控邏輯的角度,除了大量的應用白盒監(jiān)控以外浩销,還應該添加適當?shù)暮诤斜O(jiān)控贯涎。黑盒監(jiān)控即以用戶的身份測試服務的外部可見性,常見的黑盒監(jiān)控包括HTTP探針慢洋、TCP探針等用于檢測站點或者服務的可訪問性塘雳,以及訪問效率等。

黑盒監(jiān)控相較于白盒監(jiān)控最大的不同在于黑盒監(jiān)控是以故障為導向當故障發(fā)生時普筹,黑盒監(jiān)控能快速發(fā)現(xiàn)故障败明,而白盒監(jiān)控則側重于主動發(fā)現(xiàn)或者預測潛在的問題。一個完善的監(jiān)控目標是要能夠從白盒的角度發(fā)現(xiàn)潛在問題太防,能夠在黑盒的角度快速發(fā)現(xiàn)已經(jīng)發(fā)生的問題妻顶。

一、blackbox_exporter介紹

Blackbox Exporter是Prometheus社區(qū)提供的官方黑盒監(jiān)控解決方案蜒车,其允許用戶通過:HTTP讳嘱、HTTPS、DNS酿愧、TCP以及ICMP的方式對網(wǎng)絡進行探測沥潭。

HTTP/HTPPS: URL/API 可用性檢測
TCP: 端口監(jiān)聽檢測
ICMP: 主機存活檢測
DNS: 域名解析

二、安裝部署blackbox_exporter

假設Prometheus寓娩,Alert Manager 已經(jīng)安裝配置叛氨。

安裝

wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.22.0/blackbox_exporter-0.22.0.linux-amd64.tar.gz
tar -xf blackbox_exporter-0.22.0.linux-amd64.tar.gz -C /apps/
cd /apps/
mv blackbox_exporter-0.22.0.linux-amd64/ blackbox_exporter

自啟動

[root@monitoring ~]# vim /etc/systemd/system/blackbox-exporter.service
[root@monitoring ~]# cat /etc/systemd/system/blackbox-exporter.service
[Unit]
Description=Prometheus Blackbox Exporter
After=network.target

[Service]
Type=simple
User=root
Group=root

ExecStart=/apps/blackbox_exporter/blackbox_exporter \
--config.file=/apps/blackbox_exporter/blackbox.yml \
--web.listen-address=:9115
Restart=on-failure

[Install]
WantedBy=multi-user.target

[root@monitoring ~]# systemctl enable --now blackbox-exporter.service 
Created symlink /etc/systemd/system/multi-user.target.wants/blackbox-exporter.service → /etc/systemd/system/blackbox-exporter.service.
[root@monitoring ~]# systemctl status blackbox-exporter.service 
● blackbox-exporter.service - Prometheus Blackbox Exporter
   Loaded: loaded (/etc/systemd/system/blackbox-exporter.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2022-09-27 16:56:04 CST; 1min 13s ago
 Main PID: 29832 (blackbox_export)
    Tasks: 8 (limit: 49440)
   Memory: 4.9M
   CGroup: /system.slice/blackbox-exporter.service
           └─29832 /apps/blackbox_exporter/blackbox_exporter --config.file=/apps/blackbox_exporter/blackbox.yml --web.listen-address=:9115

Sep 27 16:56:04 monitoring systemd[1]: Started Prometheus Blackbox Exporter.
...

Creating BlackBox job in Prometheus

Go to the installation directory of our Prometheus e.g. /opt/prometheus and edit the prometheus.yml file

under scrape_configs add a new job named blackbox with the following snippet

- job_name: 'blackbox'
  metrics_path: /probe
  params:
   module: [http_2xx] # Look for a HTTP 200 response.
  static_configs:
  - targets:
     - https://gritfy.com
     - https://www.google.com
     - https://middlewareinventory.com
     - https://devopsjunction.com
  relabel_configs:
    - source_labels: [__address__]
      target_label: __param_target
    - source_labels: [__param_target]
      target_label: instance
    - target_label: __address__
      replacement: 127.0.0.1:9115

you can change the URLs of your choice. but the relabel_configs should remain the same.

這里針對每一個探針服務(如http_2xx)定義一個采集任務,并且直接將任務的采集目標定義為我們需要探測的站點棘伴。在采集樣本數(shù)據(jù)之前通過relabel_configs對采集任務進行動態(tài)設置寞埠。

通過以上3個relabel步驟,即可大大簡化Prometheus任務配置的復雜度

Configuring Alerts and Rules in Prometheus

As part of Alert triggering, we are going to setup alerts for two scenarios

  • SSLCertExpiringSoon ( with in 24 days )
  • TargetUrlDown (Endpoint down or returning invalid response)

To generate alerts we need to create rules in Prometheus first.

If the rules are satisfied Prometheus would send the alert to AlertManager

Create a new Rule file

Go to Prometheus installation directory i.e /opt/prometheus and create a new directory named rules

under rules directory. create a new file named blackbox-rules.yml

groups:
  - name: Blackbox rules
    rules:
      - alert: SSLCertExpiringSoon
        expr: probe_ssl_earliest_cert_expiry - time() < 86400 * 24
        for: 1m
        labels:
          severity: warning
        annotations:
          description: "TLS certificate will expire in {{ $value | humanizeDuration }} (instance {{ $labels.instance }})"

      - alert: EndpointDown
        expr: probe_success == 0
        for: 10m
        labels:
          severity: "critical"
        annotations:
          summary: "Endpoint {{ $labels.instance }} down"

for the SSL Cert Expiry: If the earliest cert expiry value is below 86400 * 24 24 days it would trigger an alert.

Adding a rule into prometheus.yml

the rule file can now be added into our prometheus.yml configuration file

under the rule_files add our recently created rule filename rules/blackbox-rules.yml

rule_files:
   - "rules/blackbox-rules.yml"

Enabling the alert manager in prometheus.yml

While you are adding new rules. you have to also enable the alertmanager configuration which is disabled by default

It should point to the localhost:9093

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
       - localhost:9093

refs:

http://www.reibang.com/p/0c2d2528f310

https://blog.csdn.net/mnasd/article/details/124413488

https://www.middlewareinventory.com/blog/ssl-expiry-and-uptime-monitor-for-urls-prometheus-blackbox-grafana/

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末焊夸,一起剝皮案震驚了整個濱河市仁连,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌阱穗,老刑警劉巖饭冬,帶你破解...
    沈念sama閱讀 216,591評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異揪阶,居然都是意外死亡昌抠,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評論 3 392
  • 文/潘曉璐 我一進店門鲁僚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來炊苫,“玉大人裁厅,你說我怎么就攤上這事∏劝” “怎么了执虹?”我有些...
    開封第一講書人閱讀 162,823評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長唠梨。 經(jīng)常有香客問我袋励,道長,這世上最難降的妖魔是什么当叭? 我笑而不...
    開封第一講書人閱讀 58,204評論 1 292
  • 正文 為了忘掉前任茬故,我火速辦了婚禮,結果婚禮上蚁鳖,老公的妹妹穿的比我還像新娘均牢。我一直安慰自己,他們只是感情好才睹,可當我...
    茶點故事閱讀 67,228評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著甘邀,像睡著了一般琅攘。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上松邪,一...
    開封第一講書人閱讀 51,190評論 1 299
  • 那天坞琴,我揣著相機與錄音,去河邊找鬼逗抑。 笑死剧辐,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的邮府。 我是一名探鬼主播荧关,決...
    沈念sama閱讀 40,078評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼褂傀!你這毒婦竟也來了忍啤?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 38,923評論 0 274
  • 序言:老撾萬榮一對情侶失蹤仙辟,失蹤者是張志新(化名)和其女友劉穎同波,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體叠国,經(jīng)...
    沈念sama閱讀 45,334評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡未檩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,550評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了粟焊。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片冤狡。...
    茶點故事閱讀 39,727評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡孙蒙,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出筒溃,到底是詐尸還是另有隱情马篮,我是刑警寧澤,帶...
    沈念sama閱讀 35,428評論 5 343
  • 正文 年R本政府宣布怜奖,位于F島的核電站浑测,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏歪玲。R本人自食惡果不足惜迁央,卻給世界環(huán)境...
    茶點故事閱讀 41,022評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望滥崩。 院中可真熱鬧岖圈,春花似錦、人聲如沸钙皮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽短条。三九已至导匣,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間茸时,已是汗流浹背贡定。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留可都,地道東北人缓待。 一個月前我還...
    沈念sama閱讀 47,734評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像渠牲,于是被迫代替她去往敵國和親旋炒。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,619評論 2 354

推薦閱讀更多精彩內(nèi)容