運維之路-監(jiān)控篇-Cacti的安裝

Cacti是一套基于PHP,MySQL,SNMP及RRDTool開發(fā)的網(wǎng)絡流量監(jiān)測圖形分析工具。
我的安裝環(huán)境如下
[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)
一、準備工作
????更新系統(tǒng)軟件;
????yum -y update
二、安裝相關(guān)的軟件包
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,據(jù)系統(tǒng)實際情況設定時區(qū);
?????[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數(shù)據(jù)庫;
????yum -y install mariadb mariadb-server
?
2.6 啟動mysql數(shù)據(jù)庫服務并設為開機啟動;
????systemctl start mariadb && systemctl enable mariadb
2.7 配置MariaDB,初始化數(shù)據(jù)庫,設定密碼,除了開始直接回車,其他的選擇都輸入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):?? //初始數(shù)據(jù)庫密碼為空,直接按回車鍵
????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數(shù)據(jù)庫并取消對它的訪問權(quán)限
????......
????Reload privilege tables now? [Y/n] Y?? //刷新授權(quán)表,讓初始化后的設定立即生效
????... Success!
?????
?
2.8 修改MariaDB配置,指定編碼為utf8,防止cacti監(jiān)控在顯示時不能顯示中文,也可以在3.2的步驟中,創(chuàng)建cacti數(shù)據(jù)庫時直接指定編碼格式為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相關(guān)參數(shù),將參數(shù)設置為cacti安裝時推薦的參數(shù);
????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 訪問權(quán)限? 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 創(chuàng)建cacti數(shù)據(jù)庫,創(chuàng)建數(shù)據(jù)庫用戶cactiuser,設置用戶相關(guān)授權(quán);
????[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 默認數(shù)據(jù)庫;
????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 配置數(shù)據(jù)庫時區(qū);
?????[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 據(jù)實際情況配置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 創(chuàng)建日志文件,后續(xù)安裝時用的到;
????touch /var/www/html/cacti/log/cacti.log
????touch /var/www/html/cacti/log/cacti_stderr.log
?
?
3.7 創(chuàng)建 cacti 系統(tǒng)用戶,設置目錄權(quán)限;
?
[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???? #修改如下部分,我全部使用默認參數(shù),所以以下內(nèi)容沒有改動
????DB_Host?????? localhost
????DB_Database?? cacti
????DB_User?????? cactiuser
????DB_Pass?????? cactiuser
????DB_Port?????? 3306
?
????/usr/local/spine/bin/spine? //執(zhí)行此命令,成功后顯示如下
????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服務是否已經(jīng)啟動,并設置開機自啟;
????systemctl status crond
????systemctl enable crond
?
五、 關(guān)閉防火墻和selinux;
5.1? 禁用防火墻? (如果是跑在公網(wǎng)的建議放行相關(guān)端口,不禁用防火墻);
????systemctl stop firewalld.service ??
????systemctl disable firewalld
5.2 禁用selinux;
???? vim /etc/sysconfig/selinux,將SELINUX修改為=disabled,然后重啟,該操作重啟后才會生效,所以一定要重啟系統(tǒng);

5.3 重啟相關(guān)服務;
????systemctl restart httpd.service && systemctl restart mariadb.service && systemctl restart ????snmpd.service && service? ?httpd restart && service mariadb restart
?
5.4 重啟系統(tǒng);
????shutdown -r now
5.6 cacti安裝;
????瀏覽器中輸入http://ip/cacti進入安裝界面,初始賬號密碼為admin/admin,首次登錄需要修改密碼,密碼有復雜性要求,后面基本就是下一步下一步,直到安裝完成;
????安裝完成后修改poller參數(shù),cacti默認使用cmd.php,我裝了spine,所以改成spine;
