Page 3 of 3 <123
Topic Options
#211003 - 2015-12-17 10:29 AM Re: SNMP with Printers [Re: NTDOC]
kelp7 Offline
Starting to like KiXtart

Registered: 2002-08-12
Posts: 124
Loc: UK
Well, okay. But I have to warn you I am no amazing coder. I'm sure there are plenty of better ways to achieve the solution. Also, I'm now being asked whether it can put the printers it's already warned about previously at the bottom of the email away from any new ones. That's gonna be a fun project.

Anyway, here's what I came up with (some personal / site details edited out):

 Code:
$x = Open(1, ".\PrinterOut.txt", 5)
$x = Open(2, ".\PrinterList.dat", 2)
$x = WriteLine(1, "The following printers are getting low on toner, please visit and assist:" + @CRLF + @CRLF)
Dim $StoreVals[6]
$EmailNeeded = 0
$PrinterIP = ReadLine(2)
While @ERROR = 0
  $Cmd = 'snmpget.exe -r:' + $PrinterIP + ' -o:.1.3.6.1.2.1.1.1.0 -q -t:10'
  $CmdResult = WshPipe($Cmd)
  $CmdResult = Join($CmdResult[0], @CRLF)
  $PModel = SubStr($CmdResult, Instr($CmdResult, "X"), (Instr($CmdResult, ";") - Instr($CmdResult, "X")))
  $Count = 0
  $Flag = 0

  ; If the device is online and the device is a Xerox machine
  If Left($CmdResult, 1) <> "%" AND $PModel <> ""
    ; Get the supported values for a printer type
    $aIniVals = Split(ReadProfileString('.\PrinterData.ini', $PModel, ''), Chr(10))
    ; Make sure the device is one of those listed in the INI file otherwise report an error
    If UBound($aIniVals) > 0
      For Each $IniVal in $aIniVals
        If $IniVal
          $Oid = ReadProfileString('.\PrinterData.ini', $PModel, $IniVal)
          $Cmd = 'snmpget.exe -r:' + $PrinterIP + ' -o:' + $Oid + ' -q -t:10'
          $CmdResult = WshPipe($Cmd)
          $CmdResult = Join($CmdResult[0], @CRLF)
          If $IniVal <> "Location"
            $Cmd = 'snmpget.exe -r:' + $PrinterIP + ' -o:' + SUBSTR($Oid, 1, 23) + "8" + SUBSTR($Oid, 25, 4) + ' -q -t:10'
            $CmdResult2 = WshPipe($Cmd)
            $CmdResult2 = Join($CmdResult2[0], @CRLF)
            $CmdResult = CDbl($CmdResult)
            $CmdResult2 = CDbl($CmdResult2)
            $StoreVals[$Count] = ($CmdResult / $CmdResult2) * 100
          Else
            $StoreVals[$Count] = $CmdResult        
          EndIf
        EndIf
      $Count = $Count + 1
      Next
    Else
      $Count = 1
      $StoreVals[$Count] = "WARNING - Printer Model Not Found : Report this to 3rd line"
    EndIf

    $PrinterText = $PrinterIP + " : " + $PModel
    If $Count > 1
      $PrinterText = $PrinterText + " : " + SubStr($StoreVals[0], 1, Len($StoreVals[0]) - 2)
      For $x = 1 to ($Count - 2)
        If $StoreVals[$x] <= 5
          $PrinterText = $PrinterText + " : " + $aIniVals[$x] + " : " + $StoreVals[$x] + "%"
          $Flag = 1
          $EmailNeeded = 1
        EndIf
      Next
    EndIf

    If $Count = 1
      $PrinterText = $PrinterText + $PrinterIP + " : " + $StoreVals[$Count]
      $Flag = 1
      $EmailNeeded = 1
    EndIf

  EndIf

  If $Count = 0
    $PrinterText = $PrinterIP + " : Device is offline (or not a Xerox device, check IP)"
    $Flag = 1
    $EmailNeeded = 1
  EndIf

  If $Flag
    $x = WriteLine(1, $PrinterText + @CRLF)
  EndIf

  $PrinterIP = ReadLine(2)
LOOP

$x = Close(2)
$x = Close(1)

If $EmailNeeded
  Shell "CMD.EXE /C C:\Scripts\blat.exe PrinterOut.txt -to blahblah@@company.com -subject " + Chr(34) + "Printers Low In Toner" + Chr(34) + " -q"
EndIf

Exit


