Skip to content

Instantly share code, notes, and snippets.

@henri
Last active March 18, 2019 21:43
Show Gist options
  • Save henri/6e7ada5879a86d28ff8af139e3690ab7 to your computer and use it in GitHub Desktop.
Save henri/6e7ada5879a86d28ff8af139e3690ab7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# Check print queue status if it is stopped restart and send out a notification email.
#
# (C)2017 Henri Shustak
# Licenced Under the GNU GPL v3 or later <https://www.gnu.org/copyleft/gpl.html>
#
# Version 2.3
#
# For email delivery, sendEmail <http://caspian.dotconf.net/menu/Software/SendEmail/> is required.
# sendEmail is expected to be installed at the following path : /usr/local/sbin/sendEmail
# configuration for email notifactions
server_name=`hostname`.chomp
from_email_address="[email protected]"
to_email_address="[email protected]" # for multiple address separated them with a space
smtp_server="smtp.example.com:587"
queues_to_start = []
printer_status = {}
# build hash of print queues and their current status.
#raw_printer_name = `lpstat -a | awk '{print $1}'`.split(/\n+/)
#raw_printer_status = `lpstat -a | awk '{print $2}'`.split(/\n+/)
raw_printer_name = `lpstat -p | grep -v -e '^\t' | awk '{print $2}'`.split(/\n+/)
raw_printer_status = `lpstat -p | grep -v -e '^\t' | sed 's/ is idle. //' | sed 's/ now printing //' | awk '{print $3}'`.split(/\n+/)
raw_status_input_count = 0
raw_printer_name.each do|name|
printer_status.store(name, raw_printer_status[raw_status_input_count])
raw_status_input_count +=1
end
# build an array of print queue names which we will report / restart
printer_status.each do|queue_name, queue_status|
if "#{queue_status}" != "enabled" then
queues_to_start.push("#{queue_name}")
end
end
if ! File.exists? '/usr/local/sbin/sendEmail' then
# sendEmail not installed - lets not send out any emails or start the queues
puts "ERROR! : sendEmail not installed at : /usr/local/sbin/sendEmail"
puts " http://caspian.dotconf.net/menu/Software/SendEmail/"
exit -2
end
if from_email_address == "[email protected]" then
# email sender not updated - lets not send out any emails or start the queues
puts "ERROR! : Configuration Required! Edit script and update email configuration"
exit -1
end
# send an email out and restart the queue
queues_to_start.each do |queue_to_restart|
`cupsenable #{queue_to_restart}`
if $?.exitstatus == 0 then
`/usr/local/sbin/sendEmail -m "#{server_name.upcase} Queue Re-enabled : #{queue_to_restart} \n\n\n" -f #{from_email_address} -t #{to_email_address} -u "#{server_name.downcase} : enabled queue : #{queue_to_restart}" -s #{smtp_server} 2>&1 > /dev/null`
else
`/usr/local/sbin/sendEmail -m "#{server_name.upcase} Unable to Re-enable : #{queue_to_restart} \n\n\n" -f #{from_email_address} -t #{to_email_address} -u "#{server_name.downcase} : ERROR! : unable to enable queue : #{queue_to_restart}" -s #{smtp_server} 2>&1 > /dev/null`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment