<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ball Dawg! &#187; scripting</title>
	<atom:link href="http://www.balldawg.net/index.php/tag/scripting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.balldawg.net</link>
	<description>Just some ninja monkeys, nothing to see here.  Move along.</description>
	<lastBuildDate>Fri, 13 Jan 2012 02:02:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Creating Your Own Hobbit / Xymon Tests</title>
		<link>http://www.balldawg.net/index.php/2009/05/creating-your-own-hobbit-xymon-tests/</link>
		<comments>http://www.balldawg.net/index.php/2009/05/creating-your-own-hobbit-xymon-tests/#comments</comments>
		<pubDate>Sat, 09 May 2009 14:54:00 +0000</pubDate>
		<dc:creator>Andrew Rankin</dc:creator>
				<category><![CDATA[Hobbit / Xymon]]></category>
		<category><![CDATA[Server Monitoring]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[xymon]]></category>

		<guid isPermaLink="false">http://www.balldawg.net/?p=96</guid>
		<description><![CDATA[Over the years I&#8217;ve had to write plenty of Hobbit / Xymon scripts to monitor various different things within my employers systems.   Since most all of our applications are custom there are not always built in tests that will work for us.   For example, we use Xen for our development virtual machines and being able to [...]]]></description>
			<content:encoded><![CDATA[<p>Over the years I&#8217;ve had to write plenty of Hobbit / Xymon scripts to monitor various different things within my employers systems.   Since most all of our applications are custom there are not always built in tests that will work for us.   For example, we use Xen for our development virtual machines and being able to track what was going on with those virtual machines is important and being able to identify a VM within Xymon at a moments glance is important to us, so we created a test that does just that.   We have created in house scripts for MySQL Status, MySQL Running Queries, our in house distributed services, Lighttpd (as discussed earlier on this blog), Apache, Memcached, etc.   This doesnt include the hundreds of different snmp tests we&#8217;ve added to Devmon for monitoring our network equiptment.</p>
<p><span id="more-96"></span></p>
<p>Since I&#8217;ve had to write quite a few of these I thought I&#8217;d share a simple recipe for getting started with these scripts.   Most all our scripts are written in Perl, its just what I&#8217;m used too and what most people in our company is fluent in.   Our older scripts (from when we actually ran Big Brother) are almost all shell scripts and I have written at least one in Python since that particular service&#8217;s library was in python.   For this example I&#8217;m going to use Perl default template:</p>
<pre class="brush: perl">
#!/usr/bin/perl -w
#############################################################################
# $Id: $
#############################################################################
use strict;

## BB and related test constants
#############################################################################

use constant GREEN =&gt; &#039;green&#039;;
use constant YELLOW =&gt; &#039;yellow&#039;;
use constant RED =&gt; &#039;red&#039;;

## BB Global variables
#############################################################################

my $prev_run_log = &quot;$ENV{BBTMP}/$ENV{MACHINE}.lighttpd.data&quot;;
my $bbtest = &#039;testname&#039;;
my $color = GREEN;
my $status = $bbtest . &quot; OK&quot;;

## Main Program
#############################################################################
{

my $DATA = &quot;&quot;;

## TEST STUFF HERE

## Send to Hobbit
#############################################################################
my $report_date = `/bin/date`;
chomp($report_date);

system(&quot;$ENV{BB} $ENV{BBDISP} &#039;status $ENV{MACHINE}.$bbtest $color $report_date - $status\n\n$DATA&#039;\n&quot;);

}
</pre>
<p>This script by itself will send a test called &#8216;testname&#8217;, which is green and has the status of &#8216;OK&#8217;.  Since we have not defined anything to test it won&#8217;t contain any data with it.  To run this script you need to be within Xymon&#8217;s &#8220;shell&#8221;, which is basically sh, with some enviroment variables set.  To get there, go to &#8217;bin&#8217; directory within your hobbit client installation, in my case this is: &#8220;/home/hobbit/client/bin&#8221;, you&#8217;ll need to run &#8216;bbcmd&#8217;, this will drop you to a prompt which looks something like this:</p>
<pre class="brush: perl">
[rar@apollo bin]$ ./bbcmd
2009-05-09 10:10:35 Using default environment file /home/hobbit/client/etc/hobbitclient.cfg
sh-3.2$
</pre>
<p>Lets change the test name to something else and gather some data from the system we are running on.   To change the test name to say &#8216;modules&#8217;, just change the $bbtest to &#8216;modules&#8217;.   How about testing what kernel modules are loaded &#8211; or basically just parse the output of &#8216;lsmod&#8217;.   To do this we&#8217;d need to capture the output of the command, this is easy enough in perl, something like this would work just fine:</p>
<pre class="brush: perl">
my $lsmod = `/sbin/lsmod`;
</pre>
<p>With the output of &#8217;lsmod&#8217; captured we can do whatever we want with it.  Lets say we just want the modules, and no other info about them in an array:</p>
<pre class="brush: perl">
my $lsmod = `/sbin/lsmod`;
my @modules = split(/\n/, $lsmod);
my @module_names;

foreach my $module (@modules){
my ($name, $junk) = split(/\s.+/, $module);
push @module_names, $name;
}
</pre>
<p>Now that we have our array created that we can easily loop through for tests, lets run a test on them to see if we have loaded any modules for &#8216;ext&#8217; file systems:</p>
<pre class="brush: perl">
foreach my $module (@module_names) {
if ($module =~ &quot;ext&quot;){
$color = RED;
$status = $bbtest . &quot; NOT OK&quot;
}
}
</pre>
<p>OK,  since we&#8217;d like a list of modules that are loaded on our hobbit test page, we can add all those modules to the &#8216;$DATA&#8217; variable, and finish off this example test.  Within that last look we added, you&#8217;d just push that modules name into &#8216;$DATA&#8217;:</p>
<pre class="brush: perl">
[...]
$status = $bbtest . &quot; NOT OK&quot;
}

$DATA .= $module . &quot;\n&quot;;
}
</pre>
<p>So what kindof output does this give us?  If you want to just see what would be run in the system() call at the bottom of the script, just change &#8216;system&#8217; to &#8216;print&#8217;, the output should be something like the following:</p>
<pre class="brush: perl">
/home/hobbit/client/bin/bb hobbitserver.xipnet.net &#039;status apollo.modules red Sat May  9 10:35:18 EDT 2009 - modules NOT OK

Module
iptable_filter
ip_tables
rfcomm
l2cap
bluetooth
af_packet
nf_conntrack_netbios_ns
ipt_REJECT
nf_conntrack_ipv4
xt_state
nf_conntrack
ip6t_REJECT
xt_tcpudp
ip6table_filter
ip6_tables
x_tables
ipv6
binfmt_misc
dm_multipath
parport_pc
lp
parport
nvram
evbug
usbcore
ext3
jbd
mbcache
evdev
raid10
raid456
async_xor
async_memcpy
async_tx
xor
raid1
raid0
multipath
linear
md_mod
dm_mirror
dm_snapshot
dm_mod
fuse
loop
8250
serial_core
&#039;
</pre>
<p>Now the test is red and has the status of &#8220;NOT OK&#8221; since we tested for any ext file system and I&#8217;m running ext3.   To have the data sent to hobbit, simply set the print back to a system call and setup the approprate section within &#8216;clientlaunch.cfg&#8217;, for our example I&#8217;d use:</p>
<pre class="brush: perl">
[modules]
ENVFILE $HOBBITCLIENTHOME/etc/hobbitclient.cfg
CMD $HOBBITCLIENTHOME/ext/bb-modules.pl
LOGFILE $HOBBITCLIENTHOME/logs/hobbitclient.log
INTERVAL 5m
</pre>
<p>I know this is a pretty basic example, but its a place to start &#8211; just let the imagination go from here on the millions of tests you could come up with.  Keep in mind I&#8217;m sending this data in on the &#8220;status&#8221; channel in Xymon &#8211; there is also a &#8220;data&#8221; channel which won&#8217;t create a column and with the test approprately setup within hobbit to capture its data to rrd&#8217;s, you can create graphs that just show up in the &#8220;trends&#8221; column if alarming is not necessary.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.balldawg.net/index.php/2009/05/creating-your-own-hobbit-xymon-tests/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

