+ Post New Thread
Page 1 of 3 123 LastLast
Results 1 to 15 of 39
*nix Thread, grep masters wanted in Technical; I'm trying to create a plugin for nagios to get the consumables status for our Konica K5440dl printers. I tried ...
  1. #1

    plexer's Avatar
    Join Date
    Dec 2005
    Location
    Norfolk
    Posts
    13,341
    Thank Post
    624
    Thanked 1,584 Times in 1,421 Posts
    Rep Power
    414

    grep masters wanted

    I'm trying to create a plugin for nagios to get the consumables status for our Konica K5440dl printers.

    I tried a snmpwalk on one and it only goes so far so I was hoping to try and copy the HP 2600 one which uses wget to download the supplies status webpage to a temporary file and then extracts the details from that.

    Code:
    <html lang="en-US">
    		<head>
    		<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    		<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
    		<meta http-equiv="content-style-type" content="text/css">
    		<style type="text/css">
    		 <!--
    		 a { color: #000000; text-decoration:none}
    		 body  { background-color: #FFFFFF }
    
    		 .menu     { background-color: #6666FF; color: #FFFFFF }
    		 .menu2    { background-color: #9999FF; color: #FFFFFF }
    		 .menu3    { background-color: #CCCCFF; color: #000000 }
    		 .bar      { background-color: #6666FF }
    		 -->
    		 </style>
    		<title>System - Detail - Consumable</title>
    		</head>
    		<body>
    		 <table width="760" cellpadding="0" cellspacing="2">
    		  <tr>
    		   <td class="bar" colspan="3"><img src="space.gif" width="756" height="13" ></td>
    		  </tr>
    		  <tr>
    		   <td align="center" valign="top" width="236">
    			<table width="100" cellpadding="0" cellspacing="0">
    			 <tr><td><img src="space.gif" height="3" ></td></tr>
    			</table>
    			<table width="236" cellspacing="6" cellpadding="4">
    			 <tr>
    			  <td class="menu3">
    			   <img src="head_s2.gif"  width="8" height="9">
    			   <a href="m_s_smm.htm"><strong>Summary</strong></a>
    			  </td>
    			 </tr>
    			 <tr>
    			  <td class="menu">
    			   <img src="head_a1.gif"  width="9" height="8">
    			   <strong>Detail</strong>
    			  </td>
    			 </tr>
        
    			 <tr>
    			  <td class="menu3">
    			   <img src="./space.gif" width="15" height="1" ><img src="head_s2.gif"  width="8" height="9">
    			   <a href="m_s_dtl1.htm"><strong>Input Tray</strong></a>
    			  </td>
    			 </tr>
    
    			 <tr>
    			  <td class="menu3">	
    			   <img src="./space.gif" width="15" height="1" ><img src="head_s2.gif"  width="8" height="9">
    			   <a href="m_s_dtl2.htm"><strong>Network</strong></a>
    			  </td>
    			 </tr>
    			 <tr>
    			  <td class="menu2">
    			   <img src="./space.gif" width="15" height="1" ><img src="head_a2.gif"  width="9" height="10">
    			   <a href="m_s_dtl3.htm"><strong><font color="#FFFFFF">Consumables</font></strong></a>
    			  </td>
    			 </tr>
    			 <tr>
    			  <td class="menu3">
    			   <img src="head_s2.gif"  width="8" height="9">
    			   <a href="m_s_cnt.htm"><strong>Counter</strong></a>
    			  </td>
    			 </tr>
    			 <tr>
    			  <td class="menu3">
    			   <img src="head_s2.gif"  width="8" height="9">
    			   <a href="m_s_onl.htm"><strong>Online Assistance</strong></a>
    			  </td>
    			 </tr>
    			</table>
    <table cellpadding="0" cellspacing="0" class="bar" width="220">
    			 <tr>
    			  <td><img src="space.gif" width="220" height="2" ></td>
    			 </tr>
    			</table>
    			</td>
    			<td class="bar" width="2"><img src="space.gif" width="2" height="1" ></td>
    		   <td align="center" valign="top" width="516" height="5">
    			<table border="0" width="520" cellspacing="0" cellpadding="0">
    			 <tr>
    			  <td width="7"></td>
    			  <td width="510">
    			   <table border="0" cellpadding="5" cellspacing="1" width="510" >
    				<tr>
    				 <td  height="40" width="197" valign="top"><u><b>Consumables</b></u></td>
    				 <td height="40" valign="top">
    				  <p align="center"><u><b>Status</b></u></p></td>
    				 <td height="40" valign="top">
    				 </td>
    				 <td  width="75" height="40" valign="top"><p align="center"><u><b>Max Life</b></u></p></td>
    				</tr>
    <tr>
    				 <td width="197" height="30">Black Toner Cartridge</td>
    				 <td width="150" >
    				  <table border="1" cellpadding="0" cellspacing="0" width="150" >
    					<tr>
    					  <td width="100" height="8">
    						<table border="0" cellpadding="0" cellspacing="0" width="79" height="8">
    						  <tr>
    							<td width="100"  bgcolor="#000000"></td>
    						  </tr>
    						</table>
    					  </td>
    					</tr>
    				  </table>
    				 </td>
    				 <td valign="bottom" align="left" width="33"><font size="2">53%</font></td>
    				 <td width="75" align="center" height="33"> 12000 </td>
    				</tr>
    <tr>
    				 <td width="197" height="30">Cyan Toner Cartridge</td>
    				 <td width="150" >
    				  <table border="1" cellpadding="0" cellspacing="0" width="150" >
    					<tr>
    					  <td width="100" height="8">
    						<table border="0" cellpadding="0" cellspacing="0" width="1" height="8">
    						  <tr>
    							<td width="100" bgcolor="#00FFFFF"></td>
    						  </tr>
    						</table>
    					  </td>
    					</tr>
    				  </table>
    				 </td>
    				 <td valign="bottom" align="left" width="33"><font size="2">1%</font></td>
    				 <td width="75" align="center" >12000</td>
    				</tr>
    <tr>
    				 <td width="197" height="30">Magenta Toner Cartridge</td>
    				 <td width="150">
    				  <table border="1" cellpadding="0" cellspacing="0" width="150">
    
    					<tr>
    					  <td width="100" height="8">
    						<table border="0" cellpadding="0" cellspacing="0" width="150" height="8">
    						  <tr>
    							<td width="100" bgcolor="#FF00FF"></td>
    						  </tr>
    						</table>
    					  </td>
    					</tr>
    				  </table>
    				 </td>
    				 <td valign="bottom" align="left"  width="33"><font size="2">100%</font></td>
    				 <td width="75" align="center" >12000</td>
    				</tr>
    <tr>
    				 <td width="197" height="30">Yellow Toner Cartridge</td>
    				 <td width="150">
    				  <table border="1" cellpadding="0" cellspacing="0" width="150" height="8">
    					<tr>
    					  <td width="100" height="8">
    						<table border="0" cellpadding="0" cellspacing="0" width="150" height="8">
    						  <tr>
    							<td width="100" bgcolor="#FFFF00"></td>
    						  </tr>
    						</table>
    					  </td>
    					</tr>
    				  </table>
    				 </td>
    				 <td valign="bottom" align="left" width="33"><font size="2">100%</font></td>
    				 <td width="75" align="center" >12000</td>
    				</tr>
    <tr>
    				 <td width="197" height="30">Transfer Roller Unit</td>
    				 <td width="150" align="left" >Ready</td>
    				 <td valign="bottom" align="left"  width="33"></td>
    				 <td width="75" align="center" >
    				  -</td>
    				</tr>
    <tr>
    				 <td width="197" height="30">Transfer Belt Unit</td>
    				 <td width="150" align="left">Ready</td>
    				 <td valign="bottom" align="left" width="33"></td>
    				 <td width="75" align="center">-</td>
    				</tr>
    <tr>
    				 <td width="197" height="30">Waste Toner Bottle</td>
    				 <td width="150" align="left" >Ready</td>
    				 <td valign="bottom" align="left" width="33"></td>
    				 <td width="75" align="center" >-</td>
    				</tr>
    </table>
    			   <BR>
    			  </td>
    			 </tr>
    			</table>
    		   </td>
    		  </tr>
    		  <tr>
    		   <td class="bar" colspan="3" width="100" height="2"><img src="space.gif" width="1" height="1" ></td>
    		  </tr>
    		 </table>
    		</body>
    		</html>
    Thats what the downloaded page looks like.

    If anyone could come up with an idea to split that into values for each toner that would be cool.

    Ben

  2. #2

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,512
    Thank Post
    1,320
    Thanked 469 Times in 306 Posts
    Blog Entries
    6
    Rep Power
    199
    wget http://blah.html && cat blah.html | grep % > output.txt

    will put the following into an output.txt file.

    Code:
    <td valign="bottom" align="left" width="33"><font size="2">53</font></td>
    <td valign="bottom" align="left" width="33"><font size="2">1%</font></td>
    <td valign="bottom" align="left"  width="33"><font size="2">100%</font></td>
    <td valign="bottom" align="left" width="33"><font size="2">100%</font></td>
    Not sure how to strip the html out though, will have a play

  3. #3

    plexer's Avatar
    Join Date
    Dec 2005
    Location
    Norfolk
    Posts
    13,341
    Thank Post
    624
    Thanked 1,584 Times in 1,421 Posts
    Rep Power
    414
    I then need to do a split on those lines then I suppose?

    Ben

  4. #4


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,032 Times in 812 Posts
    Rep Power
    339
    pipe the output to awk, then $1, $2, $3 etc to get each column
    Code:
    awk '{ print $1, $2 }'
    so
    Code:
    wget http://blah.html && cat blah.html | grep % | awk '{ print $1, $2 }'

  5. #5

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,512
    Thank Post
    1,320
    Thanked 469 Times in 306 Posts
    Blog Entries
    6
    Rep Power
    199
    Quote Originally Posted by CyberNerd View Post
    pipe the output to awk, then $1, $2, $3 etc to get each column
    Code:
    awk '{ print $1, $2 }'
    so
    Code:
    wget http://blah.html && cat blah.html | grep % | awk '{ print $1, $2 }'
    that just displays

    Code:
    <td valign="bottom"
    <td valign="bottom"
    <td valign="bottom"
    <td valign="bottom"
    changing it to just $5 gives

    Code:
    size="2">53%</font></td>
    size="2">1%</font></td>
    size="2">100%</font></td>
    size="2">100%</font></td>
    which is nearly there

  6. #6

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,512
    Thank Post
    1,320
    Thanked 469 Times in 306 Posts
    Blog Entries
    6
    Rep Power
    199
    Code:
    wget http://temp.html && cat temp.html | grep % | awk '{ print $5 }' > test.txt && sed -e 's/size="2">/ /' test.txt > test2.txt
    strips out the first bit of the html, but I cant get sed to remove the latter part. Not sure if its to do with the \ in the html tag

    Code:
     53%</font></td>
     1%</font></td>
     100%</font></td>
     100%</font></td>

  7. #7


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,032 Times in 812 Posts
    Rep Power
    339
    Quote Originally Posted by RabbieBurns View Post
    that just displays

    Code:
    <td valign="bottom"
    <td valign="bottom"
    <td valign="bottom"
    <td valign="bottom"
    Sure, I was just giving an example


    how about replacing the < with whitespace, then running awk again
    Code:
    sed 's/</ /g' test.txt | awk '{print $1}'
    complete bodge, wish I knew perl....

  8. #8

    plexer's Avatar
    Join Date
    Dec 2005
    Location
    Norfolk
    Posts
    13,341
    Thank Post
    624
    Thanked 1,584 Times in 1,421 Posts
    Rep Power
    414
    Thanks for all the suggestions guys looking good.

    I just want to end up with some variables that contain the cartridge colour and the corresponding % or just % as long as I know variable1 is black, 2 is cyan I suppose.

    Ben

  9. #9


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    4,462
    Thank Post
    866
    Thanked 845 Times in 667 Posts
    Rep Power
    195
    I would suggest learning a bit of perl.. much easier.. then use a few match lines.. but...

    try:
    wget <whatever> | grep % | cut -d\> -f3 | cut -d\< -f1

    If you really want it doing nicely I will perl it for you and post here as an example?

  10. #10

    plexer's Avatar
    Join Date
    Dec 2005
    Location
    Norfolk
    Posts
    13,341
    Thank Post
    624
    Thanked 1,584 Times in 1,421 Posts
    Rep Power
    414
    Yes please Tom then once I can get the check working I'll add that.

    Ben

  11. #11

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,512
    Thank Post
    1,320
    Thanked 469 Times in 306 Posts
    Blog Entries
    6
    Rep Power
    199
    Cracked it.

    Probably not the cleverest or neatest way, but it works.

    Code:
    wget http://temp.html && cat temp.html | grep % | awk -F\> '{print $3}' | awk -F\< '{print $1}'
    add > filename.txt to output to a file.

    Will list 1 per line, black, cyan, magenta, yellow.

    Code:
    Saving to: `temp.html'
    
    100%[============================================>] 6,315   in 0.04s   
    
    2008-10-16 18:26:37 (167 KB/s) - `temp.html' saved [6315/6315]
    
    53%
    1%
    100%
    100%

  12. #12

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,512
    Thank Post
    1,320
    Thanked 469 Times in 306 Posts
    Blog Entries
    6
    Rep Power
    199
    Quote Originally Posted by tom_newton View Post
    try:
    wget <whatever> | grep % | cut -d\> -f3 | cut -d\< -f1
    works when you change it to

    wget <whatever> && cat <whatever> | grep % | cut -d\> -f3 | cut -d\< -f1

  13. Thanks to RabbieBurns from:

    plexer (17th October 2008)

  14. #13


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    4,462
    Thank Post
    866
    Thanked 845 Times in 667 Posts
    Rep Power
    195
    Quote Originally Posted by RabbieBurns View Post
    works when you change it to

    wget <whatever> && cat <whatever> | grep % | cut -d\> -f3 | cut -d\< -f1
    Actually whatya need is wget <whatever> -O - | grep %.....

    no need for temp files
    I was assuming plexer had the "file grabby bit" down already.

  15. Thanks to tom_newton from:

    plexer (17th October 2008)

  16. #14


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    4,462
    Thank Post
    866
    Thanked 845 Times in 667 Posts
    Rep Power
    195
    tom@white-elephant:/tmp$ cat plex.txt | ./plex.pl
    Black: 53
    Cyan: 1
    Magenta: 100
    Yellow: 100

    where "plex.txt" is your HTML.

    Could be done on the commandline but this is neater IMO.

    Next step: remove wget, and go with LWP::Simple
    Attached Files Attached Files

  17. Thanks to tom_newton from:

    plexer (17th October 2008)

  18. #15

    plexer's Avatar
    Join Date
    Dec 2005
    Location
    Norfolk
    Posts
    13,341
    Thank Post
    624
    Thanked 1,584 Times in 1,421 Posts
    Rep Power
    414
    Thanks Tom.

    I can't seem to open that it just comes out gobbledegook when I un compress it.

    Ben

SHARE:
+ Post New Thread
Page 1 of 3 123 LastLast

Similar Threads

  1. WANTED: Tablet PC
    By binky in forum General Chat
    Replies: 3
    Last Post: 26th October 2007, 01:45 PM
  2. audio wanted..
    By russdev in forum General EduGeek News/Announcements
    Replies: 0
    Last Post: 4th December 2006, 09:15 AM
  3. Grep syntax for multiple srtings?
    By ChrisH in forum *nix
    Replies: 17
    Last Post: 14th October 2006, 08:20 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •