运维之路-监控篇-Cacti的安装(转载https://www.bilibili.com/read/cv17435516/)
Cacti是一套基于PHP,MySQL,SNMP及RRDTool开发的网络流量监测图形分析工具。
我的安装环境如下
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-1160.71.1.el7.x86_64 #1 SMP Tue Jun 28 15:37:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
一、准备工作
更新系统软件;
yum -y update
二、安装相关的软件包
2.1 安装httpd和php;
yum install -y httpd php
2.2 安装php扩展;
yum install –y php-mysql php-snmp php-xml php-ldap php-gd php-mbstring php-posix
2.3 设置php,据系统实际情况设定时区;
[root@localhost ~]# vim /etc/php.ini
[PHP]
……
date.timezone = Asia/Shanghai
2.4 启动httpd并设置开机启动;
systemctl start httpd && systemctl enable httpd
2.5 安装MariaDB 10.3数据库;
yum -y install mariadb mariadb-server
2.6 启动mysql数据库服务并设为开机启动;
systemctl start mariadb && systemctl enable mariadb
2.7 配置MariaDB,初始化数据库,设定密码,除了开始直接回车,其他的选择都输入Y;
[root@localhost ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
......
Enter current password for root (enter for none): //初始数据库密码为空,直接按回车键
OK, successfully used password, moving on...
......
Set root password? [Y/n] Y //输入root管理员密码
New password:
Re-enter new password:
Password updated successfully!
......
Remove anonymous users? [Y/n] Y //删除匿名账号
... Success!
......
Disallow root login remotely? [Y/n] Y //禁止root管理员从远程登录
... Success!
.......
Remove test database and access to it? [Y/n] Y //删除test数据库并取消对它的访问权限
......
Reload privilege tables now? [Y/n] Y //刷新授权表,让初始化后的设定立即生效
... Success!
2.8 修改MariaDB配置,指定编码为utf8,防止cacti监控在显示时不能显示中文,也可以在3.2的步骤中,创建cacti数据库时直接指定编码格式为utf8;
vim /etc/my.cnf.d/server.cnf
[server]
# this is only for the mysqld standalone daemon
character_set_server = utf8mb4
collation-server = utf8mb4_unicode_ci
2.9 配置mysql相关参数,将参数设置为cacti安装时推荐的参数;
vim /etc/my.cnf在[mysql]下新增
skip-grant-tables
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
max_allowed_packet = 16777216
max_heap_table_size = 128M
tmp_table_size = 128M
join_buffer_size = 256M
innodb_file_per_table = ON
innodb_file_format = Barracuda
innodb_large_prefix = 1
innodb_buffer_pool_size = 445M
innodb_additional_mem_pool_size = 80M
innodb_flush_log_at_trx_commit = 2
2.10 安装net-snmp;
yum install –y net-snmp net-snmp-libs net-snmp-utils net-snmp-devel net-snmp-perl
2.11 配置snmp;
vim /etc/snmp/snmpd.conf
//修改default为本机(cacti服务器)的ip或者127.0.0.1,修改public为自己的团体名(一般不改),42行
com2sec notConfigUser 127.0.0.1 public
//把systemview改成all ,供所有snmp 访问权限 64行
access notConfigGroup "" any noauth exact all none none
view all included .1 80 // 去掉#号 85行
2.12 启动snmp并设置开机启动;
systemctl start snmpd.service && systemctl enable snmpd.service
2.13 安装RRDTool 绘图工具;
安装编译时可能使用到的软件;
yum -y install lm_sensors gcc gcc-c++ libart_lgpl-devel zlib-devel libpng-devel freetype-devel gettext-devel glib2-devel pcre-devel pango-devel cairo-devel libxml2-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
yum安装;
yum install -y rrdtool
三、安装及配置cacti 1.2.21
3.1 下载安装包;
cd /usr/local/src
wget https://www.cacti.net/downloads/cacti-latest.tar.gz
tar -zxvf cacti-latest.tar.gz
mv cacti-1.2.21 /var/www/html/cacti
3.2 创建cacti数据库,创建数据库用户cactiuser,设置用户相关授权;
[root@localhost src]# mysql -uroot -padmin
MariaDB [(none)]> create database cacti; (可选:character set utf8 collate utf8_bin;)
Query OK, 1 row affected (0.002 sec)
MariaDB [(none)]> grant all on cacti.* to cactiuser@localhost identified by 'cactiuser';
Query OK, 0 rows affected (0.003 sec)
MariaDB [(none)]> grant select on mysql.time_zone_name to 'cactiuser'@'localhost' identified by 'cactiuser';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.002 sec)
3.3 导入 Cacti 默认数据库;
MariaDB [(none)]> use cacti;
Database changed
MariaDB [cacti]> source /var/www/html/cacti/cacti.sql;
......
MariaDB [cacti]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [cacti]> quit
3.4 配置数据库时区;
[root@localhost ~]# mysql_tzinfo_to_sql /usr/share/zoneinfo/ | mysql -u root -p mysql
Enter password:
Warning: Unable to load '/usr/share/zoneinfo//leapseconds' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo//tzdata.zi' as time zone. Skipping it.
3.5 据实际情况配置cacti的config.php,我测试的所有按默认值,所以没改,一般是修改第2、4、5行;
[root@localhost ~]# 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_retries = 5;
$database_ssl = false;
$database_ssl_key = '';
$database_ssl_cert = '';
$database_ssl_ca = '';
......
3.6 创建日志文件,后续安装时用的到;
touch /var/www/html/cacti/log/cacti.log
touch /var/www/html/cacti/log/cacti_stderr.log
3.7 创建 cacti 系统用户,设置目录权限;
[root@localhost ~]# useradd cactiuser
[root@localhost ~]# chown apache.apache /var/www/html/cacti/ -R
[root@localhost ~]# chown cactiuser /var/www/html/cacti/{rra,log}/ -R
[root@localhost ~]# chmod 777 /var/www/html/cacti/{rra,log}/ -R
四、安装及配置spine和crond(spine的版本要和cacti版本一致,这俩个我都下载的最新版,一般没问题);
4.1 安装spine;
cd /usr/local/src
wget https://www.cacti.net/downloads/spine/cacti-spine-latest.tar.gz
#tar zxvf cacti-spine-latest.tar.gz
#mv cacti-spine-1.2.21 /usr/local/spine
#cd /usr/local/spine
#yum install -y net-snmp-devel mysql-devel openssl-devel dos2unix autoconf automake binutils libtool gcc cpp glibc-headers kernel-headers glibc-devel help2man
#sh bootstrap
#./configure --with-reentrant
#make
#make install
#chown root:root /usr/local/spine/bin/spine
#chmod +s /usr/local/spine/bin/spine
4.2 编辑spine.conf;
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 //执行此命令,成功后显示如下
SPINE: Using spine config file [/etc/spine.conf]
SPINE: Version 1.2.2 starting
SPINE: Time: 0.0225 s, Threads: 1, Devices: 0
4.3 配置creond (crond一般都是默认安装好的,如果没装,就yum install crontabs -y 安装一下)
如果不配置此功能,图形界面全部会显示The Cacti Poller has not run yet;
使用crontab命令添加任务;
# crontab -e
添加以下任务,我这里配置成每五分钟运行一次
*/5 * * * * /usr/bin/php /var/www/html/cacti/poller.php > /dev/null 2>&1
设置crontab自启动,添加好crontab计划任务后,验证下crond服务是否已经启动,并设置开机自启;
systemctl status crond
systemctl enable crond
五、 关闭防火墙和selinux;
5.1 禁用防火墙 (如果是跑在公网的建议放行相关端口,不禁用防火墙);
systemctl stop firewalld.service
systemctl disable firewalld
5.2 禁用selinux;
vim /etc/sysconfig/selinux,将SELINUX修改为=disabled,然后重启,该操作重启后才会生效,所以一定要重启系统;
5.3 重启相关服务;
systemctl restart httpd.service && systemctl restart mariadb.service && systemctl restart snmpd.service && service httpd restart && service mariadb restart
5.4 重启系统;
shutdown -r now
5.6 cacti安装;
浏览器中输入http://ip/cacti进入安装界面,初始账号密码为admin/admin,首次登录需要修改密码,密码有复杂性要求,后面基本就是下一步下一步,直到安装完成;
安装完成后修改poller参数,cacti默认使用cmd.php,我装了spine,所以改成spine;