prometheus之监测服务器心跳

  • 环境:centos7, prometheus 2.3.1, blackbox_export 0.12.0
  • 其他exporter

前言

blackbox能支持什么协议:HTTP, HTTPS (via the http prober), DNS, TCP socket and ICMP.
对于机器可用性的监测,最简单的莫过于使用ping或者ssh的方式.这里使用blackbox_exporter,基于使用node_exporter的经验,可能会认为需要将blackbox_exporter部署到每个节点下,而实际上通过一个部署了blackbox_exporter的节点去检测其他机器.

img

Execute blackbox_exporter

Build with Binary and Config Systemd

Build Binary

1
2
3
4
5
6
# prometheus官网有下载地址
$ cd /opt
$ wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.12.0/blackbox_exporter-0.12.0.linux-amd64.tar.gz
$ tar -xvf blackbox_exporter-0.12.0.linux-amd64.tar.gz
# 重命名
$ mv blackbox_exporter-0.12.0.linux-amd64 blackbox_exporter-0.12.0

Config Systemd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ vim /usr/lib/systemd/system/blackbox.service
[Unit]
Description=blackbox.service
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
ExecStart=/opt/blackbox_exporter-0.12.0/blackbox_exporter --config.file=/opt/blackbox_exporter-0.12.0/blackbox.yml
Restart=on-failure
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID

[Install]
WantedBy=multi-user.target
# 启动
$ systemctl enable blackbox && systemctl start blackbox

Build with Docker

1
2
3
# pwd指定宿主机配置文件路径,我一般都放在/data/blackbox_exporter/config目录下
$ docker build -t blackbox_exporter .
$ docker run -d -p 9115:9115 --name blackbox_exporter -v `pwd`:/config blackbox_exporter --config.file=/config/blackbox.yml

Configuration

  • blackbox_exporter example.yml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    # 这里暂时只使用ping和ssh验证主机有效性的方式,其他配置可参看github上的example.yml
    $ vim blackbox.yml
    # probe探测频率由prometheus中的scripe_timeout决定.
    modules:
    ssh_banner:
    prober: tcp
    timeout: 10s # 每次探测的网络超时时间,默认为10秒
    tcp:
    query_response:
    - expect: "^SSH-2.0-"
    icmp:
    prober: icmp
    timeout: 10s
    icmp:
    preferred_ip_protocol: "ip4"

Test Blackbox Exporter

1
2
3
4
5
6
7
8
# 测试目标10.1.1.21的ping信息
$ curl http://10.1.1.26:9115/probe\?module\=icmp\&target\=10.1.1.25
......
probe_success 0 # 目标主机ping失败
# 测试目标10.1.1.21 22的ssh信息
$ curl http://10.1.1.26:9115/probe\?module\=ssh_banner\&target\=10.1.1.21:22
......
probe_success 1 # 目标主机ssh通过

参考

blackbox_exporter github

用Prometheus进行网络质量ping

坚持原创技术分享,您的支持将鼓励我继续创作!
Fork me on GitHub