您的位置:首页 > 运维架构

Cacti 流量监控全步骤、邮件报警实现过程

2014-02-17 15:09 721 查看
一、首先是 lamp 环境,接下来我们顺序安装

[root@localhost src]# yum -y install gcc gcc-c++ make

[root@localhost src]# tar zxf apr-1.4.6.tar.gz

[root@localhost src]# tar zxf apr-util-1.5.2.tar.gz

[root@localhost src]# tar zxf pcre-8.30.tar.gz

[root@localhost src]# tar zxf httpd-2.4.3.tar.gz

[root@localhost src]# cd apr-1.4.6

[root@localhost apr-1.4.6]# ./configure --prefix=/usr/local/apr

[root@localhost apr-1.4.6]# make && make install

[root@localhost apr-1.4.6]# cd ../apr-util-1.5.2

[root@localhost apr-util-1.5.2]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/

[root@localhost apr-util-1.5.2]# make && make install

[root@localhost apr-util-1.5.2]# cd ../pcre-8.30

[root@localhost pcre-8.30]# ./configure --prefix=/usr/local/pcre

[root@localhost pcre-8.30]# make && make install

[root@localhost pcre-8.30]# cd ../httpd-2.4.3

[root@localhost httpd-2.4.3]# ./configure --prefix=/usr/local/apache --enable-so --enable-cgi --enable-rewrite --with-mpm=worker --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre/

[root@localhost httpd-2.4.3]# make && make install

[root@localhost src]# yum -y install cmake ncurses ncurses-devel

[root@localhost src]# groupadd mysql

[root@localhost src]# useradd -r -s /sbin/nologin -g mysql mysql

[root@localhost src]# tar zxf mysql-5.6.4-m7.tar.gz

[root@localhost mysql-5.6.4-m7]# cmake .

[root@localhost mysql-5.6.4-m7]# make && make install

[root@localhost mysql-5.6.4-m7]# cd ../../

[root@localhost local]# chown -R root:mysql mysql/

[root@localhost local]# chown -R mysql mysql/data/

[root@localhost local]# cp mysql/support-files/mysql.server /etc/init.d/mysqld

[root@localhost local]# cp mysql/support-files/my-medium.cnf /etc/my.cnf

cp:是否覆盖"/etc/my.cnf"? y

[root@localhost local]# ./mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/

[root@localhost src]# yum -y install automake autoconf libtool libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel libxslt
libxslt-devel

[root@localhost src]# tar jxf libmcrypt-2.5.8.tar.bz2

[root@localhost src]# tar jxf php-5.4.13.tar.bz2

[root@localhost src]# cd libmcrypt-2.5.8

[root@localhost libmcrypt-2.5.8]# ./configure

[root@localhost libmcrypt-2.5.8]# make && make install

[root@localhost libmcrypt-2.5.8]# cd ../php-5.4.13

[root@localhost php-5.4.13]# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/local/mysql --with-config-file-path=/usr/local/php --enable-mbstring --enable-sockets
--with-gd --with-xsl --with-zlib-dir --with-libxml-dir --with-bz2 --enable-exif --enable-ftp --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --enable-soap --with-gd-native-tty --with-mcrypt

[root@localhost php-5.4.13]# make && make install

[root@localhost php-5.4.13]# cp php.ini-production /usr/local/php/php.ini

[root@localhost local]# vim apache/conf/httpd.conf
## 简单修改几个地方

[root@localhost local]# ./apache/bin/apachectl start

[root@localhost local]# netstat -anpt | grep httpd ## apache 正常

tcp 0 0 :::80 :::* LISTEN 33293/httpd

[root@localhost local]# echo "/usr/local/apache/bin/apachectl start" >> /etc/rc.local

[root@localhost local]# service mysqld start

Starting MySQL..... SUCCESS!

[root@localhost local]# netstat -anpt | grep mysqld
## mysql 正常

tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 33643/mysqld

[root@localhost local]# echo "PATH=$PATH:/usr/local/mysql/bin/" >> /etc/profile

[root@localhost local]# source /etc/profile

