Created
March 21, 2015 02:15
-
-
Save f4nd4ngo/94441e2426228e691e3c to your computer and use it in GitHub Desktop.
Canon Printers Counters Report
This file contains 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/env python | |
# -*- coding: UTF-8 -*- | |
#Get SNMP Value for canon printers for total page count using PySNMP, export to pdf, then email to required user# | |
#Need to install pysnmp, reportlab and smtplib using pip or easy install# | |
#Those are the OIDS for counters and serial | |
#TOTAL: .1.3.6.1.4.1.1602.1.11.1.3.1.4.102 | |
#COLOR: .1.3.6.1.4.1.1602.1.11.1.3.1.4.106 | |
#B/W: .1.3.6.1.4.1.1602.1.11.1.3.1.4.109 | |
#SERIAL: .1.3.6.1.2.1.43.5.1.1.17.1 | |
#TOTAL SOME C2020: .1.3.6.1.4.1.1602.1.11.1.3.1.4.101 | |
#COLOR SOME C2020: .1.3.6.1.4.1.1602.1.11.1.3.1.4.108 | |
import datetime | |
import os | |
import sys | |
import subprocess | |
import re | |
import smtplib | |
from pysnmp.entity.rfc3413.oneliner import cmdgen | |
from reportlab.lib.pagesizes import letter | |
from reportlab.lib.units import inch, cm | |
from reportlab.platypus import Paragraph, SimpleDocTemplate, Spacer | |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle | |
from email.mime.application import MIMEApplication | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
#from pysnmp import debug | |
#debug.setLogger(debug.Debug('all')) | |
global hostname, ip, location, c_serial | |
#set PDF report template | |
style = getSampleStyleSheet() | |
style.add(ParagraphStyle(name='Default', fontName ='Helvetica',fontSize=14, leading= 24)) | |
style.add(ParagraphStyle(name='Titler', fontName ='Helvetica',fontSize=20, leading= 30)) | |
pdf = SimpleDocTemplate('Canon_report.pdf', pagesize=letter) | |
report = [] | |
#Specified printers ip address or hostname in this file. | |
printer_list = open('printer_list.csv', 'r') | |
today = str(datetime.datetime.now().date()) | |
cg = cmdgen.CommandGenerator() | |
#Define oid for counters to use snmpget. | |
black = (1, 3, 6, 1, 4, 1, 1602, 1, 11, 1, 3, 1, 4, 109) | |
color = (1, 3, 6, 1, 4, 1, 1602, 1, 11, 1, 3, 1, 4, 106) | |
total = (1, 3, 6, 1, 4, 1, 1602, 1, 11, 1, 3, 1, 4, 102) | |
serial = (1, 3, 6, 1, 2, 1, 43, 5, 1, 1, 17, 1) | |
old_total = (1, 3, 6, 1, 4, 1, 1602, 1, 11, 1, 3, 1, 4, 101) | |
old_black = (1, 3, 6, 1, 4, 1, 1602, 1, 11, 1, 3, 1, 4, 108) | |
#function that verify csv for line beggining with ip or hostname prefix, ping to see if alive if those criteria | |
#are matched pass the printer variable to poll_value function | |
def check_counters(printers): | |
with open(os.devnull, 'wb') as limbo: | |
for printer in printers: | |
elepr = re.match('^printer0.*$', printer, re.IGNORECASE) #Printer hostname prefix if required for CSV | |
tendot = re.match('^10\.0\..*$', printer, re.IGNORECASE) #IP address verification for CSV | |
if elepr or tendot: | |
ip, hostname, location, c_serial = printer.split(';') | |
result=subprocess.Popen(["ping", "-c", "10", "-n", "-W", "1", ip], | |
stdout=limbo, stderr=limbo).wait() | |
if result: | |
offline(hostname, location) | |
else: | |
poll_value(ip, hostname, location, c_serial) | |
else: | |
pass | |
#function that poll printer for snmp value, clean oid value and send to according function by printer model. | |
#C2550 = oldest, C2020 with different oid = older, others = newer_snmp | |
def poll_value(ip, hostname, location, c_serial): | |
comm_data = cmdgen.CommunityData('default', 'public', mpModel=0) | |
transport = cmdgen.UdpTransportTarget((ip, 161)) | |
errIndication, errStatus, errIndex, ser = cg.getCmd(comm_data, transport, serial) | |
errIndication, errStatus, errIndex, bla = cg.getCmd(comm_data, transport, black) | |
errIndication, errStatus, errIndex, col = cg.getCmd(comm_data, transport, color) | |
errIndication, errStatus, errIndex, tot = cg.getCmd(comm_data, transport, total) | |
errIndication, errStatus, errIndex, old_bla = cg.getCmd(comm_data, transport, old_black) | |
errIndication, errStatus, errIndex, old_tot = cg.getCmd(comm_data, transport, old_total) | |
ser_clean = re.search('OctetString\(\'(.+?)\'\)\)\]', str(ser)) | |
bla_clean = re.search('32\((.+?)\)\)\]', str(bla)) | |
col_clean = re.search('32\((.+?)\)\)\]', str(col)) | |
tot_clean = re.search('32\((.+?)\)\)\]', str(tot)) | |
old_bla_clean = re.search('32\((.+?)\)\)\]', str(old_bla)) | |
old_tot_clean = re.search('32\((.+?)\)\)\]', str(old_tot)) | |
host_header(hostname) | |
if ser_clean and bla_clean: | |
newer_snmp(location, bla_clean, col_clean, tot_clean, ser_clean) | |
elif ser_clean and old_bla_clean: | |
if old_bla_clean and old_tot_clean: | |
old_col_clean = int(old_tot_clean.group(1)) - int(old_bla_clean.group(1)) | |
older_snmp(location, old_bla_clean, old_col_clean, old_tot_clean, ser_clean) | |
else: | |
old_col_clean = 'Unavailable' | |
older_snmp(location, old_bla_clean, old_col_clean, old_tot_clean, ser_clean) | |
elif bla_clean and not ser_clean: | |
oldest_snmp(location, bla_clean, col_clean, tot_clean, c_serial) | |
else: | |
no_snmp(hostname, location) | |
# offline printer | |
def offline(hostname, location): | |
report.append(Paragraph('==================', style["Default"])), | |
report.append(Paragraph("%s is not online" % hostname, style["Default"])) | |
report.append(Paragraph("Location :%s" % location, style["Default"])) | |
report.append(Paragraph('Refer to last counter', style["Default"])), | |
report.append(Paragraph('Printer is probably not in service.', style["Default"])), | |
report.append(Paragraph('==================', style["Default"])), | |
# header for each printer in report | |
def host_header(hostname): | |
report.append(Paragraph('==================', style["Default"])), | |
report.append(Paragraph(hostname, style["Default"])), | |
# display if no snmp is available | |
def no_snmp(hostname, location): | |
report.append(Paragraph("Location :%s" % location, style["Default"])) | |
report.append(Paragraph("No counters information available for %s" % hostname, style["Default"])) | |
report.append(Paragraph("Make sure that SNMPv1 is activated with RO community 'public'", style["Default"])) | |
report.append(Paragraph('==================', style["Default"])), | |
# display if Canon model is C2030 or C5325, etc | |
def newer_snmp(location, black_clean, color_clean, total_clean, serial_clean): | |
report.append(Paragraph("Location :%s" % location, style["Default"])) | |
report.append(Paragraph("Serial number : %s" % serial_clean.group(1), style["Default"])) | |
report.append(Paragraph('Black counter : %s' % black_clean.group(1), style["Default"])) | |
report.append(Paragraph('Color counter : %s' % color_clean.group(1), style["Default"])) | |
report.append(Paragraph('Total counter : %s' % total_clean.group(1), style["Default"])) | |
report.append(Paragraph('==================', style["Default"])), | |
# display if Canon Model is 2550 or C2020 ? | |
def older_snmp(location, black_clean, color_clean, total_clean, serial_clean): | |
report.append(Paragraph("Location :%s" % location, style["Default"])) | |
report.append(Paragraph("Serial number : %s" % serial_clean.group(1), style["Default"])) | |
report.append(Paragraph('Black counter : %s' % black_clean.group(1), style["Default"])) | |
report.append(Paragraph('Color counter : %s' % str(color_clean), style["Default"])) | |
report.append(Paragraph('Total counter : %s' % total_clean.group(1), style["Default"])) | |
report.append(Paragraph('==================', style["Default"])), | |
def oldest_snmp(location, black_clean, color_clean, total_clean, c_serial): | |
report.append(Paragraph("Location :%s" % location, style["Default"])) | |
report.append(Paragraph("Serial number : %s" % str(c_serial), style["Default"])) | |
report.append(Paragraph('Black counter : %s' % black_clean.group(1), style["Default"])) | |
report.append(Paragraph('Color counter : %s' % color_clean.group(1), style["Default"])) | |
report.append(Paragraph('Total counter : %s' % total_clean.group(1), style["Default"])) | |
report.append(Paragraph('==================', style["Default"])), | |
# function that writes title | |
def pdf_title(): | |
report.append(Paragraph("Canon Printer Report", style["Titler"])) | |
report.append(Paragraph("Generated %s" % today, style["Titler"])) | |
report.append(Paragraph("For : Bob Thompson", style["Titler"])) | |
report.append(Paragraph('', style["Default"])), | |
report.append(Paragraph('', style["Default"])), | |
#function that sends the report using smtp. | |
def smtp_send(): | |
msg = MIMEMultipart() | |
msg['Subject'] = "Canon Printer Report" | |
msg['From'] = 'CanonReport@doma.in' | |
msg['To'] = 'recipient@doma.in' | |
attachment = open("Canon_report.pdf", 'rb').read() | |
attach_rd = MIMEApplication(attachment, 'pdf') | |
attach_rd.add_header('Content-ID', '<pdf1>') | |
attach_rd.add_header('Content-Disposition', 'attachment', filename="Canon_Report.pdf") | |
attach_rd.add_header('Content-Disposition', 'inline', filename="Canon_Report.pdf") | |
msg.attach(attach_rd) | |
body = MIMEText("""Here is the report generated on %s | |
\n\nIf you need more info please contact printer.maint@doma.in\n\nThank you"""% today) | |
msg.attach(body) | |
mailer = smtplib.SMTP('192.168.1.3', 25) #smtp server address | |
mailer.sendmail(msg['From'], msg['To'], msg.as_string()) | |
mailer.close() | |
pdf_title() | |
check_counters(printer_list) | |
printer_list.close() | |
pdf.build(report) | |
smtp_send() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment