I had to put the code aside and just recently pulled it out again. Some/most of it is pretty ugly. I had quite a bit done though.
So here I am "refactoring" (and I use that term VERY VERY loosely) the code base. So I'm happily parsing away - iterating over the cfg_file/cfg_dir entries and build a nice big hash to hold all the object definitions. I dump it to a YAML file for validation when I notice this nice bit of junk in my timeperiod dump:Oh yeah...that looks right =/
For those who don't know, nagios object definitions are pretty straightforward. Here's an example:
Easy enough to parse, right? Object type named (host). A clear beginning and end denoted by curly braces. Object attributes in a seemingly key/value type markup. Some people smash the opening brace up against the object type definition but that's easily caught.
Iterating over the definition in Ruby is pretty straightforward (assuming a perfect example):
december 25 00:00-00:00
Things can be even MORE complicated if I wanted to handle non-date holidays:
monday 1 september 00:00-00:00 ; Labor Day (first Monday in September)
thursday -1 november 00:00-00:00 ; Thanksgiving (last Thursday in November)
Ugly, no? I searched high and low for the ability to split starting from the right and didn't find anything native. I had to resort to implementing my own rsplit method for String:
It works but I also can't use it across the board. If I do, I break things that were working (alias, service_description) that can have a space in the value.
I'm not even going to get into trying to convert timeperiod definitions into Date objects yet. That gives me cold sweats.
You can look at http://shinken.git.sourceforge.net/git/gitweb.cgi?p=shinken/shinken;a=blob_plain;f=src/timeperiod.py;hb=HEAD for such a parsing thing (in python here). And yes, it's just a pain in the a%s to manage all the timeperiod formats :)
ReplyDelete