Brother-Xing
2011/11/14
源码安装 cacti+nagios
目录索引
安装前的准备工作
1、 操作系统要求
2、 配置编译环境以及安装一些
依赖包
3、 关闭不需要的服务(以下未列
出的一律关闭)可选操作
4、 下载所需软件包
LAMP 环境的搭建
1、 mysql 的安装
2、 apache 的安装
3、 php 的安装
4、 验证安装
CACTI 的安装
1、 安装 rrdtool
2、 安装 net-snmp
3、 解压 cacti 包并做好安装前的配
置
4、 安装 cacti-spine
5、 安装 cacti
Nagios 的安装
1、 nagios 的安装
2、 编译并安装 Nagios 插件
3、 设置被监控的 windows 客户端
4、 邮件报警
5、 短信报警
Brother-Xing
2011/11/14
安装前的准备工作
1、 操作系统要求
Mount /dev/cdrom /media
Yum 的配置
root@Cacti+Nagios bin]# cat /etc/yum.repos.d/server.repo
[xingxing]
name=xingxing
baseurl=file:///media/
enable=1
gpgcheck=0
2、配置编译环境以及安装一些依赖包
Yum install –y pango glib pango pango-devel cairo gd gd-devel gcc glibc
glibc-common
3、关闭不需要的服务(以下未列出的一律关闭)可选操作
atd
crond
irqbalance
microcode_ctl
network
sendmail
sshd
syslog
Brother-Xing
2011/11/14
LAMP 环境的搭建
1、mysql 的安装
#groupadd mysql
#useradd -r -g mysql mysql
#cd /usr/local
#tar zxvf /path/to/ mysql-5.0.75.tar.gz
#./configure –prefix=/usr/local/mysql && make && make install
#cd mysql
#chown -R mysql .
#chgrp -R mysql .
#bin/mysql_install_db --user=mysql
#chown -R root .
#chown -R mysql var
#cp share/mysql/my-medium.cnf /etc/my.cnf
# /bin/mysqld_safe --user=mysql &
#cp share/mysql/mysql.server /etc/init.d/mysqld
#chkconfig –add mysqld
#chkconfig –level 3 mysqld on
2、apache 的安装
# tar zxvf httpd-2.2.19.tar.gz
#cd httpd-2.2.19
#./configure --prefix=/usr/local/apache2 --enable-module=so --enable-rewrite &&
make && make install
# cd /usr/local/apache2/bin
# vim apachectl
在前两行添加#chkconfig:35 85 15
#description:apache server
#cp apachectl /etc/init.d/apache
#chkconfig –add apache
#chkconfig apache on
编辑 httpd.conf 文件配置对 PHP 文件的解析
#vi /usr/local/apache/conf/httpd.conf
添加、修改以下
LoadModule php5_module modules/libphp5.so
Brother-Xing
2011/11/14
AddType application/x-httpd-php .php
DirectoryIndex index.html index.php
3、php 的安装
# tar zxvf php-5.2.17.tar.gz
#cd php-5.2.17
#./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs
--with-mysql=/usr/local/mysql --enable-sockets && make && make install
# cp php.ini-dist /usr/local/php/lib/php/php.ini
#vi /usr/local/php/etc/php.ini
4、验证安装
验证 apache 跟 php 是否工作正常,创建 index.php 文件
#vi /usr/local/apache/htdocs/index.php
将以下内容写入到 index.php 并保存推出
修改完后启动 apache,在浏览器中输入 http://服务器地址/index.php 出现下图
示工作正常。
Brother-Xing
2011/11/14
验证 php 与 mysql 是否工作正常,把里面的内容改为
#vi /var/www/html/index.php
修改完后刷新刚刚的页面出现“OK”字样表示工作正常。
Brother-Xing
2011/11/14
至此 LAMP 环境已成功搭建
CACTI 的安装
1、安装 rrdtool
#tar zxvf rrdtool-1.4.5.tar.gz && cd rrdtool-1.4.5
#./configure --prefix=/usr/local/rrdtool && make && make install
2、安装 net-snmp
#tar zxvf net-snmp-5.6.tar.gz && cd net-snmp-5.6
#./configure --prefix=/usr/local/net-snmp
--with-mysql=/usr/local/mysql/bin/mysql_config && make && make install
###########提示信息##############
default version of-snmp-version(3):3(在这里版本通常有三种形式:1,2c,3)
Systemcontact information(配置该设备的联系信息): xingrhce@163.com(也可
以是邮箱地址)
System location (该系统设备的地理位置):TJ P.R.C
Location to write logfile (日志文件位置):/var/log/snmpd.log
Location to Write persistent(数据存储目录):/var/net-snmp
Brother-Xing
2011/11/14
3、解压 cacti 包并做好安装前的配置
#tar zxvf cacti-0.8.7g.tar.gz && cd cacti-0.8.7g
#mv cacti-0.8.7g /usr/local/apache/htdocs/cacti
#useradd cacti
#passwd cacti
将 cacti 目录权限赋予 cacti 用户和 cacti 组
#chown –R cacti:cacti /usr/local/apache/htdocs/cacti
#crontab –u cacti –e
加入以下内容 每隔 1 分钟去 php 代理上的数据(取数据的时间可根据个人需求
更改)
*/1 * * * * /usr/local/php/bin/php /usr/local/apache/htdocs/cacti/poller.php >
/dev/null 2>&1
创建 cacti 数据库
#cd /usr/local/apache/htdocs/cacti
#/usr/local/mysql/bin/mysql –u root –p
mysql> create database cacti;
mysql> grant all on cacti.* to 'cacti'@'localhost' identified by 'cacti';
mysql> flush privileges;
mysql>quit
#/usr/local/mysql/bin/mysql -u cacti -p cacti < cacti.sql
Enter password:
进入 mysql 检查 cacti 数据库中的表是否创建成功
#usr/local/mysql/bin/mysql –u cacti –p
Enter password:
mysql>use cacti;
mysql>show tables;
Brother-Xing
2011/11/14
数据库创建完毕
编辑 cacti 的配置文件 config.php,global.php 设置连接数据库的用户名以及密码
编辑 cacti 的 config.php 文件找到下列参数
#vi /usr/local/apache/htdocs/include/config.php
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cactiuser";
$database_password = "cactiuser";
$database_port = "3306";
修改为
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cacti";
$database_password = "cacti";
$database_port = "3306";
编辑 cacti 的 global.php 文件找到下列参数
#vi /usr/local/apache/htdocs/include/global.php
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cactiuser";
$database_password = "cactiuser";
$database_port = "3306";
修改为
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cacti";
$database_password = "cacti";
$database_port = "3306";
4、安装 cacti-spine
#tar zxvf cacti-spine-0.8.7g.tar.gz && cd cacti-spine-0.8.7g
#./configure --prefix=/usr/local/cacti-spine --with-mysql=/usr/local/mysql
--with-snmp=/usr/local/net-snmp && make && make install
Brother-Xing
2011/11/14
5、安装 cacti
在 IE 中输入 http://服务器地址/cacti
这一步非常重要一定要配置好正确的文件路径 cacti 才能正常工作,下图提示配
置文件不能正常的找到我们可以在 linux 中使用 find 命令来查找相应的文件。
#find / -name rrdtool 来查找各个文件然后将正确的路径输入到相应的文本框中
Brother-Xing
2011/11/14
下图是我在 linux 中查找到的配置文件的路径如果你是按照本文档做的路径应该
和我的是一样的,输入正确路径后还是提示[NOT FOUND]不用管他直接 Finish
Brother-Xing
2011/11/14
初始用户密码都是 admin
登录之后点击“Settings”然后打开“Paths”检查配置文件是否全部正常找到,
下图显示[OK: FILE FOUND]表示配置文件都已工作正常。
等几分钟打开“graphs”如果有图形,那么恭喜你 cacti 已正常工作
如果没有请检查 cacti 目录下的 rra 目录下是否有生成.rra 结尾的文件,如果没有
一般是权限问题请详细检查目录权限
#ls /usr/local/apache/htdocs/cacti/rra
Brother-Xing
2011/11/14
Nagios 的安装
1、nagios 的安装
建立一个帐号
#/usr/sbin/useradd nagios
#passwd nagios
创建一个用户组名为 nagcmd 用于从 Web 接口执行外部命令。将 nagios 用户和
apache 用户都加到这个组中。
/usr/sbin/groupadd nagcmd
/usr/sbin/usermod -G nagcmd nagios
编译与安装 Nagios
#tar zxvf nagios-3.2.3.tar.gz && cd nagios-3.2.3
#./configure --with-command-group=nagcmd
#make all
#make install
#make install-init
#make install-config
#make install-commandmode
配置 web 接口编辑 httpd.conf 文件
#vim /usr/local/apache/conf/httpd.conf
在配置文件的最后边添加一下语句
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
Alias /nagios "/usr/local/nagios/share"
Options None
Brother-Xing
2011/11/14
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
创建一个 nagiosadmin 的用户用于 Nagios 的 WEB 接口登录。记下你所设置的登
录口令,一会儿你会用到它。
#/usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users
nagiosadmin
设置密码
New password:
重启 Apache 服务以使设置生效。
#/usr/local/apache/bin/httpd -k restart
2、编译并安装 Nagios 插件
#tar zxvf nagios-plugins-1.4.15.tar.gz && cd nagios-plugins-1.4.15
#./configure --with-nagios-user=nagios --with-nagios-group=nagios
#make
#make install
把 Nagios 加入到服务列表中以使之在系统启动时自动启动
#chkconfig --add nagios
#chkconfig nagios on
验证 Nagios 的样例配置文件
#/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
如果没有报错,可以启动 Nagios 服务
#service nagios start
打开 IE 浏览器输入 http://服务器地址/nagios 弹出用户密码界面输入刚刚设置
好的用户名 nagiosadmin 和密码,回车后出现下图表示 nagios 已正常安装。
恭喜您已经成功安装了 cacti 与 nagios。
Brother-Xing
2011/11/14
3.被监控的 windows 2003 客户端的配置
1、安装 NSClient++并安装
下载地址: http://sourceforge.net/projects/nscplus
NSClient++-0.3.8-Win32.msi
NSClient++-0.3.8-x64.msi
2、安装 NSClient++
双击安装 -> next -> 同意 -> next -> next -> next -> 设置允许 IP,设置密码(这里密码一
般可以不设置),Modules to load (选前三个模块就可以了,第 2 个为 chect_nt 用于监控
windows,第 3 个为 checp_nrpe 用于监控 linux) -> next -> install -> finish
3、启动 NSClientpp 服务,并允许服务交互式
Brother-Xing
2011/11/14
4、如果 windows 开启了自带的防火墙,那么必须设置让该程序通过
5 监控端主机的设置(Nagios 服务器)
1 、 检 查 Nagios 目 录 下 的 libexec 子 目 录 , 一 定 要 存 在 check_nt ( 例 如 :
/usr/local/nagios/libexec/check_nt)
2、配置 command.cfg 文件(例如:/usr/local/nagios/etc/objects/command.cfg),增加如
下几行(默认应该已经有了)
#/usr/local/nagios/etc/objects/command.cfg
# 'check_nt' command definition
define command{
command_name check_nt
command_line $USER1$/check_nt -H $HOSTADDRESS$ -p 12489 –s 密码 -v
$ARG1$ $ARG2$
}
3、编译 nagios.cfg 文件
#/usr/local/nagios/etc/nagios.cfg,打开下面这一行
# Definitions for monitoring a Windows machine
cfg_file=/usr/local/nagios/etc/objects/windows.cfg
#vi /usr/local/nagios/etc/objects/windows.cfg,下面为配置主要的监控项目
Brother-Xing
2011/11/14
#定义要被监控的 windows 主机名与主机的 IP 地址
define host{
use windows-server
host_name LSA
alias My Windows Server
address 192.168.0.5
}
#定义一个主机名组(可以不用管它)
define hostgroup{
hostgroup_name windows-servers
alias Windows Servers
}
#检查 windows 主机是否已经安装了 NSClient++,及它的版本号
define service{
use generic-service
host_name LSA
service_description NSClient++ Version
check_command check_nt!CLIENTVERSION
}
#监控 windows 主机的开机运作时间
define service{
use generic-service
host_name LSA
service_description Uptime
check_command check_nt!UPTIME
}
#监控 windows 主机的 CPU 负载
define service{
use generic-service
host_name LSA
service_description CPU Load
check_command check_nt!CPULOAD!-l 5,80,90 #CPU 如果到达 80%则报警,到
达 90%则警笛
}
#监控 windows 主机的内存使用状况
define service{
Brother-Xing
2011/11/14
use generic-service
host_name LSA
service_description Memory Usage
check_command check_nt!MEMUSE!-w 80 -c 90 #内存使用到达 80%则 warn,到
达 90%则 Critical
}
#监控 windows 主机的 C:\ 的空间使用量
define service{
use generic-service
host_name LSA
service_description C:\ Drive Space
check_command check_nt!USEDDISKSPACE!-l c -w 80 -c 90
}
#监控 windows 主机的 W3SVC 设置的动作状况
define service{
use generic-service
host_name LSA
service_description W3SVC
check_command check_nt!SERVICESTATE!-d SHOWALL -l W3SVC
}
#监控 windows 主机的 Explorer.exe 执行程序的运作状况,如程序终止了,则会发
CRITICA
define service{
use generic-service
host_name LSA
service_description Explorer
check_command check_nt!PROCSTATE!-d SHOWALL -l Explorer.exe
}
3、检查 nagios.cfg 文件有没有错误
#nagios -c nagios.cfg
4、重新启动一下 nagios 服务
#service nagios restart
5、访问页面进行查看
# http://localhost/nagios
Brother-Xing
2011/11/14
4.nagios 的报警
1 安装 sendmail 组件
首先要确保 sendmail 相关组件的完整安装,我们可以使用如下的命令来完成 sendmail 的安
装:
# yum install -y sendmail*
然后重新启动 sendmail 服务:
# service sendmail restart
然后发送测试邮件,验证 sendmail 的可用性:
# echo "Hello World" | mail abc@abc.com
2 邮件报警的配置
下面是我的邮件报警的配置过程:
1、开启系统自带的 sendmail 邮件系统
service sendmail start
如果没有安装,可以 yum 一下
yum install -y sendmail*
service sendmail restart
测试发送邮件:
echo "test" | mail lihuipeng007@163.com
我系统在关掉 iptables 和 selinux 前提下是正常的
2、配置 nagios 中报警邮箱
vi /usr/local/nagios/etc/objects/contacts.cfg
define contact{
contact_name nag
iosadmin ; Short name of user
use
Brother-Xing
2011/11/14
generic-contact ; Inherit default values from
generic-contact template (defined above)
alias
Nagios Admin ; Full name of user
email
lihuipeng007@163.com ; <<***** CHANGE THIS TO YOUR EMAIL
ADDRESS ******
}
在 email 那里加上自己的邮箱,如果有多个可以用逗号隔开
3、设置报警方式:
vi /usr/local/nagios/etc/objects/templates.cfg
define contact{
name generic-contact ; The name of this contact template
service_notification_period 24x7 ; service notifications can be sent
anytime
host_notification_period 24x7 ; host notifications can be sent anytime
service_notification_options w,u,c,r,f,s ; send notifications for all service
states, flapping events, and scheduled downtime events
host_notification_options d,u,r,f,s ; send notifications for all host states,
flapping events, and scheduled downtime events
service_notification_commands notify-service-by-email ; send service notifications via
email
host_notification_commands notify-host-by-email ; send host notifications via
email
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A
REAL CONTACT, JUST A TEMPLATE!
}
这两个 notify-service-by-email 和 notify-host-by-email 是在 command.cfg 那里定义的,
看看是怎么定义的:
Brother-Xing
2011/11/14
# 'notify-host-by-email' command definition
define command{
command_name notify-host-by-email
command_line /usr/bin/printf "%b" "*****
Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost:
$HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo:
$HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "**
$NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **"
$CONTACTEMAIL$
}
# 'notify-service-by-email' command definition
define command{
command_name notify-service-by-email
command_line /usr/bin/printf "%b" "*****
Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService:
$SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState:
$SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional
Info:\n\n$SERVICEOUTPUT$\n" | /bin/mail -s "**
$NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is
$SERVICESTATE$ **" $CONTACTEMAIL$
}
这里已经默认配置好,我们不用修改直接调用就是了
最后重启服务:
service nagios retart
然后随便关掉一些服务测试一下效果!很简单吧~
五 .飞信报警
1)1.飞信首先手机要开通中国移动的飞信业务。目前使用飞信发送短信是免费的。需要
把接受短信的手机和飞信发送手机加为好友。
2.下载飞信的 linux 客户端程序.解开包后只有一个文件 sms。不错。这个就是飞信发送
短信的程序啦。现在的飞信还是不能用的。因为它需要 libace 的 glibc 库的支持。
2) 飞信 linux 程序下载:
Brother-Xing
2011/11/14
1、 飞信官方网站:http://www.it-adv.net/
2、 飞信官方命令下载:sms
http://www.it-adv.net/fetion/fetion_linux_20080402.tar.gz
3、 飞信官方 lib 库下载:
32 位官方 lib 库:http://www.it-adv.net/fetion/library32.rar
64 位官方 lib 库:http://www.it-adv.net/fetion/library64_linux.tar.gz
3)飞信程序测试:
1、 飞信命令测试:
# wget http://www.it-adv.net/fetion/fetion_linux_20080402.tar.gz
# tar xvfz fetion_linux_20080402.tar.gz
# cp sms /usr/bin
# sms -h
sms -f mobile -p pwd -t mobile1,.. -m message -a message -d
sms -f mobile -p pwd -t mobile1,.. -i file_name[utf8] -a invite_message -d 1
-f:Fetion mobile account(only supports mobile phone No.)
-p:Account password
-t:Destination mobile list
-m:Message
-i:File name(only supports utf8)
-a:Auto send invite using invite_message.
-d:Debug on.
如果没有安装 lib 库,将会提示 缺少 libACE.so.5.4.7 包
4)解决缺少 lib 库的方法:
说明: 在 centOS5 下装提示需要 libACE.so.5.4.7 这个库,千万不要自己去安装 ACE 包,
很麻烦,官方已经给了 liunx 的下载版本:
1.先检查一下缺少的 lib 文件
[root@nagios libexec]# ldd sms
linux-gate.so.1 => (0x00b00000)
libACE.so.5.4.7 => not found
libACE_SSL.so.5.4.7 => not found
libssl.so.0.9.8 => not found
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x02a05000)
libm.so.6 => /lib/libm.so.6 (0x00cd4000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x029f7000)
libc.so.6 => /lib/libc.so.6 (0x00b92000)
/lib/ld-linux.so.2 (0x001c3000)
2.下载 lib 库:
32 位官方 lib 库:http://www.it-adv.net/fetion/library32.rar
64 位官方 lib 库:http://www.it-adv.net/fetion/library64_linux.tar.gz
3、安装 lib 库
Brother-Xing
2011/11/14
[root@nagios library32]# ls
libACE.so.5.4.7 libACE_SSL.so.5.4.7 libcrypto.so.0.9.8 libssl.so.0.9.8
[root@nagios library32]# cp *.* /lib/
[root@nagios library32]# cp *.* /usr/lib/
4、测试 sms:
[root@nagios libexec]# ./sms
************************ IMPORTANT STATEMENT ************************
** **
** PLEASE DON'T USE THIS SOFTWARE TO SEND JUNK SHORT MESSAGES. **
** OTHERWISE PLEASE BEAR YOUR OWN CONSEQUENCES. **
** **
** [version:2008-4-2] **
*********************************************************************
This program is for sending short messages via CMCC Fetion!
http://www.fetion.com.cn/
AUTHOR:KelvinHan MSN/EMAIL:shichangguo@msn.com 2008/3/8
Usage:
sms -f mobile -p pwd -t mobile1,.. -m message -a message -d
sms -f mobile -p pwd -t mobile1,.. -i file_name[utf8] -a invite_message -d 1
-f:Fetion mobile account(only supports mobile phone No.)
-p:Account password
-t:Destination mobile list
-m:Message
-i:File name(only supports utf8)
-a:Auto send invite using invite_message.
-d:Debug on.
5)Nagios 飞信测试:
1、 需要注册一个可以使用飞信的手机号码,测试期间可以自己发送给自己做测
试。
2、 给别的手机发飞信,需要给把手机绑定为好友。
3、 飞信环境解决了,发短信测试一下
136933XXXXX 为发飞信的注册手机号码
136833XXXXX、36733XXXXX 为接收飞信短信的手机号码
[root@nagios libexec]# ./sms -f 136933XXXXX -p password -t 136833XXXXX -m hello,this is
test
也可以要发的消息在一个文本文件里,如 message.txt
./sms -f 136933XXXXX -p password -t 136833XXXXX,136733XXXXX -i message.txt
如果接收短信的手机号码在你的好友列表里,就会发送成功。
Brother-Xing
2011/11/14
如果不在好友列表里,发送是不成功的。如果你想让程序邀请他们加入,那么就用 -a 参
数
sms -f 136933XXXXX -p password -t 136833XXXXX,136733XXXXX -m 你好,飞信 -a UBUNTU
这样,对方就会收到移动发的确认短信。
6)Nagios 配置:
1