LibreNMS – 用于Linux的全功能网络监控工具

LibreNMS是一个开源、功能强大且功能丰富的自动发现PHP网络监控系统,使用SNMP协议。它支持广泛的操作系统,包括Linux、FreeBSD,以及包括Cisco、Juniper、Brocade、Foundry、HP等在内的网络设备。

LibreNMS特点:

  1. 它可以通过CDP、FDP、LLDP、OSPF、BGP、SNMP和ARP等协议自动发现整个网络。
  2. 具有移动友好的Web UI,可定制仪表板。
  3. 支持Unix代理。
  4. 支持水平扩展,以适应您的网络。
  5. 支持高度灵活且可定制的警报系统;通过电子邮件、irc、slack等发送通知。
  6. 支持用于管理、绘图和检索系统数据的API。
  7. 提供流量计费系统。
  8. 还支持Android和iOS应用程序,提供核心功能。
  9. 支持与NfSen、collectd、SmokePing、RANCID和Oxidized集成。
  10. 支持多种认证方法,如MySQL、HTTP、LDAP、Radius和Active Directory。
  11. 允许自动更新和许多其他功能。

在安装LibreNMS在Linux系统上之前,您可以尝试在线演示。

Demo URL: https://demo.librenms.org/
Username: demo
Password: demo

测试环境:

  1. Ubuntu 16.04 with LEMP Stack
  2. CentOS 7 with LEMP Stack

在本教程中,我们将学习如何在新安装的UbuntuCentOS Linux上安装LibreNMS网络监控工具(相同的指令也适用于DebianRHEL基于的发行版)。

注意:本文中的所有指令都应以root用户身份运行,如果不是,请使用sudo命令获取root用户权限。

第一步:安装所需软件包

1.首先,按照所示使用默认软件包管理器安装所有所需软件包。

在Ubuntu/Debian上

$ sudo apt install composer fping git graphviz imagemagick mariadb-client mariadb-server mtr-tiny nginx-full nmap php7.0-cli php7.0-curl php7.0-fpm php7.0-gd php7.0-mcrypt php7.0-mysql php7.0-snmp php7.0-xml php7.0-zip python-memcache python-mysqldb rrdtool snmp snmpd whois

在CentOS/RHEL上

# 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 MySQL-python net-snmp net-snmp-utils nginx nmap php72w php72w-cli php72w-common php72w-curl php72w-fpm php72w-gd php72w-mysql php72w-process php72w-snmp php72w-xml php72w-zip python-memcached rrdtool

2.一旦所有软件包安装完成,将启动并启用nginxphp-fpmmariadbsnmp服务以在启动时自动启动(这通常是Ubuntu的情况),否则,您可以运行以下命令来启动和启用它们。

------------ On Debian/Ubuntu ------------ 
$ sudo systemctl nginx start php7.0-fpm mysql snmp 
$ sudo systemctl enable nginx php7.0-fpm mysql snmp

------------ On CentOS/RHEL ------------ 
# systemctl nginx start php-fpm mariadb snmpd 
# systemctl enable nginx php-fpm mariadb snmpd

第二步:安装LibreNMS监控工具

3. 接下来,使用-M标志禁用用户主目录的创建,并使用-r启用系统账户的创建,使用useradd命令创建名为librenms的系统用户。然后将librenms用户添加到www-data组(在Ubuntu上)或nginx组(在CentOS上)。

------------ On Debian/Ubuntu ------------ 
$ sudo useradd librenms -d /opt/librenms -M -r
$ sudo usermod -a -G librenms www-data   

------------ On CentOS/RHEL ------------ 
# useradd librenms -d /opt/librenms -M -r
# usermod -a -G librenms nginx           

4. 然后按如下所示使用composer命令安装LibreNMS

------------ On Debian/Ubuntu ------------ 
$ cd /opt
$ sudo composer create-project --no-dev --keep-vcs librenms/librenms librenms dev-master

------------ On CentOS/RHEL ------------ 
# cd /opt
# composer create-project --no-dev --keep-vcs librenms/librenms librenms dev-master

步骤3:创建LibreNMS数据库

5. 在开始使用MariaDB服务器之前,您需要保护您的安装,运行二进制包中提供的安全脚本。它将要求您设置根密码,删除匿名用户,禁用远程根登录并删除测试数据库。

您可以通过发出以下命令来启动脚本,并用yes/y回答所有问题。

$ sudo mysql_secure_installation   [On Debian/Ubuntu]
# mysql_secure_installation        [On CentOS/RHEL]

6. 然后登录到MariaDB数据库,为LibreNMS创建一个数据库(在生产环境中请使用强大/安全的密码)。

