前言

本篇文章紀錄如何透過 docker-compose 快速架一個 LibreNMS 起來。這篇會出現的原因是為了要監控/debug IDC 0F iDRAC/ESXi 電口對接 Cisco Switch 的問題~

安裝 docker-compose

【standalone】Install the plugin manually

1
2
3
4
5
curl -SL https://github.com/docker/compose/releases/download/v2.6.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
chmod a+x /usr/local/bin/docker-compose

## if $PATH not include '/usr/local/bin/'
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

安裝 LibreNMS

官方針對 Docker 安裝的說明都放在 GitHub - librenms/docker 裡面了。

docker-compose.yml

docker/examples/compose/docker-compose.yml

1
2
mkdir -p ~/yml/librenms/
curl -SL https://raw.githubusercontent.com/librenms/docker/master/examples/compose/docker-compose.yml -o ~/yml/librenms/docker-compose.yml

由於我們沒有發 Email 的需求,所以選擇把 msmtpd 拿掉,depends_on 也要記得拿掉!!

librenms.env

docker/examples/compose/librenms.env

這個 env file 是要給 LibreNMS,從 docker-compose.yml 能夠得知。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
  librenms:
    image: librenms/librenms:latest
    container_name: librenms
    hostname: librenms
    ....
    env_file:
      - "./librenms.env"
    environment:
      - "TZ=${TZ}"
      - "PUID=${PUID}"
      - "PGID=${PGID}"
      - "DB_HOST=db"
      - "DB_NAME=${MYSQL_DATABASE}"
      - "DB_USER=${MYSQL_USER}"
      - "DB_PASSWORD=${MYSQL_PASSWORD}"
      - "DB_TIMEOUT=60"
      - "REDIS_HOST=redis"
      - "REDIS_PORT=6379"
      - "REDIS_DB=0"
    restart: always

我自己則是增加了 TZ=Taipei/Asia 時區, MEMORY_LIMIT=1024M 增加 php 能夠使用的 RAM 上限。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
TZ=Taipei/Asia
MEMORY_LIMIT=1024M
LISTEN_IPV6=false
UPLOAD_MAX_SIZE=16M
OPCACHE_MEM_SIZE=128
REAL_IP_FROM=0.0.0.0/32
REAL_IP_HEADER=X-Forwarded-For
LOG_IP_VAR=remote_addr
LIBRENMS_SNMP_COMMUNITY=librenmsdocker
MEMCACHED_HOST=memcached
MEMCACHED_PORT=11211
LIBRENMS_WEATHERMAP=false
LIBRENMS_WEATHERMAP_SCHEDULE=*/5 * * * *
REDIS_HOST=redis

更多環境變數說明可以參考 Environment variables

.env

這個檔案裡面必須放 MariaDB 的機敏資訊,如果有進版控記得把 .env 排除在外。

1
2
3
4
5
6
TZ=Asia/Taipei
PUID=1000
PGID=1000
MYSQL_DATABASE=librenms
MYSQL_USER=librenms
MYSQL_PASSWORD=librenms

最後檢查

docker-compose 預設會吃 .env,也可以使用 --env-file 來手動指定。Environment variables in Compose

檢查所有環境變數是否有被抓到

1
docker-compose config | less

史大抖!!

1
2
3
4
docker-compose up -d

## follow log
docker-compose logs -f

LibreNMS Fast up/down checking

Fast up/down checking

LibreNMS 預設是 SNMP polling 之前才會去 ping 目標確認存活,預設是 300s 五分鐘執行,如果想縮短偵測間隔的話可以調整 ping 就好,不必 1-Miniute polling 造成 SNMP 的負擔。

  1. 如果有使用 RRDCached 的話必須停用該服務。
  • This will flush all pending writes so that the rrdstep.php script can change the steps.
  1. 調整 config.php
1
2
3
4
5
docker exec -it librenms /bin/bash
bash-5.1# mkdir -p ./librenms/config
bash-5.1# vi ./librenms/config/fastping.php

$config['ping_rrd_step'] = 60;
  1. 更新 RRD 腳步大小 (step)
1
bash-5.1# ./scripts/rrdstep.php -h all
  1. 編輯 cron
1
bash-5.1# vi /opt/librenms/librenms.nonroot.cron
1
*    *    * * *   librenms    /opt/librenms/ping.php >> /dev/null 2>&1

cron config 的路徑是 crond --help 查到的,如果要確認是否有成功可以
20220819 更新: LibreNMS 預設不啟動 crond (因為 pidof crond 沒有東西),cron config 的路徑在 /opt/librenms/librenms.nonroot.cron
請參考這個 issue: 1 Minute Polling

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
## remember current pid
bash-5.1# pidof crond
53798

bash-5.1# crond -L /path/to/log/file
bash-5.1# tail -f /path/to/log/file

## kill the crond which will log to file
bash-5.1# pidof crond
53798 53001
bash-5.1# kill 53001
  1. 如果有關閉 RRDCache 記得開回來。

Add devices using script

使用 LibreNMS 內建的 script 快速新增 devices

  1. 使用 snmp-scan.py 新增裝置
1
2
usage: snmp-scan.py [-h] [-t THREADS] [-g GROUP] [-l] [-v] [--ping-fallback] [--ping-only] [-P] [network ...]
./snmp-scan.py <network CIDR>
  1. 掃描 device 身上有哪些可用的「資料點」
1
./discovery.php -h all
  1. 對 device 抓資料
1
./poller.php -h all

debug

MariaDB 會 bind mount 到 ./db ; LibreNMS 會 bind mount 到 ./librenms如果不是一次就設定成功,記得把這兩個目錄刪除!!
這次我就發生沒有把這兩個目錄刪除,造成一直出現 [Warning] Access denied for user 'librenms'@'<private ip>' (using password: YES) 的智障情形 XD
查到這篇才恍然大悟 librenms and dispatcher won’t connect to librenms_db #80

Automatic polling on docker images not working #217

Automatic polling on docker images not working #217

恩… 又是我踩到地雷 XD,不過這個 issue 是 2021/08 開起的,至今已經一年居然還在 @@

症狀: 使用 docker-compose 佈建 LibreNMS (官方 YAML file) polling 不會自動定期執行 (也因此沒有圖),手動 ./poller.php <network CIDR>/<-h host> 可以正常執行。

處方籤: docker-compose logs -f 顯示 librenms-dispatcher exit with code 1REDIS_HOST 必須給定值。在 librenms.env 加上 REDIS_HOST=redis 即可解決。

1
2
Hi, make sure you have `REDIS_HOST=redis` in your `librenms.env` file, if you are using docker-compose.
I have the same problem until `docker-compose logs -f` shows `librenms-dispatcher exit with code 1` and `REDIS_HOST` env variable needed over and over again.

iDRAC snmp timeout 問題

由於 LibreNMS 預設使用 v2c 抓取,或許是我們的 iDRAC 版本過舊導致在 discovery 過程中出現斷線問題,必須 手動將 SNMP 版本改成 v1 來避免這個問題。

1
2
3
4
5
SNMP['/usr/bin/snmpbulkwalk' '-v2c' '-c' 'COMMUNITY' '-OteQUs' '-Ih' '-m' 'DELL-RAC-MIB:IDRAC-MIB-SMIv2' '-M' '/opt/librenms/mibs:/opt/librenms/mibs/drac:/opt/librenms/mibs/dell' 'udp:HOSTNAME:161' 'drsRedCurrStatus']

Exitcode: 1  

Timeout: No Response from udp:192.168.xxx.11:161