Cacti安装及模板导入

前言

所以除了学习zabbix,我们也可以了解一下cacti。
但是不是有特别有必要
我们的实验环境是rhel7.6,安装的软件是cacti1.2.

简介

Cacti是一套基于PHP,MySQL,SNMP及RRDTool开发的网络流量监测图形分析工具。他的架构如下图所示:
在这里插入图片描述

  • Cacti是通过 snmpget来获取数据,使用 RRDtool绘画图形,而且你完全可以不需要了解RRDtool复杂的参数。它提供了非常强大的数据和用户管理功能,可以指定每一个用户能查看树状结构、host以及任何一张图,还可以与LDAP结合进行用户验证,同时也能自己增加模板,功能非常强大完善。界面友好。软件 Cacti 的发展是基于让 RRDTool 使用者更方便使用该软件,除了基本的 Snmp 流量跟系统资讯监控外,Cacti 也可外挂 Scripts 及加上 Templates 来作出各式各样的监控图。
  • cacti是用php语言实现的一个软件,它的主要功能是用snmp服务获取数据,然后用rrdtool储存和更新数据,当用户需要查看数据的时候用rrdtool生成图表呈现给用户。因此,snmp和rrdtool是cacti的关键。Snmp关系着数据的收集,rrdtool关系着数据存储和图表的生成。
  • Mysql配合PHP程序存储一些变量数据并对变量数据进行调用,如:主机名、主机ip、snmp团体名、端口号、模板信息等变量。
  • snmp抓到数据不是存储在mysql中,而是存在rrdtool生成的rrd文件中(在cacti根目录的rra文件夹下)。rrdtool对数据的更新和存储就是对rrd文件的处理,rrd文件是大小固定的档案文件(Round Robin Archive),它能够存储的数据笔数在创建时就已经定义。关于RRDTool的知识请参阅RRDTool教学。

安装

安装http以及php及php相关扩展

yum install -y httpd php
yum install –y php-mysql php-snmp php-xml php-ldap php-gd php-mbstring php-posix

更改php时区设置:
在这里插入图片描述打开http并设置为开机启动:

systemctl start httpd && systemctl enable httpd

安装数据库mariadb,配置yum源:

[root@localhost ~]# vim /etc/yum.repos.d/MariaDB.repo
[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

启动并设置为开机启动:

systemctl start mariadb && systemctl enable mariadb

初始化数据库,设置新密码:

[root@server1 ~]# 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!

修改数据库的配置:

[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

安装snmp及其相关扩展(注意版本号得一致)

yum install –y net-snmp net-snmp-libs net-snmp-utils net-snmp-devel net-snmp-perl

配置snmp(注意行号)

[root@server1 ~]# vim /etc/snmp/snmpd.conf 
 41 com2sec notConfigUser  192.168.1.111       public
 62 access  notConfigGroup ""      any       noauth    exact  all  none none
 85 view all    included  .1                               80

开启snmp并设置为开机启动

systemctl start snmpd.service && systemctl enable snmpd.service

安装绘图工具RRDTool绘图工具

安装编译时必要的工具

yum -y install lm_sensors gcc gcc-c++ libart_lgpl-devel zlib-devel libpng-devel freetype-devel  glib2-devel pcre-devel pango-devel cairo-devel  libxml2-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker

下载rrdtool

cd /usr/local/src
wget https://oss.oetiker.ch/rrdtool/pub/rrdtool-1.7.0.tar.gz

编译安装rrdtool

tar zxf rrdtool-1.7.0.tar.gz
cd rrdtool-1.7.0.tar.gz
./bootstrap  ## 可能会少一些包,按照提示进行安装就可以
./configure --prefix=/usr/local/  ## 注意指定安装路径
make && make install ##如果make出错了,记得make clean 后再重新make

安装spine

由于效率的原因,在需要大量采集数据时,如果使用自带的cmd.php轮询器会比较慢,1分钟1次的采集频率可能无法完成轮询所有的被监控的机器,从而可能导致部分监控项目不出图或图形断断续续。为了解决效率问题,Cacti官方也推出spine,采用多线程的方式高效的轮询。

安装编译spine时要用到的相关软件:

yum install -y dos2unix autoconf automake binutils libtool cpp glibc-headers glibc-devel help2man

下载与cacti同版本的spine

cd /usr/local/src
wget https://www.cacti.net/downloads/spine/cacti-spine-1.2.12.tar.gz

编译安装:

tar zxf cacti-spine-1.2.12.tar.gz
cd cacti-spine-1.2.12
./bootstrap
./configure
make && make install
chmod +s /usr/local/spine/bin/spine

编辑spine的配置文件:

vim /etc/spine.conf
DB_Host localhost
DB_Database cacti
DB_User cactiuser
DB_Pass cactiuser
DB_Port 3306

安装中文字体

安装字体相关软件:

yum -y install fontconfig ttmkfdir

建立存放中文字体的文件夹:

cd /usr/shared/fonts
mkdir chinese

去网站下载相关字体放到目录里面:

[root@server1 chinese]# ls
simkai.ttf  simsun.ttc  华文楷体.ttf

执行ttmkfdir命令:

ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir

最好再修改一下字体配置文件,

 23 <!-- Font directory list -->
 24 
 25         <dir>/usr/share/fonts</dir>
 26         <dir>/usr/share/X11/fonts/Type1</dir> <dir>/usr/share/X11/fonts/TTF<    /dir> <dir>/usr/local/share/fonts</dir>
 27         <dir>/usr/share/fonts/chinese</dir>   ## 注意添加这一行到文件中
 28         <dir prefix="xdg">fonts</dir>
 29         <!-- the following element will be removed in the future -->
 30         <dir>~/.fonts</dir>
 31 
 32 <!--

刷新字体库缓存:

fc-cache

安装cacti

下载安装包:

cd /usr/local/src
wget https://www.cacti.net/downloads/cacti-1.2.12.tar.gz
tar zxf cacti-1.2.3.tar.gz
mv cacti-1.2.12 /var/www/html/cacti

创建cacti数据库,创建数据库用户cactiuser,设置用户相关授权

[root@server1 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)

导入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)

配置数据库的时区:

[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.

但是时间似乎有问题,需要改进一下

据实际情况配置cacti的config.php,我测试的所有按默认值,一般是修改第2、4、5行

[root@server1 ~]# vim /var/www/html/cacti/include/config.php

在这里插入图片描述创建 cacti 系统用户,并设置目录权限

useradd cactiuser
chown apache.apache /var/www/html/cacti/ -R
chown cactiuser /var/www/html/cacti/{rra,log}/ -R
chmod 775 /var/www/html/cacti/{rra,log}/ -R

重启相关服务

systemctl restart httpd && systemctl restart mariadb  && systemctl restart snmpd

初始化cacti

用浏览器访问192.168.1.111/cacti/
进行访问之前,请关闭selinux并开放服务器的80端口。
在这里插入图片描述此时的用户名为Admin密码为admin
登陆后修改自己的密码
在这里插入图片描述如果出现错误 (Error)提示,请按提示相应修改
如果出现警告(Warning)则可以不修改,建议生产环境按建议修改为好。
在这里插入图片描述在这里插入图片描述继续
在这里插入图片描述在这里插入图片描述在这里插入图片描述如果出现这个提示,请进行下面的操作

rm -rf /var/www/html/cacti/log/cacti.log
chmod 777 /var/www/html/cacti/log/ -R

然后清理浏览器的缓存(主要是cookie),重新打开浏览器,重新登录(用修改了的密码)就可以了。

在这里插入图片描述请选择正确的RRDtool版本
在这里插入图片描述我们可以再这里把时间改为1分钟,这样会更快的得到结果
在这里插入图片描述在这里插入图片描述在这里插入图片描述Install完成后重新登录
如果刚才已经正确的输入了spine.conf的路径,这里就不用再设置了

在这里插入图片描述将cmd.php改为spine
在这里插入图片描述

应用

管理设备以及创建图形

删除自动生成的设备,
管理–>设备–>选定–>删除

增加设备
在这里插入图片描述注意将设备模板更改
在这里插入图片描述删除没有数据的数据源
在这里插入图片描述建立图形
再控制台–>为新设备创建图形
在这里插入图片描述

加入树

管理–>树–>DefaulTree–>编辑树–>再将设备移入即可–>完成编辑树

在这里插入图片描述在这里插入图片描述

建立定时任务,让系统每分钟更新一次

建立之前重新刷新一下log目录权限,在Cacti系统初始化时生成的log文件权限不对,且用户及组都是apache

chmod -R 775 /var/www/html/cacti/log
chown -R cactiuser /var/www/html/cacti/log

登录用户cactiuser,测试运行,建立crontab。

[root@server1 ~]# su - cactiuser 
Last login: Fri Jul 10 14:05:39 CST 2020 on pts/0
[cactiuser@server1 ~]$ php /var/www/html/cacti/poller.php 
[root@server1 ~]# crontab -e
#For cacti
*/1 * * * * php /var/www/html/cacti/poller.php > /dev/null 2>&1

查看图形的生成:

在这里插入图片描述

其它主机加入监控

安装 net-snmp并按上面的snmpd.conf同样的设置,并开启防火墙相应的端口:161、162就可以了。

添加自定义监控-IO监控

所用软件包:snmpdiskio 0.9.4
解压后,可以查看README文件,按照这个配就行。

  1. 安装到/usr/local/bin/下:install -m 0755 -o root -g root -p -v snmpdiskio /usr/local/bin/

  2. 升级到 0.9.6 :更改snmpdiskio文件:

     Vim snmpdiskio
     #!/bin/bash
     # $Id: snmpdiskio,v 1.5 2008/01/31 21:19:50 dverlaeckt Exp $
     # snmpdiskio v0.9.6 (c) 2008 Dieter Verlaeckt <[email protected]>
     # snmpdiskio v0.9.5 (c) 2007 Pablo Destefanis <[email protected]>
     # snmpdiskio v0.9.4 (c) 2006 Mikael Fridh <[email protected]>
     # Fields in /proc/partitions (kernel 2.4)
     # major minor #blocks name rio rmerge rsect ruse wio wmerge wsect wuse running use aveq
     # Fields in /proc/diskstats (kernel 2.6) for disks (i.e. hda)
     # major minor name rio rmerge rsect ruse wio wmerge wsect wuse running use aveq
     # Fields in /proc/diskstats (kernel 2.6) for partitions (i.e. hda1)
     # major minor name rio rsect wio wsect
     # InBlocks = sectors written to disk
     # OutBlocks = sectors read from disk
     # Set default procfile for kernel 2.4
     PROCFILE="/proc/partitions"
     MODE="linux24"
     # Probably kernel 2.6:
     if [ -f /proc/diskstats ]; then
         PROCFILE=/proc/diskstats
         MODE="linux26"
     fi
     function hdNum()
     {
             awk ' BEGIN { num=0 } $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { num++ } END { print num } ' $PROCFILE
     }
     function hdIndex()
     {
             awk ' BEGIN { num=0 } $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { num++; print num } ' $PROCFILE
     
     }
     function hdDescr()
     {
             if [ "$MODE" = "linux26" ]; then
                     awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { printf "%s/n", $3 }' $PROCFILE
             else
                     awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { printf "%s/n", $4 }' $PROCFILE
             fi
     }
     function hdInBlocks()
     {
             if [ "$MODE" = "linux26" ]; then
                     awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 7 { printf "%.0f/n", $7 * 512 }
                           $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 14 { printf "%.0f/n", $10 * 512 } ' $PROCFILE
             else
                     awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 15 { printf "%.0f/n", $11 * 512 } ' $PROCFILE
             fi
     }
     function hdOutBlocks()
     {
             if [ "$MODE" = "linux26" ]; then
                     awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 7 { printf "%.0f/n", $5 * 512 }
                           $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 14 { printf "%.0f/n", $6 * 512 } ' $PROCFILE
             else
                     awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 15 { printf "%.0f/n", $7 * 512 } ' $PROCFILE
             fi
     }
     function usage()
     {
             cat <<-EOUSAGE
     Usage: $0 <hdNum|hdIndex|hdDescr|hdInBlocks|hdOutBlocks>
     EOUSAGE
     }
     if [ 1 -ne $# ]; then
             usage
             exit 1
     fi
     case $1 in
             hdNum|hdIndex|hdDescr|hdInBlocks|hdOutBlocks)
                     $1
             ;;
             'hdNum')
                     hdNum
             ;;
             'hdIndex')
                     hdIndex
             ;;
             'hdDescr')
                     hdDescr
             ;;
             'hdInBlocks')
                     hdInBlocks
             ;;
             'hdOutBlocks')
                     hdOutBlocks
             ;;
             *)
                     usage
                     exit 1
             ;;
     esac
     exit 0
    