$ sudo mysql -u root -p
MariaDB [(none)]> CREATE DATABASE librenms CHARACTER SET utf8 COLLATE utf8_unicode_ci;
MariaDB [(none)]> CREATE USER 'librenms'@'localhost' IDENTIFIED BY '=@!#@%$libre';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON librenms.* TO 'librenms'@'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> exit

7. 然后暂时禁用MySQL的严格模式(尚未添加与MySQL严格模式的兼容性)。

$ sudo vim /etc/mysql/mariadb.conf.d/50-server.cnf    [On Debian/Ubuntu]
# vi /etc/my.cnf        [On CentOS/RHEL]

请在[mysqld]部分中添加以下内容。

innodb_file_per_table=1
sql-mode=""
lower_case_table_names=0

然后重新启动数据库服务器以生效更改。

$ sudo systemctl restart mysql     [On Debian/Ubuntu]
# systemctl restart mariadb        [On CentOS/RHEL]

步骤4:配置和启动PHP-FPM

8. 接下来,在php.ini中将date.timezone设置为您当前的时区,例如“Africa/Kampala”,如下截图所示。

------------ On Debian/Ubuntu ------------ 
$ sudo vim /etc/php/7.0/fpm/php.ini
$ sudo vim /etc/php/7.0/cli/php.ini

------------ On CentOS/RHEL ------------ 
# vi /etc/php.ini
Set Time Zone in PHP File

9. 接下来在Ubuntu中启用mcrypt PHP模块并重新启动php-fpm,如下所示。

------------ On Debian/Ubuntu ------------ 
$ sudo phpenmod mcrypt
$ sudo systemctl restart php7.0-fpm

10.CentOS/RHEL中,您需要在php-fpm配置文件中进行以下更改。

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

进行以下更改。

;user = apache
user = nginx

group = apache   ; keep group as apache

;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php7.2-fpm.sock

listen.owner = nginx
listen.group = nginx
listen.mode = 0660

11. 按照以下方式重新启动php-fpm服务。

# systemctl restart php-fpm

第5步:为LibreNMS配置Nginx

12. 在这一步中,您需要为librenms配置一个Nginx服务器块,以便访问Web UI。按照下面的步骤创建一个.conf文件。

$ sudo vim /etc/nginx/conf.d/librenms.conf     [On Debian/Ubuntu]
# vi /etc/nginx/conf.d/librenms.conf           [On CentOS/RHEL]         

添加以下配置,根据需要编辑server_name

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/php7.0-fpm.sock;
 }
 location ~ /\.ht {
  deny all;
 }
}

13. 然后保存并退出文件。还要删除默认的服务器块配置并重新启动Nginx服务器。

------------ On Debian/Ubuntu ------------ 
$ sudo rm /etc/nginx/sites-enabled/default
$ sudo systemctl restart nginx

------------ On CentOS/RHEL ------------ 
# systemctl restart nginx

注意:在CentOS/RHEL上,如果这是您托管的唯一站点,则需要禁用default site部分。从/etc/nginx/nginx.conf文件中删除服务器部分。

14. 同样在CentOS/RHEL上,您需要安装SELinux策略工具并使用以下命令配置LibreNMS所需的上下文

------------ On CentOS/RHEL ------------ 
# 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/
# setsebool -P httpd_can_sendmail=1
# setsebool -P httpd_execmem 1

15. 通过创建文件http_fping.tt来允许fping的操作。

On CentOS/RHEL
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 };

16. 然后运行以下命令。

------------ On CentOS/RHEL ------------ 
# checkmodule -M -m -o http_fping.mod http_fping.tt
# semodule_package -o http_fping.pp -m http_fping.mod
# semodule -i http_fping.pp

17. 如果您在CentOS/RHEL上使用防火墙,请通过防火墙启用HTTP/HTTPS访问。

------------ On CentOS/RHEL ------------ 
# 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

第6步:为LibreNMS配置SNMPD

18. 现在使用示例snmp配置来创建您的配置文件,并打开以进行编辑,如下所示。

------------ On Debian/Ubuntu ------------ 
$ sudo cp /opt/librenms/snmpd.conf.example /etc/snmp/snmpd.conf
$ sudo vim /etc/snmp/snmpd.conf

------------ On CentOS/RHEL ------------ 
# cp /opt/librenms/snmpd.conf.example /etc/snmp/snmpd.conf
# vi /etc/snmp/snmpd.conf

查找字符串RANDOMSTRINGGOESHERE并将其更改为您自己的community string,如屏幕截图所示。

Set SNMP String

19. 接下来,在您的系统上下载一个shell脚本,该脚本有助于检测操作系统及其是否为Linux,如果是Linux,则会检测您正在使用的Linux发行版:

