2018-11-28 Nagios 配置使用要点

一、Nagios配置文件 /usr/local/nagios/etc/nagios.cfg
使用以下命令可以检查配置文件是否有问题,会告诉你有哪些“Warnings”和“Errors”,前者不影响Nagios启动,后者会中断检查并把第一个错误信息打印给你,直到你改正错误,才能够启动Nagios。

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

Nagios已经有一些模板,可以把注释的“#”取消以启用,也可以自定义存放配置文件,或者指定使用某个目录存放配置文件。

下面是可能用到的配置文件列表:

/usr/local/nagios/etc/cgi.cfg      控制CGI的配置文件
/usr/local/nagios/etc/nagios.cfg         Nagios 主配置文件
/usr/local/nagios/etc/resource.cfg    用于定义变量,其他配置文件会引用这个文件里的内容,如$USER1$
/usr/local/nagios/etc/objects     这个目录下有一些配置文件模板
       /commands.cfg          命令定义配置文件
       /contacts.cfg          联系人和联系人组配置文件
       /localhost.cfg         监控本地主机配置文件
       /printer.cfg           监控打印机的一个配置文件模板
       /switch.cfg            监控路由器的一个配置文件模板
       /templates.cfg         主机和服务的一个模板配置文件
       /timeperiods.cfg       Nagios 监控时间段的配置文件
       /windows.cfg           Windows 主机的一个配置文件模板

要使用的话,在nagios.cfg文件里把注释去掉,或者自己定义,比如我就把配置文件都放到了monitor目录下:

# Definitions for monitoring a router/switch
#cfg_file=/usr/local/nagios/monitor/switch.cfg
#指定使用monitor目录下的commands.cfg
cfg_file=/usr/local/nagios/monitor/commands.cfg

或者指定使用某个目录下所有配置文件

cfg_dir=/usr/local/nagios/monitor

配置文件之间的逻辑关系:
1、要监控哪些主机,主机组,服务和服务组;
2、用什么命令来实现监控;
3、监控时间段;
4、报警信息发送给哪些联系人和组;

根据逻辑关系,我们要定义这些配置文件

  • 创建一个文件来定义主机和主机组,可以使用hosts.cfg,在我的案例里我使用”cfl.cfg”来定义我的主机和组,使用”radios.cfg”来定义一组微波设备
  • 创建services.cfg文件来定义服务
  • 使用默认的contacts.cfg文件来定义联系人和联系人组
  • 使用默认的commands.cfg文件来定义命令
  • 使用默认的timeperiods.cfg来定义监控时间段
  • 使用默认的templates.cfg文件作为资源引用文件

二、配置案例
以增加两台路由器及链路为例,这里以Nagios自带的ping为例,增加uptime的检查。
(1)定义联系人组和联系人:我在contacts.cfg里定义的,也可以直接抄模板。这里关联的commands下面在命令里会写。

define contact {
        name                            cfl-contact
        service_notification_period     24x7
        host_notification_period        24x7
        service_notification_options    w,u,c,r,f,s
        host_notification_options       d,u,r,f,s
        service_notification_commands   cfl-notify-service-by-email
        host_notification_commands      cfl-notify-host-by-email
        register                        0
}

define contact {
        contact_name                    axing
        use                             cfl-contact
        alias                           Anthony Xing
        email                           anthony.xing@gmail.com
        host_notification_options       d,u,r,s
        service_notification_options    w,u,c,r,s
}

(2)定义命令:在commands.cfg里自定义告警发送邮件:

define command{
        command_name                    cfl-notify-host-by-email
        command_line                    /usr/bin/printf "%b" "Subject: $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$\n\n***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $HOSTSTATE$\nDuration: $HOSTDURATION$\n\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n\nhttps://nagios.communityfibre.co.uk/nagios/cgi-bin/extinfo.cgi?type=1&host=$HOSTNAME$" | /usr/sbin/sendmail -vt $CONTACTEMAIL$

}