注:为什么要升级?因为kernel 2.4 和 kernel 2.6 中记录磁盘信息的位置不同。在kernel 2.6中,是通过 /proc/diskstats 来记录;在kernel 2.4 中是通过 /proc/partitions 来记录,并且2.6的文件中的文件结构与2.4 中的结构不一样。因此在使用yum等安装的老版本时,需要升级支持kernel 2.6。

  1. 升级完成后,将以下信息,增加到 snmpd.conf 中:

     exec .1.3.6.1.4.1.2021.54 hdNum /usr/local/bin/snmpdiskio hdNum 
     exec .1.3.6.1.4.1.2021.55 hdIndex /usr/local/bin/snmpdiskio hdIndex 
     exec .1.3.6.1.4.1.2021.56 hdDescr /usr/local/bin/snmpdiskio hdDescr 
     exec .1.3.6.1.4.1.2021.57 hdInBlocks /usr/local/bin/snmpdiskio hdInBlocks 
     exec .1.3.6.1.4.1.2021.58 hdOutBlocks /usr/local/bin/snmpdiskio hdOutBlocks
    
  2. 重启snmpd服务后,如果能够通过 /usr/local/bin/snmpdiskio hdNum 或者 hdIndex的方式看到输出,那么则证明snmpdiskio 和 snmpd 的配置正确。

  3. 在cacti 的console界面中,导入 cacti_graph_template_disk_io_bytessec.xml 和 cacti_data_query_snmp_disk_statistics.xml , 同时将 partition.xml 复制到 cacti/resource/snmp_queries/partition.xml 下。

  4. 由于上述导入方法,只是将配置及脚本添加到了cacti中,cacti中已经有了 data template 和 graph template,但是并没有满足cacti通过collection methods 来收集数据,因此目前还无法将磁盘读写数据画成图。下面要做的是,建立data_quiry ,然后将disk I/O 画在界面上。

  5. 参考资料:http://wisekuma.net/networks/cacti-snmp-data-queries/ 简单的过程如下:

  • Console页面中进入Data Queries,点击“add” 增加一个 quiry;

  • Name 输入: Disk I/O ; XML Path 选择刚才复制的 partition.xml的路径及文件;Data Input Method 选择 “get SNMP Data (Indexed)” ;

  • 在Query 建立好以后,增加一个 “Associated Graph Templates”;

  • Name 依旧输入Disk I/O ; Graph Template 选择 Disk I/O (byte/sec) ; 同时选择相应的read 和 write的data source ;

  • 在“suggested values” 中,第一部分,输入:|host_description| – |query_entPhysicalDescr| , Field Name = name ; 第二部分,输入:|host_description| – |query_entPhysicalDescr| , Field name = title;

  1. 建立了以上的Data Query 后,在每台设备的 “Associated data queries” 下拉菜单中就可以选择 “Disk I/O” 作为数据收集的方法,并且进行画图。

后记

因为cacti太老旧了,目前已经很少维护。建议还是用zabbix。

猜你喜欢

转载自blog.csdn.net/Thorne_lu/article/details/107198264