Page 1 of 3 123>
Topic Options
#210954 - 2015-12-07 12:29 PM SNMP with Printers
kelp7 Offline
Starting to like KiXtart

Registered: 2002-08-12
Posts: 124
Loc: UK
Hi,

Just a quick query (with probably not a quick answer, I'm sure) but can you issue SNMP 'get' queries from Kixtart? Are there any Kix scripts already written to retrieve some information from networked printers via SNMP (ideally I'd like to get toner level and printer model).

Thanks!

Top
#210955 - 2015-12-07 12:49 PM Re: SNMP with Printers [Re: kelp7]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
If you can use a command-line utility, then WshPipe() can be used to execute the command and return the results in a string. See www.net-snmp.org/ for a free, command-line tool for Windows. (There are others, this was the top/free tool returned by Google.)

If you use the version of WshPipe() from my web site, it returns an array of arrays - the inner array is an array of lines of STDOUT, and the outer array is an array of the lines of STDERR. The version published here on KORG returns a single string of all the lines of STDOUT and STDERR, which I find a bit harder to parse.

Once you have the result from either version of WshPipe(), it's a fairly straightforward task to enumerate the list of output and find/interpret the results.

Post what you build - we can help tweak it and I for one would be interested in seeing something like this!

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#210956 - 2015-12-07 01:50 PM Re: SNMP with Printers [Re: Glenn Barnas]
kelp7 Offline
Starting to like KiXtart

Registered: 2002-08-12
Posts: 124
Loc: UK
Thanks Glenn, as ever, always appreciate your quick answers.

Firstly, I've got to get to grips with SNMP really :-) Just now trying to find some OIDs or MIB files for the printer types (Xerox) that we are using...

Top
#210957 - 2015-12-07 04:25 PM Re: SNMP with Printers [Re: kelp7]
kelp7 Offline
Starting to like KiXtart

Registered: 2002-08-12
Posts: 124
Loc: UK
Incidentally, just found this nice utility if you don't know what SNMP queries your device is capable of:

http://www.snmpsoft.com/freetools/snmpwalk.html

Top
#210958 - 2015-12-07 07:22 PM Re: SNMP with Printers [Re: kelp7]
kelp7 Offline
Starting to like KiXtart

Registered: 2002-08-12
Posts: 124
Loc: UK
Spent a little while this afternoon looking up OIDs via the SNMPWalk program and it does look like this will be possible. Annoyingly the OIDs for toner and make/model aren't always the same value between different models of the same manufacturer (Xerox in my case). Nothing that can't be dealt with in the script though.

The way I see it the script will need to:

Query current printer for Make\Model
Depending on Make\Model choose the correct list of OIDs
Query the printer again for the current OIDs for toner
Make a note if they're below a threshold
Cycle through all printers with the above script
Finish (in my case) by sending an email to the service desk logging system with the list of toners below the threshold.

It would be nice to also query the location of the printer (we configure the location on all our printers) so hopefully I can get that info as well from SNMP.

Not sure how I'll get a list from the print server of all the printers' IPs and would rather avoid having to store a static list of IP addresses in an array in the script, something else to look into I guess.

Top
#210959 - 2015-12-07 08:54 PM Re: SNMP with Printers [Re: kelp7]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Sounds like you need a lookup table! \:\)

An INI file could map a printer brand/model to specific OIDs for generic information like TonerLevel, Location, etc..

[Xerox 272]
Toner=OID...
Location=OID...

Doing so would allow you to get the printer model, then lookup the OIDs for the values. The code then becomes generic and only the config file needs to be customized for different printer models.

As for a list of printers, don't you publish your printers in AD? You can do an LDAP query if you do.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#210960 - 2015-12-08 02:32 PM Re: SNMP with Printers [Re: Glenn Barnas]
kelp7 Offline
Starting to like KiXtart

Registered: 2002-08-12
Posts: 124
Loc: UK
Thanks, yes a lookup table would certainly be first on my list!
We don't publish our printers in AD, we used to but then users were ending up with 20 / 30 printers they'd installed themselves on their profiles and used once. Annoying for our engineers when rebuilding a profile or a whole machine. We push them out with GPO to those who need it. It's possible we might start publishing them again though (something I recently considered proposing myself) as in the long run it does seem easier for users. (We have 170+ printers in our organisation)

Wow, it's taken me a while to go through the SNMP OIDs on each of the 7 different models of printer we have. For a while I couldn't suss out getting the toner levels, no integers in the SNMPWalk results seemed to match up or if they did then they'd give the same result each time for different printers. Clearly they weren't right. I then found that there's a standard OID for toner levels (and Xerox follows the standard luckily) and really weirdly you don't just get a single integer for the toner level, you actually have to look for the maximum value and the current value. Then you get your percentage of toner left by doing the usual divide current by max and * 100

Will be fun to get this working!

Top
#210961 - 2015-12-08 04:50 PM Re: SNMP with Printers [Re: kelp7]
kelp7 Offline
Starting to like KiXtart

Registered: 2002-08-12
Posts: 124
Loc: UK
Got an .ini file ready now \:\)

It's structured like this (this is not the complete file, no point posting that up here):

 Code:
[Xerox 5955]
Location=.1.3.6.1.2.1.1.5.0
Black=.1.3.6.1.2.1.43.11.1.1.9.1.1
ENDPRINTER

[Xerox 6600]
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
ENDPRINTER


and have also carried out a very quick test of WshPipe(), just a one liner that collects the model name using an SNMPGet program and it successfully printed the model name on the screen so all looking good so far. Now onto the meat of the exercise !!


Edited by kelp7 (2015-12-08 04:51 PM)

Top
#210962 - 2015-12-08 05:05 PM Re: SNMP with Printers [Re: kelp7]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
What is ENDPRINTER?

If its a comment it should start with # or ;.

Top
#210963 - 2015-12-08 06:11 PM Re: SNMP with Printers [Re: Allen]
kelp7 Offline
Starting to like KiXtart

Registered: 2002-08-12
Posts: 124
Loc: UK
No, I don't need comments in my .ini file. Everything there has a purpose. ENDPRINTER will just be checked in my script to determine how many criteria each printer has.
Top
#210964 - 2015-12-08 08:36 PM Re: SNMP with Printers [Re: kelp7]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
Not sure if you are aware of the inbuilt INI support... but here's an example...
 Code:
$Values=split(Readprofilestring($ini,"[Xerox 5955]",""),chr(10))
? "Items:  " + (ubound($values)+1)
for each $Value in $Values
  if $value
    ? readprofilestring($ini,"[Xerox 5955]",$value)
  endif
next


You can get all the sections similarly by readprofilestring($ini,"","")

Also be sure to look for Glenn's INI functions on his website as they turn the INIs into arrays and work great.


Edited by Allen (2015-12-08 08:41 PM)

Top
#210966 - 2015-12-09 03:34 AM Re: SNMP with Printers [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Kelp,

So - as Allen pointed out, the built-in ReadProfileString has some pretty useful capabilities.
 Code:
$List = ReadProfileString('inifile.ini', 'Xerox 6600')
will load all of the values into a list, delimited with @CRLF. Performing a
 Code:
$aList = Split($List, @CRLF)
will convert this to an array. The UDF EnumIni() will do this for you, btw.

So - to find out how many parameters you have:
 Code:
$Params = UBound($aList)
You can also enumerate this with "for each $V in $Array". Note that this returns the VALUES that exist in the section, and enumerating them will return "Black", "Cyan", "Magenta", and "Yellow". Here's the bigger picture:
 Code:
$PModel = 'Xerox 6600'
; Get the supported toner colors
$aColors = Split(ReadProfileString('PrinterData.ini', $PModel), @CRLF)
For Each $Color in $aColors
  ; message for debugging
  'Processing toner color: ' $Color @CRLF
  $Oid = ReadProfileString('PrinterData.ini', $PModel, $Color)
  $Cmd = 'snmpget.exe ' + $PrinterIP + ' ' + $Oid
  $CmdResult = WshPipe($Cmd)
  ; do the magic with the returned data here..
  ;
  ; then process the next toner color (if any)
