Blackbox Exporter简介
blackbox_exporter 是 Prometheus 拿来对 http/https、tcp、icmp、dns、进行的黑盒监控工具,也就是从服务、主机等外部进行探测,来查看服务、主机等是否可用。
Blackbox Exporter 默认端口是 9115,
安装1
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.25.0/blackbox_exporter-0.25.0.linux-amd64.tar.gz
tar -zxf blackbox_exporter-0.25.0.linux-amd64.tar.gz
mv xxxx /usr/local/bin/blackbox_exporter
#只监控网站官网默认配置不需要更改
# ll
total 21124
-rw-r--r-- 1 1001 1002 11357 Apr 9 21:00 LICENSE
-rw-r--r-- 1 1001 1002 94 Apr 9 21:00 NOTICE
-rw-r--r-- 1 1001 1002 956 Apr 9 21:00 blackbox.yml
-rwxr-xr-x 1 1001 1002 21606608 Apr 9 20:59 blackbox_exporter
配置快速启动文件
cat > /usr/lib/systemd/system/blackbox_exporter.service <<EOF
[Unit]
Description=blackbox_exporter
After=network.target
[Service]
User=root
Type=simple
ExecStart=/usr/local/bin/blackbox_exporter/blackbox_exporter --config.file=/usr/local/blackbox_exporter/blackbox.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl restart blackbox_exporter
systemctl enable blackbox_exporter
systemctl status blackbox_exporter
dockr快速启动
docker run -d -p 9115:9115 -v /blackbox/blackbox.yml:/config/blackbox.yml --name blackbox quay.io/prometheus/blackbox-exporter:latest
k8s 启动
cat > blackbox.yml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: blackbox-exporter
spec:
replicas: 1
selector:
matchLabels:
app: blackbox-exporter
template:
metadata:
labels:
app: blackbox-exporter
spec:
containers:
- name: blackbox-exporter
image: quay.io/prometheus/blackbox-exporter:latest
ports:
- containerPort: 9115
volumeMounts:
- name: config-volume
mountPath: /config
readOnly: true
command:
- "blackbox_exporter"
args:
- "--config.file=/config/blackbox.yml"
volumes:
- name: config-volume
configMap:
name: blackbox-exporter-config
---
apiVersion: v1
kind: Service
metadata:
name: blackbox-exporter-service
spec:
selector:
app: blackbox-exporter
ports:
- protocol: TCP
port: 9115
targetPort: 9115
type: ClusterIP
EOF
conf
cat >config-blackbox.yml<<EOF
apiVersion: v1
data:
blackbox.yml: |-
modules:
http_2xx:
prober: http
http:
preferred_ip_protocol: "ip4"
http_post_2xx:
prober: http
http:
method: POST
tcp_connect:
prober: tcp
pop3s_banner:
prober: tcp
tcp:
query_response:
- expect: "^+OK"
tls: true
tls_config:
insecure_skip_verify: false
grpc:
prober: grpc
grpc:
tls: true
preferred_ip_protocol: "ip4"
grpc_plain:
prober: grpc
grpc:
tls: false
service: "service1"
ssh_banner:
prober: tcp
tcp:
query_response:
- expect: "^SSH-2.0-"
- send: "SSH-2.0-blackbox-ssh-check"
irc_banner:
prober: tcp
tcp:
query_response:
- send: "NICK prober"
- send: "USER prober prober prober :prober"
- expect: "PING :([^ ]+)"
send: "PONG ${1}"
- expect: "^:[^ ]+ 001"
icmp:
prober: icmp
icmp_ttl5:
prober: icmp
timeout: 5s
icmp:
ttl: 5
kind: ConfigMap
metadata:
name: blackbox-exporter-config
EOF
http监控
HTTP就是通过GET或者POST的方式来检测应用是否正常。 这种场景多用于监控网页是否正常,如:公司官网是否不能正常打开,或者打开过慢这样的情况
cd /usr/local/prometheus
mkdir sd_file/blackbox
cat sd_file/blackbox/probe_http.yml
- targets:
- http://192.168.92.101:8081/ # Target to probe with http.
- https://www.bilibili.com/ # Target to probe with https.
- http://example.com:8080
labels:
web: probe_http
vim prometheus.yml
# 这个是 http 探测的结果
- job_name: 'probe_http'
metrics_path: /probe
params:
module: [http_2xx] # Look for a HTTP 200 response.
file_sd_configs:
- refresh_interval: 10s
files:
- "/usr/local/prometheus/sd_file/blackbox/*.yml"
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: blackbox.monitoring.IP:9115 # The blackbox exporter's real hostname:port.
# 这个配置是 blackbox_exporter/metrics 的数据
# collect blackbox exporter's operational metrics.
- job_name: 'blackbox_exporter' # collect blackbox exporter's operational metrics.
static_configs:
- targets: ['blackbox.monitoring.IP:9115']
### 以上自定义配置
# ============================== 以下是我的自己的配置,可根据需要进行修改
- job_name: "blackbox_http"
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets:
- https://prod1-im-m.xx000.info
- https://prod1-im-manage-web.xx000.info
- https://prod1-im-www.xx00.info
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.0.20:9115
TCP监控-探测端口
#TCP监控主要是通过类似于Telnet的方式进行检测
vim prometheus.yml
- job_name: "check-middleware-tcp"
metrics_path: /probe
params:
module: [tcp_connect] # Look for a HTTP 200 response.
static_configs:
- targets:
- xx.xx.xx.xx:80
- xx.xx.xx.xx:3306
labels:
group: middleware-tcp
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: blackbox.monitoring:9115
最后 grafana ID推荐 18538 19342
https://grafana.com/grafana/dashboards/19342-blackbox-exporter/