Install and Configure LibreNMS on Ubuntu 22.04 | Best Setup
This tutorial provides a comprehensive guide on how to Install and Configure LibreNMS on Ubuntu 22.04. LibreNMS is a robust, open-source network monitoring solution designed to oversee devices and services within your network. It boasts an array of features, including support for various protocols, performance tracking, alerting mechanisms, and more. LibreNMS is known for its ease of installation and configuration, making it adaptable to numerous platforms.
To successfully complete this guide, you’ll need to log into your server as a non-root user with sudo privileges. If you haven’t already, you can follow this guide: Initial Server Setup with Ubuntu 22.04
1. Install MariaDB for LibreNMS Setup
First, update your local package index:
sudo apt update
Next, install MariaDB on Ubuntu 22.04:
sudo apt install mariadb-server -y
Secure your MariaDB installation by running the security script and setting a root password:
sudo mysql_secure_installation
Now, configure MariaDB.
Configure MariaDB
Open the MariaDB configuration file using your preferred text editor (e.g., vi):
sudo vi /etc/mysql/mariadb.conf.d/50-server.cnf
Locate the [mysqld]
section and add the following lines:
innodb_file_per_table=1
lower_case_table_names=0
Save and close the file. Restart MariaDB to apply the changes:
sudo systemctl restart mariadb
Create a Database for LibreNMS
Create a database and user for LibreNMS. First, log in to the MariaDB shell:
sudo mysql -u root -p
From the MariaDB shell, create a database (e.g., named librenms
):
MariaDB [(none)]> CREATE DATABASE librenms CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Create a user for the database (e.g., named orca
):
MariaDB [(none)]> CREATE USER 'orca'@'localhost' IDENTIFIED BY 'securepassword';
Grant all privileges to the LibreNMS database for the created user:
MariaDB [(none)]> GRANT ALL PRIVILEGES ON librenms.* TO 'orca'@'localhost';
Flush privileges and exit the MariaDB console:
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;
2. Install Nginx and PHP for LibreNMS
LibreNMS requires Nginx, PHP, and several dependencies. Install them using:
sudo apt install nginx-full nmap php-cli php-curl php-fpm php-gd php-json php-mbstring php-mysql php-snmp php-xml php-zip python3-dotenv python3-pip python3-pymysql python3-redis python3-setuptools python3-systemd rrdtool snmp snmpd whois acl curl composer fping git graphviz imagemagick mtr-tiny -y
Configure php.ini File
Edit the php.ini
file to set your timezone. Open the following files:
# sudo vi /etc/php/8.1/fpm/php.ini
# sudo vi /etc/php/8.1/cli/php.ini
Find the date.timezone
line, uncomment it, and set it to UTC
:
date.timezone = UTC
Save and close the files.
3. Set up and Configure LibreNMS on Ubuntu 22.04
Create a user for LibreNMS:
sudo useradd librenms -d /opt/librenms -M -r -s /bin/bash
Add the LibreNMS user to the www-data
group:
sudo usermod -a -G librenms www-data
Download the latest version of LibreNMS from Git:
sudo git clone https://github.com/librenms/librenms.git /opt/librenms
Create a log file for LibreNMS:
sudo touch /opt/librenms/logs/librenms.log
Copy the SNMP sample configuration file:
sudo cp /opt/librenms/snmpd.conf.example /etc/snmp/snmpd.conf
Edit the snmpd.conf
file:
sudo vi /etc/snmp/snmpd.conf
Find:
com2sec readonly default RANDOMSTRINGGOESHERE
Replace with:
com2sec readonly default mysnmpserverkey
Save and close the file.
Download the SNMP distro binary and copy it:
# sudo curl -o distro https://raw.githubusercontent.com/librenms/librenms-agent/master/snmp/distro
# sudo chmod +x distro
# sudo mv distro /usr/bin/distro
Restart the SNMP service:
sudo systemctl restart snmpd
Copy the LibreNMS cron and logrotate files:
# sudo cp /opt/librenms/librenms.nonroot.cron /etc/cron.d/librenms
# sudo cp /opt/librenms/misc/librenms.logrotate /etc/logrotate.d/librenms
Change to the LibreNMS directory and install dependencies:
# cd /opt/librenms
# ./scripts/composer_wrapper.php install --no-dev
Change ownership and permissions:
# chown -R www-data:librenms /opt/librenms
# chmod -R 775 /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/
4. Configure Nginx as a reverse proxy for LibreNMS
Create a virtual host file for LibreNMS:
sudo vi /etc/nginx/conf.d/librenms.conf
Add the following contents (replace example.com
with your domain):
server {
listen 80;
server_name 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/php8.1-fpm.sock;
}
location ~ /.ht {
deny all;
}
}
Save and close the file. Restart Nginx and PHP-FPM:
sudo systemctl restart nginx php8.1-fpm
5. Access LibreNMS Dashboard
Access the LibreNMS web installation via your domain name:
http://your-domain-name
Follow the on-screen instructions to complete the web-based setup, including database configuration, user creation, and final validation.
That’s it, you are done.
Conclusion
This guide demonstrated how to Install and Configure LibreNMS on Ubuntu 22.04. LibreNMS provides a powerful and flexible solution for network monitoring. By following these steps, you’ve successfully deployed LibreNMS and can begin monitoring your network devices.
Alternative Solutions for Network Monitoring on Ubuntu 22.04
While LibreNMS is a powerful and versatile solution, other options exist for network monitoring on Ubuntu 22.04. Here are two alternatives, along with explanations and code examples where applicable:
1. Zabbix
Zabbix is another enterprise-class open-source monitoring solution. It offers a wide range of features, including server, network, application, and cloud monitoring.
Explanation:
Zabbix is known for its scalability and robust feature set. It uses agents on monitored hosts to collect data, but it can also monitor devices via SNMP, IPMI, JMX, and other protocols. Zabbix offers flexible alerting mechanisms and a customizable web interface.
Installation and Configuration (Simplified):
-
Install Zabbix Server:
sudo apt update sudo apt install zabbix-server-mysql zabbix-frontend-php php-mysql snmp snmpd
-
Create Database and User: Similar to LibreNMS, you’ll need to create a database and user for Zabbix in MariaDB/MySQL.
CREATE DATABASE zabbix CHARACTER SET utf8mb4 COLLATE utf8mb4_bin; CREATE USER 'zabbix'@'localhost' IDENTIFIED BY 'zabbixpassword'; GRANT ALL PRIVILEGES ON zabbix.* TO 'zabbix'@'localhost'; FLUSH PRIVILEGES;
-
Import Initial Schema:
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -u zabbix -p zabbix
-
Configure Zabbix Server: Edit
/etc/zabbix/zabbix_server.conf
and set the database parameters.DBHost=localhost DBName=zabbix DBUser=zabbix DBPassword=zabbixpassword
-
Configure PHP: Edit
/etc/zabbix/apache.conf
(or/etc/zabbix/nginx.conf
if using Nginx) to set the timezone.php_value date.timezone UTC
-
Start Zabbix Server and Apache/Nginx:
sudo systemctl restart zabbix-server sudo systemctl restart apache2 #or nginx
-
Access Web Interface: Open your web browser and navigate to
http://your-server-ip/zabbix
.
Example Monitoring Script (Bash):
To monitor CPU usage on a client machine using Zabbix agent, you would typically configure the Zabbix agent on the client and then create an item in the Zabbix web interface to retrieve this data. However, if you’re using zabbix_get
for a quick test:
#!/bin/bash
# Script to get CPU usage
CPU_IDLE=$(top -bn1 | grep "Cpu(s)" | awk '{print $5}')
CPU_USAGE=$((100 - CPU_IDLE))
echo $CPU_USAGE
This simple script would output the CPU usage percentage. You would then configure Zabbix to execute this script (or a similar, more robust one) and collect the data.
Advantages over LibreNMS: More granular control over data collection, sophisticated alerting features, better scalability for large environments.
Disadvantages over LibreNMS: More complex to set up and configure, steeper learning curve.
2. Netdata
Netdata is a real-time performance monitoring tool designed for systems and applications. It’s known for its ease of use and visually appealing dashboards.
Explanation:
Netdata excels at providing immediate insights into system performance. It collects thousands of metrics per second and presents them in a highly interactive and responsive web dashboard. Netdata requires minimal configuration and is suitable for both small and large environments.
Installation:
sudo apt update
sudo apt install netdata
Configuration (Minimal):
Netdata typically requires very little configuration out of the box. Once installed, it automatically starts collecting data and presenting it in a web dashboard.
Access Web Interface: Open your web browser and navigate to http://your-server-ip:19999
.
Example Custom Collector (Python):
While Netdata automatically collects many metrics, you can create custom collectors to monitor specific applications or services. Here’s a simple example of a Python collector:
#!/usr/bin/env python3
import time
import random
def main():
"""
Simulates collecting data and printing it in Netdata format.
"""
while True:
# Simulate some data
value1 = random.randint(1, 100)
value2 = random.randint(50, 150)
# Print the data in Netdata format
print("BEGIN cpu.random")
print("SET value1 = {}".format(value1))
print("SET value2 = {}".format(value2))
print("END")
# Wait a bit
time.sleep(1)
if __name__ == "__main__":
main()
To use this collector:
- Save it as a Python file (e.g.,
random_cpu.py
). - Make it executable:
chmod +x random_cpu.py
- Configure Netdata to run the collector by creating or modifying a configuration file (usually in
/etc/netdata/python.d/
). The name of the file should match the name of the script without the extension (e.g.random_cpu.conf
). Inside the conf file, you just need to setenabled: yes
and potentially change theupdate_every
value (default is 1 second). You also need to tell netdata where the script is with thename: /path/to/random_cpu.py
- Restart Netdata:
sudo systemctl restart netdata
Advantages over LibreNMS: Extremely easy to install and use, real-time data visualization, low resource consumption.
Disadvantages over LibreNMS: Less comprehensive than LibreNMS for large, complex environments, limited long-term data storage options without external plugins.