nagios原理(一)

一. 准备阶段:
1.实验环境
包含3台主机,详细信息如下
主机名 操作系统 IP 作用 
nagios-server redhat 9 192.168.99.99 监控机 
dbpi redhat 9 192.168.0.100 被监控机 
yahoon xp 192.168.0.28 被监控机
注:
监控机:安装了nagios软件的机器,对监控的数据做处理,并且提供web界面查看和管理.当然也可以对本机自身的信息进行监控.
被监控机:安装了NRPE等客户端,根据监控机的请求执行监控,然后将结果回传给监控机.

2.nagios原理
这里引用某位仁兄的话
nagios的功能是监控服务和主机,但是他自身并不包括这部分功能的代码,所有的监控、检测功能都是有插件来完成的。再说报警功能,如果监控系统发现问 题不能报警那就没有意义了,所以报警也是nagios很重要的功能之一。但是,同样的,nagios自身也没有报警部分的代码,甚至没有插件,而是交给用 户或者其他相关开源项目组去完成。
nagios安装,是指基本平台,也就是nagios软件包的安装。它是监控体系的框架,也是所有监控的基础。 
打开nagios官方的文档,会发现nagios基本上没有什么依赖包,只要求系统是linux或者其他nagios支持的系统。不过如果你没有安装 apache(http服务),那么你就没有那么直观的界面来查看监控信息了,所以apache姑且算是一个前提条件。关于apache的安装,网上有很 多,照着安装就是了。安装之后要检查一下是否可以正常工作。
原文链接[url]http://blog.chinaunix.net/u/28387/showart_356060.html[/url]

3.Nagios定义的监控状态
nagios定义了4中监控状态,代表不同的严重级别,除了OK代表正常不用关心外,其余3种都要引起重视.如下表
状态 代码 颜色 
正常 OK 绿色, 
警告 WARNING 黄色, 
严重 CRITICAL 红色, 
未知错误 UNKOWN 深黄色

4.所用软件
nagios-2.9.tar.gz
nagios-plugins-1.4.9.tar.gz
nrpe-2.8.1.tar.gz
NSClient++-0.2.7.zip
注:
前三个的下载地址:[url]http://www.nagios.org[/url],
后一个的[url]http://sourceforge.net/projects/nscplus[/url]

5.总体目标
学习嘛,总要有个目标,很简单,就是看到下面这张图
具体信息如下表
主机名 要监控的服务 
nagios-server 是否活动 
是否开启ftp 
磁盘使用情况 
dbpi 是否活动 
是否开启ssh 
磁盘使用情况 
cpu负载 
swap分区使用情况 
主进程数 
当前登陆用户 
僵尸进程数 
yahoon 是否活动 
是否开启80端口 
是否启动W3SVC服务 
cpu负载 
内存使用情况 
运行时间 
NSClient客户端版本(NSClient是一个windows下的nagios客户端) 
C盘使用情况 
D盘使用情况 
explorer进程运行情况

其中蓝色字体的服务是外部服务,也就是说不需要登陆被监控机,直接在外部就可以进行检查.例如我们需要检查被监控机是否打开80端口,就可以在别的一台机 器上telnet被监控机的80端口即可.
绿色字体表示是内部服务,意思是必须登陆到被监控机上才能查看.因为它们属于”本地信息”(nagios将之称为LOCAL).很容易理解,你不登陆到被 监控机上,如何知道当前磁盘的使用情况呢?

nagios是不是很强大,基本上对监控对象的任何信息都可以一手掌握了.而且不用登陆到目的机就可以看到那台机器私密的”本地信息”,比木马还厉害啊. 但是别误会,这可不是什么木马.nagios有着十分安全的措施.

这确实是一个庞大的工程,你想想这还只是三台机器而已都已经监控这么多服务了,那要是多个几十台怎么办,不用紧张,从现在开始,跟我动手吧.
家庭作业,将所用的软件下载好,配置好监控机192.168.99.99上的apache.
二. 基本安装和配置
本部分主要参考官方文档和田逸的文章<<看我出招之:我用Nagios(技术细节)>>来修改完成.
最后达到如下的功能
监控机自身的信息,包括主机信息以及对外提供的服务
被监控机对外提供的服务
如下所有的操作都在监控机192.168.99.99上进行

1.安装nagios主程序
解压缩
tar -zxvf nagios-2.9.tar.gz
cd nagios-2.9
编译,指定安装目录为/usr/local/nagios
./configure --prefix=/usr/local/nagios
输出如下信息
*** Configuration summary for nagios 2.9 04-10-2007 ***:
General Options:
-------------------------
        Nagios executable: nagios
        Nagios user/group: nagios,nagios
       Command user/group: nagios,nagios
            Embedded Perl: no
             Event Broker: yes
        Install ${prefix}: /usr/local/nagios
                Lock file: ${prefix}/var/nagios.lock
           Init directory: /etc/rc.d/init.d
                  Host OS: linux-gnu
Web Interface Options:
------------------------
                 HTML URL: [url]http://localhost/nagios/[/url]
                  CGI URL: [url]http://localhost/nagios/cgi-bin/[/url]
Traceroute (used by WAP): /usr/sbin/traceroute
Review the options above for accuracy. If they look okay,
type 'make all' to compile the main program and CGIs.

make all
输出如下信息
*** Compile finished ***
If the main program and CGIs compiled without any errors, you
can continue with installing Nagios as follows (type 'make'
without any arguments for a list of all possible options):
make install
-          This installs the main program, CGIs, and HTML files
使用make install来安装主程序,CGI和HTML文件
make install-init
-          This installs the init script in /etc/rc.d/init.d
使用make install-init在/etc/rc.d/init.d安装启动脚本
make install-commandmode
     - This installs and configures permissions on the
       directory for holding the external command file
使用make install-commandmode来配置目录权限
make install-config
     - This installs *SAMPLE* config files in /usr/local/nagios/etc
       You'll have to modify these sample files before you can
       use Nagios. Read the HTML documentation for more info
       on doing this. Pay particular attention to the docs on
       object configuration files, as they determine what/how
       things get monitored!
使用make install-commandmode来安装示例配置文件,安装的路径是/usr/local/nagios/etc.
*** Support Notes *******************************************
If you have questions about configuring or running Nagios,
please make sure that you:
     - Look at the sample config files
     - Read the HTML documentation
     - Read the FAQs online at [url]http://www.nagios.org/faqs[/url]
before you post a question to one of the mailing lists.
Also make sure to include pertinent information that could
help others help you. This might include:
     - What version of Nagios you are using
     - What version of the plugins you are using
     - Relevant snippets from your config files
     - Relevant error messages from the Nagios log file
For more information on obtaining support for Nagios, visit:
       [url]http://www.nagios.org/support/[/url]
*************************************************************
Enjoy.
很多人都不注意安装过程中的输出信息,直到make install出错了才到处找人求救,而实际上输出的内容包含很多有价值的信息,例如安装路径,版本,每一步做什么,接下来的步骤等.尤其是输出的最后一 屏信息,我个人建议好好的读一下.例如上面就列出了很多有价值的信息,我们只需要按照他说的做就行了.

安装
make install
输出如下错误
cd ./base && make install
make[1]: Entering directory `/home/yahoon/nagios/nagios-2.9/base'
make install-basic
make[2]: Entering directory `/home/yahoon/nagios/nagios-2.9/base'
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/bin
/usr/bin/install: invalid user `nagios' 非法用户nagios
make[2]: *** [install-basic] Error 1
make[2]: Leaving directory `/home/yahoon/nagios/nagios-2.9/base'
make[1]: *** [install] Error 2
make[1]: Leaving directory `/home/yahoon/nagios/nagios-2.9/base'
make: *** [install] Error 2
按照文档说明增加用户,修改权限
[root@localhost nagios-2.9]# useradd nagios
[root@localhost nagios-2.9]# mkdir /usr/local/nagios
[root@localhost nagios-2.9]# chown nagios.nagios /usr/local/nagios
查看目录权限
[root@localhost nagios-2.9]# ll /usr/local
drwxr-sr-x    2 nagios   nagios       4096 Jul 10 11:14 nagios
看到nagios目录的权限已经被正确修改了

重新执行make install输出信息如下
*** Main program, CGIs and HTML files installed ***
You can continue with installing Nagios as follows (type 'make'
without any arguments for a list of all possible options):
make install-init
     - This installs the init script in /etc/rc.d/init.d
make install-commandmode
     - This installs and configures permissions on the
       directory for holding the external command file
make install-config
     - This installs *SAMPLE* config files in /usr/local/nagios/etc
       You'll have to modify these sample files before you can
       use Nagios. Read the HTML documentation for more info
       on doing this. Pay particular attention to the docs on
       object configuration files, as they determine what/how
       things get monitored!
make[1]: Leaving directory `/home/yahoon/nagios/nagios-2.9'