[root@localhost local]# chkconfig --add mysqld

[root@localhost local]# chkconfig --level 35 mysqld on

[root@localhost local]# mysqladmin -u root -p password 123456

Enter password:

[root@localhost local]# vim apache/htdocs/test.php ## 添加php探针

[root@localhost local]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT

[root@localhost local]# service iptables save

iptables:将防火墙规则保存到 /etc/sysconfig/iptables:[确定]

[root@localhost local]# service iptables reload
## 客户端访问php探针测试

二、那接下来就是cacti的安装了

[root@localhost ~]# date ## 首先查看一下系统时间对不对 不对要更正一下

2014年 02月 12日 星期三 21:38:11 CST

[root@localhost ~]# yum -y install ntp

[root@localhost ~]# cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

cp:是否覆盖"/etc/localtime"? y

[root@localhost ~]# ntpdate us.pool.ntp.org | hwclock -w

2014年02月12日 星期三 21时59分28秒 -0.105724 seconds

[root@localhost ~]# date

2014年 02月 12日 星期三 14:32:41 CST

[root@localhost src]# tar zxf cacti-0.8.8b.tar.gz -C /usr/local/apache/htdocs/

[root@localhost src]# cd /usr/local/apache/htdocs/

[root@localhost htdocs]# mv cacti-0.8.8b cacti

[root@localhost htdocs]# useradd cactiuser

[root@localhost htdocs]# chown -R cactiuser:cactiuser cacti/rra cacti/log

[root@localhost ~]# mysql -u root -p123456

mysql> create database cactidb;

Query OK, 1 row affected (0.00 sec)

mysql> grant all on cactidb.* to cactiuser@localhost identified by 'cactiuser';

Query OK, 0 rows affected (0.00 sec)

mysql> use cactidb;

Database changed

mysql> source /usr/local/apache/htdocs/cacti/cacti.sql

mysql> quit

[root@localhost ~]# vim /usr/local/apache/htdocs/cacti/include/config.php

$database_type = "mysql";

$database_default = "cactidb";

$database_hostname = "localhost";

$database_username = "cactiuser";

$database_password = "cactiuser";

$database_port = "3306";

$database_ssl = false;

[root@localhost ~]# yum -y install rrdtool rrdtool-devel net-snmp net-snmp-utils

[root@localhost ~]# vim /etc/snmp/snmpd.conf

41 com2sec notConfigUser 192.168.0.218 public
## IP、共同体名

62 access notConfigGroup "" any noauth exact all none none ## 改为all

85 view all included .1 80 ## 注释去掉

[root@localhost ~]# service snmpd start

正在启动 snmpd: [确定]

[root@localhost ~]# chkconfig --add snmpd

[root@localhost ~]# chkconfig --level 35 snmpd on

三、访问地址安装 cacti

访问 http://192.168.0.220/cacti 安装提示下一步,之后会显示页面空白 查看网站错误信息发现如下:

[Wed Feb 12 15:33:55.510859 2014] [:error] [pid 33781:tid 140656689280768] [client 192.168.0.180:63096] PHP Fatal error: Call to undefined function session_unregister() in /usr/local/apache/htdocs/cacti/lib/functions.php on line
408, referer: http://192.168.0.220/cacti/install/index.php
处理方法:

[root@localhost ~]# vim /usr/local/apache/htdocs/cacti/lib/functions.php

408 # session_unregister($var_name); ## 把这行注释掉,刷新页面后就可以继续操作了







接下来点击 Create Graphs for this Host 就可以检测到主机的两块网卡 可以选择添加



[root@localhost ~]# crontab -u cactiuser -e

[root@localhost ~]# crontab -u cactiuser -l ## 这样的话 cacti 就可以出图了

*/2 * * * * /usr/local/php/bin/php /usr/local/apache/htdocs/cacti/poller.php > /dev/null 2>&1



[root@localhost ~]# service crond start

[root@localhost ~]# chkconfig --add crond

[root@localhost ~]# chkconfig --level 35 crond on

