2014-11-21 424 views
4

我有一个简单的问题。它最可能的用户错误,所以我开始之前道歉。如何读取SNMP OID输出

我正在尝试设置设备的阈值,以便在我们的某台打印机处于特定状态时它会提醒我们。 (卡住了,没有墨粉,没有纸张等)我找到了处理这个问题的具体oid。 (1.3.6.1.2.1.25.3.5.1.2.1)在HOST-RESOURCE-MIB下,特定的oid被称为hrPrinterDetectedErrorState。我已验证我可以通过SNMPWALK查看oid。我的问题是解释它吐出的数据。我在MIB中读取的内容以及通过SNMPWALK看到的内容不同。

这里是OID从MIB的描述:

 "This object represents any error conditions detected 
     by the printer. The error conditions are encoded as 
     bits in an octet string, with the following 
     definitions: 

      Condition  Bit # 

      lowPaper    0 

      noPaper    1 
      lowToner    2 
      noToner    3 
      doorOpen    4 
      jammed    5 
      offline    6 
      serviceRequested  7 
      inputTrayMissing  8 
      outputTrayMissing 9 
      markerSupplyMissing 10 
      outputNearFull  11 
      outputFull   12 
      inputTrayEmpty  13 
      overduePreventMaint 14 

     Bits are numbered starting with the most significant 
     bit of the first byte being bit 0, the least 
     significant bit of the first byte being bit 7, the 
     most significant bit of the second byte being bit 8, 
     and so on. A one bit encodes that the condition was 
     detected, while a zero bit encodes that the condition 
     was not detected. 

     This object is useful for alerting an operator to 
     specific warning or error conditions that may occur, 
     especially those requiring human intervention." 

奇数部分是snmpwalk的说,OID是一个十六进制字符串而MIB指定它应该是一个字节串。这两个不同?我是否需要以某种方式转换由SNMPWALK输出的数据以使其与MIB所说的内容相匹配?

为了测试一切,我把打印机放到了几个不同的“状态”中。然后我在设备上运行了一个SNMPWALK来查看oid输出的内容。结果如下。如您所见,这些结果与MIB指定的内容不匹配。

Case 1: Opened the toner door 

Expected Output based on MIB: 4 
SNMP Output: 08 

Case 2: Removed Toner & Closed the door 

Expected Output based on MIB: 1 
SNMP Output: 10 

Case 3: Placed 1 sheet of paper and printed a 2 page document. The printer ran out of paper. 

Expected Output based on MIB: 0 or 1 
SNMP Output: @ 

我对输出感到困惑。我只需要知道如何阅读oid,这样我就可以设置阈值,以便当它看到时,例如,它会执行某个操作。

感谢您的帮助!

回答

3

您正在阅读这个错误。你接收回来的数据实际上应被解释为比特列的每一位针对你的情况特殊报警自身的价值

Expected Output based on MIB: 4 
SNMP Output: 08 

你居然回来输出:

00001000 00000000 

第一这里字节涵盖这些值

lowPaper    0 
noPaper    1 
lowToner    2 
noToner    3 
doorOpen    4 
jammed    5 
offline    6 
serviceRequested  7 

所以lowpaper是位0,nopaper是位1,lowtoner是第2位等,并dooropen是第4位,正如你可以看到该位被设置为1,表明门是开着的。

编辑:

这是非常依赖于设备和实现。要知道如何解析它,需要进行大量的试验和错误(至少从我的经验来看)。作为n例如,如果你回来的消息9104,这可能是要么是

91 and 04 are separate so you first translate 91 to binary from hex and then the 
same thing with 04 
91: 10010001 
04: 00000100 

这意味着较低的纸,noToner,服务要求和inputTrayEmpty。这看起来是最有可能的方式。

如果您只返回一个字节,则表示您应该只查找前8位的报警。作为一个你需要注意的事情的例子:如果现在唯一的警报是doorOpen,你可能只能回到08,但它实际上是0008,其中前两个十六进制字符实际上是警报的第二部分,但不是显示是因为它们不存在。所以在你的情况下,你实际上首先必须切换字节(如果有4个)分别解析前两个和后两个字节,然后获得实际结果。

正如我所说的,从我所看到的这里没有真正的标准,你必须与它一起工作,直到你确信你知道如何发送数据以及如何解析它。

+0

还没有得到它,所以我必须先将08转换为二进制格式,对不对? “08”是十六进制格式,对不对?所以转换的结果是00001000,但第二个字节在哪里?那么我应该使用设置为1的位数?但是,如果我的打印机返回SNMP 9104,该怎么办?它给了我一个二进制10010001 00000100,我该怎么办? – whizzzkey 2014-11-24 22:34:53

+0

eddited我的回答 – Vajura 2014-11-25 06:20:29

+0

我认为在“SNMP Output:08”的情况下,你只接收一个字节,从左到右解释“00001000”。 – js2010 2018-02-22 22:29:17

1

Powershell函数来解释hrPrinterDetectedErrorState八位字符串(是否sharpsnmplib做到这一点?编辑:你需要专业版)。

[flags()] Enum hrPrinterDetectedErrorState 
{ 
    lowPaper   = 0x8000 
    noPaper    = 0x4000 
    lowToner   = 0x2000 
    noToner    = 0x1000 
    doorOpen   = 0x0800 
    jammed    = 0x0400 
    Offline    = 0x0200 
    serviceRequested = 0x0100 

    inputTrayMissing = 0x0080 
    outputTrayMissing = 0x0040 
    markerSupplyMissing = 0x0020 
    outputNearFull  = 0x0010 
    outputFull   = 0x0008 
    inputTrayEmpty  = 0x0004 
    overduePreventMaint = 0x0002 
    notUsed    = 0x0001 
} 

function snmpmessage($data) { 

    $bytes = [byte[]][char[]]$data 

    $code = [int]$bytes[0] 
    $code = $code -shl 8 

    if ($bytes[1]) { $code = $code + $bytes[1] } 

    [hrPrinterDetectedErrorState]$code 

} 

PS C:\> snmpmessage -join [char[]](0x91,0x04) 
inputTrayEmpty, serviceRequested, noToner, lowPaper