执行如下命令来安装脚本
make install-init

执行
make install-commandmode
输出信息如下
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/var/rw
chmod g+s /usr/local/nagios/var/rw
*** External command directory configured ***
You can continue with installing Nagios as follows (type 'make'
without any arguments for a list of all possible options):
make install-config
     - This installs *SAMPLE* config files in /usr/local/nagios/etc
       You'll have to modify these sample files before you can
       use Nagios. Read the HTML documentation for more info
       on doing this. Pay particular attention to the docs on
       object configuration files, as they determine what/how
       things get monitored!

执行
make install-config
输出信息如下
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc
/usr/bin/install -c -m 664 -o nagios -g nagios sample-config/nagios.cfg /usr/local/nagios/etc/nagios.cfg-sample
/usr/bin/install -c -m 664 -o nagios -g nagios sample-config/cgi.cfg /usr/local/nagios/etc/cgi.cfg-sample
/usr/bin/install -c -m 660 -o nagios -g nagios sample-config/resource.cfg /usr/local/nagios/etc/resource.cfg-sample
/usr/bin/install -c -m 664 -o nagios -g nagios sample-config/template-object/localhost.cfg /usr/local/nagios/etc/localhost.cfg-sample
/usr/bin/install -c -m 664 -o nagios -g nagios sample-config/template-object/commands.cfg /usr/local/nagios/etc/commands.cfg-sample
*** Sample config file installed ***
Remember, these are *SAMPLE* config files. You'll need to read
the documentation for more information on how to actually define
services, hosts, etc. to fit your particular needs.
If you have questions about configuring Nagios properly, please:
       - Look at the sample config files
       - Read the HTML documentation
       - Read the FAQs online at [url]http://www.nagios.org/faqs[/url]
*BEFORE* you post a question to one of the mailing lists.

验证程序是否被正确安装。切换目录到安装路径(这里是/usr/local/nagios),看是否存在 etc、bin、 sbin、 share、 var这五个目录,如果存在则可以表明程序被正确的安装到系统了。后表是五个目录功能的简要说明:
bin Nagios执行程序所在目录,nagios文件即为主程序 
etc Nagios配置文件位置,初始安装完后,只有几个*.cfg-sample文件 
sbin Nagios Cgi文件所在目录,也就是执行外部命令所需文件所在的目录 
Share Nagios网页文件所在的目录 
Var Nagios日志文件、spid 等文件所在的目录 
var/archives Empty directory for the archived logs 
var/rw Empty directory for the external command file

2.安装插件
解压缩
tar -zxvf nagios-plugins-1.4.9.tar.gz
cd nagios-plugins-1.4.9
编译,指定路径为之前nagios的安装路径
./configure --prefix=/usr/local/nagios/
make
安装
make install
ls /usr/local/nagios/libexec/
会显示安装的插件文件,即所有的插件都安装在libexec这个目录下

将apache的运行用户加到nagios组里面
从httpd.conf中过滤出当前的apache运行用户
grep ^User /usr/local/apache2/conf/httpd.conf
我的是vpopmail,下面将这个用户加入nagios组
usermod -G nagios vpopmail

3.修改apache配置
修改apache的配置文件,增加nagios的目录,并且访问此目录需要进行身份验证
vi /usr/local/apache2/conf/httpd.conf,在最后增加如下内容
#setting for nagios 20070707
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>

增加验证用户
也就是通过web访问nagios的时候,必须要用这个用户登陆.在这里我们增加用户test:密码为admin
[root@localhost conf]# /usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd test
New password: (输入admin)
Re-type new password: (再输入一次密码)
Adding password for user admin

查看认证文件的内容
[root@localhost conf]# less /usr/local/nagios/etc/htpasswd 
test:OmWGEsBnoGpIc 前半部分是用户名test,后面是加密后的密码

到这里nagios的安装也就基本完成了,你可以通过web来访问了.
会弹出对话框要求输入用户名密码
输入admin,密码admin,就可以进入nagios的主页面了
但是可以发现什么也点不开,因为nagios还没启动呢!下面的工作就是修改配置文件,增加要监控的主机和服务.
4.典型配置
之前已经说过nagios要用起来,就必须修改配置文件,增加要监控的主机和服务才行.在具体做这个动作之前,下面的概念必须要了解.

1)预备知识
Nagios自己定义了一套规则用于配置文件,其中最重要的概念就是”对象”----object.通俗的理解:假定我们首先定义了”性别”这个对象,它 的值只可能是男,女,人妖等等,然后定义某人为一个对象,例如张三,定义张三的时候有”性别”这个属性,它的值就必须来源了之前定义的性别这个对象,要么 是男是女,或者是人妖.
在Nagios里面定义了一些基本的对象,一般用到的有:
联系人 contact 出了问题像谁报告?一般当然是系统管理员了 
监控时间段 timeperiod 7X24小时不间断还是周一至周五,或是自定义的其他时间段 
被监控主机 host 所需要监控的服务器,当然可以是监控机自己 
监控命令 command nagios发出的哪个指令来执行某个监控,这也是自己定义的 
被监控的服务 service 例如主机是否存活,80端口是否开,磁盘使用情况或者自定义的服务等
另外,多个被监控主机可以定义为一个主机组,多个联系人可以被定义为一个联系人组,多个服务还能定义成一个服务组呢.
回到上面的例子,定义张三需要之前定义的性别,我们定义一个被监控的服务,当然就要指定被监控的主机,需要监控的时间段,要用哪个命令来完成这个监控操 作,出了问题向哪个联系人报告.
所有这些对象绝对多数都是需要我们手动定义的,这就是nagios的安装显得复杂的地方.其实了解了原理,做一遍之后余下的工作就是复制粘贴了.下面就开 始动手.

2)将示例配置文件复制为真实配置文件名
cd /usr/local/nagios/etc
cp cgi.cfg-sample cgi.cfg
cp commands.cfg-sample commands.cfg
cp localhost.cfg-sample localhost.cfg
cp nagios.cfg-sample nagios.cfg
cp resource.cfg-sample resource.cfg

3)修改配置文件
修改nagios的主配置文件nagios.cfg
vi nagios.cfg
注释行 #cfg_file=/usr/local/nagios/etc/localhost.cfg[2],然后把下面几行的注释去掉: 
cfg_file=/usr/local/nagios/etc/contactgroups.cfg //联系组配置文件路径
cfg_file=/usr/local/nagios/etc/contacts.cfg       //联系人配置文件路径
cfg_file=/usr/local/nagios/etc/hostgroups.cfg     //主机组配置文件路径
cfg_file=/usr/local/nagios/etc/hosts.cfg          //主机配置文件路径
cfg_file=/usr/local/nagios/etc/services.cfg       //服务配置文件路径
cfg_file=/usr/local/nagios/etc/timeperiods.cfg    //监视时段配置文件路径
注释掉就说明不使用这个文件,去掉了注释就是需要使用这个文件.
改check_external_commands=0为check_external_commands=1 .这行的作用是允许在web界面下执行重启nagios、停止主机/服务检查等操作。
把command_check_interval的值从默认的1改成command_check_interval=10s(根据自己的情况定这个命令检 查时间间隔,不要太长也不要太短)。
主配置文件要改的基本上就是这些,通过上面的修改,发现/usr/local/nagios/etc并没有文件hosts.cfg等一干文件,怎么办?稍 后手动创建它们。 

修改CGI脚本控制文件cgi.cfg
vi cgi.cfg
第二个要修改的配置文件是cgi.cfg,它的作用是控制相关cgi脚本。先确保use_authentication=1。曾看过不少的文章,都是建议 把use_authentication的值设置成”0”来取消验证,这是一个十分糟糕的想法。接下来修改default_user_name=test ,再后面的修改在下表列出:
authorized_for_system_information=nagiosadmin,test 
authorized_for_configuration_information=nagiosadmin,test
authorized_for_system_commands=test //多个用户之间用逗号隔开
authorized_for_all_services=nagiosadmin,test
authorized_for_all_hosts=nagiosadmin,test
authorized_for_all_service_commands=nagiosadmin,test
authorized_for_all_host_commands=nagiosadmin,test
那么上述用户名打那里来的呢?是执行命令 /usr/local/apache/bin/htpasswd –c /usr/local/nagios/etc/htpasswd test 所生成的,这个要注意,不能随便加没有存在的验证用户,为了安全起见,不要添加过多的验证用户。

