Nagios 简明教程

Nagios - Add-ons/Plugins

插件有助于使用 Nagios 监控数据库、操作系统、应用程序、网络设备、协议。插件是经过编译的可执行文件或脚本(Perl 或非 Perl),它扩展了 Nagios 的功能,以监控服务器和主机。Nagios 将执行插件来检查服务或主机的状态。Nagios 可以编译为支持嵌入式 Perl 解释器,以便执行 Perl 插件。如果没有,Nagios 将通过 fork 进程并执行插件作为外部命令来执行 Perl 和非 Perl 插件。

Plugins helps to monitor databases, operating systems, applications, network equipment, protocols with Nagios. Plugins are compiled executables or script (Perl or non-Perl) that extends Nagios functionality to monitor servers and hosts. Nagios will execute a Plugin to check the status of a service or host. Nagios can be compiled with support for an embedded Perl interpreter to execute Perl plugins. Without it, Nagios executes Perl and non-Perl plugins by forking and executing the plugins as an external command.

Types of Nagios Plugins

Nagios 具有以下插件:

Nagios has the following plugins available in it −

Official Nagios Plugins - 50 个 Nagios 官方插件。 官方 Nagios 插件由官方 Nagios 插件团队开发和维护。

Official Nagios Plugins − There are 50 official Nagios Plugins. Official Nagios plugins are developed and maintained by the official Nagios Plugins Team.

Community Plugins - 3000 多个第三方 Nagios 插件由数百名 Nagios 社区成员开发。

Community Plugins − There are over 3,000 third party Nagios plugins that have been developed by hundreds of Nagios community members.

Custom Plugins - 还可以编写自己的自定义插件。编写自定义插件必须遵循特定指南。

Custom Plugins − You can also write your own Custom Plugins. There are certain guidelines that must be followed to write Custom Plugins.

Guidelines for Writing Custom Nagios Plugins

在 Nagios 中编写自定义插件时,需要遵循以下指南:

While writing custom plugin in Nagios, you need to follow the guidelines given below −

  1. Plugins should provide a "-V" command-line option (verify the configuration changes)

  2. Print only one line of text

  3. Print the diagnostic and only part of the help message

  4. Network plugins use DEFAULT_SOCKET_TIMEOUT to timeout

  5. "-v", or "--verbose“ is related to verbosity level

  6. "-t" or "--timeout" (plugin timeout);

  7. "-w" or "--warning" (warning threshold);

  8. "-c" or "--critical" (critical threshold);

  9. "-H" or "--hostname" (name of the host to check)

多个 Nagios 插件同时运行并执行检查,为了让它们平稳地一起运行,Nagios 插件遵循一个状态码。下表列出了退出码状态及其说明 -

Multiple Nagios plugin run and perform checks at the same time, for all of them to run smoothly together, Nagios plugin follow a status code. The table given below tells the exit code status and its description −

Exit Code

Status

Description

0

OK

Working fine

1

WARNING

Working fine, but needs attention

2

CRITICAL

Not working Correctly

3

UNKNOWN

When the plugin is unable to determine the status of the host/service

Nagios 插件使用选项对其进行配置。以下是 Nagios 插件接受的一些重要参数 -

Nagios plugins use options for their configuration. The following are few important parameters accepted by Nagios plugin −

Sr.No

Option & Description

1

-h, --help This provides help

2

-V, --version This prints the exact version of the plugin

3

-v, --verbose This makes the plugin give a more detailed information on what it is doing

4

-t, --timeout This provides the timeout (in seconds); after this time, the plugin will report CRITICAL status

5

-w, --warning This provides the plugin-specific limits for the WARNING status

6

-c, --critical This provides the plugin-specific limits for the CRITICAL status

7

-H, --hostname This provides the hostname, IP address, or Unix socket to communicate with

8

-4, --use-ipv4 This lets you use IPv4 for network connectivity

9

-6, --use-ipv6 This lets you use IPv6 for network connectivity

10

-p, --port This is used to connect to the TCP or UDP port

11

-s, — send This provides the string that will be sent to the server

12

-e, --expect This provides the string that should be sent back from the server

13

-q, --quit This provides the string to send to the server to close the connection

Nagios 插件包有许多适用于主机和服务的检查,用于监测基础架构。我们尝试使用 Nagios 插件执行一些检查。

Nagios plugin package has lot of checks available for hosts and services to monitor the infrastructure. Let us try out Nagios plugins to perform few checks.

SMTP 是一种用于发送电子邮件的协议。Nagios 标准插件有用于执行 SMTP 检查的命令。SMTP 的命令定义如下:

SMTP is a protocol that is used for sending emails. Nagios standard plugins have commands for perform checks for SMTP. The command definition for SMTP −

define command {
   command_name check_smtp
   command_line $USER2$/check_smtp -H $HOSTADDRESS$
}

让我们使用 Nagios 插件监测 MySQL。Nagios 提供 2 个插件用于监测 MySQL。第一个插件检查 mysql 连接是否正常工作,第二个插件用于计算运行 SQL 查询所需的时间。

Let us use Nagios plugin to monitor MySQL. Nagios offers 2 plugins to monitor MySQL. The first plugin checks if mysql connection is working or not, and the second plugin is used to calculate the time taken to run a SQL query.

以下是这两个插件的命令定义:

The commands definitions for both are as follows −

define command {
   command_name check_mysql
   command_line $USER1$/check_mysql –H $HOSTADDRESS$ -u $ARG1$ -p $ARG2$ -d
   $ARG3$ -S –w 10 –c 30
}

define command {
   command_name check_mysql_query
   command_line $USER1$/check_mysql_query –H $HOSTADDRESS$ -u $ARG1$ -p $ARG2$ -d
   $ARG3$ -q $ARG4$ –w $ARG5$ -c $ARG6$
}

Note − 在这两个命令中,用户名、密码和数据库名称都是必须提供的参数。

Note − Username, password, and database name are required as arguments in both the commands.

Nagios 提供了用于检查安装在所有分区上的磁盘空间的插件。命令定义如下:

Nagios offers plugin to check the disk space mounted on all the partitions. The command definition is as follows

define command {
   command_name check_partition
   command_line $USER1$/check_disk –p $ARG1$ –w $ARG2$ -c $ARG3$
}

大多数检查都可以通过 Nagios 标准插件完成。但是有一些应用程序需要进行特殊检查才能进行监测,对于这些应用程序,你可以使用第三方 Nagios 插件,它们可以提供对应用程序更为复杂的检查。在你使用 Nagios Exchange 的第三方插件或从其他网站下载该插件时,重要的是了解其安全性和许可问题。

Majority of checks can be done through standard Nagios plugins. But there are applications which require special checks to monitor them, in which case you can use 3rd party Nagios plugins which will provide more sophisticated checks on the application. It is important to know about security and licensing issues when you are using a 3rd party plugin form Nagios exchange or downloading the plugin from another website.