Using DZEN with Xmonad to view Currently Active Network Shares

Currently Xmonad is my window manager of choice, because it’s clean, functional, and removes all the unnecessary crap that most modern desktops usually come with by default.

Although Xmonad is very cool, there are still some things that it’s lacking as far as functionality. Much of this is made up for by the use of Xmobar, Trayer, and other Xmonad compatible plugins and applications. I recently came across another one of these applications, and found it to be an exciting find. The tool is called Dzen.

Dzen is a desktop messaging tool which allows you to easily write some useful scripts, and have the output of those scripts become part of your desktop interface. Many examples of how this works are available on the Dzen webite, but some examples are as follows:

  • CPU Monitoring graphs
  • dmesg log monitoring
  • Notification of system events which are commonly found in syslog
  • E-mail or twitter alerts shown on your desktop as they come in
  • Custom calendar alerts
  • and much more..

Now this idea is not new – I remember there being a project called “OSD” (on-screen display) which essentially allows you to do the same thing. However, I think OSD was meant as more of an single message notification system, rather than the way that Dzen works, with master and slave windows, and the ability to implement menus, etc.

In any case, I decided to give Dzen a try, and am happy with the tool that I’ve been able to whip up. For the longest while, I wanted the ability for my xmonad environment to tell me, at a quick glance, what network mounts and removable devices I currently have mounted. I’m sure that this kind of information is easily available on many bloated desktops, including GNOME and KDE, but I was looking for something simple, small and configurable. Didn’t find it, so I ended up writing my own – with the help of Dzen.

Here are a couple of screenshots of how it looks:

Dzen “Active Mounts” widget (mouse out):
dzen-1

 

Dzen “Active Mounts” widget (mouse over):
dzen-2

 

I wrote the scripts fairly quickly, so I’m sure they could be written better, but I think they will provide those of you who are interested, a good example of how to implement a regularly updated notification widget with Dzen.

The scripts are written to check for changes in the mount list, and only update Dzen when a change is detected. It is written in two components:

1) A perl script which captures the mount information in the exact format that I want, and
2) a bash script which handles loading Dzen

Here’s the source code (perl script):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/perl
 
# Written by J. Bobby Lopez <jbl@jbldata.com> - 27 Jan 2010
# Script to -be loaded- by the 'dzen-mounts.bash' script
# This script can also be run by itself, if you want to dump a
# custom plain-text table of your network shares or removable
# devices.
#
# This script is meant to be utilized the Dzen notification system
# Information on Dzen can be found at http://dzen.geekmode.org/
 
use strict;
use warnings;
 
use Data::Dumper;
use Text::Table;
 
my @types = qw( cifs ntfs davfs sshfs smbfs vfat );
 
sub getmounts
{
    my @valid_mounts; # to hold mounts we want
    my @all_mounts = split (/\n/, `mount`);
    foreach my $mount (@all_mounts)
    {
        foreach my $type (@types)
        {
            if ( $mount =~ m/$type/ )
            {
                push (@valid_mounts, $mount);
            }
        }
    }
    return @valid_mounts;
}
 
sub getsizes
{
    my @mounts = getmounts();
    my @list;
    foreach my $mount (@mounts)
    {
        my @cols = split (/\ /, $mount);
        my @df_out = split (/\n/, `df -h $cols[2]`);
        $df_out[1] .= $df_out[2] if defined($df_out[2]);
        $df_out[1] =~ s/[[:space:]]+/\ /;
	    my @df_cols = split (/[[:space:]]+/, $df_out[1]);
        push (@list, ([@df_cols]));
    }
    return @list;
}
 
my $tb = Text::Table->new(
	"Filesystem", "Size", "Used", "Avail", "Use%", "Mounted on"
);
$tb->load(getsizes());
print "Active Mounts\n";
print $tb;

And the bash script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash
 
# Script to load Dzen with output from 'dzen-mounts.pl' script
# Written by J. Bobby Lopez <jbl@jbldata.com> - 27 Jan 2010
#
# This script utilizes the Dzen notification system
# Information on Dzen can be found at http://dzen.geekmode.org/
 
function mountlines
{
        LINES=`perl dzen-mounts.pl|wc -l`;
        echo "$LINES"
}
 
function freshmounts
{
        OUTPUT=`perl dzen-mounts.pl`;
        echo "$OUTPUT"
}
 
function rundzen
{
        OUTPUT=`freshmounts`;
        MOUNTLINES=`mountlines`;
        echo "$OUTPUT" | dzen2 -p -l "$MOUNTLINES" -u -x 500 -y 0 -w 600 -h 12 -tw 120 -ta l &
        PID=`pgrep -f "dzen2 -p -l $MOUNTLINES -u -x 500 -y 0 -w 600 -h 12 -tw 120 -ta l"`;
        echo "$PID"
}
 
function killdzen
{
        PID="$1"
        if [ ! "$PID" ]; then
            MOUNTLINES=`mountlines`;
            PID=`pgrep -f "dzen2 -p -l $MOUNTLINES -u -x 500 -y 0 -w 600 -h 12 -tw 120 -ta l"`;
        fi
 
        if [ "$PID" ]; then
            #echo "Killing $PID..";  # DEBUG STATEMENT
            kill "$PID";
        fi;
}
 
function checkchanges
{
    while true; do
        NEW=`freshmounts`;
        #echo "$NEW - new";  # DEBUG STATEMENT
        if [ "$OLD" != "$NEW" ]; then
            killdzen "$PID";
            rundzen;
            #echo "$PID started";  # DEBUG STATEMENT
            OLD="$NEW";
            #echo "$OLD - old updated"  # DEBUG STATEMENT
        fi
        sleep 1;
    done
}
 
checkchanges

You can also download the scripts in a tgz archive here. Enjoy!

5 thoughts on “Using DZEN with Xmonad to view Currently Active Network Shares”

  1. Hey this is just the sort of thing I was looking for, and it looks like it only went up a couple weeks ago. I am wanting to use dzen to show recent headlines from a website’s feed. This is no problem, but I want it to update every 10 minutes, say. I of course was scheduling a cron job when I realized I don’t think that will work, since I want the output “updated” within the currently running dzen process, not a new one to take over. I suppose my cronjob could kill the running dzen process and then start a new one with new input, but I am going to try your method first, simply changing the sleep to a much higher number. Thanks!

    P.S. Why do so many dzen users also use xmonad, are they related projects somehow? I am finding dzen quite useful before I ever even heard of xmonad…

  2. >>..But I am going to try your method first, simply changing the sleep to a much higher number. Thanks!

    No problem at all. I’ve updated the scripts I’m currently using a bit further to reduce the number of messages being printed in the console, and to be able to run the script from anywhere, not just from within the scripts working directory. If you need a hand with those items, let me know and I’ll post the updates here.

    >>P.S. Why do so many dzen users also use xmonad, are they related projects somehow? I am finding dzen quite useful before I ever even heard of xmonad…

    xmonad is fairly minimalist window manager. It’s very powerful if your the kind of person that hates reaching for the mouse every time. But because of this minimalism, it also lacks some very convenient things like a system tray for gnome applets (like Tomboy or Pidgin), or status/notification bar for the time and date, or to see workspaces (virtual desktops) have applications on them. The programs “trayer” and “xmobar” are used to give xmonad these missing features. DZEN is just another one of those applications that just seems to “fit” with xmonad, because you can use it any way you want. You can have it in your face with lots of menus and options, or keep it out of the way; for example, make it part of “xmobar” the way I did in the screen shots.

    I’m glad you found this useful – and good luck with your headlines applet!

  3. Would you mind mailing the bash script to me (I think you can see my email since I filled it in for these posts). I’m not using perl for my info gathering portion, so don’t need that. Regarding xmonad, your explanation sounds like how I am using dzen with openbox, which seems very minimalist to me, also. I’ll have to check out xmonad sometime.

Leave a Reply

Your email address will not be published. Required fields are marked *