Saturday, November 21, 2009

dzen_notify.sh script

This is a script I created to notify me of things such as attempts to break into my box with ssh, etc. I have unused screen space where there are no toolbars or windows touching, so these are the perfect places to put these notifications. They only notify me once.

This script is mostly dependent on metalog, but, with modification, should be able to be used for other tasks. The only reason that it is mostly dependent is that I executed the script using metalog, and so the arguments to the script got a bit jumbled.

For readability, I don't give it a high score. I could also split the socket checking routine into a function of its own for reuse.

The in-script configuration was done on purpose. A simple script like this, in my opinion, hardly deserves an entire configuration file, but I could see splitting the case statements into a separate rules file.

But I do like the idea of socket files, and these are used to their perhaps fullest extent that I could use them.





#!/bin/bash
#---
# This script allows dzen_notify to notify of certain things. This was
# currently tooled to use metalog and fluxbox, but can be extended to
# other things. Runs from main(). This accepts three arguments, the
# first one is the title which will create a socket in
# /tmp/dzen_notify.$argument.socket, and the last two arguments are
# descriptions.
#
# This notification is supposed to be small and simple. This was tooled
# to the fact that I had spaces that windows don't touch around my
# screen, so I could use small notifications there. This is why dzen2
# slave windows aren't used.
#
# This accepts three parameters: Description 2, Title, and Description
# 1. This is because metalog passes the paramters as date, program, and
# summary.
#---


export DISPLAY=":0"

#---
#dzen_popup()
# Makes the dzen_popup exist. Different socket names can be matched to
# different configurations.
#
#---
function dzen_popup()
{
local title="$1"
local desc="$2 $3"

#Define some default values...
local fg='-fg grey80'
local bg='-bg darkred'
local fn='-fn fixed' #font
local w='-w 150' #width
local x='-x 860' #x pos
local y='-y 945' #y pos
local e='-e entertitle=grabmouse,;leavetitle=ungrabmouse;button1=exit:0' #event
local other='-p'

#...overwritten here!
case "$title" in
sshd)x='-x 245'
y='-y 930'
bg="-bg darkred"
fg="-fg grey80";;
clamd)x='-x 400'
y='-y 930'
bg="-bg yellow"
fg="-fg black";;
*fail2ban*)x='-x 555'
y='-y 930'
bg='-bg orange'
fg='-fg black';;
pure-ftpd)x='-x 710'
y='-y 930';;
*)fg='-fg white'
bg='-bg black';;
esac


local options="$fg $bg $fn $w $x $y $e $other"

echo "$title: $desc" | dzen2 $options

#echo $options
#echo $arguments
}

function main()
{
local socket="/tmp/dzen_notify.$1.socket"
local title="$1"
local desc1="$2"
local desc2="$3"

if [ -f $socket ]; then
return 1
else
touch $socket
dzen_popup "$title" "$desc1" "$desc2"
rm $socket
fi
}



main "$2" "$3" "$1"


Pastebin: http://minozake.pastebin.com/f60670c14

Dependencies: bash, dzen2

Note: I didn't bother cleaning this up while posting, so the comments of explaining my thought are still there.

No comments: