CentOS7 / Ubuntu 20.04 安裝 LibreNMS 程序

  • CentOS Linux release 7.5.1804 (Core)
  • Ubuntu 20.04 LTS

  • CentOS7 安裝程序
  • Ubuntu 20.04 安裝程序
載入中 ...
  1. 安裝需要的套件

    yum install epel-release
    rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
    yum install composer cronie fping git ImageMagick jwhois mariadb mariadb-server mtr python36 MySQL-python net-snmp net-snmp-utils nginx nmap php72w php72w-cli php72w-common php72w-curl php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-process php72w-snmp php72w-xml php72w-zip python-memcached rrdtool

  2. 建立 librenms 用戶

    useradd librenms -d /opt/librenms -M -r
    usermod -a -G librenms nginx

  3. 下載 LibreNMS

    cd /opt
    composer create-project --no-dev --keep-vcs librenms/librenms librenms dev-master

  4. 出現異常與警告訊息, 重新開機後再執行

    cd /opt/librenms
    ./scripts/composer_wrapper.php install --no-dev

  5. 建立 DB Server

    systemctl start mariadb
    mysql -u root

    password 要依照自己需要設定

    CREATE DATABASE librenms CHARACTER SET utf8 COLLATE utf8_unicode_ci;
    CREATE USER 'librenms'@'localhost' IDENTIFIED BY '**password**';
    GRANT ALL PRIVILEGES ON librenms.* TO 'librenms'@'localhost';
    vi /etc/my.cnf
    # instructions in http://fedoraproject.org/wiki/Systemd
    systemctl enable mariadb
    systemctl restart mariadb
  6. 建立 Web Server

    vi /etc/php-fpm.d/www.conf

    ;user = apache
    user = nginx
    ; RPM: Keep a group allowed to write in log dir.
    group = apache
    ; The address on which to accept FastCGI requests.
    ; Valid syntaxes are:
    ;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
    ;                            a specific port;
    ;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
    ;                            a specific port;
    ;   'port'                 - to listen on a TCP socket to all addresses
    ;                            (IPv6 and IPv4-mapped) on a specific port;
    ;   '/path/to/unix/socket' - to listen on a unix socket.
    ; Note: This value is mandatory.
    ;listen =
    listen = /var/run/php-fpm/php7.2-fpm.sock
    ; Set listen(2) backlog.
    ; Default Value: 511 (-1 on FreeBSD and OpenBSD)
    ;listen.backlog = 511
    ; Set permissions for unix socket, if one is used. In Linux, read/write
    ; permissions must be set in order to allow connections from a web server. Many
    ; BSD-derived systems allow connections regardless of permissions.
    ; Default Values: user and group are set as the running user
    ;                 mode is set to 0660
    ;listen.owner = nobody
    ;listen.group = nobody
    ;listen.mode = 0660
    listen.owner = nginx
    listen.group = nginx
    listen.mode = 0660
    ; When POSIX Access Control Lists are supported you can set them using
    ; these options, value is a comma separated list of user/group names.
    systemctl enable php-fpm
    systemctl restart php-fpm
  7. 設定 nginx

    vi /etc/nginx/conf.d/librenms.conf

    server {
     listen      80;
     server_name librenms.example.com;
     root        /opt/librenms/html;
     index       index.php;
     charset utf-8;
     gzip on;
     gzip_types text/css application/javascript text/javascript application/x-javascript image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
     location / {
      try_files $uri $uri/ /index.php?$query_string;
     location /api/v0 {
      try_files $uri $uri/ /api_v0.php?$query_string;
     location ~ \.php {
      include fastcgi.conf;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/var/run/php-fpm/php7.2-fpm.sock;
     location ~ /\.ht {
      deny all;
    systemctl enable nginx
    systemctl restart nginx
  8. 建立 SELinux 與 Allow fping

    yum install policycoreutils-python
    semanage fcontext -a -t httpd_sys_content_t '/opt/librenms/logs(/.*)?'
    semanage fcontext -a -t httpd_sys_rw_content_t '/opt/librenms/logs(/.*)?'
    restorecon -RFvv /opt/librenms/logs/
    semanage fcontext -a -t httpd_sys_content_t '/opt/librenms/rrd(/.*)?'
    semanage fcontext -a -t httpd_sys_rw_content_t '/opt/librenms/rrd(/.*)?'
    restorecon -RFvv /opt/librenms/rrd/
    semanage fcontext -a -t httpd_sys_content_t '/opt/librenms/storage(/.*)?'
    semanage fcontext -a -t httpd_sys_rw_content_t '/opt/librenms/storage(/.*)?'
    restorecon -RFvv /opt/librenms/storage/
    semanage fcontext -a -t httpd_sys_content_t '/opt/librenms/bootstrap/cache(/.*)?'
    semanage fcontext -a -t httpd_sys_rw_content_t '/opt/librenms/bootstrap/cache(/.*)?'
    restorecon -RFvv /opt/librenms/bootstrap/cache/
    setsebool -P httpd_can_sendmail=1
    setsebool -P httpd_execmem 1
    vi /tmp/http_fping.tt

    module http_fping 1.0;
    require {
    type httpd_t;
    class capability net_raw;
    class rawip_socket { getopt create setopt write read };
    #============= httpd_t ==============
    allow httpd_t self:capability net_raw;
    allow httpd_t self:rawip_socket { getopt create setopt write read };
    checkmodule -M -m -o http_fping.mod /tmp/http_fping.tt
    semodule_package -o /tmp/http_fping.pp -m http_fping.mod
    semodule -i /tmp/http_fping.pp
  9. 設定 firewall

    firewall-cmd --zone public --add-service http
    firewall-cmd --permanent --zone public --add-service http
    firewall-cmd --zone public --add-service https
    firewall-cmd --permanent --zone public --add-service https

  10. 設定 snmpd

    cp /opt/librenms/snmpd.conf.example /etc/snmp/snmpd.conf
    vi /etc/snmp/snmpd.conf

    將 RANDOMSTRINGGOESHERE 改成你要的 community 字串

    # Change RANDOMSTRINGGOESHERE to your preferred SNMP community string
    #com2sec readonly  default         RANDOMSTRINGGOESHERE
    com2sec readonly  default         **community_string**
    #syslocation Rack, Room, Building, City, Country [GPSX,Y]
    syslocation Taipei, Taiwan
    #syscontact Your Name <[email protected]>
    syscontact Jonathan Tsai <[email protected]>
    curl -o /usr/bin/distro https://raw.githubusercontent.com/librenms/librenms-agent/master/snmp/distro
    chmod +x /usr/bin/distro
    systemctl enable snmpd
    systemctl restart snmpd
  11. 定義 cron table

    cp /opt/librenms/librenms.nonroot.cron /etc/cron.d/librenms

  12. 設定定時清 log

    cp /opt/librenms/misc/librenms.logrotate /etc/logrotate.d/librenms

  13. 設定權限

    chown -R librenms:librenms /opt/librenms
    setfacl -d -m g::rwx /opt/librenms/rrd /opt/librenms/logs /opt/librenms/bootstrap/cache/ /opt/librenms/storage/
    setfacl -R -m g::rwx /opt/librenms/rrd /opt/librenms/logs /opt/librenms/bootstrap/cache/ /opt/librenms/storage/

  14. 至網頁介面進行安裝 網址 Exp.
    1. 如果這時候無法出現安裝畫面, 表示 nginx default 設定沒有移除

      vi /etc/nginx/nginx.conf

          include             /etc/nginx/mime.types;
          default_type        application/octet-stream;
          # Load modular configuration files from the /etc/nginx/conf.d directory.
          # See http://nginx.org/en/docs/ngx_core_module.html#include
          # for more information.
          include /etc/nginx/conf.d/*.conf;
      #    server {
      #        listen       80 default_server;
      #        listen       [::]:80 default_server;
      #        server_name  _;
      #        root         /usr/share/nginx/html;
      #        # Load configuration files for the default server block.
      #        include /etc/nginx/default.d/*.conf;
      #        location / {
      #        }
      #        error_page 404 /404.html;
      #            location = /40x.html {
      #        }
      #        error_page 500 502 503 504 /50x.html;
      #            location = /50x.html {
      #        }
      #    }
      # Settings for a TLS enabled server.
      #    server {
      systemctl restart nginx
    2. 依照網頁出現的 6 個 Stage 執行
    3. Stage 5 將產生的設定參數資料手動寫入 /opt/librenms/config.php

      vi /opt/librenms/config.php

    4. 調整參數檔案權限

      chown librenms:librenms /opt/librenms/config.php

  15. 再來應該就可以透過網頁依照上一步驟建立的帳號登入

  • 假設區網為 ( -
  • SNMP 都是設定為 v1 / v2c , Community 設定為 public 或 ichiayi_snmp
  1. 先安裝 python-ipaddress 模組 yum install python-ipaddress
  2. 先手動加入一個設備進行監管 Exp.
  3. vi /opt/librenms/config.php
    # Auto-Discovery
    // v1 or v2c
    $config['snmp']['community'][] = "public";
    $config['snmp']['community'][] = "ichiayi_snmp";
    $config['nets'][] = '';
    $config['autodiscovery']['nets-exclude'][] = '';
    $config['discovery_by_ip'] = true;
  4. 第一次可以手動執行一次

    cd /opt/librenms


    [[email protected] librenms]# ./snmp-scan.py
    Scanning IPs:
    Scanned 253 IPs: 0 known devices, added 10 devices, failed to add 5 devices, 1 ips excluded by config
    Runtime: 104.31 seconds

  5. 設定 default_poller_group = 1

    lnms config:set default_poller_group 1

  6. 之後在區域網路內的新設備如果有啟用 snmp v1 / v2c communty 符合設定的值, 可以在五分鐘內被自動加入
  • 前一陣子發現設定都正確, 使用 snmp-scan.py 確實可以自動加入, 但卻無法如預期由該機制將新設備自動加入, 找了一些文章後還是無法真正解決問題, 目前我的 workaround 方法是將 snmp-scan.py 直接加入 cron 來處理
  • 設定 E-Mail 通知
    • Global Settings → Alerting Settings
  • 設定告警通知規則
  • Device Dependencies 可以減少不必要的告警通知, 例如 Firewall 故障, Firewall 內所有的主機當然無法使用, 此時可以設定裡面所有的主機的 Parents 是 Firewall 減少不必要的告警通知
  • Exp. 是 Firewall, 是內部 Wifi 設備, 外部監控的 LibreNMS 可以設定 有個 Parents 是 可點下參考相關設定畫面

  • LibreNMS Agent 的安裝方式
  • 安裝 Apache Agent 的方式
  • 加裝 Nagios Plugin 增加監控能力
  • 安裝 BIND(Named) Agent 的方式
  • 加裝 Check_MK 增加監控能力
  • nagios plugins - check_mssql_health
  • 安裝 PVE (Proxmox Virtual Environment) Agent 的方式
  • 安裝 fail2ban Agent 的方式
  • 安裝 MySQL Agent 的方式
  • 安裝 Nginx Agent 的方式
  • 安裝 PHP-FPM Agent 的方式
  • 安裝 postfix Agent 的方式
  • 安裝 Redis Agent 的方式
  • 安裝 SMART Agent 的方式
  • 安裝 ZFS Agent 的方式
載入中 ...

LibreNMS Agent 的安裝方式

  • CentOS 7 + systemd
  • 這 Agent Listen TCP-Port 6556
  • 需要驗證 LibreNMS 可以正確 TCP-Port 6556 連上 Agent 主機
  • 複製 librenms-agent 儲存庫
    cd /opt/
    git clone https://github.com/librenms/librenms-agent.git
    cd librenms-agent
    cp check_mk_agent /usr/bin/check_mk_agent
    chmod +x /usr/bin/check_mk_agent
    cp [email protected] check_mk.socket /etc/systemd/system
    mkdir -p /usr/lib/check_mk_agent/plugins /usr/lib/check_mk_agent/local
    cp agent-local/* /usr/lib/check_mk_agent/local/
    chmod a+x /usr/lib/check_mk_agent/local/*
    systemctl enable check_mk.socket && systemctl start check_mk.socket

  • 先透過執行 daily.sh 看錯誤訊息

    su - librenms

  • 看錯誤訊息進行處理, Exp. git 檔案被更改, 就移除這些檔案後, 在執行 daily.sh
  • 如果無法解決, 可以透過 validate.php 來檢查


    [FAIL]  Some folders have incorrect file permissions, this may cause issues.
            sudo chown -R librenms:librenms /opt/librenms
            sudo setfacl -d -m g::rwx /opt/librenms/rrd /opt/librenms/logs /opt/libr                                              enms/bootstrap/cache/ /opt/librenms/storage/
            sudo chmod -R ug=rwX /opt/librenms/rrd /opt/librenms/logs /opt/librenms/                                              bootstrap/cache/ /opt/librenms/storage/
             /opt/librenms/storage/framework/views/cdca770a337e07f2364ec770e7b408621                                              95170d7.php


    su - root
    chown -R librenms:librenms /opt/librenms
    setfacl -d -m g::rwx /opt/librenms/rrd /opt/librenms/logs /opt/librenms/bootstrap/cache/ /opt/librenms/storage/
    chmod -R ug=rwX /opt/librenms/rrd /opt/librenms/logs /opt/librenms/bootstrap/cache/ /opt/librenms/storage/
  • 其他實用的修復 script:
    • 修復 git 目錄檔案 ./scripts/github-remove
  • 自動更新失敗, 出現很多套件依存議題, Exp,. Carbon 1 is deprecated, see how to migrate to Carbon 2
  • 可重新完整安裝的程序如下:

    su - librenms
    ./scripts/github-remove -d -v
    git pull

PHP 7.1 and Python 2 no longer supported 
LibreNMS will soon require Python 3.5+ and PHP 7.2.5+. For more details check https://t.libren.ms/uo84c

2020-05-11 00:00:00 | Source: http://www.librenms.org/notifications.rss
  • 因為資料庫變大, 改用 Percona XtraDB Cluster 來取代本機 MySQL/MariaDB, 修改 /opt/librenms/config.php 內的 ### Database config 卻完全沒用, 看了 LibreNMS 文件才發現已經變成要改 /opt/librenms/.env 才對
  • 以下是 /opt/librenms/.env 大概的內容
  • 原本 mariadb 的設定要關閉

    systemctl stop mariadb
    systemctl disable mariadb

  • 2020/11/04 收到訊息會 2020/11 起, LibreNMS 必須使用 php 7.3 以上(建議 7.4)版本才會自動更新
  • 先移除原本 PHP 7.2

    systemctl stop php-fpm
    yum remove php72w*
    mv /etc/yum.repos.d/webtatic* /tmp/ 

  • 將安裝 REMI 的 PHP 7.4 來因應

    yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
    vi /etc/yum.repos.d/remi-php74.repo

    yum install composer php php-cli php-common php-curl php-fpm php-gd php-mbstring php-mysqlnd php-process php-snmp php-xml php-zip
  • 因為這方式是重新安裝 php 與相關套件, 所以 php.ini 與 nginx 的一些設定必須再次執行
  • 修改 php.ini

    vi /etc/php.ini

    ; Defines the default timezone used by the date functions
    ; http://php.net/date.timezone
    date.timezone = Asia/Taipei
  • 修改 php-fpm 設定

    vi /etc/php-fpm.d/www.conf

    ;user = apache
    user = nginx
    ;listen.owner = nobody
    ;listen.group = nobody
    ;listen.mode = 0660
    listen.owner = nginx
    listen.group = nginx
    listen.mode = 0660
    systemctl enable php-fpm
    systemctl restart php-fpm
  • 修改 nginx 設定

    vi /etc/nginx/conf.d/librenms.conf

     location ~ \.php {
      include fastcgi.conf;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      #fastcgi_pass unix:/var/run/php-fpm/php7.2-fpm.sock;
    systemctl restart nginx
  • 設定 ping 的執行權限

    setcap cap_net_raw+ep /usr/sbin/fping
    setcap cap_net_raw+ep /usr/sbin/fping6

  • 驗證一下是否可以正常運作

    su - librenms

    可能會出現一些 Call to undefined method … 訊息, 不過第二次執行 ./daily.sh 應該就不會再出現, 執行畫面如下

    [[email protected] ~]# su - librenms
    -bash-4.2$ ./validate.php 
    Component | Version
    --------- | -------
    LibreNMS  | 1.69-5-g240edd2
    DB Schema | 2020_07_27_00522_alter_devices_snmp_algo_columns (188)
    PHP       | 7.4.12
    Python    | 3.6.8
    MySQL     | 5.6.42-84.2-56-log
    RRDTool   | 1.4.8
    SNMP      | NET-SNMP 5.7.2
    OpenSSL   | 
    [OK]    Composer Version: 1.10.17
    [OK]    Dependencies up-to-date.
    [OK]    Database connection successful
    [OK]    Database schema correct
    -bash-4.2$ ./daily.sh
    Supported PHP and Python version, switched back to master branch. OK
    Updating to latest codebase                        OK
    In AppServiceProvider.php line 43:
      Call to undefined method Illuminate\Pagination\Paginator::useBootstrap()  
    In AppServiceProvider.php line 43:
      Call to undefined method Illuminate\Pagination\Paginator::useBootstrap()  
    Updating Composer packages                         OK
    Updated from bc02ab3 to 240edd2                    OK
    Updating SQL-Schema                                OK
    Updating submodules                                OK
    Cleaning up DB                                     OK
    Fetching notifications                             OK
    Caching PeeringDB data                             OK
    -bash-4.2$ ./daily.sh
    Updating to latest codebase                        OK
    Updating Composer packages                         OK
    Updating SQL-Schema                                OK
    Updating submodules                                OK
    Cleaning up DB                                     OK
    Fetching notifications                             OK
    Caching PeeringDB data                             OK

  • 2021/06/13 發現 Librenms 無法運作, 才發現 2021/03 起只支援 MySQL version 5.7.7 以上的版本, 所以目前使用的 Percona XtraDB Cluster 內的 5.6.42 就被迫要升級改用 MariaDB 10.5
  • 將舊資料匯出, 如果忘記資料庫連線資訊, 可以看一下 .env

    su - librenms
    head .env

    Exp. db_host: , db_user:librenms , db_pass:Password , db_name:librenms

    mysqldump -u librenms -p librenms -h> /tmp/librenms.sql 

  • 在新資料庫建立 LibreNMS 的使用者與資料庫

    mysql -u root -p
    create database librenms;
    create user 'librenms'@'%' identified by '**Password**';
    grant all on librenms.* to 'librenms'@'%';
    flush privileges;

  • 將舊資料匯入新資料庫 Exp.

    cat /tmp/librenms.sql | mysql -u librenms -p librenms -h

  • 修改 .env 內 DB 的連線設定

    su - librenms
    vi .env

  • 出現錯誤訊息如下

    In Connection.php line 678:
      SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'port_groups' already exists (SQL: create table `port_groups` (`id` int unsigned not null auto_increment primary key, `na
      me` varchar(255) not null, `desc` varchar(255) null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

    • 解決方式

      mysql -u librenms -p librenms
      drop table port_groups;

  • 再次執行 ./lnms migrate 出現找不到 librenms.port_group_port 這 table

    In Connection.php line 678:
      SQLSTATE[42S02]: Base table or view not found: 1146 Table 'librenms.port_group_port' doesn't exist (SQL: alter table `port_group_port` add constraint `port_group_port_port_group_id_for
      eign` foreign key (`port_group_id`) references `port_groups` (`id`) on delete CASCADE)

    • 解決方式 :

      mysql -u librenms -p librenms
      CREATE TABLE port_group_port (
      port_group_id int(10) unsigned NOT NULL,
      port_id int(10) unsigned NOT NULL,
      PRIMARY KEY (port_group_id,port_id),
      KEY port_group_port_port_group_id_index (port_group_id),
      KEY port_group_port_port_id_index (port_id),
      CONSTRAINT port_group_port_port_group_id_foreign FOREIGN KEY (port_group_id) REFERENCES port_groups (id) ON DELETE CASCADE,
      CONSTRAINT port_group_port_port_id_foreign FOREIGN KEY (port_id) REFERENCES ports (port_id) ON DELETE CASCADE
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

  • 再次執行 ./lnms migrate 就可以修正成功
  • 再執行 ./validate.php 看有其他那些問題, 依照說明的語法進行修正 Exp.

    su - root
    setcap cap_net_raw+ep /usr/sbin/fping6
    sudo ln -s /opt/librenms/lnms /usr/bin/lnms
    sudo cp /opt/librenms/misc/lnms-completion.bash /etc/bash_completion.d/
    sudo chown -R librenms:librenms /opt/librenms
    sudo setfacl -d -m g::rwx /opt/librenms/rrd /opt/librenms/logs /opt/librenms/bootstrap/cache/ /opt/librenms/storage/
    sudo chmod -R ug=rwX /opt/librenms/rrd /opt/librenms/logs /opt/librenms/bootstrap/cache/ /opt/librenms/storage/

  • tech/librenms.txt
  • 上一次變更: 2021/06/24 08:36
  • jonathan