网站搜索

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. 允许自动更新和许多其他功能。

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

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

测试环境:

  1. 带有 LEMP 堆栈的 Ubuntu 16.04
  2. 带有 LEMP 堆栈的 CentOS 7

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

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

第 1 步:安装所需的软件包

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

第2步:安装LibreNMS监控工具

3.接下来,使用useradd命令创建一个名为librenms的系统用户;其中 -M 标志禁用创建用户主目录,-r 启用创建系统帐户。然后将 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 服务器之前,您需要确保安装安全,运行二进制包中提供的安全脚本。它会要求您设置 root 密码、删除匿名用户、禁用 root 远程登录并删除测试数据库。

您可以通过发出以下命令来启动脚本并使用 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设置为您当前的时区,例如“非洲/坎帕拉” ”,如下图所示。

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

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 上,您需要禁用默认站点部分(如果这是您托管的唯一网站。从 /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

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 并将其更改为您自己的社区字符串,如屏幕截图所示。

19. 接下来,在您的系统上下载一个 shell 脚本,这有助于检测哪个操作系统,如果是 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

为了使该地址在本地计算机上工作,您需要使用主机文件 (/etc/hosts) 设置本地 DNS,以便在上线之前进行本地域解析或测试。

192.168.43.31 tecmint.lan
192.168.43.31 librenms.tecmint.lan

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

24. 然后输入 LibreNMS 数据库的设置(数据库主机、端口、用户名和用户密码)并单击下一步继续。

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

26.数据库导入完成后,您应该看到消息“数据库已更新!”,如下面的屏幕截图所示。然后单击转到添加用户继续。

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

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

29. 生成配置后,如上一个屏幕截图所示,将其复制并保存在安装的根目录中名为 /opt/librenms/config.php 的文件中

vi /opt/librenms/config.php
<?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. 保存并关闭文件。然后返回到 Web 安装程序,单击完成安装继续安装过程。

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

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

33. 从安装验证过程来看,LibreNMS发现了两个问题,一是设备没有添加(暂时警告),二是我们发现没有对手动添加的配置文件(/opt/librenms/config.php)设置适当的权限,如下面的屏幕截图所示。

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

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

34. 要添加设备,请访问:http://librenms.howtoing.lan/addhost。添加设备后,您可以进入首页添加各种仪表板。

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

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