Listing 1 Logbot
#!/usr/bin/perl
# logbot.pl a simple perl script designed to take input from a
# log monitoring program, and send it to a multi-user
# conference room on a jabber server.
# Bert Hayes 01 November 2003 (last edits)
use Net::Jabber qw(Client);
use Sys::Hostname;
use Net::Jabber::Message;
use Net::Jabber::Protocol;
use strict;
use English;
# If syslog-ng is running as root, this script will run as root
# Drop root priveleges, since we don't need them
my $UNPRIV = "99"; # This is the nobody uid and gid on my system; YMMV
my $EGID="$UNPRIV $UNPRIV";
my $EUID=$UNPRIV;
my $GID=$UNPRIV;
my $UID=$UNPRIV;
# Edit these lines to suit your environment
my $server = "jabberd.mydomain.org";
my $port = 5223; # 5222 by default for non-ssl connections
my $resource = hostname();
my $username = "logbot";
my $password = "secret_password";
my @words = @ARGV;
#my $recipient = "bert"; # use for sending to a single user
my $mucserver = "conference.localhost";
my $room = "geeks";
my $nick = "logbot";
$SIG{HUP} = \&Stop;
$SIG{KILL} = \&Stop;
$SIG{TERM} = \&Stop;
$SIG{INT} = \&Stop;
my $Connection = new Net::Jabber::Client();
# Establish network connection to the server
my $status = $Connection->Connect(hostname=>$server,
port=>$port,
ssl=>1);
if (!(defined($status)))
{
print "ERROR: Jabber server is down or connection was not allowed.\n";
print " ($!)\n";
exit(0);
}
# Authenticate to the server
my @result = $Connection->AuthSend(username=>$username,
password=>$password,
resource=>$resource);
if ($result[0] ne "ok")
{
print "ERROR: Authorization failed: $result[0] - $result[1]\n";
exit(0);
}
print "Logged in to $server:$port using resource $resource...\n";
# Check into conference room
$Connection->MUCJoin(room=>"$room",
server=>"$mucserver",
nick=>"$nick");
while(defined($Connection->Process())) {
###########################################################################
## Uncomment this section for use with syslog-ng
###########################################################################
while(<STDIN>) {
$Connection->MessageSend(to=>"$room\@$mucserver",
body=>"$_\n",
type=>"chat" );
}
###########################################################################
###########################################################################
## Uncomment this section if using logbot with swatch and traditional syslog
###########################################################################
#
# $Connection->MessageSend(to=>"$room\@$mucserver",
# body=>"@words\n",
# type=>"chat" );
# &Stop();
###########################################################################
} # End of while(defined($Connection->Process())) loop
print "ERROR: The connection was killed...\n";
exit(0);
sub Stop
{
print "Exiting...\n";
$Connection->Disconnect();
exit(0);
}
|