nagios安装部署#yyds干货盘点#

听闻少年二字,当与平庸相斥。这篇文章主要讲述nagios安装部署#yyds干货盘点#相关的知识,希望能为你提供帮助。
Nagios
Nagios是一款开源的计算机系统和网络监视工具,能有效的监控Windows、Linux的主机状态,在系统或服务状态异常时发出电子邮件或者短信报警,第一时间通知网站运维人员,在状态恢复后发出正常的电子邮件或短信通知。


工作原理:
Nagios的功能是监控服务器和主机,但是其本身并不包括这部分功能,所有的监控、监测功能都是通过各种插件来完成的,启动nagios后,它会周期性的自动调用插件去监测服务器状态,同时nagios会维持一个队列,所有插件返回来的状态信息都进入队列,nagios每次都从队列首开始读取信息,进行处理后,把状态结果通过web显示出来。Nagios提供了许多插件,利用这些插件可以方便的监控很多服务窗体。安装完成之后,在nagios主目录下的/libexec里放有nagios自带的可以使用的所有插件,如check_disk是检查磁盘空间的插件,check_load是检查CPU的插件等。


Nagios四种监控状态:
Nagios可以识别四种状态返回信息,即0(ok)表示正常状态,绿色;1(warning)表示出现警告,黄色;2(critical)表示出现非常严重的错误,红色;3(unknown)表示未知错误,深黄色。


Nagios通过nrpe插件来远程管理服务:
?Nagios执行安装在它里面的check_nrpe插件,并告诉check_nrpe去检测哪些服务;
?通过SSL,check_nrpe连接远端机器上的nrpe daemon;
?Nrpe运行本地的各种插件去检测本地的服务和状态;
?Nrpe把检测的结果传给主机端的check_nrpe,check_nrpe再把结果送到nagios状态队列中;
Nagios一次读取队列中的信息,再把结果显示出来


总结一下nagios主要可以监控以下方面:
1、主机是否宕机(通过ping命令,如果ping不通会认为主机属于宕机状态,但不影响所监控的其他服务)
2、服务器资源(cpu使用率、硬盘剩余空间等)
3、网络服务(smtp\\pop3\\http\\)
4、监控网络设备(路由器、交换机等)


安装部署nagios:
环境:
Nagios服务器:192.168.152.136 centos6_x64
mysql服务器:192.168.152.130 centos6_x64
Apache服务器:192.168.152.130 centos6_x64
Nagios服务器上需要安装nagios、nagios-plugins、nrpe;mysql和Apache服务器上需要安装nagios-plugins、nrpe

安装部署:
Nagios服务器
关闭防火墙:
#service iptables stop
#chkconfig iptables off
新建nagios用户和组:
#useradd –s /sbin/nologin nagios
#mkdir /usr/local/nagios
#chown –R nagios.nagios /usr/local/nagios
编译安装nagios:
#yum -y install httpd php mysql-devel openssl openssl-devel gcc glibc glibc-common gd gd-devel
首先实现lamp即可。

https://www.nagios.org/downloads/ nagios下载
#tar zxf nagios-4.2.4.tar.gz
#cd nagios-4.2.4
#./configure --prefix=/usr/local/nagios/
#make all
#make install
#make install-init
#make install-commandmode
#make install-config
#make install-webconf
注意:
?安装install-webconf是为了生成配置文件,后面在/etc/httpd/conf/httpd.conf最后添加的信息就不用手工打了,可以到/etc/httpd/conf.d/nagios.conf文件中复制。
以上命令的解释:
make install //安装主程序,CGI和html文件
make install-init //在/etc/rc.d/init.d安装启动脚本
make install-commandmode //配置目录权限
make install-config //安装示例配置文件
make install-webconf //安装nagios的web接口,会在/etc/httpd/conf.d目录中创建nagios.conf文件。
?安装完成之后会在/usr/local/nagios目录下产生6个目录,下面分别解释一下。
bin:nagios执行程序所在的目录,nagios文件即为主程序。
etc:nagios配置文件目录,当make install-config完以后etc下面就会出现默认的配置文件。
sbin:nagios CGI文件所在目录,这里存放的是一些外部命令执行程序。
share:nagios网页文件目录,存放一些html文件。
var:nagios日志文件、pid等文件目录。
Libexec:系统默认插件的存储位置

#chkconfig --add nagios
安装nagios插件(nagios监控功能通过插件来完成):
#tar zxf nagios-plugins-2.1.1.tar.gz
#cd nagios-plugins-2.1.1
#./configure --prefix=/usr/local/nagios/
#make & & make install
安装nrpe(为了监控远程服务器):
#tar zxf nrpe-3.2.1.tar.gz
#cd nrpe-3.2.1
#./configure
#make all & & make install-plugin
?接下来在/etc/httpd/conf/httpd.conf文件最后添加授权,我们可以到/etc/httpd/conf.d/nagios.conf文件中复制。
#vi /etc/httpd/conf/httpd.conf
之后使用r导入即可:
: r /etc/httpd/conf.d/nagios.conf

?执行htpasswd命令添加一个方位nagios页面的授权用户:
#htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
New password:
Re-type new password:
Adding password for user nagiosadmin

?#service httpd restart?
??浏览器中访问nagios:?
???http://192.168.152.136/nagios???

?进入之后页面:?

