Linux operation and maintenance --- use LAMP architecture to deploy Cacti monitoring

Cacti means cactus in English. Cacti is a set of network traffic monitoring graphical analysis tools developed based on PHP, MySQL, SNMP and RRDTool.

cacti is a software implemented in php language. Its main function is to use snmp (simple network management protocol) service to obtain data, and then use rrdtool (ring database) to store and update the data, and use rrdtool to generate it when users need to view the data The chart is presented to the user. Therefore, snmp and rrdtool are the key to cacti. Snmp is related to data collection, and rrdtool is related to data storage and chart generation.

Build a LAMP architecture environment

Configure Apache

Install httpd

[root@localhost ~]# yum install httpd -y
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
nginx                                                    | 2.9 kB     00:00     
yum                                                      | 4.1 kB     00:00     
nginx/x86_64/primary_db        FAILED                                           
http://nginx.org/packages/centos/7/x86_64/repodata/5aafc56eec8e29902c891c177fe8d2c8273e99e4fa4b4d6777128fd7f534a681-primary.sqlite.bz2: [Errno 12] Timeout on http://nginx.org/packages/centos/7/x86_64/repodata/5aafc56eec8e29902c891c177fe8d2c8273e99e4fa4b4d6777128fd7f534a681-primary.sqlite.bz2: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
正在尝试其它镜像。
nginx/x86_64/primary_db                                    |  55 kB   00:04     
           ...............省略安装过程.................
  正在安装    : httpd-tools-2.4.6-40.el7.x86_64                             1/3 
  正在安装    : mailcap-2.1.41-2.el7.noarch                                 2/3 
  正在安装    : httpd-2.4.6-40.el7.x86_64                                   3/3 
yum/productid                                            | 1.6 kB     00:00     
  验证中      : mailcap-2.1.41-2.el7.noarch                                 1/3 
  验证中      : httpd-tools-2.4.6-40.el7.x86_64                             2/3 
  验证中      : httpd-2.4.6-40.el7.x86_64                                   3/3 

已安装:
  httpd.x86_64 0:2.4.6-40.el7                                                   

作为依赖被安装:
  httpd-tools.x86_64 0:2.4.6-40.el7        mailcap.noarch 0:2.1.41-2.el7       

完毕!
[root@localhost ~]#

Start httpd

[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@localhost ~]# systemctl enable httpd

Open http service

[root@localhost ~]# firewall-cmd --permanent --add-service=http   
[root@localhost ~]# firewall-cmd --reload   # 重启防火墙

Configure mariadb database

Install Mariadb database

[root@localhost ~]# yum install mariadb mariadb-server mysql-devel -y
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
       ............ 省略安装过程 .............
已安装:
  mariadb.x86_64 1:5.5.44-2.el7          mariadb-devel.x86_64 1:5.5.44-2.el7  
  mariadb-server.x86_64 1:5.5.44-2.el7  

作为依赖被安装:
  keyutils-libs-devel.x86_64 0:1.5.8-3.el7                                     
  krb5-devel.x86_64 0:1.13.2-10.el7                                            
  libcom_err-devel.x86_64 0:1.42.9-7.el7                                       
  libselinux-devel.x86_64 0:2.2.2-6.el7                                        
  libsepol-devel.x86_64 0:2.1.9-3.el7                                          
  libverto-devel.x86_64 0:0.2.5-4.el7                                          
  openssl-devel.x86_64 1:1.0.1e-42.el7_1.9                                     
  pcre-devel.x86_64 0:8.32-15.el7                                              
  perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7                                 
  perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7                                  
  perl-DBD-MySQL.x86_64 0:4.023-5.el7                                          
  perl-DBI.x86_64 0:1.627-4.el7                                                
  perl-IO-Compress.noarch 0:2.061-2.el7                                        
  perl-Net-Daemon.noarch 0:0.48-5.el7                                          
  perl-PlRPC.noarch 0:0.2020-14.el7                                            
  zlib-devel.x86_64 0:1.2.7-15.el7                                             

完毕!
[root@localhost ~]#

Start Mariadb database

[root@localhost ~]# systemctl enable mariadb
[root@localhost ~]# systemctl start mariadb

Initialize Mariadb database

[root@localhost ~]# mysql_secure_installation
/usr/bin/mysql_secure_installation:行379: find_mysql_client: 未找到命令

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@localhost ~]#

Create user chuid to test the connectivity of PHP and Mariadb database

