-
-
Save luisfarfan/e93bf13b2ebc76039e1880cd9aab9775 to your computer and use it in GitHub Desktop.
Quick little script for Linux admins. Run periodically and setup to email someone with a description of printers that may happen to go down. Currently only written to run lpstat on localhost, so must be printers avilable to the localhost.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/python | |
| """ | |
| Description: Will run an lpstat -p and send an email to jtrutwin notifying him of a disabled printer, if there is one, so that he can take action. | |
| TODO: | |
| 1) Add parameter to override the recipient of the email | |
| 2) Add functionality for only sending email when a printer hasn't been found to be disabled today - ie. email sent at 5am shouldn't be sent again if only that same printer is disabled as of 8am, should only send another email if another printer has been disabled or if the original printer has been disabled, enabled and re-disabled. | |
| """ | |
| import commands | |
| import smtplib | |
| from email.mime.text import MIMEText | |
| import json | |
| from optparse import OptionParser | |
| def status_text( output ): | |
| print "lpstat_email: " + output | |
| # various pieces for adding override-email-to option | |
| usage = "usage: %prog [options]" | |
| parser = OptionParser( usage ) | |
| parser.add_option( "-e", "--email", action="store", type="string", | |
| dest="emailto", default="[email protected]", help="Specify an email to override [default]" ) #If being used may need to change email | |
| (options, args) = parser.parse_args( ) | |
| # Open a file to use python's peristent dictionary | |
| try: | |
| previously_disabled = json.load( open( 'disabled_list.json', 'rb' ) ) | |
| status_text( "Found list of previously disabled printers, don't send email about their status" ) | |
| except IOError: | |
| previously_disabled = None | |
| status_text( "No history file found of disabled printers, continuing with no previous reference" ) | |
| pass | |
| # The string used to find disabled pritners based on the stdout of lpstat | |
| string_check = 'disabled' | |
| stdout = commands.getoutput( 'lpstat -p' ) | |
| lpout = stdout.split( '\n' ) | |
| TO = options.emailto | |
| disabled = [] | |
| to_file = [] | |
| # Cycle through the output of lpstat which has been split into tokens based on each line of the output and save the lines that have a substring match with string_check | |
| for line in lpout: | |
| if string_check in line: | |
| status_text( "[disabled] " + line ) | |
| disabled.append( line ) | |
| to_file.append( line.split( " " )[1] ) | |
| # add disabled to the file list | |
| json.dump( to_file , open( 'disabled_list.json', 'wb' ) ) | |
| # Cycle through the list and only leave newly disabled printes in the array for this instance of the script | |
| # this should stop an email from being sent if the user has already been notified of one specific printer's | |
| prev_copy = previously_disabled[:] | |
| for line in disabled_copy: | |
| for prev in prev_copy: | |
| prev_line_test = str( prev ) | |
| if line.split( " " )[1] == prev: | |
| previously_disabled.remove(prev) | |
| disabled.remove( line ) | |
| # Initiate the required variables for constructing a basic email | |
| # keep in mind the TO variable is set using the parse_options above | |
| raw_message = "" | |
| SERVER = "" | |
| FROM = "" | |
| SUBJECT = "" | |
| TEXT = "" | |
| message = "" | |
| # Just some string manipulation - tries to remove useless, redundant information from the lpstat output | |
| for line in disabled: | |
| printer_tmp = line.split( " " ) | |
| line = line.replace( "printer ", "" ) | |
| line = line.replace( " is ", "" ) | |
| line = line.replace( "idle.", "\t" ) | |
| line = line.replace( string_check, "\t" ) | |
| printer = "" | |
| for l in printer_tmp[1]: | |
| printer = printer + l | |
| command = "lpq -P "+ printer | |
| lpqoutmsg = commands.getoutput( command ) | |
| lptstdout = commands.getoutput( "lpstat -t " + printer ) | |
| lpt_test = lptstdout.split( "\n" ) | |
| lptout = "" | |
| for tmp_line in lpt_test: | |
| if printer in tmp_line: | |
| lptout = lptout +"\t\t"+ tmp_line + "\n" | |
| raw_message += line +" "+"\t"+ lpqoutmsg + "\n" + lptout | |
| # Setup basic email needs | |
| SERVER = "localhost" #If being used may need to change | |
| FROM = "user@localhost" #If being used may need to change | |
| SUBJECT = "Printer unnexpectedly disabled" | |
| TEXT = raw_message | |
| # Email template | |
| message = """\ | |
| From: %s | |
| To: %s | |
| Subject: %s | |
| Elm Printers that seem to be disabled: | |
| %s | |
| """ % ( FROM, TO, SUBJECT, TEXT ) | |
| # if there ended up being some stuff in the disabled array still at this point then send the email | |
| if len( disabled ) > 0: | |
| status_text( "Sending email to " + TO ) | |
| server = smtplib.SMTP( SERVER ) | |
| server.sendmail( FROM, TO, message ) | |
| server.quit( ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment