使用cacti + snmp 实现对apache 、mysql 、 nginx 的监控的安装和配置

类别:原创 服务器

第一:说明,软件说明,和安装的目的

架设基于linux下的设备监控服务器,安装前请确保安装好了LMAP环境,mysql、apache正常使用。

snmp配置参照:    
http://javatechnology.iteye.com/blog/768001      
http://wenku.baidu.com/view/b81eef51ad02de80d4d84050.html      
http://os.51cto.com/art/201104/253261.htm      
http://ilexes.blog.51cto.com/705330/243937      
http://www.uplook.cn/index-Index-show-view18831.html?treeid=8567

cacti配置参照:    
http://1076468.blog.51cto.com/1066468/829220      
http://my.oschina.net/yisenn/blog/14626      
http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=594019

cacti使用参照:    
http://hi.baidu.com/xymclone/blog/item/19726fd3c7465b1c3bf3cf3c.html      
http://linux.chinaunix.net/techdoc/net/2006/10/03/941444.shtml

安装cacti插件参照:    
http://www.diybl.com/course/6_system/linux/Linuxjs/2008106/148273.html      
http://hi.baidu.com/shulei1234/blog/item/fd70c6db027290d7b7fd481f.html

故障解决参照:    
http://1076468.blog.51cto.com/1066468/829220

;

第二:本例操作环境

所使用的系统环境为 Centos 5.6 64位操作系统

[root@jedy ~]# uname -a      
Linux jedy.com 2.6.18-238.el5 #1 SMP Thu Jan 13 15:51:15 EST 2011 x86_64 x86_64 x86_64 GNU/Linux      
[root@jedy ~]#

本文的apache mysql使用的是rpm方式安装,apache的网页主目录是/var/www/html。当然可以自己改了。

;

第三:所需要的软件包如下

cacti :http://www.cacti.net/downloads/      
rrdtool:http://packages.express.org/rrdtool/      
cacti插件下载地址:http://docs.cacti.net/plugins      
apache插件ApacheStats_0.8.2.zip 下载地址:http://forums.cacti.net/about25227.html      
nginx插件cacti-nginx.tar.gz下载地址:http://forums.cacti.net/about26458.html

net-snmp:系统光盘里有    
本文使用的是 yum方式安装的

在软件的官网上都能下到。    

第四:安装软件包。

[root@jedy ~]# yum �Cy install rrdtool net-snmp

第五:配置snmp

本处参照了http://javatechnology.iteye.com/blog/768001

1、修改snmpd.conf

只需修改以下几个地方,其它地方保持默认即可。

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

 


#       sec.name  source          community


com2sec notConfigUser  default       test       \\将这里的snmp团体名改成一个好记的名字,当然也可以不改,此处我改成了 test


 


####


# Second, map the security name into a group name:


 


#       groupName      securityModel securityName


group   notConfigGroup v1           notConfigUser


group   notConfigGroup v2c           notConfigUser


 


####


# Third, create a view for us to let the group have rights to:


 


# Make at least  snmpwalk -v 1 localhost -c public system fast again.


#       name           incl/excl     subtree         mask(optional)


view    all           included   .1            \\增加这一行,要用all 不能用systemview 要不然取不到数据。这是增加cpu取值的。


view    systemview    included   .1.3.6.1.2.1.1


view    systemview    included   .1.3.6.1.2.1.2            \\增加这一行。这是增加网卡取值的。


view    systemview    included   .1.3.6.1.2.1.25.1.1


 


####


# Finally, grant the group read-only access to the systemview view.


 


#       group          context sec.model sec.level prefix read   write  notif


access  notConfigGroup ""      any       noauth    exact  all none none              \\将这一行中的systemview改成all 否则影响cpu等系统信息的取值


 


# -----------------------------------------------------------------------------


 


# Here is a commented out example configuration that allows less


# restrictive access.


 


# YOU SHOULD CHANGE THE "COMMUNITY" TOKEN BELOW TO A NEW KEYWORD ONLY


# KNOWN AT YOUR SITE.  YOU *MUST* CHANGE THE NETWORK TOKEN BELOW TO


# SOMETHING REFLECTING YOUR LOCAL NETWORK ADDRESS SPACE.


 


