下载
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.22.0/blackbox_exporter-0.22.0.linux-amd64.tar.gz
解压安装
tar -zxvf blackbox_exporter-0.22.0.linux-amd64.tar.gz -C /usr/local/
cd /usr/local/ && ln -s blackbox_exporter-0.22.0.linux-amd64 blackbox_exporter
配置使用Systemd管理Prometheus
vim /etc/systemd/system/blackbox_exporter.service
[Unit]
Description=blackbox_exporter
After=network.target
[Service]
User=root
Type=simple
ExecStart=/usr/local/blackbox_exporter/blackbox_exporter --config.file=/usr/local/blackbox_exporter/blackbox.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target
启动
# 重载systemd 配置,修改完systemd配置文件后需重载才会生效。
systemctl daemon-reload
# 设置服务开机启动
systemctl enable blackbox_exporter
# 启动服务
systemctl start blackbox_exporter
# 查看服务状态
systemctl status blackbox_exporter
prometheus添加监控项
vi /usr/local/prometheus/prometheus.yml
############icmp监控#############
- job_name: crawler_status
metrics_path: /probe
params:
module: [icmp]
static_configs:
- targets: ['192.168.1.134','192.168.1.132']
labels:
instance: node_status
group: 'node'
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
###########tcp监控##############
- job_name: 'crawler_port_status'
metrics_path: /probe
params:
module: [tcp_connect]
static_configs:
- targets: ['192.168.1.134:80','192.168.1.134:443','192.168.1.134:9000','192.168.1.132:80','192.168.1.132:443','192.168.1.132:9000']
labels:
instance: 'port_status'
group: 'tcp'
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
###########web监控##############
- job_name: taobao_web_status
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets: ['https://www.taobao.com']
labels:
instance: taobao_web_status
group: 'web'
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- target_label: __address__
replacement: 127.0.0.1:9115
- job_name: baidu_web_status
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets: ['https://www.baidu.com']
labels:
instance: baidu_web_status
group: 'web'
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- target_label: __address__
replacement: 127.0.0.1:9115
检查是否正常
probe_success v是1的表示正常,0的表示失败
grafana添加监控图
https://grafana.com/grafana/dashboards/9965
添加报警
cat /usr/local/prometheus/rules/blackbox-alert.yml
groups:
- name: blackbox_network_stats
rules:
- alert: blackbox_network_stats
expr: probe_success == 0
for: 1m
labels:
severity: critical
annotations:
summary: "Instance {{ $labels.instance }} is down"
description: "This requires immediate action!"
补充ssl证书检测
cat << 'EOF' > prometheus.yml
rule_files:
- ssl_expiry.rules
scrape_configs:
- job_name: 'blackbox'
metrics_path: /probe
params:
module: [http_2xx] # Look for a HTTP 200 response.
static_configs:
- targets:
- example.com # Target to probe
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 # Blackbox exporter.
EOF
cat << 'EOF' > ssl_expiry.rules
groups:
- name: ssl_expiry.rules
rules:
- alert: SSLCertExpiringSoon
expr: probe_ssl_earliest_cert_expiry{job="blackbox"} - time() < 86400 * 30
for: 10m
EOF