------------ On Debian/Ubuntu ------------ 
$ sudo curl -o /usr/bin/distro https://raw.githubusercontent.com/librenms/librenms-agent/master/snmp/distro
$ sudo chmod +x /usr/bin/distro
$ sudo systemctl restart snmpd

------------ On CentOS/RHEL ------------ 
# curl -o /usr/bin/distro https://raw.githubusercontent.com/librenms/librenms-agent/master/snmp/distro
# chmod +x /usr/bin/distro
# systemctl restart snmpd

第7步:创建Cron并配置Logrotate

20. 现在运行以下命令设置LibreNMS的cron作业。

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

21. 接下来,所有LibreNMS日志都记录在/opt/librenms/logs中,您需要配置这些日志以进行自动轮转,使用提供的logrotate配置文件,如下所示。

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

然后设置LibreNMS安装根目录和日志文件的适当权限。

------------ On Debian/Ubuntu ------------
$ sudo chown -R librenms:librenms  /opt/librenms
$ sudo setfacl -d -m g::rwx /opt/librenms/rrd /opt/librenms/logs
$ sudo setfacl -R -m g::rwx /opt/librenms/rrd /opt/librenms/logs

------------ On CentOS/RHEL ------------ 
# chown -R librenms:librenms /opt/librenms
# setfacl -d -m g::rwx /opt/librenms/rrd /opt/librenms/logs
# setfacl -R -m g::rwx /opt/librenms/rrd /opt/librenms/logs

第8步:访问LibreNMS Web安装程序

22. 接下来,使用以下URL访问Web安装程序,并按照屏幕上的说明操作。

http://librenms.tecmint.lan/install.php

要使此地址在本地计算机上工作,您需要在hosts文件(/etc/hosts)中设置本地DNS,以便进行本地域名解析或测试目的,然后再进行正式上线之前。

192.168.43.31 tecmint.lan
192.168.43.31 librenms.tecmint.lan
Setup Local DNS for Domain

23.您将看到安装欢迎页面,如下面的屏幕截图所示,点击下一阶段继续。

LibreNMS Web Installer

24.然后输入LibreNMS数据库的设置(数据库主机、端口、用户名和用户密码),然后点击下一阶段继续进行。

LibreNMS Database Settings

25.Web安装程序现在将开始导入MySQL数据库,这将需要一些时间。请注意,该过程将尝试在某些点上暂停,只需点击重试继续导入过程。

LibreNMS Database Importing

26.一旦数据库导入完成,您应该会看到消息“数据库已经是最新的!”,如下面的屏幕截图所示。然后点击转到添加用户继续进行。

LibreNMS Database Updated

27.接下来,添加一个LibreNMS用户,指定用户名、密码和电子邮件,然后单击添加用户以生效更改。

Add LibreNMS User

28.现在点击创建您的系统的LibreNMS配置,点击生成配置

Generate Librenms Config
LibreNMS Configuration

29.一旦配置生成,如前面的屏幕截图所示,复制并保存在您的安装根目录中,保存为/opt/librenms/config.php文件中。

# vi /opt/librenms/config.php
LibreNMS Configuration
<?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'] = 'localhost';
$config['db_port'] = '3306';
$config['db_user'] = 'librenms';
$config['db_pass'] = '=@!#@%$libre';
$config['db_name'] = 'librenms';
$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

30.保存并关闭文件。然后返回到网络安装程序,继续安装过程,点击完成安装

Finish LibreNMS Installation

31.现在您的LibreNMS安装已完成,您可以点击“验证您的安装并修复任何问题”,登录页面应该会出现。

Validate LibreNMS Install

32.接下来,输入您的用户凭据以访问验证页面。

LibreNMS Login Page

33.从安装验证过程中,LibreNMS发现了两个问题,一个是设备尚未添加(目前是一个警告),其次,我们尚未在手动添加的配置文件(/opt/librenms/config.php)上设置适当的权限,如下面的屏幕截图所示。

LibreNMS Permission Issues

现在运行以下命令以在配置文件上设置正确的权限。

$ sudo chown -R librenms:librenms /opt/librenms/config.php 

34.要添加设备,请访问:http://librenms.tecmint.lan/addhost。添加设备后,您可以转到主页并添加各种仪表板。

LibreNMS Dashboard

就是这样!您可以在LibreNMS文档中找到更多信息,包括安装和设置,网址为https://docs.librenms.org/

LibreNMS是一个功能齐全的网络监控系统,支持各种网络硬件。我们希望这是一个清晰的安装指南,如果您有任何问题,请通过下面的反馈表与我们联系。

Source:
https://www.tecmint.com/install-librenms-monitoring-on-ubuntu-centos/