用于 Mikrotik RouterOS 设备的 Prometheus 导出器
项目描述
描述
MKTXP 是 Mikrotik RouterOS 设备的 Prometheus 导出器。
它跨多个路由器收集和导出一组丰富的指标,所有这些都可以通过内置的 CLI 界面轻松配置。
除了导出到 Prometheus 之外,MKTXP 还可以直接在命令行上打印一些指标(参见下面的示例)。
为了轻松可视化导出到 Prometheus 的 RouterOS 指标,MKTXP 配备了专用的Grafana 仪表板:
要求:
-
Python 3.6.x或更高版本
-
支持的操作系统:
- Linux
- Mac OSX
-
Mikrotik RouterOS 设备
-
可选的:
安装:
- 来自PyPI:
❯ pip install mktxp
- 来自源存储库的最新版本:
❯ pip install git+https://github.com/akpw/mktxp
入门
安装 MKTXP 后,您需要编辑其主配置文件。最简单的方法是通过运行:
❯ mktxp edit
这将在您的默认系统编辑器中打开配置文件。
如果您更喜欢不同的编辑器,只需运行edit
带有可选-ed
参数的命令。
例如,要在 nano 中显式打开 MKTXP 配置:
❯ mktxp edit -ed nano
配置文件附带一个示例配置,可以根据需要轻松复制/编辑参数:
[Sample-Router]
enabled = False # turns metrics collection for this RouterOS device on / off
hostname = localhost # RouterOS IP address
port = 8728 # RouterOS IP Port
username = username # RouterOS user, needs to have 'read' and 'api' permissions
password = password
use_ssl = False # enables connection via API-SSL servis
no_ssl_certificate = False # enables API_SSL connect without router SSL certificate
ssl_certificate_verify = False # turns SSL certificate verification on / off
dhcp = True # DHCP general metrics
dhcp_lease = True # DHCP lease metrics
pool = True # Pool metrics
interface = True # Interfaces traffic metrics
firewall = True # Firewall rules matching traffic metrics
monitor = True # Interface monitor metrics
route = True # Routes metrics
wireless = True # WLAN general metrics
wireless_clients = True # WLAN clients metrics
capsman = True # CAPsMAN general metrics
capsman_clients = True # CAPsMAN clients metrics
use_comments_over_names = False # when available, forces using comments over the interfaces names
Mikrotik 设备配置
出于 RouterOS 设备监控的目的,最好创建一个所需权限最少的专用用户。
MKTXP 只需要API
and Read
,所以此时您可以转到路由器的终端并输入:
/user group add name=mktxp_group policy=api,read
/user add name=mktxp_user group=mktxp_group password=mktxp_user_password
对现实的检验
现在让我们在上面的 MKTXP 配置文件中放入一些 Mikrotik 设备地址/用户凭据,此时我们应该已经能够检查到目前为止的进度。由于 MKTXP 可以通过命令直接在命令行上输出选定的指标mktxp print
,因此即使没有 Prometheus 或 Grafana 也很容易做到。
例如,我们来看看我的一些智能家居 CAPsMAN 客户端:
❯ mktxp print -en MKT-GT -cc
Connecting to router MKT-GT@10.**.*.**
2021-01-24 12:04:29 Connection to router MKT-GT@10.**.*.** has been established
| dhcp_name | dhcp_address | mac_address | rx_signal | interface | ssid | tx_rate | rx_rate | uptime |
|----------------------|----------------|-------------------|-------------|-------------|--------|-----------|-----------|----------|
| Woox Runner | 10.**.*.** | 80:*************D | -64 | LR-2G-1-1 | AKP | 72 Mbps | 54 Mbps | 3 days |
| Woox Office Lamp | 10.**.*.** | 80:*************F | -59 | LR-2G-1-1 | AKP | 72 Mbps | 54 Mbps | 3 days |
| Harmony Hub | 10.**.*.** | C8:*************5 | -46 | LR-2G-1-1 | AKP | 72 Mbps | 72 Mbps | 3 days |
| Woox Office Hub | 10.**.*.** | DC:*************7 | -44 | LR-2G-1-1 | AKP | 72 Mbps | 54 Mbps | 3 days |
| Woox Ext Hub | 10.**.*.** | DC:*************E | -44 | LR-2G-1-1 | AKP | 72 Mbps | 54 Mbps | 3 days |
| Amazon Echo | 10.**.*.** | CC:*************4 | -44 | LR-2G-1-1 | AKP | 72 Mbps | 72 Mbps | a day |
| Woox Living Room Hub | 10.**.*.** | DC:*************0 | -43 | LR-2G-1-1 | AKP | 72 Mbps | 54 Mbps | 3 days |
| JBL View | 10.**.*.** | 00:*************D | -28 | LR-2G-1-1 | AKP | 144 Mbps | 117 Mbps | 7 hours |
| | | | | | | | | |
| MBP15 | 10.**.*.** | 78:*************E | -53 | GT-5G-1 | AKP5G | 877 Mbps | 877 Mbps | 3 days |
| | | | | | | | | |
| Woox Toaster | 10.**.*.** | 68:*************B | -70 | KT-2G-1-1 | AKP | 72 Mbps | 54 Mbps | 3 days |
| Woox Kettle | 10.**.*.** | B4:*************5 | -65 | KT-2G-1-1 | AKP | 65 Mbps | 54 Mbps | 2 days |
| Woburn White | 10.**.*.** | 54:*************6 | -59 | KT-2G-1-1 | AKP | 72 Mbps | 72 Mbps | 9 hours |
| Siemens Washer | 10.**.*.** | 68:*************1 | -57 | KT-2G-1-1 | AKP | 72 Mbps | 72 Mbps | 2 days |
| Woburn Black | 10.**.*.** | 54:*************8 | -57 | KT-2G-1-1 | AKP | 72 Mbps | 72 Mbps | 9 hours |
| Google Nest Display | 10.**.*.** | 1C:*************A | -49 | KT-2G-1-1 | AKP | 52 Mbps | 43 Mbps | 8 hours |
----------------------- --
Connected Wifi Devices: 15
----------------------- --
嗯,那个烤面包机可能会使用更好的信号...... :)
但是让我们回到正轨并继续将 RouterOS 指标导出到 Prometheus 的业务。
导出到普罗米修斯
要将您的路由器指标导入现有的 Prometheus 安装,我们基本上只需将 MKTXP 连接到它。
让我们通过编辑 Prometheus 配置文件来做到这一点:
❯ nano /etc/prometheus/prometheus.yml
并简单地添加:
- job_name: 'mktxp'
static_configs:
- targets: ['mktxp_machine_IP:49090']
此时,我们应该准备好运行主mktxp export
命令,该命令将按照上面的配置收集路由器指标,并通过默认端口 49090 上的 http 服务器将它们提供给 Prometheus。\
❯ mktxp export
Connecting to router MKT-GT@10.**.*.**
2021-01-24 14:16:22 Connection to router MKT-GT@10.**.*.** has been established
Connecting to router MKT-LR@10.**.*.**
2021-01-24 14:16:23 Connection to router MKT-LR@10.**.*.** has been established
2021-01-24 14:16:23 Running HTTP metrics server on port 49090
如果首选不同的端口,可以通过运行mktxp edit -i
命令根据需要进行设置。
这将打开一个内部 MKTXP 配置文件,其中包含更多与实现相关的参数。
Grafana 仪表板
现在,随着您的 RouterOS 指标被导出到 Prometheus,使用这个Grafana 仪表板很容易将它们可视化
设置 MKTXP 以作为 Linux 服务运行
如果您在 Linux 系统上安装了 MKTXP,您可以通过添加服务在系统启动时运行它。
让我们从:
❯ nano /etc/systemd/system/mktxp.service
现在复制并粘贴以下内容:
[Unit]
Description=MKTXP Exporter
[Service]
User=user # the user under which mktxp was installed
ExecStart=mktxp export # if mktxp is not at your $PATH, you might need to provide a full path
[Install]
WantedBy=default.target
让我们保存并启动服务并检查其状态:
❯ sudo systemctl daemon-reload
❯ sudo systemctl start mktxp
❯ sudo systemctl enable mktxp
❯ systemctl status mktxp
● mktxp.service - MKTXP Mikrotik Exporter to Prometheus
Loaded: loaded (/etc/systemd/system/mktxp.service; disabled; vendor preset: enabled)
Active: active (running) since Sun 2021-01-24 09:16:44 CET; 2h 44min ago
...
CLI 命令说明
mktxp 命令
. MKTXP commands:
.. info Shows base MKTXP info
.. edit Open MKTXP configuration file in your editor of choice
.. print Displays selected metrics on the command line
.. export Starts collecting metrics for all enabled RouterOS configuration entries
.. show Shows MKTXP configuration entries on the command line
❯ mktxp -h
usage: MKTXP [-h] {info, edit, export, print, show, } ...
要了解有关单个命令的更多信息,只需运行它-h
: 例如,要了解有关以下内容的所有内容mktxp show
:
❯ mktxp show -h
usage: MKTXP show [-h]
[-en ['Sample-Router']]
[-cfg]
Displays MKTXP config router entries
optional arguments:
-h, --help show this help message and exit
-en, --entry-name ['Sample-Router']
Config entry name
-cfg, --config Shows MKTXP config files paths
安装开发版
- 克隆 repo,然后运行:
$ python setup.py develop
运行测试
- 数据库
- 通过以下方式运行:
$ python setup.py test