4

Here's a more general use question.

How can I monitor a log file for specific changes, and use a "notify-send" alert in Ubuntu to alert when said changes occur?

ORIGINAL QUESTION:

I'm attempting to set up print quotas for printers here at the office. However, it appears when the quota is reached, the print just fails silently, and the user doesn't have any idea what's happening.

Page quota's set up by altering the PageLimit directive in /etc/cups/printers.conf

It appears I'm getting the message

E [04/Mar/2013:15:34:28 -0700] Returning IPP client-error-not-possible for Create-Job (ipp://localhost:631/printers/Hewlett-Packard-HP-LaserJet-4100-MFP) from localhost

in my log at /var/log/cups/error_log .

I tried hacking something together via a command like

`tail -f /var/log/cups/error_log | grep 'client-error-not-possible for Create-Job' DISPLAY=:0 notify-send -t 30000 -i 'notification-message-email' 'Printing Quota reached for this Printer'`

But that doesn't seem to really work as it only displays the message once on the first error message.

Any thoughts or solutions? It really seems like there should be a prebaked solution for this.

SOLUTION

incrontab worked perfectly.

sudo apt-get install incrontab

Add user root to /etc/incron.allow

sudo nano /etc/incron.allow

Create script monitorCUPSlog.sh

#!/bin/bash
tail -n 1 /var/log/cups/error_log | grep 'client-error-not-possible' | DISPLAY=:0 notify-send -t 30000 -i 'notification-message-email' 'Daily Print Quota exceeded for this printer'

And finally add the event to the incrontab table

sudo incrontab -e
/var/log/cups/error_log IN_MODIFY /usr/local/bin/monitorCUPSlog.sh

Seems to work, yay.

0

2 Answers 2

5

I would look into using incrontab for monitoring changes to the file system, and combine that with the little script you already have.

This looks approximately as follows.

First, save the script that you want to execute as a file, e.g. as cups_monitor.sh in /usr/local/bin (don't forget to make it executable).

#!/bin/sh
VAR=`tail -n 1 /var/log/cups/error_log | grep 'client-error-not-possible for Create-Job'`
if [[ -n "$VAR" ]]; then
   echo "$VAR" | DISPLAY=:0 notify-send -t 30000 -i
fi

You might want to check this. My scripts are never right first try :). Also note that it is possible that this script does not capture your error message if it is followed by other message: I only look at the last line of the log file (-n 1); this should be easy to change.

Then edit your incrontab

incrontab -e

by adding the line

/var/log/cups/error_log IN_CLOSE_WRITE /usr/local/bin/cups_monitor.sh

The IN_CLOSE_WRITE is called an 'event symbol' and indicates that you want to want to call your script when/var/log/cups/error_log was closed after it was opened for writing; you can find other events in the man page of incrontab.

Keep in mind that I did not test this. You can see whether the incrontab file was changed successfully and whether it calls your script or not by looking in the syslog (tail /var/log/syslog).

(My first answer on Stack Exchange ever! Yippee!)

0

You can use incron :: inotify to attach customized triggers on files that do things like make log file entries or send emails upon change events.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .