As of 2016-02-26, there will be no more posts for this blog. s/blog/pba/
Showing posts with label Conky. Show all posts

I wanted to know how I could monitor file system changes, especially activities on my home directory, and this was about this thread. I was thinking the issue could be solved by generating a snapshot of files using find, then watching changes and making changes to the snapshot file. Using grep to do the search.

But, that’s simply stupid. Just using find would do that OP wants since that program OP uses only search for file names and folder names. For the first run of find it might be slower, 31000 more files took about six seconds. Well it’s not really slow. The consecutive runs only take about 0.1 seconds even after you add or delete files.

Anyway, it’s still fun to know how to monitor files. I installed inotify-tools package for inotifywait program.

http://farm6.static.flickr.com/5087/5205625460_60c806f25a.jpg

Here is the script I use:

inotifywait -m -r --format $'%T %e %w%f' --timefmt '%H:%M:%S' --exclude ~/'(\.mozilla|Documents/KeepNote)' -e modify -e move -e create -e delete ~ 2>&1 | awk '/^[0-9]/ {
sub(/'"${HOME//\//\\/}"'/, "~", $0)
split($0, a, " ")
len=length(a[1])+length(a[2])+1
printf "%-20s %s\n", substr($0, 0, len), substr($0, len+2)
// flush stdout
system("")
next
}
{print ; system("")}
' | tee -a /tmp/home_monitor

-m and -r are for recursively monitoring on files. I set up the output format and timestamp format. I exclude two things from being listed, one is ~/.mozilla and another is KeepNote’s files. You can only have one --exclude supplied, if you have more than one, then only the last one would be effective. You have to group them up. -e specify events you want to monitor.

I use awk to do format adjustment, column alignment. Also replacing the literal of your home directory, /home/username, with just ~. Then I pipe the output to tee, so I can see on screen and write output to file at the same time. This way, Conky can ${tail /tmp/home_monitor 10} the file.

I was planning to use named pipe:

% MPIPE=/tmp/home_monitor
$ mkfifo $MPIPE
$ inotifywait ... | awk ... >$MPIPE
$ tail -f $MPIPE

It will save some disk space, but it doesn’t work with Conky.

If you want to clean up /tmp/home_monitor when it gets too big, just do echo '' > /tmp/home_monitor. An important note for you, when you tail a file in Conky, make sure it exists. If you remove the file, Conky exits. I was hoping there was a Conky variable would run program in a thread and do similar task as $tail does, only the input is the standard output of that thread not a file.

By the way, I had never thought even in my own home directory would have many activities.

I am currently synchronize Portage tree with weekly basis. I used to show the timestamp (/usr/portage/metadata/timestamp.chk) directly in Conky and synchronize when it’s been more than 24 hours. I want to have a easy way to know if I should update. So, I wrote:

${color lightblue}Portage:$color ${execpi 600 thres=$((7*24*60*60));ts="$(cat /usr/portage/metadata/timestamp.chk)";dur=$(($(date +%s) - $(date -d "$ts" +%s)));((dur>=thres)) && echo $(date -d "$ts" +%F\ %T) || echo "\${execbar echo $((100*dur/thres))}"}

The command is:

thres=$((7*24*60*60))
ts="$(cat /usr/portage/metadata/timestamp.chk)"
dur=$(($(date +%s) - $(date -d "$ts" +%s)))
((dur>=thres))
  && echo $(date -d "$ts" +%F\ %T)
  || echo "\${execbar echo $((100*dur/thres))}"

It calculates the seconds since last update. If it’s been seven days, then it shows the time of last update, otherwise a conky execbar for showing how far away from seven days.

I wrote this Bash script, lf-playcount-image.sh, for two things:

  1. Get my playcount of currently playing track with MPD on Last.fm, and
  2. Get the album cover art image URLs on Last.fm, so I can use them to show on Conky.

1   Configuration

1.1   For Last.fm

You will need to create a configuration file in ~/.lf-bash/config with these content:


APIKEY=YOUR_LASTFM_APIKEY
USERNAME=YOUR_LASTFM_USERNAME

For using Track.getinfo API, it’s required to supply API key. Since this is a Bash script, I don’t think it’s wise that I give you my API key even it’s not kind of secret key. You can apply for one. There are two things, I should tell you. First, I don’t have a key, either; Second, in that API page, there is an example link, just below the title. Take a look closer, I am sure you will understand why I want you to look at that link. Yes, I want you to see the response from Last.fm, not the parameters in that link. I swear! [finger crossed behind my back]