现在只能打开网页,很多的监控选项不能看到,如果需要监控远程的服务器,还需要做很多配置,下面开始配置。
nagios监控系统涉及知识点
1)nagios的配置文件:
Nagios.cfg:主配置文件,定义各种配置文件的名称和位置
Cgi.cfg:控制CGI的配置文件
Resource.cfg:资源文件,定义各种变量,以便于其他文件调用
Objects:其他配置文件存放目录,此目录下主要有:
Command.cfg:命令配置文件,定义各种命令格式,以备其他文件调用
contacts.cfg:联系人和组,发邮件等告警信息时可以调用
localhost.cfg:监控本机的配置文件
timeperiods.cfg:定义监控时间的配置文件,便于其他文件调用
Hostgroups.cfg:定义监控的主机(组),需手动创建。
2) 配置文件之间的关系
在nagios的配置过程中涉及的几个定义有主机、主机组、服务、服务组、联系人、联系人组、监控时间和监控命令等。从这些定义可以看出,nagios各个配置文件之间互为关联、彼此引用的。成功配置出一台nagios监控系统,必须要弄清楚每个配置文件之间依赖与被依赖的关系,最重要的有四点
a)定义监控那些主机,主机组,服务和服务组
b)定义这个监控要用什么命令实现
c)定义监控的时间段
d)定义主机或服务器出现问题时要通知的联系人和联系人祖
3) 配置nagios
为了能更清楚的说明问题,同时也为了维护方便,建议将nagios各个定义的对象创建独立的配置文件。
a)创建conf目录来定义host主机
b)创建hostgroups.cfg文件来定义主机组
c)用默认的contacts.cfg文件来定义联系人和联系人组
c)用默认的contacts.cfg文件来定义联系人和联系人组
d)用默认的commands.cfg文件来定义命令
e)用默认的timeperiods.cfg来定义监控时间段
f)用默认的templetes.cfg文件作为资源引用文件
?配置nagios
#vi /usr/local/nagios/etc/nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg
cfg_dir=/usr/local/nagios/etc/conf

#vi /usr/local/nagios/etc/objects/commands.cfg
添加用来定义check_nrpe的监控命令
define commandundefined
command_name check_nrpe
command_line U S E R 1 USER1USER1/check_nrpe -H H O S T A D D R E S S HOSTADDRESSHOSTADDRESS -c A R G 1 ARG1ARG1


?修改/usr/local/nagios/etc/objects/contacts.cfg(定义监控服务器联系人)?
?#vi /usr/local/nagios/etc/objects/contacts.cfg?

define contactundefined
contact_name zs
alias zs
service_notification_period 24x7
host_notification_period 24x7
service_notification_options 24x7
host_notification_options 24x7
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
email 724058013@qq.com

define contactgroupundefined
contactgroup_name zs
alias zs
members zs

新建/usr/local/nagios/etc/objects/hostgroups.cfg(定义主机组)
#vi /usr/local/nagios/etc/objects/hostgroups.cfg
define hostgroupundefined
hostgroup_name mysql
alias mysql
members 192.168.152.130


在/usr/local/nagios/etc/conf下面新建192.168.1.20.cfg文件(用于监控192.168.1.20的主机存活,负载,进程)(所有内容需要手工输入)


mkdir /usr/local/nagios/etc/conf
vi /usr/local/nagios/etc/conf/192.168.152.130.cfg
define host undefined
host_name 192.168.152.130 被监控主机的名称,最好别带空格
alias 192.168.152.130 别名
address 192.168.152.130
check_command check-host-alive
max_check_attempts 5
check_period 24x7
contact_groups zs
notification_period 24x7
notification_options d,u,r

define service undefined
host_name 192.168.152.130
service_description check-host-alive
check_command check-host-alive
max_check_attempts 5
normal_check_interval 2
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups zs

define service undefined
host_name 192.168.152.130
service_description check-procs
check_command check_nrpe!check_total_procs
max_check_attempts 3
normal_check_interval 2
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups zs

define service undefined
host_name 192.168.152.130
service_description check-load
check_command check_nrpe!check_load
max_check_attempts 3
normal_check_interval 2
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups zs




命令解释:
define hostundefined
use linux-server //定义使用的模板
host_name nagios //被监控主机的名称,最好别带空格
alias nagios //别名
address 127.0.0.1 //被监控主机的IP地址
check_command check-host-alive
normal_check_interval 3 //正常检测间隔时间
retry_check_interval 2 //重试检测间隔时间
//监控的命令check-host-alive,这个命令来自commands.cfg,用来监控主机是否存活
max_check_attempts 5 //检查失败后重试的次数
check_period 24x7 //检查的时间段24x7,同样来自timeperiods.cfg中定义
notification_interval 10 //提醒的间隔,每隔10秒提醒一次
notification_period 24x7 //提醒的周期, 24x7,同样来自timeperiods.cfg中定义
contact_groups admins //联系人组,上面在contactgroups.cfg中定义的admins
notification_options d,u,r //指定什么情况下提醒


?重启nagios服务:
#service nagios restart
Web中访问nagios服务:

配置被监控端(192.168.152.130的mysql):
安装nagios插件(在130上操作):
#yum -y install gcc* openssl openssl-devel
#useradd nagios
#tar zxf nagios-plugins-2.1.1.tar.gz
#cd nagios-plugins-2.1.1
#./configure --prefix=/usr/local/nagios
#make & & make install
#chown -R nagios.nagios /usr/local/nagios/
#tar zxf nrpe-3.2.1.tar.gz
#cd nrpe-3.2.1
#./configure --prefix=/usr/local/nagios/
#make all & & make install-plugin & & make install-daemon
修改配置文件,添加nagios服务器地址:
#cp /usr/local/src/nrpe-3.2.1/sample-config/nrpe.cfg /usr/local/nagios/etc/
#/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d
#vi /usr/local/nagios/etc/nrpe.cfg
allowed_hosts=127.0.0.1, 192.168.152.136

启动nrpe:
#/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d
在nagios服务器上测试nrpe运行是否正常:
#/usr/local/nagios/libexec/check_nrpe -H 192.168.152.130
NRPE v3.2.1

















































【nagios安装部署#yyds干货盘点#】


    推荐阅读