Blackbox Exporter 是用於Prometheus的一個套件,主用在偵測HTTPS、HTTP、TCP、DNS 和 ICMP 等服務。定義好服務後,Blackbox Exporter 會生成Metrics,這些Metrics可提供 Grafana 進行可視化監控。
因為是官方的套件,所以直接去抓新版的就好了
1 2 3 4 5
| mkdir source cd source wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.24.0/blackbox_exporter-0.24.0.linux-amd64.tar.gz tar -zxf blackbox_exporter-0.24.0.linux-amd64.tar.gz cd blackbox_exporter-0.24.0.linux-amd64
|
- blackbox_exporter:這是一個運行服務的可執行文件。
- blackbox.yml:YAML 文件,包含您定義要探測的所有端點、要運行模組的所有 backbox exporter 配置。
把 Blackbox 設定成系統Service
1
| vim /etc/systemd/system/blackbox.service
|
blackbox.service 內容
1 2 3 4 5 6 7 8 9 10 11 12 13
| [Unit] Description=Blackbox Exporter Service Wants=network-online.target After=network-online.target
[Service] Type=simple User=blackbox Group=blackbox ExecStart=/blackbox/blackbox_exporter-0.23.0.linux-amd64/blackbox_exporter --config.file=/blackbox/blackbox_exporter-0.23.0.linux-amd64/blackbox.yml
[Install] WantedBy=multi-user.target
|
但我個人比較懶 所以我直接 σ`∀´)σ
1
| nohup ./blackbox_exporter --config.file="/root/source/blackbox_exporter-0.24.0.linux-amd64/blackbox.yml" &
|
可以修改一下 blackbox.yml
在你要用的modules或全部的modules加一個參數: preferred_ip_protocol: "ipv4"
設定Prometheus.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| - job_name: 'blackbox_http_2xx' metrics_path: '/probe' file_sd_configs: - refresh_interval: 60s files: - "/usr/local/prometheus/job_configs/WEB.json" params: module: [ http_post_2xx ] relabel_configs: - source_labels: [__address__] target_label: __param_target replacement: https://$1/ - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: localhost:9115
- job_name: 'blackbox_tcp_connect' scrape_interval: 30s metrics_path: '/probe' file_sd_configs: - refresh_interval: 60s files: - "/usr/local/prometheus/job_configs/TCP_connect.json" params: module: [ tcp_connect ] relabel_configs: - source_labels: [__address__] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: localhost:9115
|
我把Target
另外編寫,所以目標主機資訊會寫在另外一個json裡,這麼做的好處是,下次有新的主機需要使用相同模組來抓資訊,不用重啟Premetheus,直接編輯檔案加進去就可以了。
※第一次調整完 Prometheus 還是要重啟喔
編輯設定檔
1
| vim /usr/local/prometheus/job_configs/WEB.json
|
1 2 3 4 5 6 7 8 9 10
| [ { "targets": ["sandbox.itunes.apple.com/verifyReceipt"], "labels": {"devicename": "Apple Sandbox"} }, { "targets": ["buy.itunes.apple.com/verifyReceipt"], "labels": {"devicename": "Apple Buy"} }, ]
|
可以用Prometheus的檢測工具確認設定檔是否正確
promtool check config /usr/local/prometheus/prometheus.yml
如果成功就可以去 Prometheus 上看是否有收到資料
https://localhost:9090/targets?search=
之後再去官網找你喜歡的Dashboard回來調整就大功告成囉。