##       sec.name  source          community


com2sec local     localhost       COMMUNITY


com2sec mynetwork 192.168.10.11/24      test \\将这里的snmp团体名改成和上面的一样的名字,允许访问的网络改成你的cacti主机的ip


 


 


##     group.name sec.model  sec.name


#group MyRWGroup  any        local


#group MyROGroup  any        mynetwork


#


#group MyRWGroup  any        otherv3user


#...


 


##           incl/excl subtree                          mask


#view all    included  .1                               80         \\将这一行前面的#去掉,否则影响cpu等系统信息的取值(其实我没改,好像也没关系)


 


## -or just the mib2 tree-


 


view mib2   included  .iso.org.dod.internet.mgmt.mib-2 fc                  \\将这一行前面的#去掉


 


2、开放snmp通信端口


[root@jedy ~]# iptables; -A INPUT -s IP地址 -p tcp --dport 161 -j ACCEPT      
[root@jedy ~]# iptables -A INPUT; -s IP地址 -p udp --dport 161 -j ACCEPT

注意 一定要开udp的端口,以上两行 可以写到 /etc/sysconfig/iptables 中 这样 每次开机后会自动启用的

3、测试snmp

重启 snmp服务器

[root@jedy ~]#

[root@jedy ~]# services snmpd restrart

[root@jedy ~]#netstart �Ctlnp | grep 161 查看服务端口有没有起来


如果没有snmptranslate; snmpwalk命令,请安装net-snmp-utils 包

[root@jedy ~]# snmptranslate -To | head; 如果取到部分oid 说明snmp工作正常 当然这个正常并不代表 cacti就能取到数据。还要进行下面的测试      
.1.3      
.1.3.6      
.1.3.6.1      
.1.3.6.1.1      
.1.3.6.1.2      
.1.3.6.1.2.1      
.1.3.6.1.2.1.1      
.1.3.6.1.2.1.1.1      
.1.3.6.1.2.1.1.2      
.1.3.6.1.2.1.1.3      
[root@jedy ~]#      
[root@jedy ~]# snmpwalk -v 1 -c test localhost 后将会输出很多信息 说明snmp工作没问题      
[root@jedy ~]# snmpwalk -v 1 -c test localhost .1.3.6.1.4.1.2021.11 如果有输出,说明能取到cpu值      
[root@jedy ~]# snmpwalk -v 1 -c test localhost .1.3.6.1.4.1.2021.10.1.3 如果有输出,说明能取到cpu值      
如果上面的测试输出为:End of MIB 说明不能取到cpu的值。

需要注意的是,虽然在snmpd.conf 中配置了cpu的取值,但在cacti的图表中, cpu usage 图表常常是没有曲线图的,但下面是有数值的,这是正常的,只是因为数值太小 没法绘图    

4、snmp排错

[root@jedy ~]# snmpwalk -v 1 -c test localhost 报下面的错误    
End of MIB      
[root@jedy ~]#      
参考:这个错误一般是在 snmp.conf 里view 里面没有允许该OID ,甚至做了显式拒绝,解决办法是允许该 OID.      
加入: view   all          included           1      
本处参照了 http://ilexes.blog.51cto.com/705330/243937 但原文有一点错误      
[root@jedy ~]#

第五:安装cacti

如果使用yum安装的cacti 是不支持插件功能的,如果要想让cacti支持插件功能,请下载cacti-0.8.7i-PIA-3.1.tar.gz安装

本处参照:http://hi.baidu.com/shulei1234/blog/item/fd70c6db027290d7b7fd481f.html

cacti-0.8.7i-PIA-3.1.tar.gz下载地址:http://www.cacti.net/downloads/      
[root@jedy ~]# useradd cacti -G      
[root@jedy ~]# cd /var/www/html      
[root@jedy html]# tar zxvf /software/cacti-0.8.7i-PIA-3.1.tar.gz      
[root@jedy html]# mv cacti-0.8.7i-PIA-3.1 cacti      

1、编辑cacti配置文件

[root@jedy html]# more cacti/include/config.php      
<?php      
/*      
+-------------------------------------------------------------------------+      
| Copyright (C) 2004-2011 The Cacti Group |      
| |      
| This program is free software; you can redistribute it and/or |      
| modify it under the terms of the GNU General Public License |      
| as published by the Free Software Foundation; either version 2; |      
| of the License, or (at your option) any later version.; |      
| |      
| This program is distributed in the hope that it will be useful, |      
| but WITHOUT ANY WARRANTY; without even the implied warranty of; |      
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.; See the |      
| GNU General Public License for more details.; |      
+-------------------------------------------------------------------------+      
| Cacti: The Complete RRDTool-based Graphing Solution |      
+-------------------------------------------------------------------------+      
| This code is designed, written, and maintained by the Cacti Group. See; |      
| about.php and/or the AUTHORS file for specific developer information. |      
+-------------------------------------------------------------------------+      
| http://www.cacti.net/ |      
+-------------------------------------------------------------------------+      
*/      
;
/* make sure these values refect your actual database/host/user/password */      
$database_type = "mysql"; \\数据库类型 (默认)      
$database_default = "cacti" \\cacti使用的数据库      
$database_hostname = "localhost"; \\主机名(默认)      
$database_username = "cacti"; \\访问数据库的用户      
$database_password = "cacti" \\访问数据库的用户的密码      
$database_port = "3306"; \\数据库端口      
$database_ssl = false;      
;
/* Default session name - Session name must contain alpha characters */      
#$cacti_session_name = "Cacti";
?>      
[root@jedy html]# vim /usr/share/cacti/include/global.php 将数据库部分修改成上面相同。ps:我没改这个文件 好像也没影响。