Next
Look at EnumIni() as well to simplify repeated execution of INI queries. The other function that Allen referenced is IniArray, which loads an entire INI file into an array and allows manipulation with functions similar to Read/WriteProfileString. If you find yourself doing lots of INI reads, this can improve performance, but I'd start with the basic Read and Write ProfileString commands.

Glenn

Hey Allen - when the hell did "built-in" become "inbuilt"? \:D
_________________________
Actually I am a Rocket Scientist! \:D

Top
#210967 - 2015-12-09 04:03 AM Re: SNMP with Printers [Re: Glenn Barnas]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
Basta! ;\)
Top
#210968 - 2015-12-10 03:26 AM Re: SNMP with Printers [Re: Allen]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
LOL... scripting for over a decade causes spots of memory starvation so be kind Glenn :-)
Top
#210969 - 2015-12-11 01:23 PM Re: SNMP with Printers [Re: NTDOC]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Woah, Ron! I'm just being protective!! Allen was right on the edge of that slippery slope of saying "I must revert back to him"! \:D (I like me just the way I am and have no plans to revert to anyone else!)

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#210970 - 2015-12-14 09:47 AM Re: SNMP with Printers [Re: Glenn Barnas]
kelp7 Offline
Starting to like KiXtart

Registered: 2002-08-12
Posts: 124
Loc: UK
Hi guys, sorry I haven't done any more on this yet. Thanks very much for all the advice though, and I didn't realise INI files had already been catered for \:\) Apologies for the delay though, struck down by an extremely hostile throat infection last week (nearly rid of it now) which took me off work Weds - Fri and also ruined my weekend. Nice.
Top
#210971 - 2015-12-14 11:27 AM Re: SNMP with Printers [Re: kelp7]
kelp7 Offline
Starting to like KiXtart

Registered: 2002-08-12
Posts: 124
Loc: UK
Interestingly when I execute:

 Code:
$aColors = Split(ReadProfileString('PrinterData.ini', $PModel), @CRLF)


I get a message from Kix saying "ERROR : invalid method/function call: missing required parameter 3!"

It only seems to allow the line to execute if it's specified like this:

 Code:
$aColors = Split(ReadProfileString('PrinterData.ini', $PModel, ''), @CRLF)


but then the array is empty!

I'm guessing I've missed something else somewhere so am taking a look through the code now (although I've now cut the script right down to simply printing the contents of the array on the screen, unfortunately empty at the moment). I've tried specifying the complete path to the .ini file as well, just in case that was it. But still the same.

Top
#210972 - 2015-12-14 11:41 AM Re: SNMP with Printers [Re: kelp7]
kelp7 Offline
Starting to like KiXtart

Registered: 2002-08-12
Posts: 124
Loc: UK
Also just tried a simple:

 Code:
$aColors = ReadProfileString('PrinterData.ini', $PModel, '')


and printing the variable on the screen but it is also empty. (And also tried substituting $PModel for the actual model number).


Edited by kelp7 (2015-12-14 11:42 AM)

Top
#210973 - 2015-12-14 12:20 PM Re: SNMP with Printers [Re: kelp7]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
 Originally Posted By: kelp7
Interestingly when I execute:

 Code:
$aColors = Split(ReadProfileString('PrinterData.ini', $PModel), @CRLF)


I get a message from Kix saying "ERROR : invalid method/function call: missing required parameter 3!"

....


ReadProfileString requires three parameters. First the file, second the section and third the value. So something like $aColors = Split(ReadProfileString('PrinterData.ini', $PModel, value), @CRLF) should do the trick. Off course the name of the value needs to be modified to fit your ini file.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#210974 - 2015-12-14 12:29 PM Re: SNMP with Printers [Re: Mart]
kelp7 Offline
Starting to like KiXtart

Registered: 2002-08-12
Posts: 124
Loc: UK
OK But in the manual it suggests that if the parameter is empty then it'll return all the keys. Anyway, I've worked it out now, the script simply wasn't finding the file! I'm working on a new 2012 server and had forgotten to show all file extensions. So only now do I find out that my .ini file is actually called PrinterData.ini.txt (simple mistake but what a doofus). Right, on with the scripting!!

Edited by kelp7 (2015-12-14 12:30 PM)

Top
Page 1 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 248 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.073 seconds in which 0.022 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