#!/usr/bin/perl # FREP_Mon - Monitoring app for FREP # # This product uses software developed by Spread Concepts LLC for use in the Spread toolkit. # For more information about Spread see http://www.spread.org # # Copyright (C) 2007 Mark Steele http://www.control-alt-del.org # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # use Spread qw(:ERROR :MESS); use Event::Lib; use strict; use Getopt::Long; $| = 1; my ($POOL,$SPREAD_SERVER,$SPREAD_PORT); GetOptions ("pool=s" => \$POOL, "spread-port=i" => \$SPREAD_PORT, "spread-address=s" =>\$SPREAD_SERVER) || show_help(); if (!$SPREAD_SERVER || !$POOL || !$SPREAD_PORT) { show_help(); } my ($mbox, $private_group) = Spread::connect({ spread_name => $SPREAD_PORT.'@'.$SPREAD_SERVER,private_name => "$$-".int(rand(100)) }); if (!$mbox) { die $!; } if (Spread::join($mbox, $POOL) < 0) { die; } open(SPREAD_FH,"<&=$mbox") || die "couldn't open fd: $!"; my $event = event_new(*SPREAD_FH, EV_READ|EV_PERSIST, \&get_spread_messages); $event->add; event_mainloop(); sub get_spread_messages { while(my ($st, $s, $g, $mt, $e, $mess) = Spread::receive($mbox,0)) { return if (!$mess); my ($headers,$body) = parse_message($mess); process_message($headers,$body) if ($headers); } } sub parse_message { my $mess = shift; return if ($mess !~ /^Command:/); my ($headers,$body,%headers); if ($mess =~ /^(.*?)__DATA__\n(.+?)__DATAEND__$/s) { ## This is a message that contains a data segment ($headers,$body) = ($1,$2); } else { $headers = $mess; } ## Parse headers my @headers = split(/\n/,$headers); for (@headers) { my ($key,$val) = split(/:/,$_); $headers{$key} = $val; } return(\%headers,$body); } sub process_message { my ($headers,$body) = @_; ## If this is a heartbeat, update time stamp. if ($headers->{Command} eq 'HEARTBEAT') { debug_log("Recieved heartbeat"); return; } if ($headers->{'Chunk'} > 1) { return; } debug_log("CMD: $headers->{Command} FP: $headers->{FilePath}"); } sub debug_log { print scalar localtime, " ",shift,"\n"; } sub show_help { print <<"EOF"; Usage: $0 --pool= --spread-address= --spread-port= EOF exit; }