define command{
        command_name                    cfl-notify-service-by-email
        command_line                    /usr/bin/printf "%b" "Subject: $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$\n\n***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n\nhttps://nagios.communityfibre.co.uk/nagios/cgi-bin/extinfo.cgi?type=2&host=$HOSTNAME$&service=$SERVICEDESC$" | /usr/sbin/sendmail -vt $CONTACTEMAIL$
}

(4)定义监控组:在cfl.cfg里还添加routers组:

define hostgroup {
    hostgroup_name          routers                            ; The name of the hostgroup
    alias                   NE05E-SQ and NE05E-SE                    ; Long name of the group
}

(5)定义service:可以写在services.cfg文件里,也可以直接写在cfl.cfg文件里。这里要注意的是,自定义监控内容要参考MIB库找到OID,这里监控的华为NE05E路由器的uptime的OID是:1.3.6.1.6.3.10.2.1.3.0

define service {
    use                     generic-service                     ; Inherit values from a template
    hostgroup_name          routers                  ; The name of the host the service is associated with
    service_description     PING                                ; The service description
    check_command           check_ping!200.0,20%!600.0,60%      ; The command used to monitor the service
    check_interval          5                                   ; Check the service every 5 minutes under normal conditions
    retry_interval          1                                   ; Re-check the service every minute until its final/hard state is determined
}


# Monitor uptime via SNMP

define service {

    use                     generic-service                     ; Inherit values from a template
    hostgroup_name          routers
    service_description     Uptime
    check_command           check_snmp!-C kjsd934js -o 1.3.6.1.6.3.10.2.1.3.0
}

其中 "generic-service" 在template.cfg里定义了,"generic" 系列还有contact, host, 等等,直接拿来use就好了。

(6)添加要监控的主机:在cfl.cfg文件里增加:

#
# Clem Attlee
#

define host {

    use                     generic-switch                      ; Inherit default values from a template
    host_name               ar01.cle.lon                     ; The name we're giving to this switch
    alias                   Clem Attlee NE05E-SQ              ; A longer name associated with the switch
    address                 191.209.86.143                     ; IP address of the switch
    hostgroups              routers                          ; Host groups this switch is associated with
    parents                 ar01.whi.lon                         ; Host groups this switch is associated with
    icon_image              router41.jpg
#    statusmap_image        router.gd2
}

#
# West Kensington
#

define host {

    use                     generic-switch                      ; Inherit default values from a template
    host_name               ar01.wsk.lon                     ; The name we're giving to this switch
    alias                   West Kensington NE05E-SQ              ; A longer name associated with the switch
    address                 191.209.86.162                     ; IP address of the switch
    hostgroups              routers                          ; Host groups this switch is associated with
    parents                 ar01.Cle.lon                         ; Host groups this switch is associated with
    icon_image              router41.jpg
#    statusmap_image        router.gd2
}

(7)上传到/usr/local/nagios/monitor目录下(还记得在nagios.cfg文件里定义的吗?)
运行 /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

Nagios Core 4.4.1
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
...
Running pre-flight check on configuration data...

Checking objects...
...
    Checked 299 services.
    Checked 176 hosts.
    Checked 12 host groups.
    Checked 0 service groups.
    Checked 4 contacts.
    ...
Total Warnings: 6
Total Errors:   0

Things look okay - No serious problems were detected during the pre-flight check
#

有些Warnings但是不影响,现在重启Nagios

root@nagios:~# /etc/init.d/nagios-nrpe-server restart
[ ok ] Restarting nagios-nrpe-server (via systemctl): nagios-nrpe-server.service.
或者使用
root@nagios:~# service nagios restart
下面一条命令如果正常的话没有提示

好了,使用简单写到这,下一篇添加对微波的监控。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,753评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,668评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,090评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,010评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,054评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,806评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,484评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,380评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,873评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,021评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,158评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,838评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,499评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,044评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,159评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,449评论 3 374
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,136评论 2 356

推荐阅读更多精彩内容