nagios调用它的插件库,插件将监控结果返回给nagios,nagios分析这些结果并以web的方式展现给我们看,同时也会调用插件提供报警功能。
而对于远程监控则需要check_nrpe插件,nagios执行该插件,并告诉check_nrpe去检测哪些服务。check_nrpe通过SSL连接远端机器上的NRPE daemon,然后NRPE 可以运行本地的各种插件去检测本地的服务和状态,最后NRPE把检测的结果传给nagios端的check_nrpe,nagios端的check_nrpe再把结果送到状态队列中让nagios读取信息,最终呈现给管理员的web页面结果。
Nagios的四种监控状态:
0(OK)正常,绿色
1(warning)警告,×××
2(Critical)严重错误,红色
3(unknown)位置错误,深×××
nagios服务端(监控端):192.168.80.80 需安装如下软件:
LAMP架构 提供网页监控查看
nagios-4.3.4.tar.gz 服务端nagios主程序
nagios-plugins-2.2.1.tar.gz nagios插件
nrpe-3.2.1.tar.gz 插件,负责远程连接
pnp4nagios-0.6.26.tar.gz 图表工具
ngios客户端(被监控):192.168.80.81 需安装如下软件:
nagios-plugins-2.2.1.tar.gz nagios插件
nrpe-3.2.1.tar.gz 插件,负责远程连接
mysql 编译安装,仅供测试
groupadd nagcmd
useradd -G nagcmd nagios
usermod -a -G nagcmd apache
id -n -G apache
tar xf nagios-4.3.4.tar.gz
cd nagios-4.3.4
./configure --with-command-group=nagcmd
make all
make install
make install-init //生成管理脚本
make install-commandmode //自动为nagios用户设置相关目录的权限
make install-config //安装配置文件
cp -R contrib/eventhandlers/ /usr/local/nagios/libexec/
chown -R nagios:nagios /usr/local/nagios/libexec/eventhandlers
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg //检查配置文件
/etc/init.d/nagios checkconfig //检查配置问题
make install-webconf //生成nagios/apache配置文件
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagios
service nagios start
service httpd restart
win10访问http://192.168.80.80/nagios/ //会进入nagios的主控制台,且会有登陆提示
tar xf nagios-plugins-2.2.1.tar.gz
cd nagios-plugins-2.2.1
./configure \
--with-nagios-user=nagios \
--with-nagios-group=nagcmd \
--enable-perl-modules
make && make install
tar xf nrpe-3.2.1.tar.gz
cd nrpe-3.2.1
./configure
make all
make install-plugin
make install-daemon
cp sample-config/nrpe.cfg /usr/local/nagios/etc/nrpe.cfg
ls /usr/local/nagios/libexec/ //检查插件安装情况
/usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg //启动nrpe
echo "/usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg" >> /etc/rc.local //可设置开机启动
chmod +x /etc/rc.d/rc.local
netstat -lnput|grep 5666
/usr/local/nagios/libexec/check_nrpe -H localhost //测试本地连接
NRPE v3.2.1
yum install -y perl-devel perl-CPAN openssl-devel
tar xf nagios-plugins-2.2.1.tar.gz
useradd -M nagios
cd nagios-plugins-2.2.1
./configure \
--with-nagios-user=nagios \
--with-nagios-group=nagios \
--enable-perl-modules
make && make install
tar xf nrpe-3.2.1.tar.gz
cd nrpe-3.2.1
./configure
make all
make install-plugin
make install-daemon
mkdir /usr/local/nagios/etc/
cp sample-config/nrpe.cfg /usr/local/nagios/etc/nrpe.cfg //复制模板,下面需要配置使用
ls /usr/local/nagios/libexec/ //查看nrpe插件
/usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg //启动nrpe
echo "/usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg" >> /etc/rc.local //可添加到开机启动
chmod +x /etc/rc.d/rc.local
netstat -lnput|grep 5666
/usr/local/nagios/libexec/check_nrpe -H localhost //测试本地连接
NRPE v3.2.1
cd /usr/local/nagios/etc/
vi nrpe.cfg
allowed_hosts=127.0.0.1,::1,192.168.80.80 //后面添加监控端80地址,允许服务端地址连接
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
//定义检查用户的命令,超过5个用户w-warning。超过10个用户c-critical 下同
command[check_load]=/usr/local/nagios/libexec/check_load -r -w .15,.10,.05 -c .30,.25,.20
command[check_sda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda1
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
//以上都是客户端定义的nrpe命令,可以被监控端nrpe调用,从而实现监控
/usr/local/nagios/libexec/check_nrpe -H localhost -c check_total_procs
//可先测试本地命令执行情况,确保没有问题
pkill nrpe
/usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg //重启nrpe
/usr/local/nagios/libexec/check_nrpe -H 192.168.80.81 //测试连接情况
/usr/local/nagios/libexec/check_nrpe -H 192.168.80.81 -c check_sda1 //测试命令是否可执行
vi /etc/usr/local/nagios/etc/nagios.cfg //配置主配置文件
cfg_dir=/usr/local/nagios/etc/conf //最后或合适位置新增子目录,便于配置
mkdir /usr/local/nagios/etc/conf
vi /etc/usr/local/nagios/etc/objects/commands.cfg //配置命令配置文件,其中定义的命令可被其他配置文件调用
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
} //最后插入 增加对check_nrpe的定义
vi /etc/usr/local/nagios/etc/objects/contacts.cfg //配置联系人或组,用于出现问题时通知
emial aa@163.com //管理员邮箱地址
cd /usr/local/nagios/etc/conf //开始定义主机和主机组、服务
vi host81.cfg //定义主机和主机组
define host{
use linux-server //监控使用的模板
host_name mysql //监控平台上显示的名称
alias www.bb.com //监控平台上显示的名称
address 192.168.80.81 //被控端地址
}
define hostgroup{ //定义主机组
hostgroup_name testgroup
alias mysql
members mysql
}
vi server81.cfg //定义服务 使用的check_command要在commands.cfg中定义过
define service{
host_name mysql //监控服务名
service_description check-host-alive //监控描述
check_command check-host-alive //监控命令名(主机是否存活)
max_check_attempts 3 //检查失败后重试次数
check_interval 2 //检查间隔时间
retry_interval 2 //重试间隔时间
check_period 24x7 //监控时间段 timeperiods.cfg中定义的时间
notification_interval 300 //提醒的间隔时间,每300秒提醒一次
notification_period 24x7 //提醒的周期,是在timeperiods.cfg中定义的
notification_options w,u,c,r //当主机出现w-warning, u-unknown,c-critical,r-从异常状态恢复时提醒管理员(d-down)
#contact_groups admins
process_perf_data 1
}
define service{
host_name mysql
service_description check-procs
check_command check_nrpe!check_total_procs
max_check_attempts 3
check_interval 2
retry_interval 2
check_period 24x7
notification_interval 300
notification_period 24x7
notification_options w,u,c,r
#contact_groups admins
process_perf_data 1
}
define service{
host_name mysql
service_description check-load
check_command check_nrpe!check_load
max_check_attempts 3
check_interval 2
retry_interval 2
check_period 24x7
notification_interval 300
notification_period 24x7
notification_options w,u,c,r
#contact_groups admins
process_perf_data 1
}
define service{
host_name mysql
service_description check-ssh
check_command check_ssh
max_check_attempts 3
check_interval 2
retry_interval 2
check_period 24x7
notification_interval 300
notification_period 24x7
notification_options w,u,c,r
#contact_groups admins
process_perf_data 1
}
cd /usr/local/nagios/etc //增加nagios组授权
grep nagiosadmin cgi.cfg //检查配置文件中相关授权的用户组默认只有nagiosadmin
sed -i ‘s/nagiosadmin/nagiosadmin,nagios/g‘ cgi.cfg //增加nagios组授权
service nagios restart
win10访问http://192.168.80.80/nagios 就可以看到监控主机80.81
至此,nagios监控基本配置完毕,如何图形化更直观的在web上查看呢?就要添加使用图形化模块,未完待续。。。
原文地址:http://blog.51cto.com/13469709/2094011