ActiveMQ stomp end to end test for nagios

03.09.2008 15:07

posted by: Benjamin Smith

Posted Under: , , ,

I recently found myself needing to test producing and consuming messages from activemq via stomp. It’s a pretty trivial task thanks to Perl and nagios! Here is a quick script to test the production and consumption of messages and the command you’ll need to put into core.cfg (nagios v2). It’s slim on libs to cut down on excess fat. Enjoy!

Prerequisites..

Class::Accessor (Net::Stomp wants it)
Net::Stomp
$USER3$ = custom location of nagios check scripts..
check_mq = the name of the script.
$ARG1$ = host to check
$ARG1$ = number of messages to produce/consume
$ARG2$ = name of queue to test with

The command for core.cfg:

define command{
        command_name    check_mq
        command_line    $USER3$/check_mq $ARG1$ $ARG2$ $ARG3$
        }

The code:

#!/usr/bin/perl -w

use strict;
use Net::Stomp;

my $host = $ARGV[0];
my $nummsgs = $ARGV[1];
my $queue = $ARGV[2];

my $stomp = Net::Stomp->new( { hostname => $host, port => '61613' } );

$stomp->connect();

for (my $i=0; $i <$nummsgs; $i++) {
    $stomp->send({ destination => "/queue/$queue",
                   body => "$queue$i",
                   persistent => 'true'} );
}

$stomp->subscribe({ destination => "/queue/$queue",
                    'ack' => 'client',
                    'activemq.prefetchSize' => 1});

my $count = 0;
for (my $i=0; $i <$nummsgs; $i++) {
    my $frame = $stomp->receive_frame;
    my $body = $frame->body;
    if ($body eq "$queue$i") {
        $count++;
    }
    $stomp->ack( { frame => $frame } );
}

$stomp->disconnect();

print "Produced: $nummsgs Consumed $count\n";

if ($count == $nummsgs) {
    exit(0);
}
elsif (($count != $nummsgs) && ($count != 0)) {
    exit(1);
}
elsif ($count == 0) {
    exit(2);
}

Oh so simple..

0 comments | 1 pingback

History meme, in Perl!

11.04.2008 13:19

posted by: Benjamin Smith

Posted Under: , , ,

So I submitted a post to perlmonks RE: all the 'history meme' brouhaha. I provided two one liners in Perl (Below) and asked for people to improve/shorten/spice up the 'history meme' via Perl. We'll see how that goes :).

So, here's what I gave them to start with:

history | perl -le 'while($l = <>) 
{$n=${[split(/s+/,$l)]}[2];$h{$n}++;} $i=0;foreach $k 
(reverse sort {$h{$a}<=>$h{$b}} keys %h) {last if $i > 9; print "$h{$k} $k"; $i++;}'
history | perl -le 'while($l = <>) ;
{$n=${[split(/s+/,$l)]}[2];$h{$n}++;} $i=0;@a=reverse 
sort {$h{$a}<=>$h{$b}} keys %h; print "$h{$a[$_]} $a[$_]" for (0..9)'
0 comments | 0 pingbacks

Why I still use Perl..

22.02.2008 21:30

posted by: Benjamin Smith

Posted Under: ,

I have a sizable nagios log file that I needed to parse somewhat quickly to return the most recent iteration of a particular message. I thought about implementing something in python, but I whipped up the below Perl script in about few minutes that does just what I need...

#!/usr/bin/perl -w
use strict;
use Tie::File;
use Fcntl;

# Log file to parse.
my $logfile = '/var/log/nagios/syslog-nagios';
my @msgs = ();
# Get the logfile in a data structure quietly
tie @msgs, 'Tie::File', $logfile,mode => O_RDONLY or die "Cannot access $logfile: $!
";

# Loop through reverse sorted list of messages.
foreach my $line (reverse(@msgs)) {
    # print out the matched line..
    print ((split(':',$line))[6]) && last if ($line =~ /TRAFFIC-INTERNET/);
}

I'm sure I could have done some fancy map() or grep() but the above does the job great, and does it quickly. So yeah, that's one of the reasons I still dig Perl.

2 comments | 0 pingbacks

So, at some point, I lost track..

11.09.2007 17:51

posted by: Benjamin Smith

Posted Under: , , ,

Stepped off the beaten path, veered too far off the shoulder...SOMETHING. Got an announcement today that one of my coworkers wife just had their baby(BTW, Contrats Mike and wife!). Usually this kind of announcement is followed up by me with "Aww, how cute.. Good luck sleeping! I don't envy you!".

Today was different. Today I looked at the picture of the baby, and thought "Man, I'm jealous.". That's an interesting reaction for me, because it made me remember a time when babies and family and all that were exactly what I wanted. I've not felt that for a loooong time. It was nice.. It really makes me feel like I lost track.

Moving on past the sappy stuff; I've found something fun to play with lately. It's called "Let's see how many scripts I would usually write in Perl that I can write in Python instead.". I'm up to four now, and I'm massively impressed with what I've been exposed to. I'm going to give a simple "For instance".

The other day at the office I was tasked with creating a crapload of Redirect rules for Apache. All similar format/results, but different enough that one common RegExp with RedirectMatch wouldn't have done the trick. The list of the source and destination paths were sent to me in an excel file, which I then converted to csv. Rather than copy and paste 50+ of these into the Apache conf and hand tailoring them, I decided to write a script to parse it and spit out the rules I needed.

In python, it was as simple as using an example from here: http://docs.python.org/lib/csv-examples.html

To my knowledge Perl doesn't have any CSV parsing modules in the code distribution, which means CPAN fun will ensue. To get the mega list of installed Perl modules on my host, I have to write a Perl script (no root access).

Here's the script:

#!/usr/bin/perl
use ExtUtils::Installed;
print "$_
" for ExtUtils::Installed->new()->modules;

Run that and grep the output for anything csv.

# ./list_mods.pl | grep -i csv
#

Yup, I was right.. At least as of 5.8.6 (slightly old dist) there's no CSV module installed. So now, let's install one (of the 232 results I got back in a quick search):

# perl -MCPAN -e 'install Text::CSV'

I still have to write the code! I've wasted 10 minutes! Ironically enough, 10 minutes is what it took me to modify a common example of CSV parsing in python and to top it off, the python code is easier on the eyes.

I still LOVE my Perl, and if I'm ever in a pinch and really _need_ to have a script done, like now, I'll probably run to it, or the shell first.

0 comments | 0 pingbacks
More Posts: