使用 Prometheus + Grafana 来监控Mikrotik CHR

环境

前言

接触ROS这款软路由的时间也算久了,从当年2.9.27到现在流行的CHR(暴露年龄)。但是对于RouterOS软路由的开源监控,选择真心不多。嗯,Zabbix/Cacti算吧(通过SNMP)。近发现有个名为 mikrotik-exporterPromethuesus 组件。

部署步骤

1. 在ROS系统创建相关账号

/user group add name=prometheus policy=api,read,winbox
/user add name=prometheus group=prometheus password=changeme

2.配置 mikrotik-exporter

创建用户

$ sudo useradd prometheus -M -s /sbin/nologin

创建配置文件

cat > /etc/prometheus/mikrotik-exporter.yaml<<EOF
devices:
  - name: gateway
    address: ROS地址
    user: prometheus
    password: 密码

features:
  bgp: true
  dhcp: false
  dhcpv6: false
  routes: true
  pools: true
  optics: true
EOF

创建mikrotik_exporter.service

$ sudo systemctl cat mikrotik_exporter.service
# /etc/systemd/system/mikrotik_exporter.service
[Unit]
Description=mikrotik Exporter

[Service]
User=node_exporter
ExecStart=/usr/local/bin/mikrotik-exporter -config-file /etc/prometheus/mikrotik-exporter.yaml

[Install]
WantedBy=default.target

启动服务

$ sudo systemctl start mikrotik_exporter.service
$ sudo systemctl enable mikrotik_exporter.service

测试接口

$ curl localhost:9436/metrics
# HELP mikrotik_interface_rx_byte rx-byte
# TYPE mikrotik_interface_rx_byte counter
mikrotik_interface_rx_byte{address="10.10.9.1",comment="",interface="CT-A",name="gateway"} 5.7573456118e+10
mikrotik_interface_rx_byte{address="10.10.9.1",comment="",interface="CT-B",name="gateway"} 6.4707920662e+10
mikrotik_interface_rx_byte{address="10.10.9.1",comment="",interface="LAN",name="gateway"} 1.736511323e+10
mikrotik_interface_rx_byte{address="10.10.9.1",comment="",interface="pppoe-500m-a",name="gateway"} 3.9363345924e+10
mikrotik_interface_rx_byte{address="10.10.9.1",comment="",interface="pppoe-500m-b",name="gateway"} 6.3512147435e+10
# HELP mikrotik_interface_rx_drop rx-drop
# TYPE mikrotik_interface_rx_drop counter
mikrotik_interface_rx_drop{address="10.10.9.1",comment="",interface="CT-A",name="gateway"} 0
mikrotik_interface_rx_drop{address="10.10.9.1",comment="",interface="CT-B",name="gateway"} 0
mikrotik_interface_rx_drop{address="10.10.9.1",comment="",interface="LAN",name="gateway"} 0
mikrotik_interface_rx_drop{address="10.10.9.1",comment="",interface="pppoe-500m-a",name="gateway"} 0
mikrotik_interface_rx_drop{address="10.10.9.1",comment="",interface="pppoe-500m-b",name="gateway"} 0
...<省略若干行>...

3. 配置 Prometheus

...<省略若干行>...
  - job_name: 'Gateway'
    static_configs:
      - targets: ['127.0.0.1:9436']
        labels:
          instance: 'RouterOS-CHR'
          env: 'Prodction'

4. Grafana 配置

  • Variabls
label_values(mikrotik_system_uptime, instance)
  • 系统运行时间
mikrotik_system_uptime{instance=~"$node"} / 86400
  • CPU使用比
mikrotik_system_cpu_load{instance=~"$node"}
  • 内存使用比
(((mikrotik_system_total_memory{instance=~"$node"}) - (mikrotik_system_free_memory{instance=~"$node"})) / (mikrotik_system_total_memory{instance=~"$node"})) * 100
  • 磁盘使用占比
(((mikrotik_system_total_hdd_space{instance=~"$node"}) - (mikrotik_system_free_hdd_space{instance=~"$node"})) /  (mikrotik_system_total_hdd_space{instance=~"$node"})) * 100
  • 路由条目数量
sum(mikrotik_routes_protocol_count{ip_version=~"4",instance=~"$node"}) by(protocol)
  • 网络流量
irate(mikrotik_interface_rx_byte{interface!~'^CT-.+', instance=~"$node"}[1m])*8
irate(mikrotik_interface_tx_byte{interface!~'^CT-.+', instance=~"$node"}[1m])*8

5.截图

Grafana-with-mikrotik_CHR
----_2019-10-17T00-44-26.480Z

https://grafana.com/grafana/dashboards/10950