Deployment and application of Cacti monitoring platform

Posted by mem0ri on Tue, 09 Jun 2020 04:54:13 +0200

brief introduction

Cacti is a software implemented in php language. Its main function is to obtain data through snmp service, then use rrdtool to store and update data. When users need to view data, use rrdtool to generate charts and present them to users. So the key of cacti is snmp and rrdtool. snmp is related to data collection, while rrdtool is related to data storage and chart generation
The data captured by snmp is not stored in mysql, but in the rrd file generated by rrdtool (under the rra folder of the cacti root directory). rrdtool's update and storage of data is the processing of rrd file. rrd file is a round robin archive with fixed size. The number of data that it can store has been defined at the time of creation
Cacti provides a very powerful data and user management function. It can specify that each user can view the tree structure, host and any map. It can also be combined with LDAP for user authentication. At the same time, it can also add its own template, which is very powerful, perfect and friendly
The development of Cacti is based on making RRDTool users more convenient to use the software. In addition to the basic snmp traffic and system information monitoring, Cacti can also plug in Scripts and Templates At the same time, it can store some variable data and call variable data through Mysql and PHP program, such as host name, host ip, snmp community name, port number, template information, etc

preparation in advance

Prepare two Centos7 virtual machines, turn off firewall and selinux, synchronize system time, configure IP address and hostname

hostname ip
master 192.168.29.131
node 192.168.29.133

The master node deploys Nginx and MySQL

#Download the yum source of Nginx from the official website and install it
[root@master ~]# yum install nginx -y
#Download the yum source of MySQL from the official website and install it
[root@master ~]# yum install mysql mysql-server -y
#service mysql start 
[root@master ~]# systemctl start mysqld
#Set mysql login password, etc

master node installation dependency

[root@master ~]# yum install php php-mysql php-snmp php-pdo perl-DBD-MySQL net-snmp net-snmp-utils rrdtool rrdtool-devel rrdtool-php -y
#Start snmpd service
[root@master ~]# systemctl start snmpd

Node node installation dependency

[root@node ~]# yum install net-snmp -y

master deployment Cacti

Download Cacti compressed package on the official website and extract it to the web folder of Nginx

[root@master ~]# tar -zxvf cacti-1.2.12.tar.gz -C /usr/share/nginx/html/cacti

Configure Nginx server

[root@master ~]# vi /etc/nginx/conf.d/default.conf 
server {
    listen       80;
    server_name  localhost;
    location / {
    	#Profile root
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    #Configuration parsing PHP file
    location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html/$fastcgi_script_name;
        include        fastcgi_params;
    }
}
#Start PHP FPM service
[root@master ~]# systemctl start php-fpm 
#Start Nginx server
[root@master ~]# systemctl start nginx

Configure MySQL

#Create cacti database
mysql> create database cacti;
#Table in cacti decompression folder, import table
mysql> use cacti
mysql> source /usr/share/nginx/html/cacti/cacti.sql
#Create a cactiuser user
mysql>grant all privileges on cacti.* to 'cactiuser'@'localhost' identified by 'your_password';
mysql>flush privileges;

Configure Cacti connection database file

[root@master ~]# vi /usr/share/nginx/html/cacti/include/config.php 
$database_type     = 'mysql';
$database_default  = 'cacti';
$database_hostname = 'localhost';
$database_username = 'cactiuser';
$database_password = 'your_password';
$database_port     = '3306';
$database_retries  = 5;
$database_ssl      = false;
$database_ssl_key  = '';
$database_ssl_cert = '';
$database_ssl_ca   = '';

Installing Cacti

Browser access http://master_ IP / cache: modify the content according to the error message on the page until the installation is completed
Configure php

[root@master ~]# vi /etc/php.ini
date.timezone = Asia/Shanghai
memory_limit = 800M
max_execution_time = 60
#Restart service after modification
[root@master ~]# systemctl restart php-fpm

Configure MySQL

[root@master ~]# vi /etc/my.cnf
default-time_zone='+8:00'
tmp_table_size=28M
join_buffer_size=57M
innodb_buffer_pool_size	=444M
innodb_flush_log_at_timeout=3
innodb_read_io_threads=32
innodb_write_io_threads=16
innodb_buffer_pool_instances=2
innodb_io_capacity=5000
innodb_io_capacity_max=10000
max_allowed_packet=16777216
max_heap_table_size=40M
tmp_table_size=40M
collation_server=utf8mb4_unicode_ci
character_set_server=utf8mb4
innodb_buffer_pool_instances=11
innodb_buffer_pool_size=1G
#Fill time zone
[root@master ~]# mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
#Restart mysql after modification
[root@master ~]# systemctl restart mysqld

Profile permissions

[root@master ~]# cd /usr/share/nginx/html/
#Set users and user groups
[root@master html]# chown -R nginx.nginx cacti/
#Create log file
[root@master cacti]# touch log/cacti.log
#Set file execution permission
[root@master cacti]# chmod 777 log/cacti.log
[root@master cacti]# chmod 777 resource/snmp_queries/
[root@master cacti]# chmod 777 resource/script_server/
[root@master cacti]# chmod 777 resource/script_queries/
[root@master cacti]# chmod 777 scripts/
[root@master cacti]# chmod 777 log/
[root@master cacti]# chmod 777 cache/boost/
[root@master cacti]# chmod 777 cache/mibcache/
[root@master cacti]# chmod 777 cache/realtime/
[root@master cacti]# chmod 777 cache/spikekill/

Node node configuration snmp

[root@node ~]# vi /etc/snmp/snmpd.conf
com2sec notConfigUser  192.168.29.131       public
view all    included  .1                               80
#Start snmpd service
[root@node ~]# systemctl start snmpd

master configuration automatic data collection

[root@master ~]# vi /etc/crontab 
*/1 * * * *  root /usr/bin/php /usr/share/nginx/html/cacti/poller.php --force  > /dev/null 2>&1

Visual monitoring

visit http://master_ip/cacti can log in (the default login account password is admin)

Forget login password

mysql> use cacti;
mysql> update user_auth set password=md5("new_password") where id ='1';

Enter the home page after successful login

New node monitoring

Fill in the node information and click Create

View monitored devices

Add monitoring model for master node

Add monitoring model for node node

View Drawings of monitoring objects

master node image

Node node image

Custom monitoring object

Write monitoring script

[root@master ~]# vi /usr/share/nginx/html/cacti/scripts/tcpstate.sh
#Monitor the number of TCP connections LISTEN
#!/bin/bash
LISTEN_STATE=$(ss -anlp| cut -d " " -f3|grep LISTEN|wc -l)
echo  "listen:$LISTEN_STATE"

#Modify script permissions
[root@master ~]# chmox +x /usr/share/nginx/html/cacti/scripts/tcpstate.sh

Add and configure data entry methods



Output field configuration (output field should be consistent with script output)

Add configuration data source template



Add configuration drawing template



Configure drawing items


Associated input


Associate devices and data sources

Add drawing template to management device


Test whether there is data input to rrd file

[root@master ~]# rrdtool lastupdate /usr/share/nginx/html/cacti/rra/local_linux_machine_tcpstate.rrd
 tcplisten
1591668242: 35
#Data input
#Refresh image manually
[root@master ~]# php /usr/share/nginx/html/cacti/poller.php

View image

Configuration of custom monitoring items completed

Topics: MySQL Nginx PHP yum