目錄
1、Cacti架構
Cacti三層架構:
– 數據採集層:通過SNMP或自定義腳本進行數據採集
– 數據存儲層:通過cacti模板等數據存放至MYSQL中
– 數據展現層:通過WEB方式呈現出來
Cacti實際上就是一個運行php腳本的網站
2、安裝相關的軟件包
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源
[root@localhost ~]# vim /etc/yum.repos.d/MariaDB.repo
# MariaDB 10.3 CentOS repository list - created 2019-05-16 02:03 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
安裝MariaDB
# yum install -y MariaDB-server MariaDB-client MariaDB-devel
啓動mysql數據庫服務並設爲開機啓動
# systemctl start mariadb && systemctl enable mariadb
配置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!
修改MariaDB配置
[root@cacti ~]# 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
max_heap_table_size = 256M
max_allowed_packet = 16777216
tmp_table_size = 64M
join_buffer_size = 64M
innodb_file_per_table = ON
innodb_buffer_pool_size = 1024M
innodb_doublewrite = OFF
innodb_flush_log_at_timeout = 3
innodb_read_io_threads = 32
innodb_write_io_threads =16
innodb_file_format = Barracuda
innodb_large_prefix = 1
2.6 安裝net-snmp
# yum install –y net-snmp net-snmp-libs net-snmp-utils net-snmp-devel net-snmp-perl
配置snmp
# vim /etc/snmp/snmpd.conf
//修改default爲本機(cacti服務器)的ip,修改public爲自己的團體名(一般不改),42行
com2sec notConfigUser 192.168.2.186 public
//把systemview改成all ,供所有snmp 訪問權限 64行
access notConfigGroup "" any noauth exact all none none
view all included .1 80 // 去掉#號 85行
啓動snmp並設置開機啓動
# systemctl start snmpd.service && systemctl enable snmpd.service
監控的端口是161,查看是否已打開
# netstat -an |grep snmp
驗證是否有數據
# snmpwalk -v 2c -c public localhost
如果出現Timeout: No Response from localhost則/etc/snmp/snmpd.conf中的ip地址沒有配置正確或者snmp服務沒啓動。
2.7 安裝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
方法一:用wget下載並安裝rrdtool1.7.0(本人測試安裝成功,但是在cacti頁面路徑處始終過不去,失敗了。)
下載rrdtool 1.7.0
# cd /usr/local/src
# wget https://oss.oetiker.ch/rrdtool/pub/rrdtool-1.7.0.tar.gz
編譯安裝rrdtool
# tar zxvf rrdtool-1.7.0.tar.gz
# cd rrdtool-1.7.0
# ./bootstrap //如果提示沒有libtoolize命令,則運行yum install -y libtool
//一定要指定prefix
# ./configure --prefix=/usr/local/
# make & make install
方法二:直接用yum安裝(yum默認安裝的是rrdtool 1.4.8 ,本人測試成功,建議使用)
# yum install -y rrdtool
2.8. 安裝spine
先安裝編譯spine時的相關軟件
# yum install -y dos2unix autoconf automake binutils libtool cpp glibc-headers glibc-devel help2man
一定要下載與cacti相同版本的
# cd /usr/local/src
# wget https://www.cacti.net/downloads/spine/cacti-spine-1.2.3.tar.gz
編譯安裝
# tar zxvf cacti-spine-1.2.3.tar.gz
# cd cacti-spine-1.2.3
# ./bootstrap
# ./configure
# make && make install
# chown root:root /usr/local/spine/bin/spine
# chmod +s /usr/local/spine/bin/spine
————————————————
如果make時出現
/usr/bin/ld: cannot find -lmysqlclient
collect2: ld returned 1 exit status
make: *** [spine] Error 1
原因:
編譯你的工程代碼之前之前,
先處理一下mysql的庫,默認查找libmysqlclient_r.so,
可是mysql默認爲libmysqlclient.so,
內容完全一樣,做個鏈接即可
請在root下執行
# cd /usr/local/mysql/lib/mysql/ (庫所在目錄)
# ln -s libmysqlclient.so.15.0.0 libmysqlclient_r.so
————————————————
編輯spine.conf:
# cp /usr/local/spine/etc/spine.conf.dist /etc/spine.conf
我測試不作修改,可按實際情況修改/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
3 安裝及配置cacti 1.2.3
3.1 下載安裝包
# cd /usr/local/src
# wget https://www.cacti.net/downloads/cacti-1.2.3.tar.gz
# tar zxvf cacti-1.2.3.tar.gz
# mv cacti-1.2.3 /var/www/html/cacti
3.2 創建cacti數據庫,創建數據庫用戶cactiuser,設置用戶相關授權
[root@localhost src]# mysql -uroot -p
MariaDB [(none)]> create database cacti;
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
3.8 重啓相關服務
# systemctl restart httpd && systemctl restart mariadb && systemctl restart snmpd
3.9 初始化cacti
在瀏覽器訪問:http://ip/cacti,進入安裝界面。
在進入安裝之前建議關閉selinux,另請開放服務器的80端口,我這裏是測試,所以我關閉了防火牆和selinux。
systemctl stop firewalld.service //關閉防火牆
setenforce 0 //臨時關閉selinux
或者用永久關閉
systemctl stop firewalld.service //關閉防火牆
systemctl disable firewalld.service //設置開啓不啓動
sed -i '/^SELINUX=/s/enforcing/disabled/' /etc/selinux/config //永久關閉selinux,需要重啓服務器(只內網使用的話,建議永久關閉)
netstat -ntap | egrep '(80|3306)'
---------如netstat出現未找到命令,則需要安裝net工具包-----
yum install net-tools
選擇接受,再按開始,可以選擇中文界面
如果出現錯誤 (Error)提示,請按提示相應修改,如果出現警告(Warning)則可以不修改,建議生產環境按建議修改爲好。
按next繼續
選擇主服務器繼續
出現這個繼續
全部正確後,再按next繼續
一般選擇5分鐘就行了,生產環境也可以了,我爲了快點得到結果就選擇了一分鐘。
網絡網段,請據實際情況選擇。這個功能挺好用的,Cacti會將同一網段內所有設置好snmp的服務器自動加入監控,不過有些設置選項有問題導致圖片沒數據,我這裏就關閉了。
繼續就會出現以下畫面:
繼續
繼續
Install
完成後重新登錄
4 輪詢器及任務的配置
4.1 設置語言
重新登錄後,如果還是英文,到 系統配置-設置-基本-修改English爲中文,保存,然後重新登錄即可。
4.2 Poller輪詢器配置
spine是一個高速的輪詢器,是cmd.php的替代品,在cmd.php的基礎上,擁有更好的靈活性、速度和併發性。
cacti默認使用的cmd.php進行輪詢,由於我們之前已經安裝好了spine,所以我這裏就更改成spine。
在cacti監控系統中,選擇系統配置→設置→Poller→採集類型選擇spine→保存
將cmd.php改爲spine
4.3 驗證snmp
使用以下命令驗證snmp能否獲取到本機的數據
# snmpwalk -v 2c -c public localhost
如果正常,會返回以下內容
SNMPv2-MIB::sysDescr.0 = STRING: Linux whsir7 3.10.0-957.5.1.el7.x86_64 SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10 DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (208948) 0:34:49.48 SNMPv2-MIB::sysContact.0 = STRING: Root <root@localhost> (configure /etc/snmp/snmp.local.conf) SNMPv2-MIB::sysName.0 = STRING: whsir7 SNMPv2-MIB::sysLocation.0 = STRING: Unknown (edit /etc/snmp/snmpd.conf) SNMPv2-MIB::sysORLastChange.0 = Timeticks: (0) 0:00:00.00 SNMPv2-MIB::sysORID.1 = OID: SNMP-MPD-MIB::snmpMPDCompliance SNMPv2-MIB::sysORID.2 = OID: SNMP-USER-BASED-SM-MIB::usmMIBCompliance SNMPv2-MIB::sysORID.3 = OID: SNMP-FRAMEWORK-MIB::snmpFrameworkMIBCompliance SNMPv2-MIB::sysORID.4 = OID: SNMPv2-MIB::snmpMIB SNMPv2-MIB::sysORID.5 = OID: SNMP-VIEW-BASED-ACM-MIB::vacmBasicGroup SNMPv2-MIB::sysORID.6 = OID: TCP-MIB::tcpMIB SNMPv2-MIB::sysORID.7 = OID: IP-MIB::ip SNMPv2-MIB::sysORID.8 = OID: UDP-MIB::udpMIB SNMPv2-MIB::sysORID.9 = OID: SNMP-NOTIFICATION-MIB::snmpNotifyFullCompliance SNMPv2-MIB::sysORID.10 = OID: NOTIFICATION-LOG-MIB::notificationLogMIB SNMPv2-MIB::sysORDescr.1 = STRING: The MIB for Message Processing and Dispatching. SNMPv2-MIB::sysORDescr.2 = STRING: The management information definitions for the SNMP User-based Security Model. SNMPv2-MIB::sysORDescr.3 = STRING: The SNMP Management Architecture MIB. SNMPv2-MIB::sysORDescr.4 = STRING: The MIB module for SNMPv2 entities SNMPv2-MIB::sysORDescr.5 = STRING: View-based Access Control Model for SNMP. SNMPv2-MIB::sysORDescr.6 = STRING: The MIB module for managing TCP implementations SNMPv2-MIB::sysORDescr.7 = STRING: The MIB module for managing IP and ICMP implementations SNMPv2-MIB::sysORDescr.8 = STRING: The MIB module for managing UDP implementations SNMPv2-MIB::sysORDescr.9 = STRING: The MIB modules for managing SNMP Notification, plus filtering. SNMPv2-MIB::sysORDescr.10 = STRING: The MIB module for logging SNMP Notifications. SNMPv2-MIB::sysORUpTime.1 = Timeticks: (0) 0:00:00.00 SNMPv2-MIB::sysORUpTime.2 = Timeticks: (0) 0:00:00.00 SNMPv2-MIB::sysORUpTime.3 = Timeticks: (0) 0:00:00.00 SNMPv2-MIB::sysORUpTime.4 = Timeticks: (0) 0:00:00.00 SNMPv2-MIB::sysORUpTime.5 = Timeticks: (0) 0:00:00.00 SNMPv2-MIB::sysORUpTime.6 = Timeticks: (0) 0:00:00.00 SNMPv2-MIB::sysORUpTime.7 = Timeticks: (0) 0:00:00.00 SNMPv2-MIB::sysORUpTime.8 = Timeticks: (0) 0:00:00.00 SNMPv2-MIB::sysORUpTime.9 = Timeticks: (0) 0:00:00.00 SNMPv2-MIB::sysORUpTime.10 = Timeticks: (0) 0:00:00.00 HOST-RESOURCES-MIB::hrSystemUptime.0 = Timeticks: (274817) 0:45:48.17 HOST-RESOURCES-MIB::hrSystemUptime.0 = No more variables left in this MIB View (It is past the end of the MIB tree)
4.4 配置crontab
如果不配置此功能,圖形界面全部會顯示The Cacti Poller has not run yet。
Centos7安裝crontab服務(系統默認一般都自帶此服務)
# yum install crontabs -y
配置crontab前,請使用以下命令測試是否可以獲取數據
# /usr/bin/php /var/www/html/cacti/poller.php //注意後面的poller.php地址,一定找確認地址正確。
正常會返回以下內容
OK u:0.00 s:0.01 r:1.02 OK u:0.00 s:0.01 r:1.02 OK u:0.00 s:0.01 r:1.04 OK u:0.00 s:0.01 r:1.04 OK u:0.00 s:0.01 r:1.05 OK u:0.00 s:0.01 r:1.05 OK u:0.00 s:0.01 r:1.06 OK u:0.00 s:0.01 r:1.06 OK u:0.00 s:0.02 r:1.08 OK u:0.00 s:0.02 r:1.08 2019/00/25 00:20:48 - SYSTEM STATS: Time:1.3197 Method:spine Processes:1 Threads:1 Hosts:1 HostsPerProcess:1 DataSources:5 RRDsProcessed:5
使用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
驗證
登陸cacti監控平臺,查看圖形,等個十來分鐘就可以看到了
5 解決圖形中的方框亂碼
如果圖像中的標題漢字出現方框亂碼,可用以下解決
安裝中文字體庫
安裝相關軟件
# yum -y install fontconfig ttmkfdir
建立存放中文字體庫的文件夾
# cd /usr/shared/fonts
# mkdir chinese
將字體上傳到/usr/shared/fonts/chinese
我這裏就只安裝個華文楷體和宋體,上傳方式就不細說了。
執行ttmkfdir命令
# ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir
因爲cacti指定的字體庫也沒有在配置文件內指定,我估計不做下面這一步也是可以的,但我就不測試了。
修改字體配置文件
# vim /etc/fonts/fonts.conf
增加紅色框內一行
刷新字體庫緩存
# fc-cache
查詢已經安裝字體
[root@localhost]# fc-list
/usr/share/fonts/dejavu/DejaVuSansCondensed-Oblique.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Oblique,Oblique
/usr/share/fonts/dejavu/DejaVuSansCondensed-Bold.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Bold,Bold
/usr/share/fonts/chinese/simsun.ttc: SimSun,宋體:style=Regular,常規
/usr/share/fonts/chinese/STKAITI.TTF: STKaiti:style=Regular
/usr/share/fonts/dejavu/DejaVuSans.ttf: DejaVu Sans:style=Book
/usr/share/fonts/chinese/simsun.ttc: NSimSun,新宋體:style=Regular,常規
/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf: DejaVu Sans:style=Bold
/usr/share/fonts/dejavu/DejaVuSansCondensed.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed,Book
/usr/share/fonts/dejavu/DejaVuSans-ExtraLight.ttf: DejaVu Sans,DejaVu Sans Light:style=ExtraLight
/usr/share/fonts/dejavu/DejaVuSansCondensed-BoldOblique.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Bold Oblique,Bold Oblique
/usr/share/fonts/dejavu/DejaVuSans-Oblique.ttf: DejaVu Sans:style=Oblique
/usr/share/fonts/dejavu/DejaVuSans-BoldOblique.ttf: DejaVu Sans:style=Bold Oblique
刷新圖形頁面即能看到方框變成中文了。
6、安裝插件
Cacti插件很多,可以到下面網址找你需要的Cacti插件包
https://github.com/Cacti/
下面以安裝Thold和Monitor插件爲例:
由於develop.zip文件名一樣,我們一個一個的裝,
先裝Thold:
wget https://github.com/Cacti/plugin_thold/archive/develop.zip
unzip develop.zip
#會解壓出叫做plugin_thold-develop的文件夾,改成cacti熟悉的名稱
mv plugin_thold-develop/ thold
#拷貝到Cacti的plugin目錄
[root@localhost html]# mv thold /var/www/html/cacti/plugins
再裝Monitor
wget https://github.com/Cacti/plugin_monitor/archive/develop.zip
unzip develop.zip
#解壓出plugin_monitor-develop文件夾,更改爲cacti能識別的名稱
mv plugin_monitor-develop/ monitor
[root@localhost html]# mv monitor /var/www/html/cacti/plugins
登錄到Cacti Web界面, 插件已經啓用了。
Cacti Console --> Configuration --> Plugin Management
就可以看到 Monitor 與 Thold ,點擊 Actions 的下方的齒輪進行安裝,安裝完之後點擊啓用,如果有歎號鼠標移上去可以看到說明,依賴關係之類。