验证 LibreNMS 安装
安装和配置 LibreNMS 网络监控软件后,我们可以执行 validate.php 脚本来检查其中的任何问题。
# su - librenms Last login: Sun Jan 31 07:07:41 UTC 2021 on pts/0 $ ./validate.php ==================================== Component | Version --------- | ------ LibreNMS | 1.70.1-70-g17f5a3f23 DB Schema | 2020_11_02_164331_add_powerstate_enum_to_vminfo (191) PHP | 7.3.20 Python | 3.6.8 MySQL | 10.3.27-MariaDB RRDTool | 1.7.0 SNMP | NET-SNMP 5.8 ==================================== [OK] Composer Version: 2.0.9 [OK] Dependencies up-to-date. [WARN] You have no devices. [FIX]: Consider adding a device such as localhost: /addhost [OK] Database connection successful [OK] Database schema correct
为 LNMS 命令配置 Bash 完成
我们可以为 lnms 命令启用 bash 完成,就像使用其他 Linux 命令一样。
执行以下命令为 lnms 命令配置 bash 完成。
# ln -s /opt/librenms/lnms /usr/bin/lnms # cp /opt/librenms/misc/lnms-completion.bash /etc/bash_completion.d/
在 CentOS 8 服务器上安装 Composer
在 Linux 服务器上安装最新稳定版本的 Composer。
# wget https://getcomposer.org/composer-stable.phar
重命名下载的文件并授予其执行权限。
# mv composer-stable.phar /usr/bin/composer # chmod +x /usr/bin/composer
在本文中,我们将学习如何在 CentOS/RHEL 8 上安装此网络监控工具。
设置 LibreNMS Cron 作业
LibreNMS 网络监控软件还提供了一个 crontab 文件。
只需将其复制到 crontab 目录中即可设置所需的 cron 作业。
# cp /opt/librenms/librenms.nonroot.cron /etc/cron.d/librenms
配置 MySQL 数据库服务器
在 vim 编辑器中打开 MySQL 配置文件。
# vi /etc/my.cnf.d/mariadb-server.cnf
在 [mysqld] 部分下添加以下指令。
innodb_file_per_table=1 lower_case_table_names=0
启用并启动 MySQL 数据库服务。
# systemctl enable --now mariadb.service Created symlink /etc/systemd/system/mysql.service -> /usr/lib/systemd/system/mariadb.service. Created symlink /etc/systemd/system/mysqld.service -> /usr/lib/systemd/system/mariadb.service. Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service -> /usr/lib/systemd/system/mariadb.service.
通过在 Linux bash 提示符下执行以下命令,首次配置 MySQL 数据库服务器。
# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] Y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] Y ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] Y ... Success! By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] Y - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] Y ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!
以 root 用户身份登录 MySQL shell。
# mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 16 Server version: 10.3.27-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
为 LibreNMS 软件创建 MySQL 数据库。
该数据库将被网络监控应用程序用作后端存储库。
MariaDB [(none)]> CREATE DATABASE librenms CHARACTER SET utf8 COLLATE utf8_unicode_ci; Query OK, 1 row affected (0.001 sec)
创建一个 MySQL 数据库用户。
MariaDB [(none)]> CREATE USER 'librenms'@'localhost' IDENTIFIED BY 'Str0ngPa55w0rd'; Query OK, 0 rows affected (0.001 sec)
向 librenms 用户授予对 librenms 数据库的完整权限。
MariaDB [(none)]> GRANT ALL PRIVILEGES ON librenms.* TO 'librenms'@'localhost'; Query OK, 0 rows affected (0.000 sec)
重新加载权限表并退出 MySQL shell。
MariaDB [(none)]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.000 sec) MariaDB [(none)]> exit Bye
在 CentOS 8 上安装 EPEL yum 软件库
某些必需的软件包在默认 yum 存储库中不可用。
因此,我们必须在安装 LibreNMS 软件之前添加 EPEL(Extra Packages for Enterprise Linux)yum 存储库。
# dnf -y install epel-release
下载 LibreNMS 网络监控工具
现在,使用 git 命令克隆 LibreNMS GitHub 存储库。
# cd /opt # git clone https://github.com/librenms/librenms.git Cloning into 'librenms'...
通过在 Linux Bash 提示符下执行以下命令来调整克隆目录的权限。
# chown -R librenms:librenms /opt/librenms # chmod 771 /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/
以 librenms 用户身份连接并根据 LibreNMS 网络监控软件的要求安装 PHP 库。
# su - librenms Last login: Sun Jan 31 11:16:07 PKT 2021 on pts/0 $ ./scripts/composer_wrapper.php install --no-dev ... Discovered Package: darkghosthunter/larapoke Discovered Package: fideloper/proxy Discovered Package: fruitcake/laravel-cors Discovered Package: laravel/tinker Discovered Package: laravel/ui Discovered Package: librenms/laravel-vue-i18n-generator Discovered Package: nesbot/carbon Discovered Package: oriceon/toastr-5-laravel Discovered Package: tightenco/ziggy Discovered Package: wpb/string-blade-compiler Package manifest generated successfully. 55 packages you are using are looking for funding. Use the `composer fund` command to find out more! > LibreNMS\ComposerHelper::postInstall setfacl -R -m g::rwx rrd/ logs/ storage/ bootstrap/cache/ setfacl -d -m g::rwx rrd/ logs/ storage/ bootstrap/cache/ > Illuminate\Foundation\ComposerScripts::postInstall > @php artisan vue-i18n:generate --multi-locales --format=umd > @php artisan view:cache Compiled views cleared! Blade templates cached successfully! > @php artisan optimize Configuration cache cleared! Configuration cached successfully! Route cache cleared! Routes cached successfully! Files cached successfully! > @php artisan config:clear Configuration cache cleared! > scripts/check_requirements.py || pip3 install --user -r requirements.txt || : $ exit logout
配置 Nginx 网络服务器
为 LibreNMS 创建一个 Nginx 配置文件并在 vim 文本编辑器中编辑它。
# vi /etc/nginx/conf.d/librenms.conf
在此文件中添加以下几行。
server { listen 80; server_name librenms-01.onitroad.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 ~ [^/]\.php(/|$) { fastcgi_pass unix:/run/php-fpm-librenms.sock; fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi.conf; } location ~ /\.(?!well-known).* { deny all; } }
在 Linux vim 编辑器中编辑 Nginx 默认配置文件。
# vi /etc/nginx/nginx.conf
在此文件中查找并注释完整的“服务器”部分。
启用并启动 php-fpm 和 nginx 服务。
# systemctl enable --now nginx.service php-fpm.service Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service -> /usr/lib/systemd/system/nginx.service. Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service -> /usr/lib/systemd/system/php-fpm.service.
创建 Linux 用户和目录
创建一个 Linux 用户来拥有 LibreNMS 软件文件和进程。
# useradd librenms -d /opt/librenms -M -r -s /bin/bash
启动 LibreNMS Web 安装程序
在谷歌浏览器(或者其他网络浏览器)中打开 URL http://librenms-01.onitroad.com/install。
Web 安装程序可能会提示我们在 LibreNMS 安装位置手动创建一个 config.php 文件,将屏幕上显示的内容复制到该文件中。
我们还需要设置此文件的所有权。
# vi /opt/librenms/config.php # chown librenms:librenms /opt/librenms/config.php
Web 安装程序执行准备工作检查。
如果我们正确执行了上述步骤,它将不会显示任何警告。
提供 MySQL 服务器连接详细信息,并为 LibreNMS 网络监控软件构建数据库。
为 LibreNMS 软件创建管理员用户。
LibreNMS 安装成功完成。
使用 admin 用户登录 LibreNMS Web UI。
配置 PHP-FPM 服务
要为网络监控工具配置 php-fpm 服务,请创建默认配置文件的副本并将其重命名为 librenms.conf 。
# cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/librenms.conf
在 Linux vim 编辑器中编辑此文件。
# vi /etc/php-fpm.d/librenms.conf
定位并将 [www] 更改为 [librenms]
还要其中设置以下指令。
user = librenms group = librenms listen = /run/php-fpm-librenms.sock
设置 Linux 服务器时区
我们需要在 PHP 和 Linux 操作系统中设置一个公共时区。
首先编辑PHP配置文件。
# vi /etc/php.ini
在 PHP 配置文件中查找并设置以下变量。
date.timezone = America/New_York
现在,在 Linux 操作系统中设置相同的时区。
为此,我们可以使用以下 Linux 命令。
# timedatectl set-timezone America/New_York
为 LibreNMS 配置 Logrotate
同样复制 logrotate 目录下的 logrotate 配置文件。
该文件也在 LibreNMS 软件中提供。
# cp /opt/librenms/misc/librenms.logrotate /etc/logrotate.d/librenms
配置 SELinux 策略
因为,我们使用的是最小的 CentOS 8 Linux 服务器。
因此,要配置 SELinux,我们必须如下安装 SELinux 工具。
# dnf install -y policycoreutils-python-utils
现在在 Linux bash 提示符下执行以下命令为 LibreNMS 网络监控工具配置 SELinux。
# semanage fcontext -a -t httpd_sys_content_t '/opt/librenms/html(/.*)?' # semanage fcontext -a -t httpd_sys_rw_content_t '/opt/librenms/(logs|rrd|storage)(/.*)?' # restorecon -RF /opt/librenms # setsebool -P httpd_can_sendmail=1 # setsebool -P httpd_execmem 1 # chcon -t httpd_sys_rw_content_t /opt/librenms/.env
创建一个 SELinux 策略以允许通过 httpd_t 上下文类型进行 fping。
# cd # vi 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 };
在 Linux 服务器上加载并应用此 SELinux 策略。
# 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
什么是 LibreNMS?
LibreNMS 是一种基于 PHP/MySQL/SNMP 的自动发现网络监控软件,它支持广泛的网络硬件和操作系统,包括 Cisco、Linux、Juniper、Foundry 等。
LibreNMS 是免费、开源和社区支持的软件,它最初是从 Observium 的最后一个 GPL 许可版本fork出来的。
在 Linux 服务器上配置 SNMPD
随 LibreNMS 网络监控软件提供了 snmp 示例配置。
创建此示例文件的副本,然后在 Linux 文本编辑器中对其进行编辑。
# cp /opt/librenms/snmpd.conf.example /etc/snmp/snmpd.conf # vi /etc/snmp/snmpd.conf
编辑显示 RANDOMSTRINGGOESHERE 的文本并设置我们自己的社区字符串。
下载 snmp 配置脚本并将其放在 /usr/bin 目录中。
# curl -o /usr/bin/distro https://raw.githubusercontent.com/librenms/librenms-agent/master/snmp/distro % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 4637 100 4637 0 0 3233 0 0:00:01 0:00:01 --:--:-- 3231
授予下载文件的执行权限。
# chmod +x /usr/bin/distro
启用并启动 SNMPD 服务。
# systemctl enable --now snmpd.service Created symlink /etc/systemd/system/multi-user.target.wants/snmpd.service -> /usr/lib/systemd/system/snmpd.service.
如果我们不熟悉 snmp,我们建议我们购买并阅读 Douglas Mauro 和 Kevin Schmidt 所著的 Essential SNMP,第二版。
安装服务器
我们的服务器配置:
- CPU - 3.4 Ghz(2 核)
- 内存 - 4 GB
- 存储 - 60 GB
- 操作系统 - CentOS Stream 8.0
- 主机名 - librenms-01.onitroad.com
- IP 地址 - 192.168.1.230 /24
安装必备软件
将 yum 存储库中的 PHP 模块重置为默认流。
# dnf module reset php
LibreNMS 需要 PHP 7.3,因此,我们应该使用 dnf 命令启用 PHP 7.3 流。
# dnf module enable -y php:7.3
现在,执行以下 dnf 命令以安装 LibreNMS 网络监控工具所需的所有必备软件包。
# dnf install -y bash-completion \ > cronie fping git ImageMagick \ > mariadb-server mtr net-snmp \ > net-snmp-utils nginx nmap \ > php-fpm php-cli php-common \ > php-curl php-gd php-json \ > php-mbstring php-process \ > php-snmp php-xml php-zip \ > php-mysqlnd python3 \ > python3-PyMySQL python3-redis \ > python3-memcached python3-pip \ > rrdtool unzip
配置 Linux 防火墙
LibreNMS 为其 Web 服务使用默认 HTTP 端口。
因此,请执行以下命令在 Linux 防火墙中允许这些端口。
# firewall-cmd --zone public --add-service http --add-service https success # firewall-cmd --permanent --zone public --add-service http --add-service https success