I realise I didn't comment a lot of it so if you can unpack this code, good luck to you.

Here are the file structures that it uses (not all contents of files uploaded, these are just to give the general idea):

1. The INI file (PrinterData.ini)

 Code:
[Xerox WorkCentre 5855 v1]
Location=.1.3.6.1.2.1.1.5.0
Black=.1.3.6.1.2.1.43.11.1.1.9.1.1

[Xerox WorkCentre 5955 v1]
Location=.1.3.6.1.2.1.1.5.0
Black=.1.3.6.1.2.1.43.11.1.1.9.1.1

[Xerox Phaser 6600DN]
Location=.1.3.6.1.2.1.1.6.0
Cyan=.1.3.6.1.2.1.43.11.1.1.9.1.1
Magenta=.1.3.6.1.2.1.43.11.1.1.9.1.2
Yellow=.1.3.6.1.2.1.43.11.1.1.9.1.3
Black=.1.3.6.1.2.1.43.11.1.1.9.1.4

[Xerox WorkCentre 6605DN]
Location=.1.3.6.1.2.1.1.6.0
Cyan=.1.3.6.1.2.1.43.11.1.1.9.1.1
Magenta=.1.3.6.1.2.1.43.11.1.1.9.1.2
Yellow=.1.3.6.1.2.1.43.11.1.1.9.1.3
Black=.1.3.6.1.2.1.43.11.1.1.9.1.4
etc


2. The list of printers (PrinterList.dat)

 Code:
10.140.192.129
10.140.192.134
10.140.192.135
10.140.192.139
10.140.192.143
10.140.192.180
10.140.192.202
etc




Edited by kelp7 (2015-12-17 10:33 AM)

Top
#211004 - 2015-12-17 10:36 AM Re: SNMP with Printers [Re: kelp7]
kelp7 Offline
Starting to like KiXtart

Registered: 2002-08-12
Posts: 124
Loc: UK
Also, if you'd like me to comment it then I can spend some time doing so if necessary...
Top
#211005 - 2015-12-17 11:05 AM Re: SNMP with Printers [Re: kelp7]
kelp7 Offline
Starting to like KiXtart

Registered: 2002-08-12
Posts: 124
Loc: UK
And I've just noticed that I could have moved all those $EmailNeeded = 1 statements to one single statement in the last IF statement of the WHILE loop.
Top
#211008 - 2015-12-17 10:14 PM Re: SNMP with Printers [Re: kelp7]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11625
Loc: CA
No problem. Just that shared code and idea is the main reason for this site. No matter who writes the code there is almost always another way to write it. Commenting code is a good practice to get into though as one can leave it sit for years then when you pick it back up and are rusty or someone else has to take over the code it makes it much easier to see what's going on when it is documented.

You don't have to comment the code here that you posted, but in your production code it would be a good idea.

Thanks for posting and sharing

Top
#211010 - 2015-12-22 10:30 AM Re: SNMP with Printers [Re: NTDOC]
kelp7 Offline
Starting to like KiXtart

Registered: 2002-08-12
Posts: 124
Loc: UK
No problem. I have since developed the script further where, in its report, it will now distinguish between new problem printers and ones which it has previously notified on. The new file (PrinterPrevious.dat) is just a list of IP addresses.

 Code:
Global $PreviousIP[200]
$x = Open(1, ".\1PrinterOut.txt", 5)
$x = Open(2, ".\PrinterList.dat", 2)
$x = Open(3, ".\PrinterPrevious.dat", 2)
$x = Open(4, ".\2PreviousOut.txt", 5)

$PreviousIP[0] = ReadLine(3)
$PreviousTotal = 0
While @ERROR = 0
  $PreviousTotal = $PreviousTotal + 1
  $PreviousIP[$PreviousTotal] = ReadLine(3)
Loop
$x = Close(3)

Del ".\PrinterPrevious.dat"
$x = Open(3, ".\PrinterPrevious.dat", 5)

