Nagios是一个监视系统运行状态和网络信息的监视系统。Nagios能监视所指定的本地或远程主机以及服务,同时提供异常通知功能等。
Nagios可运行在Linux/Unix平台之上,同时提供一个可选的基于浏览器的WEB界面以方便系统管理人员查看网络状态,各种系统问题,以及日志等等。
nagios常用配置文件主要用以下6个:
cgi.cfg(控制cgi访问的配置文件),nagios.cfg(Nagios主配置文件),resource.cfg(resource.cfg定义了一些变量,以便被其他文件引用,如$USER1$),objects(objects是一个目录,用于定义Nagios对象),servers(servers是自己创建的目录,Nagios可以加载该目录下所有配置文件,常用于定义各远程服务器监控信息,需要在nagios.cfg中配置)
其中:
objects目录下文件::
commands.cfg #命令定义配置文件,里面定义的命令可以被其他文件引用
contacts.cfg #联系人和联系人组配置文件
localhost.cfg #监控本地机器的配置文件
printer.cfg #监控打印机的一个事例配置文件(默认未启用)
switch.cfg #监控路由器的一个事例配置文件(默认未启用)
templates.cfg #模板配置文件,在此可以定义模板,在其他文件中引用
timeperiods.cfg #定义监控时间段的配置文件
windows.cfg #监控Windows的一个事例配置文件(默认未启用)
Nagios所需要的运行条件是机器必须可以运行Linux(或是Unix变种)并且有C语言编译器。你必须正确地配置TCP/IP协议栈以使大多数的服务检测可以通过网络得以进行。你需要但并非必须正确地配置Nagios里的CGIs程序,而一旦你要使用CGI程序时,你必须要安装以下这些软件。
1.nagios安装##可参考官网文档
所需软件包:gd-devel-2.0.35-11.el6.x86_64.rpm nagios-plugins-2.1.1.tar.gz nagios-cn-3.2.3.tar.bz2
tar jxf nagios-cn-3.2.3.tar.bz2 yum install gd-devel-2.0.35-11.el6.x86_64.rpm -y
cd nagios-cn-3.2.3 groupadd nagcmd useradd -M -d /usr/local/nagios -G nagcmd nagios usermod -G nagcmd apache
./configure --with-command-group=nagcmd make all ##可以看到后面步骤 make install make install-init make install-config make install-commandmode make install-webconf
/etc/httpd/conf.d/nagios.conf cat /usr/local/nagios/etc/htpasswd.users ##可看到用户名和密码 htpasswd /usr/local/nagios/etc/htpasswd.users nagiosadmin ##修改nagiosadmin的密码
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/httpd start /etc/init.d/nagios start
http://172.25.45.1/nagios/
用户名:nagiosadmin 密码:修改的密码
2.nagios-plugins安装
tar zxf nagios-plugins-2.1.1.tar.gz cd nagios-plugins-2.1.1
./configure##可看到插件安装的信息,可根据缺少的插件进行安装。例如:mysql-devel,openssl-devel make make install
cd /usr/local/nagios/libexec chown nagios.nagios * -R
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios reload
cd /usr/local/nagios/etc/ vim nagios.cfg
添加两个cfg_file(33行下面)
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
注释掉cfg_file=/usr/local/nagios/etc/objects/localhost.cfg(38行)
cd objects/ cp -p localhost.cfg hosts.cfg cp -p localhost.cfg services.cfg vim hosts.cfg
将内容修改为以下:
define host{
use linux-server
host_name server1.example.com
alias Manager
# parents MainSwitch
address 172.25.45.1
icon_p_w_picpath server.gif
statusmap_p_w_picpath server.gd2
2d_coords 500,200
3d_coords 500,200,100
}
define hostgroup{
hostgroup_name linux-servers ; The name of the hostgroup
alias Linux Servers ; Long name of the group
members * ; Comma separated list of hosts that belong to this group
}
vim services.cfg
将166行之前全部删掉,再删除第8行开始的servicegroup,只留下一个servicegroup。将此servicegroup的members最后两个成员HomeRouter和PING删除
:%s/田朝阳家用机/server1.example.com/g
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios reload
http://172.25.45.1/nagios/
cd /usr/local/nagios/libexec ./check_disk ./check_disk -w 20 -c 10 ./check_disk -w 20 -c 10 -p /
vim services.cfg
添加以下内容:
max_check_attempts 2
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios reload
http://172.25.45.1/nagios/
【server2】
yum install -y mysql-server /etc/init.d/mysqld start mysql_secure_installation
mysql -p mysql> create database nagdb; mysql> grant select on nagdb.* to nagios@'172.25.45.1' identified by 'westos'; mysql> quit
【server1】
cd /usr/local/nagios/libexec ./check_mysql -H 172.25.45.2 -u nagios -p westos
mysql -h 172.25.45.2 -u nagios -p mysql> show databases; mysql> quit
将serevr2的mysql服务关闭
cd /usr/local/nagios/etc/objects vim commands.cfg
在212行后添上以下内容:
# 'check_mysql' command definition
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H $HOSTADDRESS$ -u $ARG1$ -p $ARG2$
}
vim services.cfg
在最后添上以下内容:
#######################check_mysql
define service{
use local-service
host_name server2.example.com
service_description MYSQL
check_command check_mysql!nagios!westos
}
vim hosts.cfg
在上面host下再添加一个host,内容如下:
define host{
use linux-server
host_name server2.example.com
alias Manager
# parents MainSwitch
address 172.25.45.2
icon_p_w_picpath server.gif
statusmap_p_w_picpath server.gd2
2d_coords 400,100
3d_coords 400,100,100
}
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios reload
http://172.25.45.1/nagios/
###添加连线
vim hosts.cfg
将alias的值改为server2
将parents的注释取消,并将值改为server1.example.com
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios reload
http://172.25.45.1/nagios/
此时HTTP的告警状态是因为没有index.html页面,在/var/www/html下建立一个index.html文件就可以解决HTTP告警问题
3.安装nrpe
【server2】
所需软件包:nrpe-2.15.tar.gz nagios-plugins-2.1.1.tar.gz
tar zxf nrpe-2.15.tar.gz tar zxf nagios-plugins-2.1.1.tar.gz
useradd -M -d /usr/local/nagios nagios cd nagios-plugins-2.1.1 ./configure
出现以下错误,是因为缺少软件,下载gcc可解决
[root@server2 nagios-plugins-2.1.1]# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether to disable maintainer-specific portions of Makefiles... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/root/nagios-plugins-2.1.1':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details.
yum install openssl-devel -y
./configure##这里不需要--with-mysql,可以不安装
make make install
cd /usr/local/nagios/ chown nagios.nagios . -R
yum install -y xinetd
cd /root/nrpe-2.15 ./configure make all make install-plugin make install-daemon make install-daemon-config make install-xinetd
cd /etc/xinetd.d vim nrpe
将15行的only_from改为172.25.45.1
vim /etc/services
添加以下内容:
nrpe 5666/tcp
/etc/init.d/xinetd start netstat -antlpe
vim /usr/local/nagios/etc/nrpe.cfg
修改221行,改为以下内容:
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
/etc/init.d/xinetd restart
cd /usr/local/nagios/libexec/ scp check_nrpe root@172.25.45.1:/usr/local/nagios/libexec/
【server1】
cd /usr/local/nagios/libexec/ chown nagios.nagios check_nrpe ./check_nrpe -H 172.25.45.2 ./check_nrpe -H 172.25.45.2 -c check_disk
测试:
在server2将/usr/local/nagios/etc/nrpe.cfg修改,将command[check_disk] -w 改为90%,并重启服务
./check_nrpe -H 172.25.45.2 -c check_disk
将server2的/usr/local/nagios/etc/nrpe.cfg文件恢复上一步之前,并重启服务。
【server1】
cd /usr/local/nagios/etc/objects vim commands.cfg
在# 'check_mysql' command definition这一段下添加以下内容:
# 'check_nrpe' command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
vim services.cfg
在最后添上以下内容:
#######################check_nrpe
define service{
use local-service ; Name of service template to use
host_name server2.example.com
service_description 根分区
check_command check_nrpe!check_disk
}
define service{
use local-service ; Name of service template to use
host_name server2.example.com
service_description 登录用户数
check_command check_nrpe!check_users
}
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios reload
http://172.25.45.1/nagios/
4.虚拟机连网
ip addr add 172.25.254.101/24 dev eth0 ip addr show
ip route show route -n ping 172.25.254.45 route del default gw 172.25.45.250 route add default gw 172.25.254.45 route -n
vim /etc/resolv.conf
ping 192.168.122.1 ping 172.25.254.45
测试网络是否连通
dig baidu.com ping baidu.com
已连网
在家里用无限上网,也可以用NAT的方式进行虚拟机连网,网络配置选择DHCP分配网络
可以看到本地发送邮件的地址。
登录邮箱,进行设置。在反垃圾处设置邮件地址白名单:nagios@server1.example.com
【server1】
yum install mail -y
su - nagios mail wjl19940429@qq.com
cd /usr/local/nagios/etc/objects vim contacts.cfg
修改35行email的值为自己的邮箱
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios reload
【server2】
/etc/init.d/mysqld stop
【server1】
vim /usr/local/nagios/etc/objects/templates.cfg
修改186行max_check_attempts的值为3
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios reload
为了尽快测试,将检测次数减少一些
##110云告警##
所需软件包:alert-agent-4.1.3.1-linux-x64.tar.gz
su - nagios -bash-4.1$ tar zxf alert-agent-4.1.3.1-linux-x64.tar.gz -C /usr/local/nagios/libexec/ -bash-4.1$ cd /usr/local/nagios/libexec/ -bash-4.1$ cd alert-agent/ -bash-4.1$ ll
-bash-4.1$ cd plugin/nagios-plugin/ -bash-4.1$ cp nagios /usr/local/nagios/libexec/ -bash-4.1$ cp 110monitor.cfg /usr/local/nagios/etc/objects/
在110云告警页面选择配置,添加应用云告警
复制下key
cd /usr/local/nagios/etc/objects vim 110monitor.cfg
将复制下的key粘贴在18行,paper后
vim contacts.cfg
在54行members后添加110monitor
cd .. vim nagios.cfg
在35行之后添加cfg_file=/usr/local/nagios/etc/objects/110monitor.cfg
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios reload
网页进行下一步,完成配置。
选择通知策略,进行配置
例如
【server2】
/etc/init.d/mysqld stop
【server1】
cd /usr/local/nagios/var tail -f nagios.log
/etc/init.d/nagios reload
缺云告警成功示例截图
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。