---
tags: 2020 IT 幫幫忙鐵人賽
---
# Day 9 開源機房 SNMP 協定監控軟體 LibreNMS 建置與使用
> 本日重點與方向 (TAG): SNMP、Simple Network Management Protocol、LibreNMS、MySQL、Apache2、開源網管系統、NMS、Network Management System
> 今天將會介紹使用 Bare Metal 進行 LibreNMS 設備監控,因為使用 SNMP 協定進行通訊,SNMP 設定部分請依照各家廠商配置(Ubuntu、Windows 也有軟體跟設定可用),SNMP V1、V2c、V3 在於 LibreNMS 皆可以設定,這邊就是介紹 LibreNMS 安裝與添加設備,還有稍微看一下納管的狀態,詳細的其他設定就不做贅述了,配合一下計算節點算是我們軟體業手上的小劍劍,節點一關機等同於你的武器掉在路上,準備等死或是客戶的電話塞爆你了(~~老闆海 K 揍爆你~~),安裝 LibreNMS 進行監控的話,我們就比較可以馬上知道小劍劍掉在路上,掉了馬上撿起來www,並透過告警功能與網頁監控方式,解決設備掉線而你在狀況外的問題了,此外 LibreNMS 感謝 台中軟體開源社群 節省哥 Jason 推薦使用!,詳細的配置與神奇功能與同步機制等等的,可以到 [節省哥的Blog](http://blog.jason.tools/) 參考 。
## 本次使用設備資訊
### Network Router
- 數量: 1
- 型號: DrayTek Vigor300B
### Network Switch
- 數量: 1
- 型號: D-Link 1210-28 (L2 Switch)
### LibreNMS Server
- Ubuntu: 16.04 / 18.04
- CPU: E3-1230_v3
- RAM: 16GB
- Disk: 120 GB (SSD)
- Network: 1Gbps
### LibreNMS SNMP Device
- AXIS M5014 Webcam
- IP: 10.0.0.5
- SNMP Community: public
## LibreNMS 安裝 (Apache2+PHP7.2)
> 參考網站:https://docs.librenms.org/Installation/Installation-Ubuntu-1804-Apache/
> LibreNMS 版本更新列表:https://docs.librenms.org/General/Changelog/
### 系統套件安裝
- Ubuntu 16.04 / 18.04 適用
#### 優先處理時間問題
- 時間不對會造成 LibreNMS 無法登陸跟套件安裝問題
```
timedatectl set-timezone Asia/Taipei
```
#### 處理 PHP 7.2 在 apt install 無法安裝問題
* PHP 7.0 部分對應 LibreNMS 1.48 ([已過時](https://https://docs.librenms.org/Installation/Installation-Ubuntu-1604-Apache/))
```
add-apt-repository ppa:ondrej/php
apt-get update
apt-get install php7.2
```
#### LibreNMS 相關服務套件安裝
- PHP 版本請用 7.1.3 版本以上
```
apt-get install software-properties-common
add-apt-repository universe
apt-get update
apt-get install curl apache2 composer fping git graphviz imagemagick libapache2-mod-php7.2 mariadb-client mariadb-server mtr-tiny nmap php7.2-cli php7.2-curl php7.2-gd php7.2-json php7.2-mbstring php7.2-mysql php7.2-snmp php7.2-xml php7.2-zip python-memcache python-mysqldb rrdtool snmp snmpd whois
```
### 安裝 Librenms
#### Github 上 LibreNMS 來源下載
```
cd /opt
git clone https://github.com/librenms/librenms.git
```
#### 新增使用者
```
useradd librenms -d /opt/librenms -M -r
usermod -a -G librenms www-data
```
#### LibreNMS 資料夾權限設定
```
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/
setfacl -R -m g::rwx /opt/librenms/rrd /opt/librenms/logs /opt/librenms/bootstrap/cache/ /opt/librenms/storage/
```
#### 安裝 PHP 相依項目
```
su - librenms
./scripts/composer_wrapper.php install --no-dev
exit
```
### 安裝設定 Mysql / MarialDB
#### 安裝 DataBase 服務
##### MariaDB
```
apt-get install mariadb-server mariadb-client
```
##### Mysql
```
apt-get install mysql-server mysql-client
```
- 設定 root 密碼

- 確認 root 密碼

#### 啟動 DataBase 服務與登入
- 預設帳號:root
- MariaDB 預設密碼:**None**
```
systemctl restart mysql
mysql -uroot -p
```
#### 新增資料庫與使用者給 LibereNMS 使用
- db-name:LibreNMS 存取的資料庫名稱
- user-name:LibreNMS 取用 Mysql 登入使用的使用者名稱
- user-passwd:LibreNMS 取用 Mysql 登入使用的使用者密碼
```
CREATE DATABASE ${db-name} CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE USER '${user-name}'@'localhost' IDENTIFIED BY '${user-passwd}';
GRANT ALL PRIVILEGES ON ${db-name}.* TO '${user-name}'@'localhost';
FLUSH PRIVILEGES;
exit
```
#### 修改 DataBase 配置
##### MariaDB
```
nano /etc/mysql/mariadb.conf.d/50-server.cnf
```
##### Mysql
```
nano /etc/mysql/mysql.conf.d/mysqld.cnf
```
##### [mysqld] 區塊添加以下設定
```
innodb_file_per_table=1
lower_case_table_names=0
```
#### 重新啟動 Mysql 吃設定檔
```
systemctl restart mysql
```
### Web Server 設定
#### PHP 時區校正
- 兩個檔案修改一樣的項目 => **timezone** 的部分
```
nano /etc/php/7.2/apache2/php.ini
nano /etc/php/7.2/cli/php.ini
```
- 修改前內容
```
# date.timezone
```
- 修改後內容
```
date.timezone = Asia/Taipei
```
#### Apache2 開啟服務模組
```
a2enmod php7.2
a2dismod mpm_event
a2enmod mpm_prefork
phpenmod mcrypt
```
### 建立 LibreNMS 的虛擬後端導向
```
nano /etc/apache2/sites-available/librenms.conf
```
- 填入資訊
```
<VirtualHost *:80>
DocumentRoot /opt/librenms/html/
ServerName librenms.example.com
AllowEncodedSlashes NoDecode
<Directory "/opt/librenms/html/">
Require all granted
AllowOverride All
Options FollowSymLinks MultiViews
</Directory>
</VirtualHost>
```
- 關閉 Apache2 初始80埠的導向頁面
```
a2dissite 000-default
```
- 開啟 Apache2 指定虛擬後端站點
```
a2ensite librenms.conf
```
- 開啟 Apache2 虛擬後台路由複寫模組
> 參考功能:https://medium.com/@awonwon/htaccess-with-rewrite-3dba066aff11
```
a2enmod rewrite
```
- 重啟 Apache2 更新所有取用模組設定
```
systemctl restart apache2
```
## LibreNMS 網頁初始化設定
- host-ip:安裝 LibreNMS 的主機 IP
- host-port:LibreNMS 服務對應埠號
```
http://${host-ip}:${host-port}/install.php
```
### 基礎套件相依檢查

### Mysql 設定與連線登入測試

### Mysql sql 資料表載入與設定

### LibreNMS 使用者新增


### LibreNMS 環境建置
#### config.php 已新增

#### config.php 不存在

- 新增 config.php
```
nano /opt/librenms/config.php
```
- config.php 檔案內容
- mysql-ip:Mysql 安裝的主機IP
- mysql-port:Mysql 使用的主機Port
- db-user:使用 librenmms 資料庫的 Mysql 使用者
- db-passwd:使用 librenmms 資料庫的 Mysql 使用者密碼
- db-name:使用 librenmms 資料庫名稱
```
<?php
## Have a look in defaults.inc.php for examples of settings you can set here. DO NOT EDIT defaults.inc.php!
### Database config
$config['db_host'] = '${mysql-ip}';
$config['db_port'] = '${mysql-port}';
$config['db_user'] = '${db-user}';
$config['db_pass'] = '${db-passwd}';
$config['db_name'] = '${db-name}';
$config['db_socket'] = '';
// This is the user LibreNMS will run as
//Please ensure this user is created and has the correct permissions to your install
$config['user'] = 'librenms';
### Locations - it is recommended to keep the default
#$config['install_dir'] = "/opt/librenms";
### This should *only* be set if you want to *force* a particular hostname/port
### It will prevent the web interface being usable form any other hostname
#$config['base_url'] = "http://librenms.company.com";
### Enable this to use rrdcached. Be sure rrd_dir is within the rrdcached dir
### and that your web server has permission to talk to rrdcached.
#$config['rrdcached'] = "unix:/var/run/rrdcached.sock";
### Default community
$config['snmp']['community'] = array("public");
### Authentication Model
$config['auth_mechanism'] = "mysql"; # default, other options: ldap, http-auth
#$config['http_auth_guest'] = "guest"; # remember to configure this user if you use http-auth
### List of RFC1918 networks to allow scanning-based discovery
#$config['nets'][] = "10.0.0.0/8";
#$config['nets'][] = "172.16.0.0/12";
#$config['nets'][] = "192.168.0.0/16";
# Update configuration
#$config['update_channel'] = 'release'; # uncomment to follow the monthly release channel
#$config['update'] = 0; # uncomment to completely disable updates
```
#### LibreNMS 安裝完成

## LibreNMS 網頁登入測試
### 登入頁面網址
- host-ip:安裝 LibreNMS 的主機 IP
- host-port:LibreNMS 服務對應埠號
```
http://${host-ip}:${host-port}/login
```

### 主頁面

----
## LibreNMS 網路設備管理
### 添加設備
> 需要知道設備的區域網路 IP, SNMP Community
- 設定路徑: Devices > Add Device

- SNMP

- SNMP 添加設備的設定頁面

- SNMP 添加完成狀態檢視

### 區網內自動添加設備
> 這邊會用官方的 snmp-scan.py 腳本進行區網設備抓取,SNMP Community 會依照先前的 confug.php 中的配置進行使用,需要修改的話就是往這邊去調整,因為自動添加的資料會塞在 RAM ,因此需要的 RAM 空間較大的主機,如果使用樹莓派開搞的,建議還是手動用網頁添加為主。
- 添加 /opt/librenms/config.php 的抓取 SNMP Community
```
~~
$config['snmp']['community'][] = "your-community";
~~
```
- 修改 /opt/librenms/config.php 的抓取網段
```
~~
### List of RFC1918 networks to allow scanning-based discovery
$config['nets'][] = "10.0.0.0/8";
$config['nets'][] = "172.16.0.0/12";
$config['nets'][] = "192.168.0.0/16";
~~
```
- 依照 /opt/librenms/config.php 的區網配置
```
python3 snmp-scan.py
```
- 自訂網段的抓取方式
```
python3 snmp-scan.py 10.0.0.0/24
```
- 自訂設備 IP 的抓取方式
```
python3 snmp-scan.py 10.0.0.165
```
### 檢視受監控的設備
> 這邊是觀看目前託管的設備,LibreNMS 會自動協助分類,有種類錯誤的下一小節進行設定,我們這邊只要能看到即可,另外抓取時間需要等 5 分鐘才會有穩定的資料,所以需要等待一陣子。
- 設定路境: Devices > All Devices > 選設備種類

### 修改設備種類異常問題
> LibreNMS 會有給予設備分類的作法,提供使用者快速找到您社設備,但是如果你有自己的設備種類,或是需要修改 LibreNMS 判斷錯誤的設備種類,這邊可以用此方法進行配置。
- 設定路徑: Devices > All Devices > 設備種類 > 您的設備 > 齒輪(設定) > edit

- 找到 Type 的欄位,用下拉選單修改

- 這邊下拉式選單提供多個種類去做修改,弄好之後按下 Save 即可

- 修改完後切到 Device > All Device,即可看到你修改的設備種類,設備也會在裡面。