[root@localhost ~]# cat /usr/local/apache/logs/error_log ## 这是 cacti 出图无数据的情况 php 时区报错

[Wed Feb 12 16:01:41.030899 2014] [:error] [pid 33783:tid 140656573892352] [client 192.168.0.180:63160] PHP Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting
or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your
timezone. in /usr/local/apache/htdocs/cacti/include/global_arrays.php on line 672, referer: http://192.168.0.220/cacti/graph_view.php?action=tree&tree_id=1&leaf_id=7
处理方法:

[root@localhost ~]# vim /usr/local/php/php.ini

919 date.timezone = "Asia/shanghai" ## 将时区改为上海

[root@localhost ~]# /usr/local/apache/bin/apachectl restart ## 重启 apache 查看 cacti 是否有数据

[root@localhost ~]# iptables -I INPUT -p tcp --dport 199 -j ACCEPT ## 如果检测非本机要开放199端口,以便net-snmp去抓取数据

[root@localhost ~]# service iptables save

[root@localhost ~]# service iptables reload

## 到现在为止 cacti 已经能够正常运行了,总结一下:安装步骤呢就是上述这些,首先保证你的 lamp 环境可以正常运行,之后要检查你的系统时间是否正确,因为这会影响到 cacti 图中是否有数据,接下来的 cacti 安装按步骤来就好了,第一次访问 cacti 时会出现空白页的情况,这是因为 php 的问题导致,高版本的 php 中会话函数被取消,而我们使用的 cacti 版本中还在继续使用,所以我们要注释掉这个函数,装完 cacti 要定义一个任务计划,让 rrdtool 工具来定时画图,有没有图去
cacti/rra/ 这个目录底下一看遍知,出图之后还要注意一下 php.ini 中的时区配置,这可能会导致你的图出的很晚,一般晚八个小时,这个在 apache 的 error.log 中也是有体现出来的,最后:检测主机外的设备时,例如检查其他 Linux 主机,还要 iptables 开放 tcp 199 端口,可以让 net-snmp 去抓取数据(因为我一般 OUTPUT 链是 ACCEPT ,所有不用做什么)、被检测的主机要安装 net-snmp 包,然后按照上面的设置配置 /etc/snmp/snmpd.conf
,这里注意一下, 41 行的 IP 地址为 source 也就是源地址,是你 cacti 监控机的地址,不是被检测主机的地址,如果你的 cacti 在内网,而被监控机在外网,这里需要写你路由器的公网地址(这个地址会变)、然后还有一点要注意,被监控机要开启 udp 161 端口。

四、Cacti 插件的安装

首先说一下,cacti 在安装插件前要确认一下你的这个版本有没有集成plugin架构扩展,这是安装所有插件的前提,在0.8.7i后的cacti版本才集成这个,否则要手动安装。(我使用的是0.8.8b 所以不用安装)这个是插件下载地址,按第一个字母排序 http://docs.cacti.net/plugins
monitor 提供更简略、直观的设备状态图 Settings 、thold 用于设置邮件报警

[root@localhost src]# tar zxf monitor-v1.3-1.tgz

[root@localhost src]# mv monitor ../apache/htdocs/cacti/plugins/

[root@localhost src]# tar zxf settings-v0.71-1.tgz

[root@localhost src]# mv settings ../apache/htdocs/cacti/plugins

[root@localhost src]# tar zxf thold-v0.5.0.tgz

[root@localhost src]# mv thold ../apache/htdocs/cacti/plugins

[root@localhost src]# vim ../apache/htdocs/cacti/include/plugins.php

9 $plugin_hooks = array(); ## 搜索到第九行的字段之后在下一行加入内容,保存退出

10 $plugins[] = 'monitor';

11 $plugins[] = 'settings';

12 $plugins[] = 'thold';

## 接着打开 cacti->console->Plugin Management->在这里可以看到添加的插件名前面有一个蓝色向下的箭头->点击之后在点旁边的绿色向右的箭头就可以显示出你的插件了。

五、配置插件实现邮件报警



































好的 Cacti 全部完工,有什么不懂的地方给我留言!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: