CentOS7.5安装Cacti1.2.2

参考链接:https://blog.csdn.net/networken/article/details/81567337

一、基础环境准备

安装 Cacti 需要 Mysql,PHP,RRDTool,net-snmp 和 支持 PHP 的 Web服务器,如 Apache,Nginx 或 IIS等。
本次使用Mariabdb、PHP、Apache环境

1、环境搭建

1.1、安装Mariadb

配置yum源

1
2
3
4
5
6
7
[root@cacti ~]# cat > /etc/yum.repos.d/MariaDB.repo <<EOF
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF

安装mariadb server和mariadb client

1
# yum install -y MariaDB-server MariaDB-client MariaDB-devel

1.2、安装httpd和php
1
[root@cacti ~]# yum install -y httpd php

安装php扩展

1
[root@cacti ~]# yum install –y php-mysql php-snmp php-xml php-ldap php-gd php-mbstring php-posix

1.3、安装net-snmp简单网络管理协议
1
[root@cacti ~]# yum install –y net-snmp net-snmp-libs net-snmp-utils net-snmp-devel net-snmp-perl
1.4、安装RRDTool 绘图工具
1
[root@cacti ~]# yum install –y rrdtool rrdtool-devel rrdtool-php rrdtool-perl perl-devel perl-CPAN perl-YAML

2、配置webserver

查看httpd.conf配置文件,确保包含以下内容:

1
2
3
[root@cacti ~]# vim /etc/httpd/conf/httpd.conf
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf

配置php.conf配置文件,末尾添加以下内容:

1
2
3
4
5
6
7
8
9
10
11
[root@cacti ~]# vim /etc/httpd/conf.d/php.conf
........
LoadModule php5_module modules/libphp5.so
#
# Cause the PHP interpreter to handle files with a .php extension.
AddHandler php5-script .php
AddType text/html .php
#
# Add index.php to the list of files that will be served as directory
# indexes.
DirectoryIndex index.php

启动httpd服务并设为开机启动

1
[root@cacti ~]#systemctl start httpd && systemctl enable httpd

3、配置php

修改php.ini配置文件,添加或找到如下内容
关闭安全模式并修改时区

1
2
3
4
5
[root@cacti ~]# vim /etc/php.ini
[PHP]
safe_mode = Off
……
date.timezone = Asia/Shanghai

测试php与apache解析是否正常,创建php测试页面

1
2
3
4
[root@cacti ~]# vim /var/www/html/index.php
<?php
phpinfo();
?>

重启httpd服务,并在浏览器里输入服务器ip地址测试http://ip,如果出现php详情页面说明php环境配置成功

4、配置Mysql

启动mysql数据库服务并设为开机启动

1
[root@cacti ~]# systemctl start mariadb && systemctl enable mariadb

初始化MariaDB数据库,主要创建mysql数据库密码,然后全部Y下一步:

修改mysql配置文件,在[mysqld]下增加如下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
character_set_server = utf8mb4
collation-server = utf8mb4_unicode_ci
max_heap_table_size = 256M
max_allowed_packet = 16777216
tmp_table_size = 128M
join_buffer_size = 256M
innodb_file_per_table = ON
innodb_buffer_pool_size = 2048M
innodb_doublewrite = OFF
innodb_flush_log_at_timeout = 3
innodb_read_io_threads = 32
innodb_write_io_threads =16
innodb_buffer_pool_instances = 19

数据库与php测试

1
2
3
4
5
[root@cacti ~]# vim /var/www/html/linkdb.php
<?php
$a=mysql_connect("localhost","root","123456");
if($a){echo "ok";}else{echo "err";}
?>

浏览器打开http://localhost/linkdb.php
显示ok说明配置正确

5、配置SNMP

修改snmp配置文件,找到如下内容修改

1
2
3
4
5
6
7
[root@cacti ~]# vim /etc/snmp/snmpd.conf
//修改default为本机ip,修改public为自己的团体名(可以不改),41行
com2sec notConfigUser default public
//把systemview改成all ,供所有snmp 访问权限 62行
access notConfigGroup "" any noauth exact all none none
//去掉此行注释
view all included .1 80 // 去掉#号 85行

重启snmp服务

1
[root@cacti ~]# systemctl restart snmpd.service && systemctl enable snmpd.service

本机测试snmp数据

1
[root@cacti ~]# snmpwalk -v 2c -c public localhost system

远程测试snmp数据(修改ip为服务器ip,snmpwalk命令需要安装net-snmp)

1
[root@cacti ~]# snmpwalk -v 2c -c public ip system

二、安装和配置Cacti

