Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 伺服器綜合

使用 Nagios 透過 SNMP 監控 Windows 伺服器 CPU 利用率

編輯:伺服器綜合

方案選擇

  • **NSClient++** 雖方便但需在被監控端安裝代理,可能影響業務應用。
  • **SNMP 無代理監控** 輕量、標準、不侵入業務系統,推薦採用。

實作步驟

1. 部署 Perl 監控腳本

路徑:/usr/lib/nagios/plugins/check_snmp_win_cpuload.pl

#!/usr/bin/perl
# 語法: check_win_snmp_cpuload.pl HOST COMMUNITY WARN CRIT
# 返回所有 CPU 平均負載百分比

use strict;
use Net::SNMP;

my $host = shift;
my $community = shift;
my $warn = shift;
my $crit = shift;

unless($crit) {
    errorExit("syntax: check_win_snmp_cpu.pl HOST COMMUNITY WARN CRIT");
}

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

my $oidCpuTable = '.1.3.6.1.2.1.25.3.3.1.2';  # HOST-RESOURCES-MIB::hrProcessorLoad

# 建立 SNMP 會話
my ($snmp, $err) = Net::SNMP->session(
    -hostname  => $host,
    -community => $community,
    -port      => 161,
    -version   => 1
);
errorExit($err) unless defined($snmp);

# 取得 CPU 負載表
my $response = $snmp->get_table(-baseoid => $oidCpuTable);
errorExit("error getting cpu table") unless $response;
my %value = %{$response};
$snmp->close();

# 計算平均負載
my $cnt = 0; my $sum = 0;
foreach my $load (values %value) {
    $cnt += 1; $sum += $load;
}
my $pct = int($sum / $cnt);

# 判斷狀態
my $err = ($pct > $crit) ? 'CRITICAL' : ($pct > $warn) ? 'WARNING' : 'OK';
print "$err : CPU Load $pct%\n";
exit $ERRORS{$err};

sub errorExit {
    my $msg = shift;
    print "UNKNOWN: $msg\n";
    exit $ERRORS{UNKNOWN};
}

賦予執行權限:chmod +x /usr/lib/nagios/plugins/check_snmp_win_cpuload.pl

2. 定義 Nagios 命令

檔案:mysnmp.cfg

define command {
    command_name    snmp_win_cpu
    command_line    /usr/lib/nagios/plugins/check_snmp_win_cpuload.pl '$HOSTADDRESS$' public 80 90
}

參數說明:`public` 為 Community String,`80` 警告閾值,`90` 嚴重閾值(依環境調整)。

3. 定義服務

檔案:service.cfg

define service {
    host_name               jw-inside
    service_description     CPU
    check_command           snmp_win_cpu
    use                     generic-service
}

4. 定義主機

檔案:host.cfg

define host {
    host_name           jw-inside
    alias               jw-inside
    address             10.10.10.11
    use                 generic-host
    icon_image          vendors/windowsxp.png
    icon_image_alt      jw-inside-server
    vrml_image          vendors/windowsxp.png
    statusmap_image     vendors/windowsxp.png
}

Windows 端 SNMP 服務啟用提醒

  1. 「控制台」→「新增/移除程式」→「新增/移除 Windows 元件」→「管理與監控工具」→勾選「簡易網路管理協定 (SNMP)」。
  2. 服務中啟動 **SNMP Service**,設定 Community Name(如 `public`)、**接受來自 Nagios 伺服器 IP 的封包**。
  3. 防火牆開放 UDP 161 埠。

優勢總結

  • 零代理部署,業務零干擾。
  • 標準 SNMP 協議,支援所有支援 HOST-RESOURCES-MIB 的裝置(Windows/Linux/網路設備)。
  • 腳本邏輯簡單,易於擴展監控記憶體、磁碟、網路等其他 OID。
Copyright © Windows教程網 All Rights Reserved