ksaitoの日記

日々試したことの覚え書き

nagiosでDHCPサーバを監視する

移転しました。

自動的にリダイレクトします。

nagios-pluginsには、DHCPサーバを監視するためのプラグインがあります。
このプラグインは、HDCPサーバに問い合わせをするためにroot権限が必要です。
Nagiosに組み込み監視するには、sudoと組み合わせる必要があります。

こんな感じになります。

動作確認

Debian/Ubuntuの場合、プラグインは、下記にあります。

$ cat /etc/nagios-plugins/config/dhcp.cfg 
# note: these plugins require root privilege.  see README.Debian for
# more information on how it is recommended that you do this.

# 'check_dhcp' command definition
define command{
	command_name	check_dhcp
	command_line	/usr/lib/nagios/plugins/check_dhcp -s $HOSTADDRESS$
	}

# 'check_dhcp_interface' command definition
define command{
	command_name	check_dhcp_interface
	command_line	/usr/lib/nagios/plugins/check_dhcp -s $HOSTADDRESS$ -i $ARG1$
	}

プラグインを実行するとroot権限が必要なことが分かります。

$ /usr/lib/nagios/plugins/check_dhcp
Error: Could not bind socket to interface eth0.  Check your privileges...
$ sudo /usr/lib/nagios/plugins/check_dhcp
OK: Received 1 DHCPOFFER(s), max lease time = 14400 sec.
$

シェルを作る

nagiosプラグインを実行するときには、root権限はありません。
プラグインを呼び出すときにroot権限を得るために次のようなシェルを準備します。

$ cat check_dhcp.sh 
#! /bin/sh

/usr/bin/sudo /usr/lib/nagios/plugins/check_dhcp
$

sudoの設定

visudoコマンドを実行して作成したシェルとプラグインをパスワードなしでroot権限で実行できるように設定します。

$ sudo visudo
 ... ここで/etc/sudoersファイルを編集 ...
$ sudo grep nagios /etc/sudoers 
nagios	ALL=(root) NOPASSWD:/etc/nagios2/check_dhcp.sh
nagios	ALL=(root) NOPASSWD:/usr/lib/nagios/plugins/check_dhcp
$

nagiosの設定

作成したシェルをnagiosに登録します。

$ cat conf.d/commands.cfg
define command {
	command_name    check_dhcp_free
        command_line    /etc/nagios2/check_dhcp.sh
}
$

次にサービスを登録します。

$ hg diff
diff -r 30c2c90deb5b conf.d/services.cfg
 --- a/conf.d/services.cfg	Tue Sep 29 22:19:27 2009 +0900
 +++ b/conf.d/services.cfg	Tue Sep 29 22:27:10 2009 +0900
@@ -1,3 +1,11 @@ define service {
 +define service {
 +	host_name			gateway
 +	service_description		DHCP
 +	check_command		check_dhcp_free
 +	use					generic-service
 +	notification_interval         0 ; set > 0 if you want to be renotified
 +}
 +	

nagiosの再起動

いつもの手順で設定ファイルのチェックと再起動をします。

$ sudo nagios2 c/nagios2/nagios.cfg 

Nagios 2.6
Copyright (c) 1999-2006 Ethan Galstad (http://www.nagios.org)
Last Modified: 11-27-2006
License: GPL

Reading configuration data...

Running pre-flight check on configuration data...

Checking services...
	Checked 12 services.
Checking hosts...
	Checked 4 hosts.
Checking host groups...
	Checked 11 host groups.
Checking service groups...
	Checked 0 service groups.
Checking contacts...
	Checked 1 contacts.
Checking contact groups...
	Checked 1 contact groups.
Checking service escalations...
	Checked 0 service escalations.
Checking service dependencies...
	Checked 0 service dependencies.
Checking host escalations...
	Checked 0 host escalations.
Checking host dependencies...
	Checked 0 host dependencies.
Checking commands...
	Checked 121 commands.
Checking time periods...
	Checked 5 time periods.
Checking extended host info definitions...
	Checked 3 extended host info definitions.
Checking extended service info definitions...
	Checked 0 extended service info definitions.
Checking for circular paths between hosts...
Checking for circular host and service dependencies...
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:   0

Things look okay - No serious problems were detected during the pre-flight check
$ sudo /etc/init.d/nagios2 restart
Restarting nagios2 monitoring daemon: nagios2.
$