定义监控时间段,创建配置文件timeperiods.cfg
[root@localhost etc]# vi timeperiods.cfg
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
        }
定义了一个监控时间段,它的名称是24x7,监控的时间是每天全天24小时

定义联系人,创建配置文件contacts.cfg
[root@localhost etc]# vi contacts.cfg 
define contact{
        contact_name                    test   //联系人的名称,这个地方不要有空格
        alias                           sys admin
        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
        email                           yahoon@test.com
        pager                           1338757xxxx
        address1                        xxxxx.xyyy@icq.com
        address2                        555-555-5555
        }
创建了一个名为test的联系人,下面列出其中重要的几个选项做说明
service_notification_period     24x7
服务出了状况通知的时间段,这个时间段就是上面在timeperiods.cfg中定义的. 
host_notification_period        24x7
主机出了状况通知的时间段, 这个时间段就是上面在timeperiods.cfg中定义的 
service_notification_options    w,u,c,r
当服务出现w—报警(warning),u—未知(unkown),c—严重(critical),或者r—从异常情况恢复正常,在这四种情况下通知联系 人. 
host_notification_options       d,u,r
当主机出现d¬¬¬¬—当机(down),u—返回不可达(unreachable),r—从异常情况恢复正常,在这3种情况下通知联系人 
service_notification_commands   notify-by-email
服务出问题通知采用的命令notify-by-email,这个命令是在commands.cfg中定义的,作用是给联系人发邮件.至于 commands.cfg之后将专门介绍 
host_notification_commands      host-notify-by-email
同上,主机出问题时采用的也是发邮件的方式通知联系人 
email                           yahoon@test.com
很明显,联系的人email地址 
pager                           1338757xxxx
联系人的手机,如果支持短信的通知的话,这个就很有用了. 
alias是联系人别名,address是地址 意义不大.
按照上面的方式简单的复制修改就可以创建多个联系人了.

下面就可以将多个联系人组成一个联系人组,创建文件contactgroups.cfg
[root@localhost etc]# vi contactgroups.cfg
define contactgroup{
        contactgroup_name               sagroup   
//联系人组的名称,同样不能空格
        alias                   System Administrators        //别名
        members                 test 
//组的成员,来自于上面定义的contacts.cfg,如果有多个联系人则以逗号相隔
        }

定义被监控主机,创建文件hosts.cfg
[root@localhost etc]# vi hosts.cfg
define host{
        host_name                       nagios-server
//被监控主机的名称,最好别带空格
        alias                           nagios server
        //别名
        address                         192.168.99.99
        //被监控主机的IP地址,我现在暂时先填本机的IP
        check_command                   check-host-alive
        //监控的命令check-host-alive,这个命令来自commands.cfg,用来监控主机是否存活
        max_check_attempts              5
        //检查失败后重试的次数
        check_period                    24x7
        //检查的时间段24x7,同样来自于我们之前在timeperiods.cfg中定义的
contact_groups                  sagroup
        //联系人组,上面在contactgroups.cfg中定义的sagroup
notification_interval           10
        //提醒的间隔,每隔10秒提醒一次
notification_period             24x7
        //提醒的周期, 24x7,同样来自于我们之前在timeperiods.cfg中定义的
notification_options            d,u,r
//指定什么情况下提醒,具体含义见之前contacts.cfg部分的介绍
        }
通过简单的复制修改就可以定义多个主机了.我们在这加上另外两台机器:
名为dbpi的linux主机,ip为192.168.99.99
名为yahoon的xp主机,ip为192.168.0.28

与联系人可以组成联系人组一样,多个主机也可以组成主机组.创建文件hostgrops.cfg
[root@localhost etc]# vi hostgroups.cfg
define hostgroup{
        hostgroup_name          sa-servers //主机组名称
        alias                   sa Servers //别名
        members                 nagios-server 
//组的成员主机,多个主机以逗号相隔,必须是上面hosts.cfg中定义的
        }

  

原文地址:https://www.cnblogs.com/yuchunju/p/2531041.html