webcam and chat stats bash scripts

Well I’m doing a great job of avoiding my homework by writing these posts… Anyway, I wrote a couple Bash scripts this weekend to automate some common tasks involved with my site. The first one was to automate processing and updating of my chat logs. I run Xchat2 as an IRC client on my channel #the_end on irc.cyberarmy.net and keep logs. Every day, I run the IRC stats analyzer pisg and then FTP the resultant HTML file to my chat stats page. With the help of a cronjob and a few Bash commands, however, I no longer have to remember to do that little annoyance myself. Here it is:

#!/bin/bash
FTPSERVER='ftp.themikecam.com'
FTPUSER='username'
FTPPASS='password'
SOURCE='/data/mikecam/public_html/newmikecam/chatstats/index.html'
DEST='/public_html/newmikecam/chatstats/index.html'


echo -e "\nRunning pisg to analyze stats...\n"
/data/mikecam/utils/chatstats/pisg;


echo -e "Running ftp to upload stats page...\n"
ftp -n $FTPSERVER <<EOF
user $FTPUSER $FTPPASS
put $SOURCE $DEST
quit
EOF
echo "Done!"
exit 0

The only difficult/annoying part of this was figuring out how to pass commands to ftp once it was running. I couldn’t use echo because that would wait until FTP finished running to execute. I couldn’t manage to get the right search terms to find a solution on Google, but luckily Kyle came to my rescue again, making a suggestion that led me to the magic of Here Documents, which let me stream commands to FTP while it was running. Now I just have to figure out either how to make pisg only process the past week or two or write something to lop old days off of the current xchat log.

Woops, forgot the crontab! Here ’tis, every night at midnight:
0 0 * * * /data/mikecam/utils/chatstats.sh

My second script is to grab a webcam image and upload it. I’ve been using Camorama, which is a very nice little Gnome program to view a webcam and automate capture and FTP upload. Unfortunately, it suffers from a few deficiencies, including a lack of flexibility on image size (only choices are “small,” “medium,” and “large”) and on capture interval (it is in minutes rather than seconds). I also had come up with the idea of using my ASU web space to store my cam so I wouldn’t have to worry about using up transfer here on the MikeCam and could decrease the interval between shots. ASU, however, only permits secure FTP connections to manage files, so I had to write a script to use one of the already available capture scripts and the sftp program. Ta-da:

#!/bin/bash
export FTPUSER='username'
export FTPSERVER='general.asu.edu'
export FTPDIR='www'
export FTPPASS='password'
export CAMFILE='webcam/webcam.jpg'


while [ 1 ]; do
vidcat -d /dev/video0 -f jpeg -o $CAMFILE -q 60 -s 320x240
expect <<'END'
set timeout -1
match_max 1000000
spawn sftp $env(FTPUSER)@$env(FTPSERVER):$env(FTPDIR)/
expect "password: "
send -- "$env(FTPPASS)\r"
expect "sftp> "
send -- "put $env(CAMFILE)\r"
expect "sftp> "
send -- "quit\r"
END
sleep 5.1;
done


exit 0

A couple challenges here: First, finding a capture script that I could use to just capture and let me handle the transferring manually. I went with w3cam for now, but hopefully I can find something, either a replacement or an additional program, that will allow me to easily overlay text on the image so I can add “themikecam.com” and a timestamp. The second challenge was how to deal with SFTP. At first I thought I could do the same as FTP, but the SFTP program doesn’t allow you to pass it a password through an argument, nor does it allow you to use run it and use the “open” and “user” commands, so I had to figure out how to give it the password when it prompted for it. I searched on Google a bit, found someone who had posted about a similar problem, and someone suggested the “expect” program, which lets you wait until you get a certain output before continuing. As you can see, I used it to wait until it prompts for the password, then wait until it authenticates, then wait until the file is transferred. There’s also a loop to capture and upload the cam image every 5.1 seconds. I should probably rewrite the loop so that it doesn’t disconnect and reconnect every time.

firewire hard drive woes

Okay, so the first of a series of Linux tips tonight, or rather solutions to problems I’ve had:
I recently bought a ByteCC 3.5″ aluminum combo USB2.0/Firewire external enclosure and a 160gb Samsung hard drive both from NewEgg, for the purpose of having an external backup and/or mobile disk. I was able to format the disk to ReiserFS fine, but I’ve been having a problem whereby after transferring data steadily for a while, it would suddenly stop and spit errors like this:
ieee1394: sbp2: aborting sbp2 command
Test Unit Ready 00 00 00 00 00
ieee1394: sbp2: reset requested
ieee1394: sbp2: Generating sbp2 fetch agent reset
ieee1394: sbp2: aborting sbp2 command
ieee1394: sbp2: aborting sbp2 command

The result would be that I could not umount the drive nor rmmod the sbp2 module nor reboot because it would hang waiting for the module and I’d be forced to do a hard reset. Now, as to the solution to this, the linux1394 FAQ suggests trying the serialize_io option and a user on the mailing list said that this did in fact solve his problem. So, I opened up /etc/modprobe.conf and add the line “options sbp2 serialize_io=1″. I can’t say definitely yet whether that IS the solution, but cp has copied 8gb so far without so much as a chirp, so I’m hopeful that it will last.

Some miscellaneous tips I learned along the way:
When resetting after the failures, if I leave the fstab entry for the partition (/dev/sda1 in my case), Mandrake (10.1) will want to check it on boot because it wasn’t cleanly unmounted. Since the Firewire driver isn’t loaded at that point, however, it can’t access it and winds up constantly checking my second hard drive, /dev/hdb1. It would get mostly through checking it, then get stuck, and even if I went to single-user mode and ran reiserfsck to clear it, it would still check. The solution to this, it turns out, is just to comment out the fstab entry. I also now take the precaution of doing “init 1″ before doing my hard reset, so I can softly shut down everything possible.

Update: Just finished transferring 40gigs of data and no problems yet. I’ll be running rsync all night to backup my entire drive, so hopefully my luck will hold
Update 3 Mar. 2005: I’ve now been using my Firewire drive without a single problem for a week and half, so turning on serialize_io has definitely cured it.

autopackage

Autopackage is another effort to standardize and ease installation on Linux and eliminate dependency hell, the notable other that I know of being Zero-Install. Since I use Mandrake Linux, the main software management tool I use is their URPMI, which is nice for a quick and easy install — IF the program you want has been packaged by someone. Even if you get that lucky, though, it’s usually out of date.

Autopackage looks exciting (at least at first glance) because it allows for a single binary, cross-distribution installation package with the possibility of downloading and installing necessary dependencies. I’m not clear, however, on how it resolves dependencies if they don’t use a centralized server for finding dependencies. I’ll have to take a closer look when I have more time. Encouraging is that GAIM and GIMP have already started releasing packages using this framework. A usable installer with easy dependency resolution is key to bringing desktop Linux to the next level.