Nagios 插件编写

来源:互联网 发布:闪电精灵seo 编辑:程序博客网 时间:2024/05/17 09:21

脚本编写步骤

1、nagios插件编写规范

Nagios的插件可以用脚本(shell、Perl、python等)C编译后的可执行程序,但必须满足以下两件事:

a、退出时有一个返回值

b、至少向标准输出设备(STDOUT)输出一行文件。(但也不能太大,默认是4K )

Plugin Return Codes(具体参考官网 http://nagios-plugins.org/doc/guidelines.html#PREFACE

Numeric Value

Service Status

Status Description

0

OK

The plugin was able to check the service and it appeared to be functioning properly

1

Warning

The plugin was able to check the service, but it appeared to be above some "warning" threshold or did not appear to be working properly

2

Critical

The plugin detected that either the service was not running or it was above some "critical" threshold

3

Unknown

Invalid command line arguments were supplied to the plugin or low-level failures internal to the plugin (such as unable to fork,or open a tcp socket) that prevent it from performing the specifiedoperation. Higher-level errors (such as name resolution errors,socket timeouts, etc) are outside of the control of plugins and shouldgenerally NOT be reported as UNKNOWN states.

下面是一个插件,就输出一句话:

#!/usr/bin/perl -w

my(%ERRORS) = ( OK=>0, WARNING=>1, CRITICAL=>2, UNKNOWN=>3 );

my $status=$ERRORS{OK};;
if( 1 ) {
    print "OK,it's 1.";
    exit $status;
}

写完脚本之后,要修改它的执行权限:chmod a+x 文件名

// 注意文件有后缀名的别忘了,要文件全称。


使用 Nagios 进行注册:

在 command.cfg中添加命令:(或者在 /etc/nagios-plugins/config/ 目录下创建一个文件,例如 mygetloadavg2.cfg ,内容和下面一样)

define command{
    command_name zccheck_disk
    command_line /usr/lib/nagios/plugins/zccheck_disk.plmygetloadavg2.cfg
}

最后创建一个插件的服务:

define service{
    host_name localhost
    service_description zccheck-disk
    check_command zccheck_disk
    max_check_attempts 5
    normal_check_interval 5
    retry_check_interval 2
    check_period 24x7
    notification_interval 10
    notification_period 24x7
    notification_options w,u,c,r
    contact_groups sagroup
}

这样这个插件就可以显示在网页上了。




0 0
原创粉丝点击