跳转到主内容
新派编程网 - 新潮编程理念,全新技术课堂!

CentOS 7安装Cacti 1.2.3

目录 1、Cacti架构 2、安装相关的软件包 3 安装及配置cacti 1.2.3 4 轮询器及任务的配置 5 解决图形中的方框乱码 6、安装插件 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 (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 的下方的齿轮进行安装,安装完之后点击启用,如果有叹号鼠标移上去可以看到说明,依赖关系之类。

相关文章