Dbus, naughty and logs

From awesome
Jump to: navigation, search

You can make your computer watch your logs and tell you by a popup notification when there is a new log.

Features of this tutorial[edit]

  • Window manager independant (relies on dbus to send the notifications, so any notify-daemon like naughty could do the job)
  • No monitoring is used, the notifications come in real time thanks to the inotify feature of the linux kernel

Other implementations[edit]

  • There is another implementation which doesn't rely on any external tools (only luainotify bindings): Naughty log watcher
  • Approach of this script will lead to inaccurate results if log is updating too frequently. the following script has been written for the same purpose. This modification uses 'tail -F -n0' instead of 'inotifywait' .

Requirements[edit]

You'll need to install libnotify, libnotify-bin, inotify-tools, incron and source-highlight.

verify that naughty has support for dbus[edit]

 notify-send hello world

If no popup appears, check that you have 'require("naughty")' in you rc.lua.

make sure you can display you logs[edit]

 tail -n 10 /var/log/kernel.log

If your system tells you you can't do that because of poor rights, you must add yourself to the log group (gpasswd -a <user> log), then logout/login to enable the modification.

enable source-highlight for awesome[edit]

source-highlight is a tool that does syntax highlighting. The output can be in html, terminal escape color codes, and as it is very extendable, you can provide your own output format. A cool feature of naughty is that it can display colored words, using

<span color="mycolor">word</span>

Here is a file to provide to source-highlight so that it outputs in naughty format. Save it wherever you whish as 'awesome.outlang'.

extension "awesome"

color "<span color=\"$style\">$text</span>"

colormap
"green" "#33CC00"
"red" "#FF0000"
"darkred" "#990000"
"blue" "#0000FF"
"brown" "#9A1900"
"pink" "#CC33CC"
"yellow" "#FFCC00"
"cyan" "#66FFFF"
"purple" "#993399"
"orange" "#FF6600"
"brightorange" "#FF9900"
"brightgreen" "#33FF33"
"darkgreen" "#009900"
"black" "#000000"
"teal" "#008080"
"gray" "#808080"
"darkblue" "#000080"
default "#66FFFF"
end

Note: This file is customisable: if you do not like the default colors, you can obviously modify them very easily. If you whish to perform advanced modifications, you'll find ressources to do it on the source-highlight website).

Write a script to output notifications[edit]

The following script reads the first line of a file, highlights it, and sends it to dbus as a notification. Save it wherever you want (say /home/me/scripts/popLog.sh)

#!/bin/bash

# Usage: ./popLog.sh /var/log/yourlog
# pops a colored log with the last line of the log

export DISPLAY=":0.0"
export SYNTAXHIGHLIGHTFILE="/home/joe/.config/awesome/awesome.outlang" # CHANGE ME

#Urgency
infoUrgency='low'
warningUrgency='normal'
errorUrgency='critical'
securityUrgency='critical'
 
#Popup time
infoPopupTime=5000
warningPopupTime=8000
errorPopupTime=11000
securityPopupTime=11000
 
#Icons
infoIcon='/usr/share/icons/gnome/32x32/status/dialog-information.png'
warningIcon='/usr/share/icons/gnome/32x32/status/dialog-warning.png'
errorIcon='/usr/share/icons/gnome/32x32/status/dialog-error.png'
securityIcon='/usr/share/icons/gnome/32x32/status/security-medium.png'

coloredLog=$(tail -n 1 $1 |                   \
  source-highlight --failsafe                 \
                   --src-lang=log             \
                   --style-file=default.style \
                   --outlang-def=${SYNTAXHIGHLIGHTFILE})

if [ -n "$coloredLog" ] ; then
    #echo $coloredLog
    
    if $(echo $1|grep info) ; then messageType='info'; fi
    if $(echo $1|grep warn) ; then messageType='warning'; fi
    if $(echo $1|grep err) ; then messageType='error'; fi
    if $(echo $1|grep auth) ; then messageType='security'; fi
    if $(echo $1|grep access) ; then messageType='security';fi
    if $(echo $notification|grep 'UFW BLOCK INPUT') ; then
        messageType='security'; fi
    if [ -z "$messageType" ] ; then messageType='info'; fi
       
    case $messageType in
    info)
        urgency=$infoUrgency
        icon=$infoIcon
        popupTime=$infoPopupTime
    ;;
    warning)
        urgency=$warningUrgency
        icon=$warningIcon
        popupTime=$warningPopupTime
    ;;
    error)
        urgency=$errorUrgency
        icon=$errorIcon            
        popupTime=$errorPopupTime
    ;;
    security)
        urgency=$securityUrgency
        icon=$securityIcon        
        popupTime=$securityPopupTime
    ;;
    *)
        urgency=$errorUrgency
        icon=$errorIcon            
        popupTime=$errorPopupTime
    ;;
    esac

    notify-send -u $urgency -t $popupTime -i "$icon" "$1" "$coloredLog" 2> /tmp/notify-send-error
    #Maybe you will get something like „(notify-send:15339): GLib-GObject-CRITICAL **: g_object_unref: assertion `G_IS_OBJECT (object)' failed“ when started from incrond :/ What to do in this case?
fi

If you get the above mentioned error when run thru incrond modify the notify-send command as follows (replace USERID with your username):

    DBUS_SESSION_BUS_ADDRESS=""                                                                                    
    pids=`pgrep -u USERID`
    for pid in $pids; do
                    grep -z DBUS_SESSION_BUS_ADDRESS \
                        /proc/$pid/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//'
                echo
     done|sort -u|while read DBUS_SESSION_BUS_ADDRESS
     do
          export DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS                                                                                                                                                                                                                     
          notify-send -u $urgency -t $popupTime -i "$icon" "$1" "$coloredLog" 2> /tmp/notify-send-error                                                                                                                                                                                 
    done

Monitor your logs with incron[edit]

Edit you incrontab with 'incrontab -e', and the following line to the crontab:

/var/log/kernel.log IN_MODIFY sh /home/me/scripts/popLog.sh /var/log/kernel.log

Add the logs you wish. Beware careful when choosing what you want to monitor. For exemple, doing this on /var/log/everything.log can cause infinite loops (since it is going to log the execution of popLog.sh, which will cause a new notification to happen, which will log in /var/log/everything.log...).

Enjoy[edit]

You should now get nice colorized notifications! For example you can try to trigger the wifi button.

The script is a modification of [1]. Many thanks to Zanko!

Note: One could set width for naughty windows manually. Add those lines to rc.lua (tested on 3.4.2):

naughty_width = 600 -- in pixels
naughty.config.presets.low.width = naughty_width
naughty.config.presets.normal.width = naughty_width
naughty.config.presets.critical.width = naughty_width