1.2   For Conky

The following is my own Conky code:


${if_running mpd}${color lightblue}MPD: ${color #408040}$mpd_status$color$alignr ${if_running mpdscribble}s$endif${if_match "$mpd_repeat" == "On"}r$endif${if_match "$mpd_random" == "On"}z$endif $mpd_vol%
${alignc}${color red}$mpd_title$color
${alignc}${color #9999ff}$mpd_album$color
${alignc}${color green}$mpd_artist$color
${alignc}${color yellow}Played <b>${execi 6 lf-playcount-image.sh}</b><b>${execi 6 cut -f 2 -d \ "/tmp/lf-playcount-image"}</b> times$color
[$mpd_elapsed/$mpd_length] ${color #ccddff}${mpd_bar 3,0}$color<b>${execpi 6 echo "\${image /tmp/lf-images/large.png -p 125,600 -s 200x200 -n -f 6}"}</b>${endif}

It looks like:

http://farm5.static.flickr.com/4134/4935908273_01a7596905_z.jpg

The first $execi is to let the script to update. It will get song info from MPD directly and use it to query on Last.fm. A cache file will be stored at /tmp/lf-playcount-image, it only gets updated when different song is played. In other words, if you play only one song repeatedly, the playcount will not be updated, this is a caveat.

The cache file will read:


a687c5e49a9740c93ae30eb38c8bc729 36 http://userserve-ak.last.fm/serve/64s/50562771.png http://userserve-ak.last.fm/serve/126/50562771.png http://userserve-ak.last.fm/serve/174s/50562771.png http://userserve-ak.last.fm/serve/300x300/50562771.png

First one is a hash for identify if it’s the same song. Second is the playcount, then small image URL, medium image URL, large image URL, and extra large image URL. They are space-separated values.

Once this script retrieves these information from Last.fm, it will also try to download those images to /tmp/lf-images/. That directory might look like:


% ll /tmp/lf-images/
total 700
-rw-r--r-- 1 livibetter livibetter 2374 May 20 2008 http:--cdn.last.fm-flatness-catalogue-album-jewelcase_large.png
-rw-r--r-- 1 livibetter livibetter 2081 May 20 2008 http:--cdn.last.fm-flatness-catalogue-album-jewelcase_medium.png
-rw-r--r-- 1 livibetter livibetter 1444 Jun 18 2009 http:--cdn.last.fm-flatness-catalogue-album-jewelcase_small.png
-rw-r--r-- 1 livibetter livibetter 33671 Aug 29 09:14 http:--userserve-ak.last.fm-serve-126-50562771.png
-rw-r--r-- 1 livibetter livibetter 162117 Aug 28 11:38 http:--userserve-ak.last.fm-serve-174s-50562771.png
-rw-r--r-- 1 livibetter livibetter 471950 Aug 29 09:14 http:--userserve-ak.last.fm-serve-300x300-50562771.png
-rw-r--r-- 1 livibetter livibetter 9718 Aug 21 23:53 http:--userserve-ak.last.fm-serve-64s-50562771.png
lrwxrwxrwx 1 livibetter livibetter 78 Aug 29 09:19 large.png -> /tmp/lf-images/http:--cdn.last.fm-flatness-catalogue-album-jewelcase_large.png
lrwxrwxrwx 1 livibetter livibetter 79 Aug 29 09:19 medium.png -> /tmp/lf-images/http:--cdn.last.fm-flatness-catalogue-album-jewelcase_medium.png
lrwxrwxrwx 1 livibetter livibetter 78 Aug 29 09:19 small.png -> /tmp/lf-images/http:--cdn.last.fm-flatness-catalogue-album-jewelcase_small.png

As you can see, it creates symbolic links, so you can always get the current song’s cover art at /tmp/lf-images/{small,large,medium,extralarge}.png. In the example above, there is no extra large image. The other files are cached, so this script won’t download multiple times for same image.

Second $execi is to get the playcount, it has nothing special if you know shell script.

The $execpi is to create a Conky $image variable, still nothing too special.

2   Notes

I haven’t tested this script seriously, you may see some error which I clearly haven’t met. If so and you want it to get fixed, please provide your Last.fm username, song’s title and album. These information is enough for me to reproduce the issue.

And if you are interested in the song from the screenshot above, its page on Last.fm is here, I also wrote about it few days ago.

3   Updated: Showing Love status

If you also want to show the love status like:

http://farm2.static.flickr.com/1061/5155271854_c5ea850aba.jpg

Be sure to read this follow-up post. <3