DHCP Cop – A Linux Script for DHCP lease email notification
In my job, sometimes I write little scripts (usually either Bash scripts if it's for Linux or VBS if it's for Windows) to make my life and the lives of my colleagues easier. Usually, they are quite specific to our environment, so there isn't much use in sharing them with the world at large. However, in this case, I think this is general enough that someone else may find a use for it. I'm not claiming to be the best scripter in the world or that this is the best way to do this, just that it works for me and I didn't find too many other things along the same lines when I looked originally. I did find one site that had something similar that put me on the right track, but I can't find it now to give proper credit (and it used a different method than I did, though it was one that got me thinking).
What this basic script does is monitor the active dynamic pool DHCP leases on the server (assuming CentOS, can't speak for other Linux variants) and email a given address with the lease information. My assumption when writing this (based on where I planned to use it) is that the server uses static DHCP leases for all permanent equipment, so you want to be notified by email when something gets a temporary lease, and that anything given a temporary lease is either going to be disconnected (if unauthorized) or given a static lease (if authorized), so the temporary lease pool should usually be empty. Perhaps, based on this, I should have called it 'DHCP Snitch', but it's too late to change it now. The script should be scheduled to run via cron at the desired interval (I use fifteen minutes, which is frequent enough for me), and it will only email once when it detects a new lease. As a downside, if a second lease is given out before the first is cleared, it will not send a second email. I do have vague plans to modify it to notify on each new lease, but that's for 'later'.
The logic is dead simple, it pretty much just greps the dhcpd.leases file for the 'active' string, then mails the specified email address with the lines above and below it in the file (for context), so long as the sent email file doesn't exist in the /tmp/ folder. If there are no leases active, and the sent email file exists, it deletes it. That's it:
#!/bin/sh
#DHCP Cop - alerts via email when new temporary DHCP address is leased
#Set to run at desired interval via cron.
#Emails one time when there is a new active lease
#Will not email again until after there are no active leases for at least one check
if grep -q "binding state active" /var/lib/dhcp/dhcpd.leases; then
if [ -e /tmp/dhcpemail.txt ]; then
exit
else
SUBJECT="New DHCP Lease on $HOSTNAME"
ADDRESS="user@example.com"
BODY="/tmp/dhcpemail.txt"
grep -A 8 -B 3 "binding state active;" /var/lib/dhcp/dhcpd.leases > $BODY
/bin/mail -s "$SUBJECT" "$ADDRESS" < $BODY
fi
else
if [ -e /tmp/dhcpemail.txt ]; then
rm -f /tmp/dhcpemail.txt
else
exit
fi
fi
I feel that I should have some sort of disclaimer here: This script is offered without any guarantees of functionality. It's just something I threw together that seems to be working for me so far in my environment, and I figured I would share at the world at large in the hopes that someone else would find it useful. Use at your own risk.
Ubuntu Upgrade Followup
I finally had a spare moment to do some testing, and discovered that a lot of the problems I have been having (the slow boot, the remote not working, one or two others) stem from a single problem: the printer. Once the printer was unplugged, the system started up normally (though still a little slower than I would like) and the ATI remote started working. Plugging the printer in post-boot caused it to properly appear in lsusb (it wasn't before - which is why I suspected it as the problem).
A quick reinstall of the printer drivers later and the printer... still doesn't work. I don't really want to fight with it right now (I have my old configs and my new configs battling each other, plus firmware issues to deal with...), so I'll just print to the network printer on the rare occasion that I need to print (not too often since school ended).
Though I have to wonder... my copy of Windows 7 Ultimate shows up in the mail on the day that I am fighting with my Ubuntu install... is that some sort of sign, do you think?
Oh, great… another Ubuntu upgrade…
I have been running Ubuntu for years now. I think the first version I installed was 5.10 (the Breezy Badger, to use the whimsical code name), and I have been using it as my main operating system since then. I usually do in-place upgrades when new releases come out (every six months). Generally, these upgrades are painful (I have never had an upgrade go smoothly), but are overall worth it for the speed or features that they bring to the table.
Earlier this week, I upgraded from 9.04 (Jaunty Jackalope) to 9.10 (Karmic Koala). I have not done a clean install on this computer since I first built it back in May of 2008, so that would make this my third Ubuntu upgrade since then. This is by far the worst upgrade experience that I have had to date. The actual install process was relatively smooth (a rarity for Ubuntu), but when it finished, I found that it had created all sorts of issues. Here are the problems that I have found so far since upgrading:
- Significantly longer boot times (both to the login screen and from login to desktop)
- Mythfrontend not working
- ATI remote not working
- Sound and video not working on every boot (it comes and goes)
- Sound (when 'working') full of loud pops and crackles
- Programs crashing and freezing
- Changed a number of default fonts to less attractive alternatives
- Changed default desktop to XFCE from Gnome
Now, these problems aren't altogether major (the system still works, and I can do most of the things I need to - most of the time), and certainly aren't insurmountable (I've already fixed a few of them). I also admit that many of them are due to me tweaking and bolting non-standard bits on to the install in the 18ish months since I first set it up (not the best situation for an upgrade at the best of times). Still, I think it may be the last straw. I'm just tired of fixing and refixing problems with the OS (the sound, mythfrontend, and remote are all things that I have had to fix at least once before - if not more). It can be fun when I am in the right mood and when the problem is new so I can learn from it, but the third or fourth time I need to spend hours messing with and testing sound libraries just to watch a TV show it loses a bit of its charm.
Just for comparison's sake, what about the OS has improved over the previous version?
- Firefox 3.5 (which I could have installed under 9.04 if I really wanted to)
- Other things that I neither notice or use
So the question then is, why did I upgrade? I knew that there wasn't a lot that this release would add or change, and I certainly didn't want to mess around fixing the OS after the upgrade (which I assumed would be necessary - though not to this extent). Honestly, I don't know why I upgraded. Perhaps the need to run the latest and greatest overruled my common sense. It wouldn't be the first time.
Where do I go from here? the way I see it, I have three options.
- Fix my current install (Pros: keep most of my settings and tweaks. Cons: time and frustration, plus need to do it all again in 6 months)
- Do a clean install of Ubuntu 9.10 (Pros: Keep the OS and programs I know and like, hopefully more stable and upgradeable than the current house of cards. Cons:Need to start from scratch on customization, including things I don't even remember doing)
- Switch to Windows 7 (Pros: No need to dual-boot for games, no more living in the Linux ghetto for HW and SW, I really like the OS. Cons: Less customizability, have to find suitable Windows alternatives for Linux programs I use, I want to keep running Linux)
I'm not sure what way I'm going to go. Right now option #3 is looking mighty nice, especially considering I already have a copy of Ultimate on the way (originally intended for the laptop, but I think that can keep on the RC for the time being). I doubt I'll do #2, I don't have the patience for a complete reinstall right now. For the moment, though, I'll probably just let it sit and fix the problems that are really annoying me as they come up.
Perhaps the real question should be this: if this release is the Karmic Koala, then what have I done to deserve this?