Linux运维 --- 使用LAMP架构部署Cacti监控

Cacti 在英文中的意思是仙人掌的意思,Cacti是一套基于PHP,MySQL,SNMP及RRDTool开发的网络流量监测图形分析工具。

cacti是用php语言实现的一个软件,它的主要功能是用snmp(简单网络管理协议)服务获取数据,然后用rrdtool(环状数据库)储存和更新数据,当用户需要查看数据的时候用rrdtool生成图表呈现给用户。因此,snmp和rrdtool是cacti的关键。Snmp关系着数据的收集,rrdtool关系着数据存储和图表的生成。

搭建LAMP架构环境

配置Apache

安装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 ~]#

启动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

开放http服务

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

配置mariadb数据库

安装Mariadb数据库

[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 ~]#

启动Mariadb数据库

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

初始化Mariadb数据库

[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 ~]#

创建用于测试PHP和Mariadb数据库连通性的用户chuid

[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 ~]#

重启Mariadb数据库

[root@localhost ~]# systemctl restart mariadb

配置PHP

安装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 ~]#

修改时区

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

 编辑index.php测试页

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

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

打开火狐浏览器输入IP地址出现如下页面,说明PHP环境搭建成功!

配置cacti监控

安装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

创建cacti数据库,并赋予chuid用户对cacti数据库的操作权限

[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]#

将cacti.sql导入数据库

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

编辑config.php和global.php,修改数据库的登录用户和密码

[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;

配置 rrdtool

安装 rrdtool 以生成图像

[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]#

配置SNMP

安装SNMP服务

[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]#

编辑配置文件

[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

重启 snmpd服务程序

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

给chuid用户授予目录权限

[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

配置一个抓图的计划任务

注意:"*/5"表示每隔5分钟执行一次任务

[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

浏览器访问cacti管理页面进行安装

出现这个界面表示安装成功了!

初始账号密码是adminadmin

登录之后会提示要求修改密码

修改好登录密码之后点击Save(保存),自动进入cacti界面

点击右上角第二个按钮,graphs查看cacti图像,选择localhost主机,

右边会显示cacti每5分钟的监控图像

如果没出图像,就需要手动运行

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

查看监控日志

[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!

猜你喜欢

转载自blog.csdn.net/C_huid/article/details/107493890