文章

【Linux】開源監控軟體LibreNMS安裝

【Linux】開源監控軟體LibreNMS安裝

最近在公司管理了不少台主機,因此也開始研究了一些監控軟體,像是NagiosZabbix,目前比較讓我有興趣的是LibreNMS,包含了許多功能以及能夠管理多種不同的機器,甚至是思科的網路設備。

目錄

  • LibreNMS 介紹
  • LibreNMS 安裝
  • 安裝上遇到的問題
  • 結論

LibreNMS 介紹

很多時候我們需要透過監控軟體來幫助我們了解機器的狀況,甚至時能夠及時收到告警的訊息,以至於工程師可以立即處理目前機器的狀況,透過各種訊息來解決問題。

LibreNMS主要以 PHP、MariaDB、SNMP 技術為主進行資料搜集,並提供Web的操作介面、多種警報的發送機制、檢查服務的運行、網路裝置的連線狀態等等,可以針對你所需要的監控項目下去做設定。

LibreNMS 安裝

LibreNMS在官方有很清楚的安裝文件,今天我們要安裝的系統環境是CentOS7,WebServer會使用Apache。

安裝常用資源和功能,這邊提醒一下,MariaDB需要10.5以上的版本

1
2
yum install epel-release
yum install git cronie fping jwhois ImageMagick mtr MySQL-python net-snmp net-snmp-utils nmap python-memcached rrdtool policycoreutils-python httpd mariadb mariadb-server unzip python3 python3-pip

安裝PHP,PHP版本需要7.3以上

1
2
3
4
yum localinstall http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum install yum-utils
yum-config-manager --enable remi-php73
yum install mod_php php-cli php-common php-curl php-gd php-mbstring php-process php-snmp php-xml php-zip php-memcached php-mysqlnd

在系統上新增librenms的user

1
2
useradd librenms -d /opt/librenms -M -r
usermod -a -G librenms apache

下載LibreNMS

1
2
cd /opt
git clone https://github.com/librenms/librenms.git

設定權限

1
2
3
4
chown -R librenms:librenms /opt/librenms
chmod 770 /opt/librenms
setfacl -d -m g::rwx /opt/librenms/rrd /opt/librenms/logs /opt/librenms/bootstrap/cache/ /opt/librenms/storage/ /opt/librenms/cache
setfacl -R -m g::rwx /opt/librenms/rrd /opt/librenms/logs /opt/librenms/bootstrap/cache/ /opt/librenms/storage/ /opt/librenms/cache

安裝PHP dependencies

1
2
3
su - librenms
./scripts/composer_wrapper.php install --no-dev
exit

設定MySQL

1
2
systemctl enable --now mariadb
mysql -u root
1
2
3
4
5
CREATE DATABASE librenms CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'librenms'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON librenms.* TO 'librenms'@'localhost';
FLUSH PRIVILEGES;
exit
1
vi /etc/my.cnf.d/server.cnf
1
2
3
4
#server.cnf
[mysqld]
innodb_file_per_table=1
lower_case_table_names=0

設定PHP時區

1
vi /etc/php.ini
1
2
#php.ini
date.timezone = "Asia/Taipei"

設定Apache,新增Vhost的設定檔librenms.conf

1
vi /etc/httpd/conf.d/librenms.conf
1
2
3
4
5
6
7
8
9
10
11
     DocumentRoot /opt/librenms/html/
     ServerName librenms.example.com
     AllowEncodedSlashes NoDecode

     
          Require all granted
          AllowOverride All
          Options FollowSymLinks MultiViews
     

1
systemctl enable --now httpd

SELinux

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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/  
semanage fcontext -a -t httpd_sys_content_t '/opt/librenms/cache(/.*)?'  
semanage fcontext -a -t httpd_sys_rw_content_t '/opt/librenms/cache(/.*)?'  
restorecon -RFvv /var/www/opt/librenms/cache/  
setsebool -P httpd_can_sendmail=1  

audit2why  public
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

設定Cron Job

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

Logrotate

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

接下來就可以進入網頁一步步完成安裝囉!

安裝上遇到的問題

在Web Install結束後會有一些小問題需要解決,我遇到了一個Python3的問題(如下圖所示),解法如下

1
2
yum install gcc python3-devel
pip3 install -r /opt/librenms/requirements.txt

結論

安裝的過程非常的煩瑣,也會遇到一些些的小問題,像是MariaDB、PHP的版本等等,但裝好之後就可以開始新增機器來查看機器的狀況,LibreNMS上有許多的圖表繪製,也可以選擇時間軸來查看,很多異常的狀況都可以一眼就發現,真的滿方便的!希望未來可以多多善用並且加以成為資安的輔助工具!

本文章以 CC BY 4.0 授權