Paragraph grep with perl

Here is a very quick and easy perl one-liner that I use fairly often. When I am working with data that is contextualized in chunks of paragraphs (such as Cassandra columnfamily info), I often want to quickly find a single paragraph for a specific piece of information. Here is how I go about doing that in perl:

my_output_command | perl -00 -ne 'm/ninja/ && print'

This results in any paragraph containing the word “ninja” to be printed. The way this works is by the magic of the -00 flag. This flag causes the perl line-separator variable($/) to be set to ‘\n\n’, resulting in the one-liner iterating over STDIN for each paragraph, instead of each line.

The train is departing in…

It has been a crazy few months for me. I moved half-way across the country to start my new job at reddit. The dust is just now settling, and I finally have a permanent residence nailed down. I should be back to blogging more regularly soon.

One of the benefits of my relocation is that I finally have a viable public transportation option for my commute, namely BART. I have been trying to figure out the train schedule, but I quickly learned the BART timetable is really more of an ideal than concrete fact. The one resource which has been useful is the real time departure info available on the BART website.

Being the typical admin that I am, I decided to replace the act of visiting this site each morning with a very small shell script which audibly informs me of the impending train departures from my local station. In a further demonstration of standard sysadmin behaviour, I threw it together in about five minutes and it is ugly as sin… but it works.

(Note to prospective stalkers, I changed the link to hide my true departing station)

curl 'http://www.bart.gov/wireless/stationdetails.aspx?station=PHIL&submit=Submit' | \
grep SFO | \
perl -ne '`espeak "The train is departing in "`; while ($x = m/(\d+) min/g) { `espeak "$1 minutes,"` }'

Apparently BART has a full-fledged API for pulling this type of stuff down, but this works for now.

I’ll leave the details of how it works as an exercise for the reader. It should work on any of the stations listed on the departing info page.

Happy trails!

The tragedy of bash

I found this little function in the default init script for puppetmaster in Debian Squeeze. While I understand what is being done here, I hang my head at the fact that there isn’t a better way. The “x$1″ = “xblah” comparison method is a pandemic plague upon the world of bash.

is_true() {
    if [ "x$1" = "xtrue" -o "x$1" = "xyes" -o "x$1" = "x0" ] ; then
        return 0
    else
        return 1
    fi
}

Busy month!

Hey everyone!

Been a very busy month for me. I spent a lot of time for one of my RHCA exams (which I passed!), and a lot more time on various projects at work. I’ll be back to posting regular on a more regular schedule soon. Send me a comment if there is any sysadmin topic, utility, or how-to that you would like to see me post on.

Cheers,
Jason

Generating large amounts of “emergency” entropy

I have ran into a handful of cases where OS services have come to a screeching halt due to entropy starvation. The most common circumstance has been when postfix starts handling a huge amount of outbound mail, attempting to make a flurry of outbound TLS connections to mail servers. There is an emergency fix for this situation, if you are willing to accept the potential security consequences.
Read more

Assuming a new group without relogging

If you’ve ever added yourself to a POSIX group, you’ll notice that your group membership doesn’t change until you restart your current session. The reason being is that your group information is set at your initial login, and all child processes and virtual terminals under that login will maintain those initial groups.

Unfortunately there is no way (that I know of) to reload your group information for your parent session. However, it is possible to assume your new group membership using the newgrp command:

newgrp - nameofgroup

This command will intialize a new session and your user will now belong to the requested group. Please note that this command does not work if your group membership has not yet been set in /etc/group.

Time saving tip: Automatically ls when changing directories

If you spend a lot of time in the shell, you may notice a certain pattern when hopping around the filesystem. You “cd” to where you want to go, then immediately “ls” to see what is in that directory. I found myself doing this extremely often, so I decided to cut my typing time down by automatically listing as I cd.
Read more

Using SystemTap to monitor files being opened

SystemTap allows you to take a real-time peek into any function call in the kernel. To demonstrate this, I wrote an stap script which will print the full path of any files opened by a specific user.
Read more

Creating re-usable SystemTap modules

I have been heavily delving into SystemTap lately. One of the draw-backs most people run into is having to install the kernel devel, kernel debugging, and gcc stuff on any system you want run your SystemTap scripts. You can get around this by building the SystemTap module once, and re-using it on any system with the same kernel.
Read more

The blight of multi-CD installs

I installed Centos 5 on a VM this morning using CD images. By default, the install requested CDs 1, 2, 3, 4, and 5. I removed every package group I could find other than base, and the install informed me that I would need CDs 1, and 5. The install progressed, and the progress bar reached the very end before it requested CD 5. I reluctantly attached the ISO, anticipating that I was about to witness something excruciatingly disappointing. The 5th CD was only used to install a single library package. Really? Really?