no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | tutorials:ssmodule [2009/02/01 03:09] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | This is from a conversation on the hobbit list. It provides an example of a server-side module by Henrik. | ||
+ | ---- | ||
+ | |||
+ | I'm not familiar with Perl at all, but a couple of hours work produced this, which appears to work fine. I'll include it as a sample of how to hook into the Xymon server-side channels. | ||
+ | |||
+ | To use it, put it in your ~hobbit/ | ||
+ | |||
+ | < | ||
+ | [rootlogin] | ||
+ | ENVFILE / | ||
+ | NEEDS hobbitd | ||
+ | CMD hobbitd_channel --channel=client --log=$BBSERVERLOGS/ | ||
+ | </ | ||
+ | |||
+ | <code perl> | ||
+ | # | ||
+ | |||
+ | # | ||
+ | #* Xymon client message processor. | ||
+ | #* */ | ||
+ | #* This perl program shows how to create a server-side module using the */ | ||
+ | #* data sent by the Xymon clients. This program is fed data from the */ | ||
+ | #* Xymon " | ||
+ | #* message is processed by looking at the [who] section and generates | ||
+ | #* a " | ||
+ | #* */ | ||
+ | #* Written 2007-Jan-28 by Henrik Storner < | ||
+ | #* */ | ||
+ | #* This program is in the public domain, and may be used freely for */ | ||
+ | #* creating your own Xymon server-side modules. | ||
+ | #* */ | ||
+ | # | ||
+ | |||
+ | # $Id: rootlogin.pl, | ||
+ | |||
+ | |||
+ | my $bb; | ||
+ | my $bbdisp; | ||
+ | my $hobbitcolumn = " | ||
+ | |||
+ | my $hostname = ""; | ||
+ | my $msgtxt = ""; | ||
+ | my %sections = (); | ||
+ | my $cursection = ""; | ||
+ | |||
+ | sub processmessage; | ||
+ | |||
+ | |||
+ | # Get the BB and BBDISP environment settings. | ||
+ | $bb = $ENV{" | ||
+ | $bbdisp = $ENV{" | ||
+ | |||
+ | |||
+ | # Main routine. | ||
+ | # | ||
+ | # This reads client messages from < | ||
+ | # delimiters that separate each message, and also looking for the | ||
+ | # section markers that delimit each part of the client message. | ||
+ | # When a message is complete, the processmessage() subroutine | ||
+ | # is invoked. $msgtxt contains the complete message, and the | ||
+ | # %sections hash contains the individual sections of the client | ||
+ | # message. | ||
+ | |||
+ | while ($line = < | ||
+ | if ($line =~ / | ||
+ | # It's the start of a new client message - the header looks like this: | ||
+ | # @@client# | ||
+ | |||
+ | # Grab the hostname field from the header | ||
+ | @hdrfields = split(/\|/, $line); | ||
+ | $hostname = $hdrfields[3]; | ||
+ | |||
+ | # Clear the variables we use to store the message in | ||
+ | $msgtxt = ""; | ||
+ | %sections = (); | ||
+ | } | ||
+ | elsif ($line =~ /^\@\@/) { | ||
+ | # End of a message. Do something with it. | ||
+ | processmessage(); | ||
+ | } | ||
+ | elsif ($line =~ / | ||
+ | # Start of new message section. | ||
+ | |||
+ | $cursection = $1; | ||
+ | $sections{ $cursection } = " | ||
+ | } | ||
+ | else { | ||
+ | # Add another line to the entire message text variable, | ||
+ | # and the the current section. | ||
+ | $msgtxt = $msgtxt . $line; | ||
+ | $sections{ $cursection } = $sections{ $cursection } . $line; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | # This subroutine processes the client message. In this case, | ||
+ | # we watch the [who] section of the client message and alert | ||
+ | # if there is a root login active. | ||
+ | |||
+ | sub processmessage { | ||
+ | my $color; | ||
+ | my $summary; | ||
+ | my $statusmsg; | ||
+ | my $cmd; | ||
+ | |||
+ | # Dont do anything unless we have the " | ||
+ | return unless ( $sections{" | ||
+ | |||
+ | # Is there a " | ||
+ | # Note that we must match with /m because there are multiple | ||
+ | # lines in the [who] section. | ||
+ | if ( $sections{" | ||
+ | $color = " | ||
+ | $summary = "ROOT login active"; | ||
+ | $statusmsg = "& | ||
+ | } | ||
+ | else { | ||
+ | $color = " | ||
+ | $summary = " | ||
+ | $statusmsg = "& | ||
+ | } | ||
+ | |||
+ | # Build the command we use to send a status to the Xymon daemon | ||
+ | $cmd = $bb . " " . $bbdisp . " \" | ||
+ | |||
+ | # And send the message | ||
+ | system $cmd; | ||
+ | } | ||
+ | </ |