2.1下载安装包
1
2
3
[root@cacti ~]# wget https://github.com/Cacti/cacti/archive/release/1.2.2.tar.gz
[root@cacti ~]# tar -zxvf 1.2.2.tar.gz
[root@cacti ~]# cp -R cacti-release-1.2.2/ /var/www/html/cacti
2.创建 cacti 数据库

创建cacti数据库,创建数据库用户cactiuser,设置用户相关授权,使用默认cactiuser做为用户名和密码,后面不用修改配置文件

1
2
3
4
5
6
7
8
[root@cacti ~]# mysql –u root -p
MariaDB [(none)]> create database cacti CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

MariaDB [(none)]> grant all on cacti.* to cactiuser@localhost identified by "cactiuser";

MariaDB [(none)]> GRANT SELECT ON mysql.time_zone_name TO 'cactiuser'@'localhost' IDENTIFIED BY 'cactiuser';

MariaDB [cacti]> flush privileges;

3.导入 Cacti 默认数据库
1
2
3
MariaDB [(none)]> use cacti;

MariaDB [cacti]> source /var/www/html/cacti/cacti.sql;

配置数据库时区:

1
[root@cacti ~]# mysql_tzinfo_to_sql /usr/share/zoneinfo/ | mysql -u root -p mysql

4. 修改cacti配置文件config.php

如果使用自定义数据库用户名密码,需要在这里修改一下配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@cacti ~]# vim /var/www/html/cacti/include/config.php

………………

$database_type = 'mysql';
$database_default = 'cacti';
$database_hostname = 'localhost';
$database_username = 'cactiuser';
$database_password = 'cactiuser';
$database_port = '3306';
$database_ssl = false;

……………

5、创建 cacti 系统用户,设置目录权限
1
2
3
4
[root@cacti ~]# useradd -r -M cacti
[root@cacti ~]# chown -R cacti /var/www/html/cacti/{rra,log}/
[root@cacti ~]# chown -R apache.apache /var/www/html/cacti/{resource,cache,scripts}/
[root@cacti ~]# chmod -R 777 /var/www/html/cacti/
6、添加定时任务

将每1分钟执行一次的数据获取脚本加入crontab

1
2
[root@cacti ~]# crontab -e
*/1 * * * * php /var/www/html/cacti/poller.php > /dev/null 2>&1

7、重新启动相关服务
1
2
3
[root@cacti ~]# systemctl restart httpd && systemctl restart mariadb

[root@cacti ~]# systemctl restart crond && systemctl restart snmpd

三、安装spine

cacti默认使用cmd.php来轮询数据,速度会很慢,因此我们采用Spine来轮询数据。cacti-spine是一个由C语言开发的,用于替代cmd.php的快速获取速度的引擎。(也可以不装)

安装spine高速数据采集插件

下载与cacti同版本的spine

1
2
3
4
5
6
7
8
9
10
11
[root@cacti ~]# wget https://www.cacti.net/downloads/spine/cacti-spine-1.2.2.tar.gz
[root@cacti ~]# yum install -y mysql-devel dos2unix autoconf automake binutils libtool gcc cpp glibc-headers glibc-devel help2man
[root@cacti ~]# tar zxvf cacti-spine-1.2.2.tar.gz
[root@cacti ~]# cp -R cacti-spine-1.2.2 /usr/local/spine
[root@cacti ~]# cd /usr/local/spine
# ln -s /usr/lib64/libmysqlclient.so.18.0.0 /usr/lib64/libmysqlclient.so
# sh bootstrap
# ./configure
# make && make install
# chown root:root /usr/local/spine/bin/spine
# chmod +s /usr/local/spine/bin/spine

编辑spine.conf,修改数据库连接相关信息:

1
2
3
4
5
6
7
# cp  /usr/local/spine/etc/spine.conf.dist /etc/spine.conf
# vim /etc/spine.conf
DB_Host localhost
DB_Database cacti
DB_User cactiuser
DB_Pass cactiuser
DB_Port 3306

进行初始化:
/usr/local/spine/bin/spine //执行此命令,成功后显示如下

1
2
3
4
[root@cacti spine]# /usr/local/spine/bin/spine
SPINE: Using spine config file [/etc/spine.conf]
SPINE: Version 1.2.2 starting
SPINE: Time: 0.0385 s, Threads: 5, Devices: 0

四、初始化cacti

重启httpd服务,在浏览器访问:http://ip/cacti,进入安装界面。
检查各个模块时,可能有部分错误,比如php配置参数和数据库配置参数,自行排查,修改配置后,重启相应服务即可,然后按提示按下一步即可。

0%