The main task of a system admin is to ensure the proper running of hardware and software resources in infrastructure. To achieve this, continuous system monitoring may be involved to determine the health and status of components such as the CPU, memory, bandwidth, and application performance. This can be used to sound/ raise alarms or alerts in the event of malfunctions to the response team. Several tools can be used to perform system monitoring. Some of the commonly used tools are New Relic, Elastic Stack, Prometheus, Dynatrace, N-able RMM, Zenoss, Datadog Infrastructure Monitoring, PRTG Network Monitor, Nagios, Zabbix e.t.c
Cacti is a free and open-source web-based, industry-standard RRD graphing, network monitoring, and data logging tool. This tool uses the SNMP protocol to monitor network devices. The devices that can be observed are routers, switches, and servers among others.
Cacti, written in PHP, uses three primary operations: data retrieval, data storage, and data representation. It sends ICPM or SNMP packets to the managed devices to get the required information. Once the information has been obtained, it is saved in the MySQL database. The RRD Tool now retrieves the information and uses it to generate graphs.
The below diagram will help you understand the Cacti architecture:
Cacti is preferred due to the following features:
- Device graph and tree automation
- Remote data collection
- Graph aggregation
- User management functionality with ACL
- Fast polling of metrics
- Support for multiple data acquisition methods
- Support for advanced graph templating
- Unlimited graph items
- Custom data-gathering scripts
- Built-in SNMP support
- Flexible data sources
Getting Started
Update the system and the available packages:
sudo dnf -y update
sudo reboot
Enable the EPEL repository and install Development tools.
sudo dnf install epel-release -y
sudo dnf group -y install "Development Tools"
Step 1 – Install SNMP and RRD Tool
Cacti uses SNMP to send requests to retrieve information from the monitoring targets. RRD is used to generate the required graphs.
SNMP and the RRD tool can be installed with the command:
sudo dnf install -y net-snmp net-snmp-utils rrdtool
Once the installation is complete, start and enable SNMP.
sudo systemctl start snmpd
sudo systemctl enable snmpd
Verify if the service is running:
$ systemctl status snmpd
● snmpd.service - Simple Network Management Protocol (SNMP) Daemon.
Loaded: loaded (/usr/lib/systemd/system/snmpd.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2022-08-28 15:08:45 CEST; 6s ago
Main PID: 34073 (snmpd)
Tasks: 1 (limit: 23441)
Memory: 5.2M
CPU: 28ms
CGroup: /system.slice/snmpd.service
└─34073 /usr/sbin/snmpd -LS0-6d -f
Step 2 – Install PHP and Required Extensions
The Cacti web UI requires Apache PHP and the required extensions installed. Rocky Linux 9 / AlmaLinux 9 has PHP 8.0 in the default repositories. To install this PHP version and extensions use the command:
sudo dnf -y install vim httpd php php-{mysqlnd,curl,gd,intl,pear,ldap,snmp,mbstring,gettext,gmp,json,xml,common}
Once the installation is complete, check the PHP version:
$ php -v
PHP 8.0.13 (cli) (built: Nov 16 2021 18:07:21) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.13, Copyright (c) Zend Technologies
with Zend OPcache v8.0.13, Copyright (c), by Zend Technologies
Start and enable the Apache web server:
sudo systemctl enable --now httpd
Configure PHP parameters by editing the below file:
sudo vim /etc/php.ini
In the file, make the below changes:
date.timezone = Africa/Nairobi
memory_limit = 512M
max_execution_time = 300
Save the file and start the PHP-FPM service.
sudo systemctl enable --now php-fpm
Check if the service is running:
$ systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2022-08-28 15:10:06 CEST; 56s ago
Main PID: 37784 (php-fpm)
Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 6 (limit: 23441)
Memory: 15.1M
CPU: 57ms
CGroup: /system.slice/php-fpm.service
├─37784 "php-fpm: master process (/etc/php-fpm.conf)"
├─37788 "php-fpm: pool www"
├─37789 "php-fpm: pool www"
├─37791 "php-fpm: pool www"
├─37792 "php-fpm: pool www"
└─37793 "php-fpm: pool www"
Step 3 – Install and Configure Cacti Database
Cacti requires a MySQL database to store its data. For this guide, we will use MariaDB as the database server.
Install MariaDB on Rocky Linux 9 / AlmaLinux 9.
sudo dnf install mariadb-server mariadb
Start and enable the MariaDB service
sudo systemctl enable --now mariadb
Secure the MariaDB instance:
$ sudo mysql_secure_installation
....
Enter current password for root (enter for none): Just press Enter
......
Switch to unix_socket authentication [Y/n] Y
.....
Change the root password? [Y/n] Y
New password: New-root-password
Re-enter new password: Re-enter New-root-password
....
Remove anonymous users? [Y/n] Y
....
Disallow root login remotely? [Y/n] Y
.....
Remove test database and access to it? [Y/n] Y
......
Reload privilege tables now? [Y/n] Y
...
Thanks for using MariaDB!
Now login to the database server with the created root password.
sudo mysql -u root -p
Create a database for Cacti:
CREATE DATABASE cacti;
GRANT ALL ON cacti.* TO 'cacti'@'localhost' IDENTIFIED BY 'Passw0rd';
FLUSH PRIVILEGES;
EXIT
We need optimum performance for the MariaDB database server. Some we will tweak the configurations as shown;
sudo vim /etc/my.cnf.d/mariadb-server.cnf
Make the below adjustments:
[mysqld]
#Configure inside [mysqld] block
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
max_heap_table_size=128M
tmp_table_size=128M
join_buffer_size=256M
innodb_buffer_pool_size=1405M
innodb_file_format=Barracuda
innodb_doublewrite=OFF
innodb_large_prefix=1
innodb_flush_log_at_timeout=3
innodb_read_io_threads=32
innodb_write_io_threads=16
innodb_buffer_pool_instances=16
innodb_io_capacity=5000
innodb_io_capacity_max=10000
Save the file and restart the service:
sudo systemctl restart mariadb
Step 4 – Install Cacti on Rocky Linux 9 / AlmaLinux 9
Once the required packages have been installed, we will download the latest version of cacti from the Cacti downloads page. It is also possible to pull the tarball with the command:
mkdir cacti && cd cacti
curl -SL https://www.cacti.net/downloads/cacti-latest.tar.gz | tar --strip 1 -xzf -
Once downloaded, and extracted, copy the file to the web root directory /var/www/html
cd ..
sudo mv cacti/ /var/www/html/
Set the correct permissions of the files.
sudo chown -R apache:apache /var/www/html/cacti
sudo rm /etc/httpd/conf.d/welcome.conf
We will then import schemas to the created MariaDB database
$ sudo mysql -u root -p cacti < /var/www/html/cacti/cacti.sql
Enter password: Root_password
Also, configure the Timezone information.
$ mysql_tzinfo_to_sql /usr/share/zoneinfo | sudo mysql -u root -p mysql
Enter password: Root_password
Allow the database user to use the added Timezone.
$ sudo mysql -u root -p
GRANT SELECT ON mysql.time_zone_name TO cacti@localhost;
ALTER DATABASE cacti CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
FLUSH PRIVILEGES;
QUIT
For the Cacti directory to be accessible, configure SELinux with the command below.
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/cacti(/.*)?"
sudo restorecon -Rv /var/www/html/cacti
Restart Apache and PHP-FPM.
sudo systemctl restart httpd php-fpm
Configure Cacti to use the created database.
sudo vim /var/www/html/cacti/include/config.php
Adjust the database parameters
$database_type = 'mysql';
$database_default = 'cacti';
$database_hostname = 'localhost';
$database_username = 'cacti';
$database_password = 'Passw0rd';
$database_port = '3306';
.............
Save the file and create a log directory with the right permissions for Cacti.
sudo touch /var/www/html/cacti/log/cacti.log
sudo chmod 777 /var/www/html/cacti/log/cacti.log
Create a scheduled task for Cacti polling:
$ sudo crontab -u apache -e
*/5 * * * * php /var/www/html/cacti/poller.php > /dev/null 2>&1
Allow HTTP and HHTPS through the firewall
sudo firewall-cmd --add-service={http,https} --permanent
sudo firewall-cmd --reload
Step 5 – Finish Cacti Installation from UI
At this point, the Cacti server installation can be completed from the browser. Access the installer using the URL http://IP_address/cacti
Login with the default credentials.
Username: admin
Password: admin
Change the default password to a preferred password.
Read and accept the Licence terms.
The pre-requisites check will be performed.
Next, select the installation type.
A permissions check will be performed before proceeding.
Also, the required binaries should be in the correct location.
Read through and accept the Input Validation Whitelist protection
Set the default data profile for Cacti as well as the Network range
Install the default templates for the profile
The database settings will be provided.
To allow the installation to proceed, click Confirm Installation
Once the installation is complete, click Get started to proceed to the below Cacti dashboard.
Step 6 – Perform Network Components Monitoring with Cacti
At this point, the cacti templates have been downloaded and failed to install due to signature verification. Therefore, we need to install the cacti Templates manually by navigating to the templates directory;
cd /var/www/html/cacti/install/templates
Begin by installing the SNMP Generic device template
sudo /var/www/html/cacti/cli/import_package.php --filename=./Generic_SNMP_Device.xml.gz
Sample Output:
Read 48323 bytes of Package data
Import Results Cacti has imported the following items for the Package:Package Files/var/www/html/cacti/scripts/ss_hstats.php [written]/var/www/html/cacti/scripts/ping.pl [written]/var/www/html/cacti/resource/snmp_queries/interface.xml [written]CDEF[success] Turn Uptime to Days [new][success] Turn Bytes into Bits [unchanged]GPRINT Preset[success] Normal [unchanged]Data Input Method[success] Device - Polling Time [new][success] Device - Uptime [new][success] Unix - Ping Host [updated]Table: data_input, Column: input_string, New Value: perl <path_cacti>/scripts/ping.pl <hostname>, Old Value: perl <path_cacti>/scripts/ping.pl <ip>Table: data_input_fields, Column: data_name, New Value: hostname, Old Value: ip[success] Get SNMP Data [unchanged][success] Get SNMP Data (Indexed) [unchanged]Data Template[success] Device - Polling Time [new][success] Device - Uptime [new][success] Unix - Ping Host [new][success] SNMP - Generic OID Template [new][success] Interface - Traffic [new][success] Interface - Errors/Discards [new][success] Interface - Unicast Packets [new][success] Interface - Non-Unicast Packets [new]Graph Template[success] Device - Polling Time [new][success] Device - Uptime [new][success] Unix - Ping Latency [new][success] SNMP - Generic OID Template [new][success] Interface - Traffic (bits/sec) [new][success] Interface - Errors/Discards [new][success] Interface - Unicast Packets [new][success] Interface - Non-Unicast Packets [new][success] Interface - Traffic (bytes/sec) [new][success] Interface - Traffic (bits/sec, 95th) [new][success] Interface - Traffic (bits/sec, BW) [new][success] Interface - Traffic (bytes/sec, BW) [new][success] Interface - Multicast Packets [new][success] Interface - Broadcast Packets [new]Data Query[success] SNMP - Interface Statistics [new]Device Template[success] Generic SNMP Device [new]
Once complete, install the Local Linux system template:
sudo /var/www/html/cacti/cli/import_package.php --filename=./Local_Linux_Machine.xml.gz
Also install the NetSNMP template which loads the UNIX, net-snmp, load, cpu, memory, context switches, interrupts, interface, errors, disk, logins, processes, iops.
sudo /var/www/html/cacti/cli/import_package.php --filename=./NetSNMP_Device.xml.gz
Verify if the templates have been added.
To add a new server either remote or local, log in as the admin user then navigate to Console > Create > New Device
Once the device has been added, you will see it under Management -> Devices
With SNMP already installed, you can see monitoring data for this server by navigating to Graph –>Default Tree –>Local –>Machine –> Select Your Device
You can also create new graphs under Console > Create > New Graphs
Wrap Up
At this point, you should be able to install and configure the Cacti Server on Rocky Linux 9 / AlmaLinux 9. Now perform network monitoring, graphing and data logging. I hope this was helpful.
See more: