1、(一)下载所需软件包
1.nagios.3.2.0.tar.gzNagios的主程序[root@UnixHotsrc]# wgethttp://prdownloads.sourceforge.net/sourceforge/nagios/nagios.3.2.0.tar.gz 2.nagios-plugins-1.4.13.tar.gzNagios 的插件[root@UnixHotsrc]# wgethttp://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.13.tar.gz3.nrpe-2.12.tar.gz 代理检测程序
安装前的依赖检查
rpm -q httpd phpgcc glibc glibc-common gd gd-devel
创建Nagios账户和组
[root@UnixHotsrc]# useradd -m nagios
[root@UnixHot src]#groupadd nagcmd
[root@UnixHot src]# usermod -a -G nagcmdnagios
[root@UnixHotsrc]#usermod -a -G nagcmd apache
编译安装
[root@UnixHot src]# tar xvf nagios-3.2.0.tar.gz
[root@UnixHot src]# cd nagios-3.2.0 (至于每一步的具体意思,我不再详述,因为运行完毕后,提示信息会明确的告诉你,它干了什么,是怎么干的。当然,还有一个最好的办法,就是看Makefile文件,里面都会有注释的。) 参考Makefile文件
[root@UnixHot nagios-3.2.0]# less Makefile
[root@UnixHot nagios-3.2.0]# ./configure --with-command-group=nagcmd \--with-nagios-user=nagios \ --with-nagios-group=nagios
[root@UnixHot nagios-3.2.0]# make all
[root@UnixHot nagios-3.2.0]# make install
[root@UnixHot nagios-3.2.0]# make install-init (生成init启动脚本)
[root@UnixHot nagios-3.2.0]# make install-config (生成一些模板配置文件)
[root@UnixHot nagios-3.2.0]# make install-commandmode (设置相应的权限)
[root@UnixHot nagios-3.2.0]# make install-webconf (生成Apache配置文件nagios.conf)
为Nagios设置Web验证的密码。
注意第一次添加用户用-c选项,以后再添加千万别在用这个选项了,会覆盖以前的所有用户的,这点之时在《RHCE考前冲刺》我已经讲过了
2.主配置文件nagios.cfg的配置。主配置文件的内容很多,对于这个版本,我们需要修改和添加的主要是对象配置文件,即:
[root@UnixHot nagios-3.2.0]# htpasswd -c/usr/local/nagios/etc/htpasswd.users nagiosadmin
设置Nagios的开机启动
[root@UnixHot~]# chkconfig --add nagios
[root@UnixHot~]# chkconfig nagios on
修改SELinux
两种方法: 第一种最直接,关闭SELinux,对于SELinux不是很熟悉的用户,请选择此。[root@UnixHotnagios-3.2.0]# cat /etc/sysconfig/selinux SELINUX=disabled
第二种给打上正确的安全脉络。
chcon-R -t httpd_sys_content_t /usr/local/nagios/sbin/ chcon -R -t httpd_sys_content_t/usr/local/nagios/share/
安装Nagios的插件nagios-plugin
[root@UnixHotsrc]# tar xvf nagios-plugins-1.4.13.tar.gz
[root@UnixHot src]# cd nagios-plugins-1.4.13
[root@UnixHotnagios-plugins-1.4.13]#./configure --prefix=/usr/local/nagios --with-nagios-user=nagios--with-nagios-group=nagios
[root@UnixHotnagios-plugins-1.4.13]# make && make install
配置检测主机是否存活
如果安装上面的步骤,安装完成后,配置文件在安装时放在了/usr/local/nagios/etc/目录下
[root@UnixHot~]# cd /usr/local/nagios/etc
[root@UnixHotetc]# ls -l -rw-rw-r-- 1 nagios nagios 11408 08-30 11:55 cgi.cfg (CGI配置文件) -rw-r--r--1 root root 26 08-30 11:56 htpasswd.users (Apache的验证密码文件)
-rw-rw-r-- 1nagios nagios 43776 08-30 11:55 nagios.cfg (主配置文件)
drwxrwxr-x 2 nagios nagios 4096 08-30 11:55 objects (对象定义文件目录)
-rw-rw---- 1 nagios nagios 1340 08-30 11:55 resource.cfg (资源配置文件)
我们修要修改的的是nagios.cfg 和 objects 目录下的文件,来检测主机是否存活。
主配置文件nagios.cfg的配置
cfg_file=<file_name>cfg_file=/usr/local/nagios/etc/objects/commands.cfg (命令定义文件)
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg(联系人信息定义文件)
cfg_file=/usr/local/nagios/etc/objects/contactgroups.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/timeperiods.cfg(时间周期定义文件)
cfg_file=/usr/local/nagios/etc/objects/templates.cfg# Definitions for monitoring the local (Linux) host
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg(注释掉此行)
3.主机定义文件的配置。
[root@UnixHot etc]# vi objects/hosts.cfg
define host{
host_nameNagios-Server
aliasNagios Server
address192.168.0.206
check_commandcheck-host-alive
check_interval5
retry_interval1
max_check_attempts5
check_period24x7
process_perf_data0
retain_nonstatus_information0
contact_groupssagroup
notification_interval30
notification_period24x7
notification_optionsd,u,r
}
define host{
host_nameJava-Server
aliasJava Server
address10.100.2.180
check_commandcheck-host-alive
check_interval5
retry_interval1
max_check_attempts5
check_period24x7
process_perf_data0
retain_nonstatus_information0
contact_groupssagroup
notification_interval30
notification_period24x7
notification_optionsd,u,r
}
define host{
host_nameOracle-Server
aliasOracle Server
address10.100.2.180
check_commandcheck-host-alive
check_interval5
retry_interval1
max_check_attempts5
check_period24x7
process_perf_data0
retain_nonstatus_information0
contact_groupssagroup
notification_interval30
notification_period24x7
notification_optionsd,u,r
}
define host{
host_nameMySQL-Server
aliasMySQL Server
address10.100.2.180
check_commandcheck-host-alive
check_interval5
retry_interval1
max_check_attempts5
check_period24x7
process_perf_data0
retain_nonstatus_information0
contact_groupssagroup
notification_interval30
notification_period24x7
notification_optionsd,u,r
}
主机组定义文件配置
[root@UnixHot etc]# viobjects/hostgroups.cfg
define hostgroup {
hostgroup_nameSystem-Admin
aliassystem Admin
membersNagios-Server,Oracle-Server,Java-Server,MySQL-Server
}
服务定义文件的配置
[root@UnixHot etc]# vi objects/services.cfg
define service {
host_nameNagios-Server
service_descriptioncheck-host-alive
check_period24x7
max_check_attempts4
normal_check_interval3
retry_check_interval2
contact_groupssagroup
notification_interval10
notification_period24x7
notification_optionsw,u,c,r
check_commandcheck-host-alive
}
define service {
host_nameOracle-Server
service_descriptioncheck-host-alive
check_period24x7
max_check_attempts4
normal_check_interval3
retry_check_interval2
contact_groupssagroup
notification_interval10
notification_period24x7
notification_optionsw,u,c,r
check_commandcheck-host-alive
}
define service {
host_nameJava-Server
service_descriptioncheck-host-alive
check_period24x7
max_check_attempts4
normal_check_interval3
retry_check_interval2
contact_groupssagroup
notification_interval10
notification_period24x7
notification_optionsw,u,c,r
check_commandcheck-host-alive
}
define service {
host_nameMySQL-Server
service_descriptioncheck-host-alive
check_period24x7
max_check_attempts4
normal_check_interval3
retry_check_interval2
contact_groupssagroup
notification_interval10
notification_period24x7
notification_optionsw,u,c,r
check_commandcheck-host-alive
}
6.联系人定义文件的配置
[root@nagios objects]# vi contacts.cfg
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-service-by-email
host_notification_commands notify-host-by-email
7.联系组定义文件配置
[root@UnixHot etc]# viobjects/contactgroups.cfg
define contactgroup {
contactgroup_namesagroup
aliassystem administrator group
membersnagiosadmin
}
8.修改目录的所有者
[root@UnixHot etc]# chown -R nagios:nagiosobjects/
9.检测配置文件是否正确
[root@UnixHot etc]#/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Total Warnings: 0 Total Errors: 0 如果都是0说明配置成功了,如果有错误,好好检查一下。
10.启动Nagios服务
[root@UnixHot etc]# /etc/init.d/httpd restart
[root@UnixHot etc]# /etc/init.d/nagiosstart
11.增加邮件通知
sendEmail的主页http://caspian.dotconf.net/menu/Software/SendEmail/
下载地址http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.55.tar.gz
软件十分小,是一个通过命令来发smtp邮件的程序.安装也十分简单(查看其README文件即可).
解压缩tar –zxvf sendEmail-v1.55.tar.gz
cd sendEmail-v1.55
将可执行程序复制cp sendEmail /usr/local/bin
然后给确认确实它具有执行权限
ll /usr/local/bin/sendEmail
-rwxr-xr-x 1 root root 77882 11-03 14:23 /usr/local/bin/sendEmail |
这样程序就装好了,使用也很简单.直接运行sendEmail就会显示详细的用法
先看一个典型的例子
/usr/local/bin/sendEmail –f nagios@test.com –t yahoon@test.com –s mail.test.com –u “from nagios”–xu nagios –xp p#3isoda –m happy
解释:
-f 表示发送者的邮箱
-t 表示接收者的邮箱
-s 表示SMTP服务器的域名或者ip
-u 表示邮件的主题
-xu 表示SMTP验证的用户名
-xp 表示SMTP验证的密码(注意,这个密码貌似有限制,例如我用d!5neyland就不能被正确识别)
-m 表示邮件的内容
如果你不带-m参数的话,就会提示你自行输入
Reading message body from STDIN because the ‘-m’ option was not used. If you are manually typing in a message: - First line must be received within 60 seconds. - End manual input with a CTRL-D on its own line |
输入完成后使用CTRL-D来结束
当然我们也可以将一个文件的内容作为邮件的正文发出去的
那么就可以使用:
cat 文件名 | /usr/local/bin/sendEmail –f nagios@test.com –t yahoon@test.com –s mail.test.com –u “from nagios”–xu nagios –xp p#3isoda
有关sendEmail的用法就讲到这里
既然nagios要使用sendEmail来发警告邮件,那么就要修改commands.cfg中关于发邮件的命令的定义,我们现在来修改notify-by-email这个命令,如下(注意其中粗体的部分)
# 'notify-by-email' command definition define command{ command_name notify-by-email command_line /usr/bin/printf "%b" "***** Nagios 2.9 *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /usr/local/bin/sendEmail -f nagios@test.com -t $CONTACTEMAIL$ -s mail.test.com -u "** $NOTIFICATIONTYPE$ alert - $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -xu nagios -xp p#3isoda } |
Nagios监控服务器的配置
1.安装nrpe
[root@UnixHot src]# tar zxvfnrpe-2.12.tar.gz
[root@UnixHot nrpe-2.12]# ./configure&& make all
[root@UnixHot nrpe-2.12]# makeinstall-plugin
[root@UnixHot nrpe-2.12]# makeinstall-daemon
[root@UnixHot nrpe-2.12]# make install-daemon-config
[root@UnixHot nrpe-2.12]# makeinstall-xinetd
配置nrpe
vi /etc/xinetd.d/nrpe
xinetd.d没有的话要安装xinetd
yum installxinetd
3.添加端口
[root@UnixHot nrpe-2.12]# vi /etc/services
在最后添加nrpe 5666/tcp #nrpe
分类: LINUX
1. 监控内容
windows服务器的内部参数包括以下
a. 内存使用状况
b. CPU负载
c. 磁盘使用状况
d. 服务状态
e. 运行的进程
2. 监控原理
在windows服务器内安装NSClient++的监控引擎,nagios服务器通过check_nt来获取监控数据
3.配置步骤
a.修改启动参数
b.windows服务器安装监控引擎
c.在Nagios服务器内建立监控主机和监控服务配置
d.重新启动Nagios服务
4.确认已经配置是否存在
在/usr/local/nagios/etc/objects/commands.cfg中是否有以下内容(允许使用check_nt来监控windows服务)
# 'check_nt' command definition
define command{
command_name check_nt
command_line $USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -v $ARG1$ $ARG2$
}
在/usr/local/nagios/etc/object/templates.cfg中是否有以下内容(这个用于未来添加新主机的时候的范例)
define host{
name windows-server ; The name of this host template
use generic-host ; Inherit default values from the generic-hosttemplate
check_period 24x7 ; Bydefault, Windows servers are monitored round the clock
check_interval 5 ; Actively check the server every 5 minutes
retry_interval 1 ; Schedule host check retries at 1 minute intervals
max_check_attempts 10 ; Check each server 10 times (max)
check_command check-host-alive ; Default command tocheck if servers are "alive"
notification_period 24x7 ; Sendnotification out at any time - day or night
notification_interval 30 ; Resend notifications every 30 minutes
notification_options d,r ;Only send notifications for specific host states
contact_groups admins ; Notificationsget sent to the admins by default
hostgroups windows-servers ; Host groups that Windows servers should be a member of
register 0 ; DONT REGISTER THIS - ITS JUST A TEMPLATE
}
5.nagios在第一次监控windows所必须修改的内容
修改/usr/local/nagios/etc/nagios.cfg
查找
#cfg_file=/usr/local/nagios/etc/objects/windows.cfg
改为
cfg_file=/usr/local/nagios/etc/objects/windows.cfg
保存文件。
/usr/local/nagios/etc/objects/windows.cfg 内包含了一些样例的主机、主机群和服务的定义模式。可以通过修改这些案例来测试监控对象。
6.安装windows监控引擎
下载NSClient++的安装包,注意根据实际的32/64CPU来下载
下载地址 http://sourceforge.net/projects/nscplus
安装方式,解压缩到C:\NSClient++
在指令模式下,在该目录下面运行以下命令,注册NSCient++的服务
nsclient++ /install
将NSCient++作为启动进程
nsclient++ SysTray
在服务列表中找到NSClientpp的服务。双击打开该服务的配置窗口则在登录分页中将“允许服务与桌面交互”勾选上。
编辑NSC.INI,做以下更改
- 在[Module]部分,除了CheckWMI.dll和RemoteConfiguration.dll这两行的注释不要去掉,其他DLL的注释都去掉。
- 在[Setting]部分的password设定一个密码,并把前面的注释去掉。
password=secret-password 改为password=123456
- 在[Setting]部分的allowed_hosts部分设定Nagios的服务器IP或者设定一个IP段,并把前面的注释去掉。
allowed_hosts=192.168.200.249/32
- 在[NSClient]部分将port前的注释去掉,确认port的端口号是12489
启动nsclient++服务
当右下角出现了一个蓝色十字的ICON这表示
7.配置Nagios
建立需要监控的windows主机和服务对象
编辑文件windows.cfg
/usr/local/nagios/etc/objects/windows.cfg
在host的定义部分,可以修改实例中的host_name,alias,address三个部分来的参数。第一个参数use则是继承了template.cfg中对Windows的监控参数
define host{
use windows-server ; 从template中继承相关监控参数
host_name winserver ; 主机名称
alias My WindowsServer ; 别名
address 192.168.1.2 ; IP地址
}
在Service定义部分
首先需要设定以下内容,用于确认监控对象的NSClient++的版本是否正确
define service{
use generic-service
host_name winserver
service_description NSClient++ Version
check_command check_nt!CLIENTVERSION
}
然后是定义监控对象的正常运行时间
check_command check_nt!UPTIME
随后是定义CPU的负载状况,下面的定义表示在5分钟内的平均负载超过80%则发出警告WARNING,而超过90%则是危机报警CRITICAL alert
check_command check_nt!CPULOAD!-l 5,80,90
定义内存负载状况,当内存使用率达到80则warning 90%则CRITICAL alert
check_command check_nt!MEMUSE!-w 80 -c 90
监控C盘空间,使用率达到80则warning 90%则CRITICAL alert
check_command check_nt!USEDDISKSPACE!-l c-w 80 -c 90
监控服务状态的格式,当服务停止了则发送CRITICAL alert
check_command check_nt!SERVICESTATE!-dSHOWALL -l W3SVC
监控系统进程,当进程处于非运行状态时,则发送CRITICAL alert
check_command check_nt!PROCSTATE!-d SHOWALL-l Explorer.exe
8.密码设定
编辑/usr/local/nagios/etc/objects/commands.cfg 在check_nt部分中,command_line后面添加一个参数-s"NSClient主机的密码"
command_line $USER1$/check_nt -H$HOSTADDRESS$ -p 12489 -s PASSWORD -v $ARG1$ $ARG2$
可以通过以下指令测试设备响应是否正常
/usr/local/nagios/libexec/check_nt -H 主机IP -p 12489-s 密码 -v UPTIME
如果反馈的信息是System Uptime - 0 day(s) 8 hour(s) 44 minute(s) 则表示连接正常。
如果显示为could not fetch information from server,则有以下可能
密码不正确(最傻的可能,也是经常发生的可能)
服务器上有防火墙,需要开放12489端口。
在windows上的NSC.INI上的allow_hosts没有添加正确的nagios IP、
9.重新启动Nagios服务
在修改nagios配置文件之后,则可以运行/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
来验证配置文件是否正确。
service nagios restart
看看nagios的管理页面,被监控的windows服务器是否显示
被监控端安装Nagios-plugins插件和NRPE
1、添加nagios用户
[root@ClientNrpe ~]# useradd -s /sbin/nologin nagios
2、安装nagios-plugins,因为NRPE依赖此插件
[root@ClientNrpe ~]# yum -y install gcc gcc-c++ make openssl openssl-devel
[root@ClientNrpe ~]# tar xf nagios-plugins-2.0.3.tar.gz
[root@ClientNrpe ~]# cd nagios-plugins-2.0.3
[root@ClientNrpe nagios-plugins-2.0.3]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
[root@ClientNrpe nagios-plugins-2.0.3]# make && make install
#注意:如何要监控mysql 需要添加 --with-mysql
3、安装NRPE
[root@ClientNrpe ~]# tar xf nrpe-2.15.tar.gz
[root@ClientNrpe ~]# cd nrpe-2.15
[root@ClientNrpe nrpe-2.15]# ./configure--with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios--with-nagios-group=nagios --enable-command-args --enable-ssl
[root@ClientNrpe nrpe-2.15]# make all
[root@ClientNrpe nrpe-2.15]# make install-plugin
[root@ClientNrpe nrpe-2.15]# make install-daemon
[root@ClientNrpe nrpe-2.15]# make install-daemon-config
4、配置NRPE
[root@ClientNrpe ~]# grep -v '^#' /usr/local/nagios/etc/nrpe.cfg |sed '/^$/d'
log_facility=daemon
pid_file=/var/run/nrpe.pid
server_port=5666 #监听的端口
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=192.168.0.105 #允许的地址通常是Nagios服务器端
dont_blame_nrpe=0
allow_bash_command_substitution=0
debug=0
command_timeout=60
connection_timeout=300
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
例子
command[check_users]=/usr/local/nagios/libexec/check_users-w 8 -c 12
command[check_load]=/usr/local/nagios/libexec/check_load-w 15,10,5 -c 30,25,20
command[check_disk]=/usr/local/nagios/libexec/check_disk-w 20% -c 10% -p /
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 200 -c 250
command[check_free_mem]=/usr/local/nagios/libexec/check_memory.pl-f -w 10 -c 5
5、启动NRPE
#以守护进程的方式启动
[root@ClientNrpe ~]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@ClientNrpe ~]# netstat -tulpn | grep nrpe
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 22597/nrpe
tcp 0 0 :::5666 :::* LISTEN 22597/nrpe
有两种方式用于管理nrpe服务,nrpe有两种运行模式:
-i # Run as a service under inetd or xinetd
-d # Run as a standalone daemon
可以为nrpe编写启动脚本,使得nrpe以standard alone方式运行:
[root@ClientNrpe ~]# cat /etc/init.d/nrped
#!/bin/bash
# chkconfig: 2345 88 12
# description: NRPE DAEMON
NRPE=/usr/local/nagios/bin/nrpe
NRPECONF=/usr/local/nagios/etc/nrpe.cfg
case "$1" in
start)
echo -n "Starting NRPE daemon..."
$NRPE -c $NRPECONF -d
echo " done."
;;
stop)
echo -n "Stopping NRPE daemon..."
pkill -u nagios nrpe
echo " done."
;;
restart)
$0 stop
sleep 2
$0 start
;;
*)
echo "Usage: $0 start|stop|restart"
;;
esac
exit 0
[root@ClientNrpe ~]# chmod +x /etc/init.d/nrped
[root@ClientNrpe ~]# chkconfig --add nrped
[root@ClientNrpe ~]# chkconfig nrped on
[root@ClientNrpe ~]# service nrped start
Starting NRPE daemon... done.
[root@ClientNrpe ~]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1031/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1108/master
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 22597/nrpe
tcp 0 0 :::22 :::* LISTEN 1031/sshd
tcp 0 0 ::1:25 :::* LISTEN 1108/master
tcp 0 0 :::5666 :::* LISTEN 22597/nrpe
监控端安装NRPE(linux)
1、安装NRPE
[root@Nagios ~]# tar xf nrpe-2.15.tar.gz
[root@Nagios ~]# cd nrpe-2.15
[root@Nagios nrpe-2.15]# ./configure
> --with-nrpe-user=nagios \
> --with-nrpe-group=nagios \
> --with-nagios-user=nagios \
> --with-nagios-group=nagios \
> --enable-command-args \
> --enable-ssl
[root@Nagios nrpe-2.15]# make all
[root@Nagios nrpe-2.15]# make install-plugin
#安装完成后,会在Nagios安装目录的libexec下生成check_nrpe的插件
[root@Nagios ~]# cd /usr/local/nagios/libexec/
[root@Nagios libexec]# ll -d check_nrpe
-rwxrwxr-x. 1 nagios nagios 76769 9月 28 08:07 check_nrpe
2、check_nrpe的用法
[root@Nagios libexec]# ./check_nrpe -h
NRPE Plugin for Nagios
Copyright (c) 1999-2008 Ethan Galstad (nagios@nagios.org)
Version: 2.15
Last Modified: 09-06-2013
License: GPL v2 with exemptions (-l for more info)
SSL/TLS Available: Anonymous DH Mode, OpenSSL 0.9.6 or higher required
Usage: check_nrpe -H <host> [ -b <bindaddr> ] [-4] [-6] [-n] [-u] [-p <port>] [-t <timeout>] [-c <command>] [-a <arglist...>]
Options:
-n = Do no use SSL
-u = Make socket timeouts return an UNKNOWN state instead of CRITICAL
<host> = The address of the host running the NRPE daemon
<bindaddr> = bind to local address
-4 = user ipv4 only
-6 = user ipv6 only
[port] = The port on which the daemon is running (default=5666)
[timeout] = Number of seconds before connection times out (default=10)
[command] = The name of the command that the remote daemon should run
[arglist] = Optional arguments that should be passed to the command. Multiple
arguments should be separated by a space. If provided, this must be
the last option supplied on the command line.
Note:
This plugin requires that you have the NRPE daemon running on the remote host.
You must also have configured the daemon to associate a specific plugin command
with the [command] option you are specifying here. Upon receipt of the
[command] argument, the NRPE daemon will run the appropriate plugin command and
send the plugin output and return code back to *this* plugin. This allows you
to execute plugins on remote hosts and 'fake' the results to make Nagios think
the plugin is being run locally.
通过NRPE监控远程Linux主机要使用chech_nrpe插件进行,其语法格式如下:
check_nrpe -H <host> [-n] [-u] [-p <port>] [-t <timeout>] [-c <command>] [-a <arglist...>]
[root@Nagios libexec]# ./check_nrpe -H 192.168.0.81
NRPE v2.15
3、定义命令
[root@Nagios ~]# cd /usr/local/nagios/etc/objects/
[root@Nagios objects]# vim commands.cfg
#增加到末尾行
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H "$HOSTADDRESS$" -c "$ARG1$"
}
4、定义服务
[root@Nagios objects]# cp windows.cfg linhost.cfg
[root@Nagios objects]# grep -v '^#' linhost.cfg |sed '/^$/d'
define host{
use linux-server
host_name linhost
alias My Linux Server
address 192.168.0.81
}
define service{
use generic-service
host_name linhost
service_description CHECK USER
check_command check_nrpe!check_users
}
define service{
use generic-service
host_name linhost
service_description Load
check_command check_nrpe!check_load
}
define service{
use generic-service
host_name linhost
service_description SDA1
check_command check_nrpe!check_hda1
}
define service{
use generic-service
host_name linhost
service_description Zombie
check_command check_nrpe!check_zombie_procs
}
define service{
use generic-service
host_name linhost
service_description Total procs
check_command check_nrpe!check_total_procs
}
问题:安装完成nagios后,发现居然没有check_mysql插件,怎么办?
解决方法:
# yum install mysql-devel <我系统默认没装mysql的>
重装下nagios-plugins插件包
[root@svr4 nagios-plugins-1.4.15]#./configure
--with-apt-get-command:
--with-ping6-command: /bin/ping6-n -U -w %d -c %d %s
--with-ping-command: /bin/ping-n -U -w %d -c %d %s
--with-ipv6: yes
--with-mysql:/usr/bin/mysql_config
--with-openssl: yes
--with-gnutls: no
--enable-extra-opts: no
--with-perl:/usr/bin/perl
--enable-perl-modules: no
--with-cgiurl:/nagios/cgi-bin
--with-trusted-path:/bin:/sbin:/usr/bin:/usr/sbin
--enable-libtap: no
[root@svr4 nagios-plugins-1.4.15]# make&& make install
结果:这个插件有了!
[root@svr4 nagios-plugins-1.4.15]# ll/usr/local/nagios/libexec/check_mysql*
-rwxr-xr-x 1 root root 124343 Aug 31 13:13/usr/local/nagios/libexec/check_mysql
-rwxr-xr-x 1 root root 120909 Aug 31 13:13/usr/local/nagios/libexec/check_mysql_query
安装mysql
说到数据库,我们大多想到的是关系型数据库,比如mysql、oracle、sqlserver等等,这些数据库软件在windows上安装都非常的方便,在Linux上如果要安装数据库,咱不得不首先推荐的是mysql数据库了,而且Mysql数据库的第一个版本就是发行在Linux系统上的。
MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司。MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。MySQL的SQL语言是用于访问数据库的最常用标准化语言。MySQL软件采用了双授权政策(本词条“授权政策”),它分为社区版和商业版,由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择MySQL作为网站数据库。由于其社区版的性能卓越,搭配PHP和Apache可组成良好的开发环境。
在Linux上安装mysql数据库,我们可以去其官网上下载mysql数据库的rpm包,http://dev.mysql.com/downloads/mysql/5.6.html#downloads,大家可以根据自己的操作系统去下载对应的数据库文件,目前最新的版本是5.6.10了。
在这里我是通过yum来进行mysql数据库的安装的,通过这种方式进行安装,可以将跟mysql相关的一些服务、jar包都给我们安装好,所以省去了很多不必要的麻烦!!!
二、卸载掉原有mysql
因为mysql数据库在Linux上实在是太流行了,所以目前下载的主流Linux系统版本基本上都集成了mysql数据库在里面,我们可以通过如下命令来查看我们的操作系统上是否已经安装了mysql数据库
[root@xiaoluo ~]# rpm -qa | grep mysql // 这个命令就会查看该操作系统上是否已经安装了mysql数据库
有的话,我们就通过rpm -e 命令 或者 rpm -e --nodeps 命令来卸载掉
[root@xiaoluo ~]# rpm -e mysql // 普通删除模式
[root@xiaoluo ~]# rpm -e --nodeps mysql // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除
在删除完以后我们可以通过rpm -qa | grep mysql 命令来查看mysql是否已经卸载成功!!
三、通过yum来进行mysql的安装
我是通过yum的方式来进行mysql的数据库安装,首先我们可以输入 yum list | grep mysql 命令来查看yum上提供的mysql数据库可下载的版本:
[root@xiaoluo ~]# yum list | grep mysql
就可以得到yum服务器上mysql数据库的可下载版本信息:
然后我们可以通过输入 yum install -y mysql-servermysql mysql-devel 命令将mysql mysql-server mysql-devel都安装好(注意:安装mysql时我们并不是安装了mysql客户端就相当于安装好了mysql数据库了,我们还需要安装mysql-server服务端才行)
[root@xiaoluo ~]# yum install -y mysql-server mysql mysql-deve
在等待了一番时间后,yum会帮我们选择好安装mysql数据库所需要的软件以及其它附属的一些软件
我们发现,通过yum方式安装mysql数据库省去了很多没必要的麻烦,当出现下面的结果时,就代表mysql数据库安装成功了
此时我们可以通过如下命令,查看刚安装好的mysql-server的版本
[root@xiaoluo ~]# rpm -qi mysql-server
我们安装的mysql-server并不是最新版本,如果你想尝试最新版本,那就去mysql官网下载rpm包安装就行了,至此我们的mysql数据库已经安装完成了。
四、mysql数据库的初始化及相关配置
我们在安装完mysql数据库以后,会发现会多出一个mysqld的服务,这个就是咱们的数据库服务,我们通过输入 servicemysqld start 命令就可以启动我们的mysql服务。
注意:如果我们是第一次启动mysql服务,mysql服务器首先会进行初始化的配置,如:
[root@xiaoluo ~]# service mysqld start
初始化 MySQL 数据库: WARNING: The host 'xiaoluo' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MySQL version. The MySQL daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MySQL privileges !
Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h xiaoluo password 'new-password'
Alternatively you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/bin/mysqlbug script!
[确定]
正在启动 mysqld: [确定]
这时我们会看到第一次启动mysql服务器以后会提示非常多的信息,目的就是对mysql数据库进行初始化操作,当我们再次重新启动mysql服务时,就不会提示这么多信息了,如:
[root@xiaoluo ~]# service mysqld restart
停止 mysqld: [确定]
正在启动 mysqld: [确定]
我们在使用mysql数据库时,都得首先启动mysqld服务,我们可以 通过 chkconfig--list | grep mysqld 命令来查看mysql服务是不是开机自动启动,如:
[root@xiaoluo ~]# chkconfig --list | grep mysqld
mysqld 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
我们发现mysqld服务并没有开机自动启动,我们当然可以通过 chkconfigmysqld on 命令来将其设置成开机启动,这样就不用每次都去手动启动了
[root@xiaoluo ~]# chkconfig mysqld on
[root@xiaoluo ~]# chkconfig --list | grep mysql
mysqld 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
mysql数据库安装完以后只会有一个root管理员账号,但是此时的root账号还并没有为其设置密码,在第一次启动mysql服务时,会进行数据库的一些初始化工作,在输出的一大串信息中,我们看到有这样一行信息 :
/usr/bin/mysqladmin -u root password 'new-password' // 为root账号设置密码
所以我们可以通过 该命令来给我们的root账号设置密码(注意:这个root账号是mysql的root账号,非Linux的root账号)
[root@xiaoluo ~]# mysqladmin -u root password'root' // 通过该命令给root账号设置密码为 root
此时我们就可以通过 mysql-u root -p 命令来登录我们的mysql数据库了
五、mysql数据库的主要配置文件
1./etc/my.cnf 这是mysql的主配置文件
我们可以查看一下这个文件的一些信息
[root@xiaoluo etc]# ls my.cnf
my.cnf
[root@xiaoluo etc]# cat my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
2./var/lib/mysql mysql数据库的数据库文件存放位置
我们的mysql数据库的数据库文件通常是存放在了/ver/lib/mysql这个目录下
[root@xiaoluo ~]# cd /var/lib/mysql/
[root@xiaoluo mysql]# ls -l
总用量 20488
-rw-rw----. 1 mysql mysql 10485760 4月 6 22:01 ibdata1
-rw-rw----. 1 mysql mysql 5242880 4月 6 22:01 ib_logfile0
-rw-rw----. 1 mysql mysql 5242880 4月 6 21:59 ib_logfile1
drwx------. 2 mysql mysql 4096 4月 6 21:59 mysql // 这两个是mysql数据库安装时默认的两个数据库文件
srwxrwxrwx. 1 mysql mysql 0 4月 6 22:01 mysql.sock
drwx------. 2 mysql mysql 4096 4月 6 21:59 test // 这两个是mysql数据库安装时默认的两个数据库文件
我们可以自己创建一个数据库,来验证一下该数据库文件的存放位置
创建一个我们自己的数据库:
mysql> create database xiaoluo;
Query OK, 1 row affected (0.00 sec)
[root@xiaoluo mysql]# ls -l
总用量 20492
-rw-rw----. 1 mysql mysql 10485760 4月 6 22:01 ibdata1
-rw-rw----. 1 mysql mysql 5242880 4月 6 22:01 ib_logfile0
-rw-rw----. 1 mysql mysql 5242880 4月 6 21:59 ib_logfile1
drwx------. 2 mysql mysql 4096 4月 6 21:59 mysql
srwxrwxrwx. 1 mysql mysql 0 4月 6 22:01 mysql.sock
drwx------. 2 mysql mysql 4096 4月 6 21:59 test
drwx------. 2 mysql mysql 4096 4月 6 22:15 xiaoluo // 这个就是我们刚自己创建的xiaoluo数据库
[root@xiaoluo mysql]# cd xiaoluo/
[root@xiaoluo xiaoluo]# ls
db.opt
3./var/log mysql数据库的日志输出存放位置
我们的mysql数据库的一些日志输出存放位置都是在/var/log这个目录下
[root@xiaoluo xiaoluo]# cd
[root@xiaoluo ~]# cd /var/log
[root@xiaoluo log]# ls
amanda cron maillog-20130331 spice-vdagent.log
anaconda.ifcfg.log cron-20130331 mcelog spooler
anaconda.log cups messages spooler-20130331
anaconda.program.log dirsrv messages-20130331 sssd
anaconda.storage.log dmesg mysqld.log tallylog
anaconda.syslog dmesg.old ntpstats tomcat6
anaconda.xlog dracut.log piranha wpa_supplicant.log
anaconda.yum.log gdm pm-powersave.log wtmp
audit httpd ppp Xorg.0.log
boot.log ibacm.log prelink Xorg.0.log.old
btmp lastlog sa Xorg.1.log
btmp-20130401 libvirt samba Xorg.2.log
cluster luci secure Xorg.9.log
ConsoleKit maillog secure-20130331 yum.log
其中mysqld.log这个文件就是我们存放我们跟mysql数据库进行操作而产生的一些日志信息,通过查看该日志文件,我们可以从中获得很多信息
因为我们的mysql数据库是可以通过网络访问的,并不是一个单机版数据库,其中使用的协议是 tcp/ip 协议,我们都知道mysql数据库绑定的端口号是 3306 ,所以我们可以通过 netstat-anp 命令来查看一下,Linux系统是否在监听 3306 这个端口号:
结果如上所示,Linux系统监听的3306端口号就是我们的mysql数据库!!!!
nagios监控mysql服务器:
1、建立专用数据库:
[root@svr3 ~]#/usr/local/webserver/mysql/bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 51910
Server version: 5.5.3-m3-log Sourcedistribution
Type 'help;' or '\h' for help. Type '\c' toclear the current input statement.
mysql> create database nagios;
Query OK, 1 row affected (0.00 sec)
mysql> grant select on nagios.* tonagios@'%' identified by '123qweasd';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select User,Password,Host fromuser;
+----------------+-------------------------------------------+------------------+
| User | Password | Host |
+----------------+-------------------------------------------+------------------+
| root |*84BB5DF4823DA319BBF86C99624479A198E6EEE9 | localhost |
| nagios |*A1F1CB851D62F002C09A0C9C4A76262473432F55 | % |
+----------------+-------------------------------------------+------------------+
14 rows in set (0.00 sec)
12 rows in set (0.00 sec)
[root@svr3 ~]# /usr/local/nagios/libexec/check_mysql-H 10.1.1.30 -u nagios -d nagios -p 123qweasd
Uptime: 970046 Threads: 1 Questions: 684583 Slow queries:3 Opens: 672 Flush tables: 1 Open tables:
369 Queries per second avg: 0.705
[root@svr4 nrpe_install]#/usr/local/nagios/libexec/check_mysql -H 10.1.1.30 -u nagios -d nagios -p123qweasd
Uptime: 970348 Threads: 1 Questions: 684584 Slow queries:3 Opens: 672 Flush tables: 1 Open tables: 369 Queries per second avg: 0.705
果断开始设置:
#定义用户
#定义组,及隶属用户
#用户增加
在command.cfg里增加插件命令:
#check mysql
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H$HOSTADDRESS$ -u nagios -d nagios -p 123qweasd
}
#定义服务:
define service {
host_name svr3.labexam.com
service_description MysqlDB
check_period 24x7
max_check_attempts 2
normal_check_interval 1
retry_check_interval 1
contact_groups sagroup,dbagroup
notification_interval 5
notification_period 24x7
notification_options w,u,c,r
check_command check_mysql
}
添加tomcat监控
在tomcat的webapps目录下,新建一个目录jiankong(这个目录随便建),然后在其下面放一个asp文件。然后修改commands.cfg ,在里面添加
#tomcat1 set
define command{
command_name check_tomcat_8028
command_line/usr/local/nagios/libexec/check_http -I $HOSTADDRESS$ -p 8028 -u/jiankong/test.jsp -e 200
}
如果有多个端口,可以建立多个,只需要修改端口号,上面这个是8028端口,然后在servers.cfg中添加服务就好了。
监控linux内存使用情况
被监控端的配置
1.下载check_memory.pl脚本,此脚本下载地址:
免费下载地址在 http://linux.linuxidc.com/
用户名与密码都是www.linuxidc.com
具体下载目录在 /2014年资料/1月/23日/Nagios脚本监控内存使用情况
下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm
------------------------------------分割线------------------------------------
2.将此脚本上传至/usr/local/nagios/libexec目录下并更改权限
chmod +x check_memory.pl
chown nagios:nagios check_memory.pl
3.修改nrpe配置文件
vim /usr/local/nagios/etc/nrpe.cfg
添加以下内容
command[check_free_mem]=/usr/local/nagios/libexec/check_memory.pl-f -w 10 -c 5
4.重启nrpe服务
/etc/init.d/xinetd restart (本例中我配置成了nrpe受xinetd管理)
若没有安装配置xinetd,使用这种方式重启服务
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
服务端的配置
5.修改主机配置文件
vim /usr/local/nagios/etc/servers/trainsystem.cfg
添加以下内容
define service{
use generic-service
host_name train
service_description Memory Usage
check_command check_nrpe!check_free_mem
register 1
}
6.检查配置文件正确性并重载配置文件
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
service nagios reload
7.配置完毕,过一会监控内存使用情况就出来了!
监控特定进程
command[check_onesearch_procs]=/usr/local/nagios/libexec/check_procs-w 1:1 -c 1:1024 -a 'OneSearch' -u ekp
对特定故障自动修复
定义一个command
define command{
command_name restart_wms_datahub
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -p 5666 -c win_service_restart -a Tomcat7_datahub_1 $SERVICESTATE$ $SERVICESTATETPYE$ $SERVICEATTEMPT$
}
define command{
command_name restart_imis_converter
command_line /usr/bin/ssh ekp@$HOSTADDRESS$ "/home/ekp/dingshi.sh"
}
在监控服务对象中
define service{
use generic-service-imis
host_name IMIS_zhuanhuanserver_64
service_description converter
check_command check_nrpe!check_converter_procs
event_handler_enabled 1
event_handler restart_imis_converter
}
define service{
use wms-service
host_name wms_2.73
service_description DATAHUB_SERVICE
check_command check_nt!SERVICESTATE! -d SHOWALL -l Tomcat7_datahub_1
event_handler_enabled 1
event_handler restart_wms_datahub
}