Bash system monitor

So far as a system monitor I used a heavily edited pyampc. I thought it would be cool to have a system monitor in bash, and separate the mpd stuff. That way I can redo the mpd client in a non idiosyncratic way (have not begun on that), but also because it’s more stable and looks a lot better.

The system monitor is about done now. I was able to keep it fairly simple.

ncurses was necessary, because otherwise you get a tickertype system which looks bad and clear and redrawing is way too slow. So I used a hacked `watch` (procps) that supports unicode.

If you don’t use unicode, then skip this. What I added to watch was: curs_set(0), and the main loop is this, instead of the char by char approach it was before
for (y = show_title; y < height; y++) {
size_t len = 0;
char *line = NULL;

Making it support escape codes is a lot harder, I added color codes, but I end up didn’t using it because it confuses `pr` used below. If you want it let me know.

Then the actual information script.

I wanted two columns to save space. And the following information:
left: worded diff pstree to previous
right: date/freespace/loadavg/ps mem cpu top 10/ clipboards / atjobs / mpd filename and kakasi decoding.

Well which commands you want is different for everyone, if you want to know the above let me know in the comments. So let’s continue with a little example script.

First get the actual amount of columns ($COLUMNS doesn’t seem to work in scripts)
realcols=$(stty size | sed 's/.* //')

Form the left column, PARTA
PARTA=$(pstree -A

Make sure it is longer than the right column unless you want more work to do. You could add something like
echo $'\n \n \n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n'
`watch` displays from the top anyway.

Right column:

We need to know how many lines PARTB is because we line it up with `pr`
TOPRINTL=$(( $(echo "$PARTB" | wc -l) +1 ))
Add 1 because we’ll add a line to it later.

Now to print it
( echo "$PARTA" | head -n $TOPRINTL
( echo "$PARTB" ; echo '`---------------------------------------------------------' ) | sed 's/^$/|/;s/^\([^|`]\)/| \1/'
) | pr -2 -T -W $realcols

So we take the first bit of PARTA
we pad PARTB with | and —— to make a small box (make it longer if necessary, if it’s too long pr will cut it off anyway)
Because they are an equal amount of lines we can send it to pr -2 without a problem.

And finally
echo "$PARTA" | tail -n +$((TOPRINTL+1))

watch -n 5 -t scriptname

This entry was posted in music, Programming. Bookmark the permalink.

One Response to Bash system monitor

  1. procyon says:

    So this is where free() is used! There is a memleak in the watch hack, it needs free(line). I have edited the post.

Comments are closed.