Dbus, naughty and logs
From awesome
You can make your computer watch your logs and tell you by a popup notification when there is a new log.
Contents |
[edit] Features of this tutorial
- 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
[edit] Other implementations
- 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 writter for same purpose. This modification uses 'tail -F -n0' instead of 'inotifywait' .
[edit] Requirements
You'll need to install libnotify, inotify-tools, incron and source-highlight.
[edit] verify that naughty has support for dbus
notify-send hello world
If no popup appears, check that you have 'require("naughty")' in you rc.lua.
[edit] make sure you can display you logs
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.
[edit] enable source-highlight for awesome
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).
[edit] Write a script to output notifications
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)
#!/usr/bin/env bash
# Usage: sh popLog.sh /var/log/yourlog
# pops a colored log with the last line of the log
export DISPLAY=":0.0"
export SYNTAXHIGHLIGHTFILE="/home/me/scripts/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 [[ $coloredLog!='' ]]; then
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 [[ $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
;;
esac
notify-send -u $urgency -t $popupTime -i "$icon" "$1" "$coloredLog"
fi
[edit] Monitor your logs with incron
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...).
[edit] Enjoy
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