$x = WriteLine(1, "The following printers are getting low on toner, please visit and assist:" + @CRLF + @CRLF)
$x = WriteLine(4, @CRLF + "Previously notified printers:" + @CRLF + @CRLF)
Dim $StoreVals[6]
$EmailNeeded = 0
$PrinterIP = ReadLine(2)
While @ERROR = 0

  ; Check we haven't reported this printer previously
  $PreviousReport = 0
  For $x = 0 To $PreviousTotal
    If $PreviousIP[$x] = $PrinterIP
      $PreviousReport = 1
    EndIf
  Next

  $Cmd = 'snmpget.exe -r:' + $PrinterIP + ' -o:.1.3.6.1.2.1.1.1.0 -q -t:20'
  $CmdResult = WshPipe($Cmd)
  $CmdResult = Join($CmdResult[0], @CRLF)
  $PModel = SubStr($CmdResult, Instr($CmdResult, "X"), (Instr($CmdResult, ";") - Instr($CmdResult, "X")))
  $Count = 0
  $Flag = 0
  $FlagPrevious = 0

  ; If the device is online and the device is a Xerox machine
  If Left($CmdResult, 1) <> "%" AND $PModel <> ""
    ; Get the supported values for a printer type
    $aIniVals = Split(ReadProfileString('.\PrinterData.ini', $PModel, ''), Chr(10))
    ; Make sure the device is one of those listed in the INI file otherwise report an error
    If UBound($aIniVals) > 0
      For Each $IniVal in $aIniVals
        If $IniVal
          $Oid = ReadProfileString('.\PrinterData.ini', $PModel, $IniVal)
          $Cmd = 'snmpget.exe -r:' + $PrinterIP + ' -o:' + $Oid + ' -q -t:20'
          $CmdResult = WshPipe($Cmd)
          $CmdResult = Join($CmdResult[0], @CRLF)
          If $IniVal <> "Location"
            $Cmd = 'snmpget.exe -r:' + $PrinterIP + ' -o:' + SUBSTR($Oid, 1, 23) + "8" + SUBSTR($Oid, 25, 4) + ' -q -t:20'
            $CmdResult2 = WshPipe($Cmd)
            $CmdResult2 = Join($CmdResult2[0], @CRLF)
            $CmdResult = CDbl($CmdResult)
            $CmdResult2 = CDbl($CmdResult2)
            $StoreVals[$Count] = ($CmdResult / $CmdResult2) * 100
          Else
            $StoreVals[$Count] = $CmdResult        
          EndIf
        EndIf
      $Count = $Count + 1
      Next
    Else
      $Count = 1
      $StoreVals[$Count] = "WARNING - Printer Model Not Found : Report this to 3rd line"
    EndIf

    $PrinterText = $PrinterIP + " : " + $PModel
    If $Count > 1
      $PrinterText = $PrinterText + " : " + SubStr($StoreVals[0], 1, Len($StoreVals[0]) - 2)
      For $x = 1 to ($Count - 2)
        If $StoreVals[$x] <= 5
          $PrinterText = $PrinterText + " : " + $aIniVals[$x] + " : " + $StoreVals[$x] + "%"
          $Flag = 1
          $EmailNeeded = 1
        EndIf
      Next
    EndIf

    If $Count = 1
      $PrinterText = $PrinterText + $PrinterIP + " : " + $StoreVals[$Count]
      $Flag = 1
      $EmailNeeded = 1
    EndIf      
  EndIf

  If $Count = 0
    $PrinterText = $PrinterIP + " : Device is offline (check IP)"
    $Flag = 1
    $EmailNeeded = 1
  EndIf

  If $Flag = 1 And $PreviousReport = 0
    $x = WriteLine(1, $PrinterText + @CRLF)
    $x = WriteLine(3, $PrinterIP + @CRLF)
  EndIf

  If $Flag = 1 And $PreviousReport = 1
    $x = WriteLine(4, $PrinterText + @CRLF)
    $x = WriteLine(3, $PrinterIP + @CRLF)
  EndIf

  $PrinterIP = ReadLine(2)
Loop

$x = Close(3)
$x = Close(4)
$x = Close(2)
$x = Close(1)

If $EmailNeeded
  Shell "CMD.EXE /C COPY /B C:\Scripts\*.txt C:\Scripts\Message.txt > nul"
  Shell "CMD.EXE /C C:\Scripts\blat.exe Message.txt -to blahblah@example.com -subject " + Chr(34) + "Printers Low In Toner" + Chr(34) + " -q"
EndIf
Del ".\Message.txt"
Del ".\1PrinterOut.txt"
Del ".\2PreviousOut.txt"
Exit


Edited by kelp7 (2015-12-22 10:32 AM)

Top
Page 3 of 3 <123


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 201 anonymous users online.
Newest Members
BeeEm, min_seow, Audio, Hoschi, Comet
17882 Registered Users

Generated in 0.147 seconds in which 0.108 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org