这样做完后,可能会图象不对(前提,你设置的url是http://xxx/cacti),那么需要修改如下的内容.      
vim include/global.php      
$config['url_path'] = ‘/cacti/’;

2、设置cacti数据库

[root@jedy html]# mysql �Cu root �Cp    
password: 输入数据库管理员的密码      
mysql> create database cacti;      
mysql> use cacti;      
mysql> grant all on cacti.* to cacti@localhost identified by "cacti";   
mysql> flush privileges;      
mysql> \q      
[root@jedy html]#      

3、导入cacti数据库

[root@jedy html]# mysql �Cu root �Cp cacti<cacti/cacti.sql      
password: 输入数据库用户cacti的密码; 本处是cacti

4、新建自动化任务

[root@jedy html]# crontab -l      
*/5 * * * * /usr/bin/php /usr/share/cacti/poller.php >/dev/null 2>&1    
[root@jedy html]#      
此处网上说是在php 前面加上 cacti这个用户 让这个用户来执行,但本人试了好多次都没成功 所以只能用root用户来做了。      


第六:使用cacti

本处参照了:

http://hi.baidu.com/xymclone/blog/item/19726fd3c7465b1c3bf3cf3c.html

http://linux.chinaunix.net/techdoc/net/2006/10/03/941444.shtml

1、初始化cacti

在本地浏览器里输入 http://localhost/cacti    
默认用户名/密码是:admin/admin。第一次进入会要求更改密码。然后按照提示 一直进行下一步。      
Console-&gt;Configuration-&gt;Setting-&gt;Genneral-&gt;SNMP Defaults-&gt;SNMP Community      
确认这里的community 的名称是之前我们设置的 test,当然这里的community 会做为之后新建的Devices的默认的community。      
image

;

2、新建监控主机

第一步创建主机 management-&gt;devices-&gt;add-&gt;devices[new]

  1. Descritpion 对服务器的描述信息,任意取一个容易记的名称

  2. Hostname 监控主机的IP地址或主机名,如果使用主机名,别忘了设置/etc/hosts

  3. Host Template 这里实际上是一个监控模板,在创建图形时会有一些默认的监控项,比如CPU、内存等。根据监控主机的类型来选择,如果是Linux系统,通常选择“Local Linux Machine”和“ucd/net SNMP Host”。监控网络设备这里一般选择了“ucd/net SNMP Host”。

  4. SNMP Community 要和snmpd.conf中的团体名一致(和设备中设置的团体名一致比如前面设置的public_cacti)

  5. SNMP Version 如果选择Version 1,就不用设置SNMP Username和SNMP Password两项了,因为只有在Version 3中才会使用。

  6. SNMP Port 默认使用161端口

  7. SNMP Timeout 获取目标主机信息的超时时间

进行完这一步,就完成了一个主机基本信息的描述。保存后在device里就生成了一条记录,点击记录再进入记录条目,此时如果SNMP infomation下面有一个红色提示snmp error说明可能是CACTI服务器连接不到目标主机,也可能是SNMP团体名设置错误,可做适当调整,知道没有错误。

如果没有错误 会出现一些设备信息,如下:    
SNMP Information      
System:Linux jedy.com 2.6.18-238.el5 #1 SMP Thu Jan 13 15:51:15 EST 2011      
x86_64      
Uptime: 1522636 (0 days, 4 hours, 13 minutes)      
Hostname: localhost      
Location: Unknown (edit /etc/snmp/snmpd.conf)      
Contact: Root root@localhost (configure /etc/snmp/snmp.local.conf)

第二步 为主机创建图形      
在上图的界面点击旁边的create Graphs for this host.为刚刚创建的主机创建图形。

实际上这一步看到的界面是和前面选用的模板有关的,另外在下面还可增加监控模板。如果监控模板不对,则显示的图形不合实际或者无法显示。这里可以选择要监控那些信息。最后保存就为这个主机创建了一个流量信息图集。

第三步 创建图形树

做完了第二步,实际上在graphs里是看不到图形的,需要用图形树把图形链接出来,使用自己创建的图形树的目的是为了规划图形分类,让管理更清晰。      
3.1 Management-&gt;Graph Trees-&gt;Graph Trees-&gt;add
; 新建图形树

   
NAME 起一个一级目录的名字    
sorting type分类类型 实际上是目录排序,分别是无排序、安字母、自然排序、数字排序。一般选无排序。

; 创建完了一级目录后,进入一级目录,单击ADD创建二级目录

此项注意: 先选择节点类型,因为选择节点类型后页面要刷新,其他的白选。页面类型中    
; header 是菜单项,意思就是点击后弹出子菜单。    
; graphs 图形 点击后链接到具体的一个图形。    
; host; 主机 点击后现实设备中定义的主机下所有的图形集。    
; 这一级菜单只有header可以定义名称。graphs和host是集成前面定义的名称来的。所有的时候为了看起来清晰明了 需要将你的主机再向下包含一层目录。

; 到此简单的流量监控图就创建完了,主菜单的graphs项列出了我们创建的图形树,点击可看到相应的数据图表。

;

第七:安装、配置cacti插件(monitor thold setting)

本处参照:

http://hi.baidu.com/shulei1234/blog/item/fd70c6db027290d7b7fd481f.html

http://www.diybl.com/course/6_system/linux/Linuxjs/2008106/148273_2.html

插件下载地址:

http://docs.cacti.net/plugins

[root@jedy html]# tar zxvf /software/monitor-v1.2-1.tgz �CC cacti/plugins

[root@jedy html]# tar zxvf /software/thold-v0.4.9-3.tgz �CC cacti/plugins

[root@jedy html]# tar zxvf /software/settings-v0.7-1.tgz �CC cacti/plugins

在cacti的配置文件中增加以下几行:

[root@jedy html]# more /var/www/html/include/config.php      
<?php      
/*      
+-------------------------------------------------------------------------+      
| Copyright (C) 2004-2011 The Cacti Group |      
| |      
| This program is free software; you can redistribute it and/or |      
| modify it under the terms of the GNU General Public License |      
| as published by the Free Software Foundation; either version 2; |      
| of the License, or (at your option) any later version.; |      
| |      
| This program is distributed in the hope that it will be useful, |      
| but WITHOUT ANY WARRANTY; without even the implied warranty of; |      
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.; See the |      
| GNU General Public License for more details.; |      
+-------------------------------------------------------------------------+      
| Cacti: The Complete RRDTool-based Graphing Solution |      
+-------------------------------------------------------------------------+      
| This code is designed, written, and maintained by the Cacti Group. See; |      
| about.php and/or the AUTHORS file for specific developer information. |      
+-------------------------------------------------------------------------+      
| http://www.cacti.net/ |      
+-------------------------------------------------------------------------+      
*/      
;
/* make sure these values refect your actual database/host/user/password */      
$database_type = "mysql";      
$database_default = "cacti";      
$database_hostname = "localhost";      
$database_username = "cacti";      
$database_password = "cacti";      
$database_port = "3306";      
$database_ssl = false;      
;
/*      
Edit this to point to the default URL of your Cacti install      
ex: if your cacti install as at http://serverip/cacti/ this      
would be set to /cacti/      
*/      
$url_path = "/";      
;
/* Default session name - Session name must contain alpha characters */      
$cacti_session_name = "Cacti";      
;
$plugins = array();      
$plugins[] = 'thold';      
$plugins[] = 'settings';      
$plugins[] = 'monitor';      
;
?>

配置cacti插件(monitor等)

Console-&gt;Utilities-&gt;User Management-&gt;admin-&gt;Realm Permission

勾选Plugin Management 后保存

image

Console-&gt;Configuration-&gt;Plugin Management-&gt;Action 激活monitor、settings、thold插件

image

使用cacti插件

1、使用monitor插件

; 1、打开“Console-&gt;Settings-&gt;Misc”,可以调整Monitor的各项配置。例如:勾选“Show Icon Legend”可以在监控页面显示图例;    
“View”可以选用Tiles类型,以显示设备状态表格。      
; 2、为cacti添加新设备时,勾选上“Monitor Host”项。对已添加的设备可以通过“Management-&gt;Devices”进去修改。      
; 3、单击Web页面上方的“monitor”标签链接,可以进入查看各设备/主机的状态图示。      

2、使用thold插件

; 1、打开“Console-&gt;Settings-&gt;Mail/DNS”,可以调整“SMTP Options”和“DNS Options”等配置,用于确保预警邮件能够发出。    
; 2、打开“Console-&gt;Settings-&gt;Alerting/Thold”,可以调整Thold的各项配置。例如:“Dead Host Notifications Email”处可填写设      
备当机时发送警告信息到哪个邮箱;勾选“Send alerts as text”项,可以设置只发送文本格式的邮件。      
; 3、打开“Templates-&gt;Threshold Templates”,可以添加、取出需要关联到预警系统的模板,例如:可以添加和“Interface-Traffic”      
模板的关联,选择“traffic_in”、“traffic_out”数据项,分别设置警告阈值“High Threshold”(如200000000比特/秒);可以添加和“      
Unix-Disk Space”模板的关联……。      
; 4、打开“Create-&gt;New Graphs”,选择需要提供预警的设备,单击右侧上方的“Auto-create thresholds”创建预警项目。      
; 5、打开“Management-&gt;Thresholds”,可以管理已经创建的预警项目。      

第八:配置cacti监控 apache、nginx、mysql

http://apps.hi.baidu.com/share/detail/8457760

1、监控apache

本处参照:http://blog.tianbaoyong.com/systemconfig/cacti-apache-configuration-monitoring-service.html

插件ApacheStats_0.8.2.zip 下载地址:http://forums.cacti.net/about25227.html      
首先是要给被监控服务器上的apache加上mod_status模块,一般默认是装的      
bin/apxs -i -c -a /usr/local/source/httpd-2.2.8/modules/generators/mod_status.c      
-i 表示安装      
-c 编译      
-a 自动增加 LoadModule 到httpd.conf中


修改httpd.conf文件      
<Location /server-status>      
SetHandler server-status      
Order allow,deny      
Allow from all      
</Location>      
ExtendedStatus On //这句千万要加上,apache官方文档上没有这个,如果不增加这个,就不能得到apache使用率等等信息。      
打上补丁后一定要关闭apache再开启,不能restart和graceful

在cacti服务器上将软件包里的ss_apache_stats.php 拷贝到cacti的脚本目录里

[root@jedy ~]#cp ss_apache_stats.php /var/lib/cacti/scripts/      
[root@jedy ~]#chmod 0755 /var/lib/cacti/scripts/ss_apache_stats.php      
[root@jedy ~]#; /var/lib/cacti/scripts/ss_apache_stats.php http://localhost/server-status; 本地连接测试 有输出就是成功      

最后应该看到的形式为http://localhost/server-status    
得出的内容类似于http://www.apache.org/server-status 就算成功了

导入软件包里面的cacti_host_template_webserver_-_apache.xml 模板cacti里

导入方法:Console-&gt;Import/Export-&gt;Import Templates

增加cacti监控:和增加其它机器差不多,只不过要注意 模板选择 “WebServer - Apache”

;

2、监控nginx

本处参照:http://os.51cto.com/art/201104/253004.htm

插件cacti-nginx.tar.gz下载地址:http://forums.cacti.net/about26458.html      
首先要在被监控服务器的nginx.conf文件中加上如下内容      
location /nginx_status {      
stub_status on;     
# disable access_log if requared      
access_log off;      
#allow XX.YY.AA.ZZ;      
#allow YY.ZZ.JJ.CC;      
#deny all;      
}      
最后再重启下nginx就可以了。上面内容可以根据自己需求进行更改的。


在cacti服务器上将软件包里的 get_nginx_clients_status.pl; get_nginx_socket_status.pl 拷贝到cacti的脚本目录里      
[root@jedy ~]#cp get_nginx_clients_status.pl /var/lib/cacti/scripts/      
[root@jedy ~]#cp get_nginx_socket_status.pl /var/lib/cacti/scripts/      
[root@jedy ~]#chmod 0755 /var/lib/cacti/scripts/get_nginx_socket_status.pl      
[root@jedy ~]#chmod 0755 /var/lib/cacti/scripts/get_nginx_clients_status.pl      
然后通过命令行验证是否可以正确运行      
[root@jedy ~]#/var/lib/cacti/scripts/get_nginx_clients_status.pl http://localhost/nginx_status      

如果能看到如下内容表示能正常运行了      
nginx_accepts:1169 nginx_handled:1169 nginx_requests:1225      
如果出现no (LWP::UserAgent not found),报错请进行以下步骤安装perl组件。      
[root@jedy ~]# perl -MCPAN -e shell      
[root@jedy ~]# cpan&gt; install LWP::UserAgent      
导入软件包里面的cacti_graph_template_nginx_clients_stat.xml; cacti_graph_template_nginx_sockets_stat.xml; 模板到cacti里      
导入方法:Console-&gt;Import/Export-&gt;Import Templates      
增加cacti监控:和增加其它机器差不多,只不过要注意 模板选择 “None”然后在Create Graphs for this Host里增加模块      

3、监控mysql

;

第九:故障解决

1、[root@jedy ~]# snmpwalk -v 1 -c test localhost 报下面的错误    
End of MIB      
[root@jedy ~]#      
参考:这个错误一般是在 snmp.conf 里view 里面没有允许该OID ,甚至做了显式拒绝,解决办法是允许该 OID.      
加入: view all included .1      
本处参照了 http://ilexes.blog.51cto.com/705330/243937 但原文有一点错误      
[root@jedy ~]#

2、[root@jedy ~]# snmpwalk -v 1 -c public localhost      
getaddrinfo: localhost1 Name or service not known      
snmpwalk: Unknown host (localhost)

说明 主机名不对或是snmp服务不正常

3、[root@jedy ~]# snmpwalk -v 1 -c test localhost
Timeout: No Response from localhost

说明Community 不正确 或是snmp服务不正常

以下错误是从网上摘抄的,供参考。本人没有碰到过,所以没有测试。

图片不能显示

总结cacti图形采集正确,却无法实现部分监控图像的问题    
有些时候,使用的cacti出现莫名其妙的问题,网卡图形无法显示,其他图形正常显示,      
在确保snmp数据采集正常的情况下,总结如下

1.--console-----system utilities --&gt;Rebuild poller cache    
2.系统时间不准确,重新设置时间      
3.图像没有生成,运行#/usr/bin/php /srv/www/htdocs/cacti/poller.php --force      
4.权限问题,修改权限#chmod 777 -R /srv/www/htdocs/cacti/rra      
5.数据库表有损坏,修复#mysqlcheck -ao cacti --auto-repair -uroot -p      
6.重启snmpd服务#service snmpd restart      
7.cacti的日志太大,清理日志      
8.查看日志,---console-----System Utilities---View Cacti log File"      
一般查看日志,会出现大量数据采集失败的警告

你可能感兴趣的:(apache,nginx,监控,cacti)