[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.44-MariaDB MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> grant all privileges on *.* to chuid@localhost identified by '123456';
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye
[root@localhost ~]#

Restart Mariadb database

[root@localhost ~]# systemctl restart mariadb

Configure PHP

Install PHP

[root@localhost ~]# yum -y install php php-mysql php-gd php-pear
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
           .................省略安装过程...............
已安装:
  php.x86_64 0:5.4.16-36.el7_1            php-gd.x86_64 0:5.4.16-36.el7_1     
  php-mysql.x86_64 0:5.4.16-36.el7_1      php-pear.noarch 1:1.9.4-21.el7      

作为依赖被安装:
  libzip.x86_64 0:0.10.1-8.el7             php-cli.x86_64 0:5.4.16-36.el7_1    
  php-common.x86_64 0:5.4.16-36.el7_1      php-pdo.x86_64 0:5.4.16-36.el7_1    
  php-process.x86_64 0:5.4.16-36.el7_1     php-xml.x86_64 0:5.4.16-36.el7_1    
  t1lib.x86_64 0:5.1.2-14.el7             

完毕!
[root@localhost ~]#

Modify time zone

[root@localhost ~]# vim /etc/php.ini
date.timezone =PRC

 Edit index.php test page

[root@localhost ~]# vim /var/www/html/index.php
<?php
phpinfo();
?>

[root@localhost ~]# systemctl restart httpd  # 重启Apache

Open the Firefox browser and enter the IP address, and the following page appears, indicating that the PHP environment has been successfully set up!

Configure cacti monitoring

Install cacti

[root@localhost ~]# cd /usr/local/src
[root@localhost src]# wget http://www.cacti.net/downloads/cacti-0.8.8f.tar.gz
--2020-07-22 09:49:31--  http://www.cacti.net/downloads/cacti-0.8.8f.tar.gz
正在解析主机 www.cacti.net (www.cacti.net)... 104.28.8.127, 172.67.196.107, 104.28.9.127, ...
正在连接 www.cacti.net (www.cacti.net)|104.28.8.127|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 301 Moved Permanently
位置:https://www.cacti.net/downloads/cacti-0.8.8f.tar.gz [跟随至新的 URL]
--2020-07-22 09:49:32--  https://www.cacti.net/downloads/cacti-0.8.8f.tar.gz
正在连接 www.cacti.net (www.cacti.net)|104.28.8.127|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2594409 (2.5M) [application/octet-stream]
正在保存至: “cacti-0.8.8f.tar.gz”

100%[================================>] 2,594,409    180KB/s 用时 13s    

2020-07-22 09:50:02 (190 KB/s) - 已保存 “cacti-0.8.8f.tar.gz” [2594409/2594409])

[root@localhost src]# ls | grep cacti
cacti-0.8.8f.tar.gz
[root@localhost src]# tar xf cacti-0.8.8f.tar.gz 
[root@localhost src]# mv cacti-0.8.8f /var/www/html/cacti

Create the cacti database, and give the chuid user the authority to operate the cacti database

[root@localhost src]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.44-MariaDB MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database cacti default character set utf8;  # 创建数据库cacti,默认字符集utf8
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all privileges on cacti.* to chuid@localhost identified by '123456';  # 赋予chuid用户对cacti数据库的所有表操作权限
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> use mysql
MariaDB [mysql]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cacti              |
| mysql              |
| performance_schema |
| zabbix             |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [mysql]> 
MariaDB [mysql]> exit
Bye
[root@localhost src]#

Import cacti.sql into the database

[root@localhost src]# cd ~
[root@localhost ~]# mysql -u chuid -p cacti < /var/www/html/cacti/cacti.sql
Enter password: 
[root@localhost ~]#

Edit config.php and global.php, modify the login user and password of the database

[root@localhost ~]# vim /var/www/html/cacti/include/config.php
26 $database_type = "mysql";
27 $database_default = "cacti";
28 $database_hostname = "localhost";
29 $database_username = "chuid";
30 $database_password = "123456";
31 $database_port = "3306";
32 $database_ssl = false;

[root@localhost ~]# vim /var/www/html/cacti/include/global.php
34 $database_type = "mysql";
35 $database_default = "cacti";
36 $database_hostname = "localhost";
37 $database_username = "chuid";
38 $database_password = "123456";
39 $database_port = "3306";
40 $database_ssl = false;

Configure rrdtool

Install rrdtool to generate images

[root@localhost ~]# cd /usr/local/src
[root@localhost src]# yum -y install rrdtool rrdtool-devel rrdtool-php rrdtool-perl
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
nginx                                              | 2.9 kB     00:00     
yum                                                | 4.1 kB     00:00     
            ................省略安装过程................
  正在安装    : rrdtool-1.4.8-9.el7.x86_64                            1/1 
  验证中      : rrdtool-1.4.8-9.el7.x86_64                            1/1 

已安装:
  rrdtool.x86_64 0:1.4.8-9.el7                                            

完毕!
[root@localhost src]# 
[root@localhost src]# yum -y install gd gd-devel php-gd
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
软件包 gd-2.0.35-26.el7.x86_64 已安装并且是最新版本
软件包 php-gd-5.4.16-36.el7_1.x86_64 已安装并且是最新版本
[root@localhost src]#

Configure SNMP

Install SNMP service

[root@localhost src]# yum -y install net-snmp net-snmp-utils php-snmp net-snmp-libs
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
没有可用软件包 php-snmp。
软件包 1:net-snmp-libs-5.7.2-24.el7.x86_64 已安装并且是最新版本
........................省略安装过程....................
  正在安装    : 1:net-snmp-agent-libs-5.7.2-24.el7.x86_64             1/3 
  正在安装    : 1:net-snmp-5.7.2-24.el7.x86_64                        2/3 
  正在安装    : 1:net-snmp-utils-5.7.2-24.el7.x86_64                  3/3 
  验证中      : 1:net-snmp-agent-libs-5.7.2-24.el7.x86_64             1/3 
  验证中      : 1:net-snmp-5.7.2-24.el7.x86_64                        2/3 
  验证中      : 1:net-snmp-utils-5.7.2-24.el7.x86_64                  3/3 

已安装:
  net-snmp.x86_64 1:5.7.2-24.el7   net-snmp-utils.x86_64 1:5.7.2-24.el7  

作为依赖被安装:
  net-snmp-agent-libs.x86_64 1:5.7.2-24.el7                               

完毕!
[root@localhost src]#

Edit configuration file

[root@localhost src]# vim /etc/snmp/snmpd.conf
41 com2sec notConfigUser  127.0.0.1       public
62 access  notConfigGroup ""      any       noauth    exact  all none none
85 view all    included  .1                               80

Restart the snmpd service program

[root@localhost src]# systemctl restart snmpd.service
[root@localhost src]# systemctl enable snmpd.service

Grant directory permissions to chuid users

[root@localhost src]# useradd -r -M chuid
[root@localhost src]# chown -R chuid /var/www/html/cacti/rra
[root@localhost src]# chown -R chuid /var/www/html/cacti/log

Configure a scheduled task to capture a picture

Note: "*/5" means the task will be executed every 5 minutes

[root@localhost src]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
*/5 * * * * /usr/bin/php  /var/www/html/cacti/poller.php >> /tmp/cacti_rrdtool.log

Browser visit cacti management page to install

This interface shows that the installation is successful!

The initial account password is admin and admin

After login, you will be prompted to change the password

After modifying the login password, click Save to automatically enter the cacti interface

Click the second button in the upper right corner, graphs to view the cacti image, select the localhost host,

On the right, cacti monitoring images every 5 minutes will be displayed

If there is no image, you need to run it manually

[root@localhost ~]# /usr/bin/php /var/www/html/cacti/poller.php
[root@localhost ~]#

View monitoring log

[root@localhost ~]# cat /var/www/html/cacti/log/cacti.log
07/22/2020 11:35:00 AM - POLLER: Poller[0] Maximum runtime of 298 seconds exceeded. Exiting.
07/22/2020 11:35:00 AM - SYSTEM STATS: Time:298.2214 Method:cmd.php Processes:1 Threads:N/A Hosts:2 HostsPerProcess:2 DataSources:0 RRDsProcessed:0
07/22/2020 11:40:00 AM - POLLER: Poller[0] Maximum runtime of 298 seconds exceeded. Exiting.
07/22/2020 11:40:00 AM - SYSTEM STATS: Time:298.8324 Method:cmd.php Processes:1 Threads:N/A Hosts:2 HostsPerProcess:2 DataSources:0 RRDsProcessed:0
07/22/2020 11:42:58 AM - CMDPHP: Poller[0] WARNING: Cacti Master Poller process terminated by user
07/22/2020 11:45:01 AM - POLLER: Poller[0] WARNING: Cron is out of sync with the Poller Interval!  The Poller Interval is '300' seconds, with a maximum of a '300' second Cron, but 312 seconds have passed since the last poll!

 

Guess you like

Origin blog.csdn.net/C_huid/article/details/107493890