监控服务器端的安装部署
一、apache的安装
下载httpd-2.2.15.tar.gz
gunzip httpd-2.2.15.tar.gz
tar xvf httpd-2.2.15.tar
cd httpd-2.2.15
./configure --prefix=/usr/local/apache2 --enable-so --enable-mods-shared=all --enable-cgi --enable-rewrite --enable-deflate --with-mpm=worker
make
make install
安装完后增加nagios用户
groupadd nagios
useradd -g nagios -d /home/nagios -m nagios
passwd nagios
修改httpd.conf中的user group为下面的参数
User nagios
Group nagios
修改ServerName=ip(服务器的ip地址):80
mkdir /usr/local/nagios
chown nagios:nagios /usr/local/nagios
二、安装php+mysql(mysql可以不用安装,为了以后使用最好先安装)
安装mysql
rpm -ivh MySQL-server-community-5.1.46-1.rhel5.i386.rpm
rpm -ivh MySQL-devel-community-5.1.46-1.rhel5.i386.rpm
rpm -ivh MySQL-client-community-5.1.46-1.rhel5.i386.rpm
安装php前先安装以下软件:
安装zlib (安装libpng和gd前需要先安装zlib),
gunzip zlib-1.2.3.tar.gz
tar xvf zlib-1.2.3.tar
cd zlib-1.2.3
./configure --prefix=/usr/local/zlib
make
make install
安装libpng,
gunzip libpng-1.2.18.tar.gz
tar xvf libpng-1.2.18.tar
cd libpng-1.2.18
./configure --prefix=/usr/local/libpng
make
make install
安装freetype
gunzip freetype-2.3.12.tar.gz
tar xvf freetype-2.3.12.tar
cd freetype-2.3.12
./configure --prefix=/usr/local/freetype
make
make install
4. 安装jpeg
gunzip jpegsrc.v6b.tar.gz
tar xvf jpegsrc.v6b.tar
cd jpeg-6b
mkdir /usr/local/jpeg
mkdir /usr/local/jpeg/bin
mkdir /usr/local/jpeg/lib
mkdir /usr/local/jpeg/include
mkdir /usr/local/jpeg/man
mkdir /usr/local/jpeg/man/man1
./configure --prefix=/usr/local/jpeg --enable-shared --enable-static
make
make install
5. 安装gd,
gunzip gd-2.0.35.tar.gz
tar xvf gd-2.0.35.tar
cd gd-2.0.35
./configure --prefix=/usr/local/gd --with-jpeg=/usr/local/jpeg --with-freetype=/usr/local/freetype --with-png --with-zlib
//编译过程中会看到如下信息
** Configuration summary for gd 2.0.35:
Support for PNG library: yes
Support for JPEG library: yes
Support for Freetype 2.x library: yes
Support for Fontconfig library: no
Support for Xpm library: no
Support for pthreads: yes
//可以看到png 、 jpeg 、 freetype都已经安装上了
make
make install
上面软件都安装好后,现在就可以安装php了
gunzip php-5.3.2.tar.gz
tar xvf php-5.3.2.tar
cd php-5.3.2
./configure --prefix=/usr/local/php5 --with-mysql --with-apxs2=/usr/local/apache2/bin/apxs --with-gd=/usr/local/gd --with-zlib --with-libpng=/usr/local/libpng --with-jpeg=/usr/local/jpeg --with-freetype=/usr/local/freetype --enable-sockets --with-iconv --enable-mbstring --enable-track-vars --enable-force-cgi-redirect --with-config-file-path=/usr/local/php5/etc
make
make install
安装完后执行
cp php.ini-development /usr/local/php5/etc/php.ini
最后修改httpd.conf,使apache能使用php,增加如下参数
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
三、安装nagios软件
下载nagios-3.2.1.tar.gz
gunzip nagios-3.2.1.tar.gz
tar xvf nagios-3.2.1.tar
cd nagios-3.2.1
./configure --prefix=/usr/local/nagios --with-gd-lib=/usr/local/lib --with-gd-inc=/usr/local/include
make all //编译nagios
make install //安装主要的程序,CGI及HTML文件
make install-init //在/etc/rc.d/init.d安装启动脚本
make install-commandmode //给外部命令访问nagios配置文件的权限
make install-config //将配置文件的例子复制到nagios的安装目录
验证程序是否被正确安装
看是否存在etc,bin,sbin,share,var五个目录
bin Nagios执行程序所在目录,nagios文件即为主程序
etc Nagios配置文件位置
sbin Nagios cgi文件所在目录,执行外部命令所需文件所在的目录
share Nagios网页文件所在的目录
var Nagios日志文件,spid等文件所在的目录
四、安装nagios插件
下载nagios-plugins-1.4.14.tar.gz
gunzip nagios-plugins-1.4.14.tar.gz
tar xvf nagios-plugins-1.4.14.tar
cd nagios-plugins-1.4.14
./configure --prefix=/usr/local/nagios-plugins
make all
make install
安装完成以后在/usr/local/nagios-plugins会产生一个libexec的目录,将该目录全部移动到/usr/local/nagios目录下
cp -r /usr/local/nagios-plugins/libexec /usr/local/nagios/libexec
chown -R nagios:nagios /usr/local/nagios/
五、安装imagepak-base
下载imagepak-base.tar.gz
gunzip imagepak-base.tar.gz
tar xvf imagepak-base.tar
解压以后是一个base目录,将该目录拷贝到 /usr/local/nagios/share/images/logos目录下
cp -r base /usr/local/nagios/share/images/logos
chown -R nagios:nagios /usr/local/nagios/share/images/logos
六、配置httpd.conf使用nagios
在httpd.conf后面加入如下内容
vi /usr/local/apache2/conf/httpd.conf
#Setting for nagios
ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin
<Directory "/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
Require valid-user
</Directory>
Alias /nagios /usr/local/nagios/share
<Directory "/usr/local/nagios/share">
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
</Directory>
对增加后的参数进行验证
/usr/local/apache2/bin/apachectl -t //检查配置文件是否正确
生成apache访问的认证文件并启动apache进行验证
/usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd nagios
cat /usr/local/nagios/etc/htpasswd //查看认证文件内容
/usr/local/apache2/bin/apachectl start //启动apache
到目前为止,监控主机上的nagios的所有安装已经全部完成,接下来就是要配置具体的监控项目了
七、配置nagios(主要是定义监控的对象所存放的是哪些文件)
预备知识 :
在Nagios里面定义了一些基本的对象,一般用到的有:
监控时间段 timeperiod 7X24小时不间断还是周一至周五,或是自定义的其他时间段
联系人 contact 出了问题向谁报告?一般当然是系统管理员了
被监控主机 Host 所需要监控的服务器,当然可以是监控机自己
监控命令 command nagios发出的哪个指令来执行某个监控,这也是自己定义的
被监控的服务 Service 例如主机是否存活,80端口是否开,磁盘使用情况或者自定义的服务等
注意:多个被监控主机可以定义为一个主机组,多个联系人可以被定义为一个联系人组
修改Nagios的配置文件:
------------------------------------------------------------------------
vi /usr/local/nagios/etc/nagios.cfg //修改nagios的主配置文件
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg //注释此行
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg //监视时段配置文件路径
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg //联系人配置文件路径
cfg_file=/usr/local/nagios/etc/objects/commands.cfg //监控命令配置文件路径
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg //主机配置文件路径
cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg //主机组配置文件路径
cfg_file=/usr/local/nagios/etc/objects/services.cfg //服务配置文件路径
cfg_file=/usr/local/nagios/etc/objects/contactgroups.cfg //联系组配置文件路径
check_external_commands=0 //将 0 改成 1,允许在web界面下执行重启Nagios
command_check_interval=60s //改成 60s, 命令检查时间间隔
check_external_commands=0 //将0改为1,允许在web界面执行external_commands
然后检查配置文件是否出错
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
如果正常就显示如下信息
Total Warnings: 0
Total Errors: 0
因为默认的nagios配置文件没有 hosts.cfg、hostgroups.cfg等文件,因此在检查的时候会报错,这时需要手工的去创建这些文件:
cd /usr/local/nagios/etc/objects
touch hosts.cfg
touch hostgroups.cfg
..........
建完后再检查是否报错:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
八、配置cgi.cfg (主要是)
vi cgi.cfg //修改cgi脚本控制文件
use_authentication=1 //确保值为 1
default_user_name=nagios //修改为认证用户
//后面修改内容如下:
authorized_for_system_information=nagiosadmin,nagios
authorized_for_configuration_information=nagiosadmin,nagios
authorized_for_system_commands=nagios //多个用户之间用逗号隔开
authorized_for_all_services=nagiosadmin,nagios
authorized_for_all_hosts=nagiosadmin,nagios
authorized_for_all_service_commands=nagiosadmin,nagios
authorized_for_all_host_commands=nagiosadmin,nagios
-------------------------------------------------------------------------
九、配置misccommands.cfg
vi misccommands.cfg //主要功能是用于发送报警短信和报警邮件
#host-notify-by-sms //发送短信报警
define command {
command_name host-notify-by-sms
command_line /usr/local/bin/sms_send "Host $HOSTSTATE$ alert for $HOSTNAME$! on '$DATETIME$' " $CONTACTPAGER$
}
#service notify by sms //发送短信报警
define command {
command_name service-notify-by-sms
command_line /usr/local/bin/sms_send "'$HOSTADDRESS$' $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$" $CONTACTPAGER$
}
十、定义nagios各对象的具体配置文件:
vi /usr/local/nagios/etc/objects/timeperiods.cfg //定义监控时间段,名称是24*7,监控时间是全天24小时
define timeperiod {
timeperiod_name 24x7 //时间段的名称,这个地方不要有空格
alias 24 Hours A Day,7Days A Week
sunday 00:00-24:00
monday 00:00-24:00
tuesday 00:00-24:00
wednesday 00:00-24:00
thursday 00:00-24:00
friday 00:00-24:00
saturday 00:00-24:00
}
vi contacts.cfg //定义联系人
define contact {
contact_name sa //不要有空格
alias system administrator
service_notification_period 24x7 //服务出了状况通知的时间段,由timeperiods.cfg中定义的.
host_notification_period 24x7 //主机出了状况通知的时间段,由timeperiods.cfg中定义的.
service_notification_options w,u,c,r //当服务出现故障
host_notification_options d,u,r //当主机出现故障
service_notification_commands service-notify-by-sms,service-notify-by-email //命令读配置miscommands.cfg
host_notification_commands host-notify-by-email,host-notify-by-sms //命令读配置miscommands.cfg
email monitor@xxx.com
pager 132********
}
define contact {
contact_name ritto
alias system administrator
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-by-email
//notify-by-mail是在commands.cfg中定义的,给联系人发邮件
host_notification_commands host-notify-by-email
//主机出现故障时,给联系人发邮件
email ritto.zhao@xxx.com //联系人邮件地址
pager 132********
//联系人的手机,如果支持短信通知的话,会很实用
}
//如果不需要手机报警的话,则改成如下:
service_notification_commands notify-by-email
host_notification_commands host-notify-by-email
//上面的文件定义了2个联系人,如果有更多联系人的话,照这个格式在后面追加即可。
//服务通知选项(service_notification_options)
//与主机通知选项(host_notification_options)的几个选项在这里说明一下:
//w-warning 报警 , u-unknown 未知, c-critical 严重 , r-recovery 从异常情况恢复正常 ; d-down 关机了,
//u-unreachable,注意一下,主机报警和服务报警有些差异。
-----------------------------------------------------------------------------
vi contactgroups.cfg //将多个联系人定义一个联系人组
define contactgroup{
contactgroup_name sagroup
alias System Administrators
members sa,ritto
}
-----------------------------------------------------------------------------
vi hosts.cfg //定义被监控主机
#define monitor host
define host {
host_name nagios-server
alias nagios server
address 192.168.4.226
contact_groups sagroup
//多个联系组用逗号分隔,数据来源于contactgroups.cfg
check_command check-host-alive
//这个命令来自commands.cfg,用来监控主机是否存活
max_check_attempts 5 //检查失败后重试的次数
notification_interval 10 //提醒的时间,每隔10秒提醒一次
notification_period 24x7
//提醒的周期,24*7,来自之前timeperiods.cfg中定义的
notification_options d,u,r
//指定什么情况下提醒,来自contacts.cfg中定义的
}
define host {
host_name mail12.supertalent.com
alias nagios test client
address 192.168.4.41
contact_groups sagroup
check_command check-host-alive
max_check_attempts 5
notification_interval 10
notification_period 24x7
notification_options d,u,r
}
------------------------------------------------------------------------------
vi hostgroups.cfg //将多个主机定义一个主机组
define hostgroup{
hostgroup_name sa-servers //主机组名称
alias sa Servers //别名
members nagios-server
//组的成员主机,多个主机以逗号相隔,必须是上面hosts.cfg中定义的
}
------------------------------------------------------------------------------
vi services.cfg //定义监控的服务
#service definition
define service{
host_name nagios-server //要监控的主机, 必须是hosts.cfg 中定义的
service_description check-host-alive //定义的是监控这个主机是不是存活,给监控项目起个名字,任意起都可以,如check ftp
check_command check-host-alive //所用的命令,是commands.cfg中定义的,所用的命令,必须是commands.cfg中定义的
check_period 24x7 //监控的时间段,是timeperiods.cfg中定义的
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup //联系人组, contactgroups.cfg中定义的
notification_interval 10
notification_period 24x7 //通知的时间段, ,是timeperiods.cfg中定义的
notification_options w,u,c,r
}
define service {
host_name mail12.supertalent.com
service_description check_http
check_command check_http //检查tcp 80端口服务是否正常
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
}
//书写时要注意的是,check_tcp与要监控的服务端口之间要用”!”做分隔符。如果服务太多,以考虑用脚本来生成。
-----------------------------------------------------------------------------
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg //检查所有配置文件的正确性
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg //作为守护进程后台启动Nagios
echo "/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg" >> /etc/rc.local //开机自动运行
-----------------------------------------------------------------------------
使用命令和插件监控更多信息
cd /usr/local/nagios/libexec //插件默认的安装路径
./check_disk -w 10% -c 5% /
//检查根分区的使用情况,若剩余10%以下,为警告状态(warning)
//若剩余 5%以下,为严重状态(critical)
设置并熟悉以上的配置后,下面就需要进行具体的监控工作了
被监控主机上的安装部署配置
十一、监控一台主机
在hosts.cfg中定义主机名 --- 在services.cfg中定义监控内容
------------------------------------------------------------------------------
使用NRPE监控Linux上的"本地信息"
对系统为Linux的主机进行如下监控: CPU负载,磁盘容量,登陆用户数,总进程数,僵尸进程数,swap分区使用情况
在被监控主机上.
useradd nagios
passwd nagios
tar zxvf nagios-plugins-1.4.9.tar.gz
cd nagios-plugins-1.4.9
./configure --prefix=/usr/local/nagios
make
make install
chown nagios.nagios /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios/libexec/
tar zxvf nrpe-2.8.1.tar.gz
cd nrpe-2.8.1
./configure //NRPE port: 5666
make all
make install-plugin
make install-daemon //安装daemon
make install-daemon-config //安装配置文件
ls /usr/local/nagios/
bin/ etc/ libexec/ share/ //现在nagios目录会有4个目录了
将NRPE daemon作为xinetd下的一个服务运行
yum -y install xinetd
service xinetd start
chkconfig --level 3 xinetd on
make install-xinetd //安装xinetd脚本
vi /etc/xinetd.d/nrpe
only_from = 127.0.0.1 192.168.4.226 //在后面增加监控主机的地址,以空格间隔
vi /etc/services
nrpe 5666/tcp # nrpe //增加这一行
service xinetd restart
netstat -at | grep nrpe //查看NRPE是否已经启动
netstat -an | grep 5666 //查看5666端口是否被监听
vi /etc/sysconfig/iptables //增加一条5666的端口
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5666 -j ACCEPT
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 //测试NRPE是否正常工作
NRPE v2.8.1 //正常的结果会显示当前NRPE的版本号
查看NRPE的监控命令
cd /usr/local/nagios/etc
vi nrpe.cfg
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/hda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
[***]中是命令名,也就是check_nrpe的-c参数可以接的内容,=后面是实际执行的插件程序
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_users //检测登陆用户数
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_load //CPU负载
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_zombie_procs //僵尸进程
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_total_procs //总进程数
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_disk //磁盘使用量
其中:
//check_load -w 15,10,5 -c 30,25,20
//在unix里面负载的均值通常表示是1分钟,5分钟,15分钟内平均有多少进程处于等待状态
//当1分钟多于15个进程等待,5分钟多于10个,15分钟多于5个则为warning状态
//当1分钟多于30个进程等待,5分钟多于25个,15分钟多于20个则为critical状态
记住监测磁盘时,先用
fdisk -l //查看磁盘的分区,如果sda, 则应该改成下面的形式,把默认的hda1改成sda1
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 268 2048287+ 82 Linux swap / Solaris
/dev/sda3 269 19457 154135642+ 83 Linux
command[check_sda1]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/sda1
command[check_sda3]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/sda3
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_sda1
DISK OK - free space: /boot 77 MB (82% inode=99%);| /boot=16MB;78;88;0;98
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_sda3
DISK OK - free space: / 135169 MB (97% inode=99%);| /=3113MB;145789;145799;0;145809
例如查看根分区的使用情况,执行
/usr/local/nagios/libexec/check_disk -w 10% -c 5% /
命令的含义是检查分区/的使用情况,若剩余10%以下,为警告状态(warning),5%以下为严重状态(critical),
执行后我们会看到下面这条信息
DISK WARNING - free space: / 135169 MB (97% inode=99%);| /=3113MB;145789;145799;0;145809
说明当前是warning的状态,空闲空间还有97%.
在运行Nagios的监控主机上
因为之前已经将Nagios运行起来了,现在要做的是:
安装check_nrpe插件
在commands.cfg中创建check_nrpe中的命令,只有在commands.cfg中定义过的命令才能在services.cfg中使用
创建对被监控主机的监控项目
tar zxvf nrpe-2.8.1.tar.gz
cd nrpe-2.8.1
./configure
make all
make install-plugin //只运行这一步即可,只需要check_nrpe插件
/usr/local/nagios/libexec/check_nrpe -H 192.168.4.30
NRPE v2.8.1
//测试一下监控机使用check_nrpe与被监控机运行的nrpedaemon之間的通信
//看到已经返回了正确的NRPE的版本信息,说明一切正常
//如返回"Could not complete SSL handshake",则要检查被监控机/etc/xinetd.d/nrpe中only_from这项是否允许监控机访问
在commands.cfg中增加对check_nrpe的定义
vi /usr/local/nagios/etc/commands.cfg
#################################################################
# 2008.12.4 by ritto
#################################################################
# 'check_nrpe' command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
//command_name check_nrpe 定义命令名称为check_nrpe, 在services.cfg中要使用这个名称
//command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ 定义实际运行的插件程序
接下来可以在services.cfg中定义对主机CPU负载的监控
vi services.cfg
define service {
host_name mail1
service_description check-load
check_command check_nrpe!check_load
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
define service {
host_name mail1
service_description check-users
check_command check_nrpe!check_users
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
define service {
host_name mail1
service_description check-zombie-process
check_command check_nrpe!check_zombie_procs
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
----------------------------------------------------------------------
在被监控机上增加check_swap命令的定义
vi /usr/local/nagios/etc/nrpe.cfg
增加下面这一行
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
//如果在被监控机上是以daemon运行的nrpe,则需要手动重启
//如果在被监控机上是以xinetd运行的,则不需要
----------------------------------------------------------------------
在监控机上增加这个监控项目:
vi /etc/services.cfg
define service {
host_name mail1
service_description check-swap
check_command check_nrpe!check_swap
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
------------------------------------------------------------------------------
所有配置都已经修改好,现在重启Nagios,杀掉Nagios进程,再重启,过一会就可以看到画面了
killall nagios
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
或使用
/etc/init.d/nagis restart/start/stop/status
如查报错,则可能是脚本路径设置错误,
vi /etc/init.d/nagios
将prefix=/usr/local/nagiosaa改为安装的目录/etc/init.d/nagios