我有很多台计算机,它们都带有本地 USB 打印机。我希望能够使用 Nagios 监控诸如卡纸和墨粉量之类的情况,但我所能找到的只有使用 SNMP。
如何使用 Nagios 监控本地 USB 打印机?
答案1
类似这样的 PowerShell 脚本应该可以解决问题。可以通过 Nagios 调用该脚本,它将返回退出代码 0 和“正常:所有打印机均正常”或退出代码 2 和有关有问题的打印机的信息。
#Initialize variables
$nagiosStatus = "0"
$nagiosDescription = ""
$detectedErrorStateDescriptions = New-Object string[] 12
#Assign error state descriptions - see http://msdn.microsoft.com/en-us/library/windows/desktop/aa394363(v=vs.85).aspx
$detectedErrorStateDescriptions[0] = "Unknown"
$detectedErrorStateDescriptions[1] = "Other"
$detectedErrorStateDescriptions[2] = "No Error"
$detectedErrorStateDescriptions[3] = "Low Paper"
$detectedErrorStateDescriptions[4] = "No Paper"
$detectedErrorStateDescriptions[5] = "Low Toner"
$detectedErrorStateDescriptions[6] = "No Toner"
$detectedErrorStateDescriptions[7] = "Door Open"
$detectedErrorStateDescriptions[8] = "Jammed"
$detectedErrorStateDescriptions[9] = "Offline"
$detectedErrorStateDescriptions[10] = "Service Requested"
$detectedErrorStateDescriptions[11] = "Output Bin Full"
#Check the status of each printer on the system
ForEach ( $printer in ( Get-WmiObject win32_printer ) ) {
If ( ( $printer.DetectedErrorState -ne "0" ) -and ( $printer.DetectedErrorState -ne "2" ) ) {
$nagiosStatus = "2"
If ($nagiosDescription -ne "") {
$nagiosDescription = $nagiosDescription + ", "
}
$nagiosDescription = $nagiosDescription + $printer.Name + ":" + $detectedErrorStateDescriptions[$printer.DetectedErrorState]
}
}
#Output the status
If ( $nagiosStatus -eq "2" ) {
Write-Host "CRITICAL: " $nagiosDescription
}
Else {
Write-Host "OK: All printers are normal"
}
exit $nagiosStatus
我目前没有可供测试的 Nagios 安装,但这是从我以前与 Nagios 一起使用的另一个 PowerShell 脚本修改而来的。
看http://msdn.microsoft.com/en-us/library/windows/desktop/aa394363(v=vs.85).aspx有关您可以从 win32_printer WMI 对象获取的可能信息的更多信息。