在Ubuntu 16.04上使用LibreNMS监控设备
Anne655
・13 分钟阅读
使用不同的系统?
LibreNMS是一个功能齐全的开源网络监控系统,它使用SNMP
来获取来自不同设备的数据,LibreNMS支持各种设备如Cisco Linux FreeBSD Juniper Brocade Foundry HP等,它支持多种身份验证机制,并支持双因素身份验证。它有一个可定制的警报系统,可以通过电子邮件,IRC或slack通知网络管理员。
前提条件
- Ubuntu 16.04服务器实例,
- 一个sudo用户 ,
对于本教程,我们将使用nms.example.com
作为指向实例的域名,请确保将例子域名的所有匹配项替换为实际名称。
安装Nginx和PHP
LibreNMS的前端是用PHP编写的,因此我们需要安装一个web服务器和PHP,在本教程中,我们将安装Nginx和PHP 7.2以获得最大的安全性和性能。
安装Nginx 。
sudo apt -y install nginx
启动Nginx并使其开机时自动启动。
sudo systemctl start nginx
sudo systemctl enable nginx
添加并启用REMI存储库,因为默认的APT存储库包含旧版本的PHP。
sudo add-apt-repository --yes ppa:ondrej/php
sudo apt update
安装PHP版7.2以及LibreNMS所需的模块。
sudo apt -y install php7.2 php7.2-cli php7.2-common php7.2-curl php7.2-fpm php7.2-gd php7.2-mysql php7.2-snmp php7.2-mbstring php7.2-xml php7.2-zip zip unzip
在编辑器中打开加载的配置文件。
sudo nano /etc/php/7.2/fpm/php.ini
查找下列行。
;cgi.fix_pathinfo=1
;date.timezone =
取消注释,用本地时区替换Asia/Kolkata。
cgi.fix_pathinfo=0
date.timezone = Asia/Kolkata
你还需要通过运行以下命令来更改系统时区。
sudo ln -sf /usr/share/zoneinfo/Asia/Kolkata /etc/localtime
重新启动php fpm 。
sudo systemctl restart php7.2-fpm
安装MariaDB
MariaDB是MySQL的开源fork ,将MariaDB存储库添加到系统中,因为默认的Ubuntu存储库包含较老的MariaDB版本。
sudo apt-key adv --yes --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
sudo add-apt-repository 'deb [arch=amd64,i386,ppc64el] http://mariadb.biz.net.id/repo/10.2/ubuntu xenial main'
sudo apt update
安装MariaDB安装过程中,安装程序将询问MySQL root
用户的密码,提供强密码。
sudo apt -y install mariadb-server
我们开始使用MariaDB之前,我们需要稍微调整一下配置,打开配置文件。
sudo nano /etc/mysql/conf.d/mariadb.cnf
将下面的代码添加到文件的末尾。
[mysqld]
innodb_file_per_table=1
sql-mode=""
lower_case_table_names=0
重新启动MariaDB,并且使它在启动时自动启动。
sudo systemctl restart mariadb.service
sudo systemctl enable mariadb.service
在配置数据库之前,你需要保护MariaDB实例。
sudo mysql_secure_installation
你将被要求输入当前MariaDB root密码,然后提示你更改root
密码,由于我们已经在安装过程中为root
用户设置了一个强密码,所以,通过应答"N
"来跳过它,对于所有其他问题,请回答"Y
",
作为root登录到MySQL shell 。
mysql -u root -p
提供MariaDB root用户登录的密码,运行以下查询为LibreNMS安装创建数据库和数据库用户。
CREATE DATABASE librenms CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'librenms'@'localhost' IDENTIFIED BY 'StrongPassword';
GRANT ALL PRIVILEGES ON librenms.* TO 'librenms'@'localhost';
FLUSH PRIVILEGES;
EXIT;
你可以根据你的选择替换数据库名librenms
和用户名librenms
,请确保将StrongPassword更改为强健的密码。
安装LibreNMS
除了上面的依赖项之外,LibreNMS还需要更多的依赖项。
sudo apt -y install fping git imagemagick jwhois mtr graphviz nmap python-memcache python-mysqldb rrdtool snmp snmpd whois composer
为LibreNMS应用程序添加新的非特权用户。
sudo useradd librenms -d /opt/librenms -M -r
sudo usermod -aG www-data librenms
LibreNMS可以通过克隆它的Github存储库直接安装。
cd /opt
sudo git clone https://github.com/librenms/librenms.git librenms
更改所有权。
sudo chown librenms:librenms -R /opt/librenms
安装PHP依赖项。
cd /opt/librenms
sudo su librenms -c"composer install"
LibreNMS在许多任务中依赖于SNMP,由于已经安装了SNMP,所以将例子配置文件复制到它所在的位置。
sudo cp /opt/librenms/snmpd.conf.example /etc/snmp/snmpd.conf
在编辑器中打开配置文件。
sudo nano /etc/snmp/snmpd.conf
查找此行。
com2sec readonly default RANDOMSTRINGGOESHERE
编辑文本RANDOMSTRINGGOESHERE
并用你选择的任意字符串替换社区字符串。 例如.
com2sec readonly default my-org
记住字符串,因为稍后我们添加第一个SNMP设备时需要它。
SNMP还需要有关分发版本的信息,下载并安装脚本以查找分发版本。
sudo curl -o /usr/bin/distro https://raw.githubusercontent.com/librenms/librenms-agent/master/snmp/distro
sudo chmod +x /usr/bin/distro
启动SNMP守护进程服务,并且使它在引导时自动启动。
sudo systemctl enable snmpd
sudo systemctl restart snmpd
现在,你需要添加一些crontab条目来运行计划的任务,创建新的cron作业文件。
sudo cp /opt/librenms/librenms.nonroot.cron /etc/cron.d/librenms
重新启动cron守护进程服务。
sudo systemctl restart cron
设置logrotate
以便在一段时间内自动刷新日志文件。
sudo cp /opt/librenms/misc/librenms.logrotate /etc/logrotate.d/librenms
最后,设置适当的所有权和权限。
sudo chown -R librenms:www-data /opt/librenms
sudo chmod g+w -R /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
SSL和Nginx VHost配置
如果连接未使用SSL加密,则通过LibreNMS的Web界面发送的登录和其他信息不受保护。我们将配置Nginx以便使用let's Encrypt免费SSL生成的SSL 。
添加Certbot存储库
sudo add-apt-repository --yes ppa:certbot/certbot
sudo apt-get update
安装Certbot,它是let's Encrypt CA的客户端应用程序。
sudo apt -y install certbot
注:要从let's Encrypt CA获取证书,必须将要生成证书的域指向服务器,Certbot在提供证书之前检查域颁发机构。
生成SSL证书。
sudo certbot certonly --webroot -w /var/www/html -d nms.example.com
生成的证书存储在 /etc/letsencrypt/live/nms.example.com/
目录,存储为fullchain.pem证书,而私钥存储为privkey.pem 。
Let's Encrypt证书在90天内到期,因此建议使用cron作业为证书设置自动续订。
打开cron作业文件。
sudo crontab -e
在文件的末尾添加以下行。
30 5 * * 1 /usr/bin/certbot renew --quiet
上面的5:30作业将在本地时间每星期一运行一次,如果证书到期到期,它将自动续订。
创建新虚拟主机。
sudo nano /etc/nginx/sites-available/librenms
填充文件。
server {
listen 80;
server_name nms.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name nms.example.com;
ssl_certificate /etc/letsencrypt/live/nms.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/nms.example.com/privkey.pem;
ssl on;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;
access_log /opt/librenms/logs/librenms.nginx.access.log;
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:/run/php/php7.2-fpm.sock;
}
location ~ /.ht {
deny all;
}
}
将nms.example.com
替换为上面配置中的实际域。
激活新创建的配置。
sudo ln -s /etc/nginx/sites-available/librenms /etc/nginx/sites-enabled/librenms
重新启动Nginx 。
sudo systemctl restart nginx
使用WebUI安装
要完成安装,请在你喜爱的浏览器上打开https://nms.example.com
,提供数据库详细信息,并且创建新的管理帐户,安装之后,你将得到一条消息来验证安装,单击链接并使用管理员帐户登录,你应该看到除了"Poller
"之外的所有内容都有"Ok
"状态。
添加设备后,你可以通过转到"Devices
"选项卡来查看详细信息,同样,你可以在LibreNMS应用程序中添加更多设备来进行"全天候"监控。