#!/usr/bin/perl -w
#
# A script to send BB alerts to a GTalk client
# @author Justin Funk
# @date 9/29/2009
#
#
# Based off of work by Thus0 <Thus0@free.fr>
# <http://www.pervasive-network.org/SPIP/Google-Talk-with-perl-bis>
# Copyright (c) 2005, Thus0 <thus0@free.fr>. All rights reserved.
# released under the terms of the GNU General Public License v2
#
# The script get the following environment variables pre-defined so
# that they can send a meaningful alert:
#
# BBCOLORLEVEL - The color of the alert: "red", "yellow" or "purple"
# BBALPHAMSG - The full text of the status log triggering the alert
# ACKCODE - The "cookie" that can be used to acknowledge the alert
# RCPT - The recipient, from the SCRIPT entry
# BBHOSTNAME - The name of the host that the alert is about
# MACHIP - The IP-address of the host that has a problem
# BBSVCNAME - The name of the service that the alert is about
# BBSVCNUM - The numeric code for the service. From SVCCODES definition.
# BBHOSTSVC - HOSTNAME.SERVICE that the alert is about.
# BBHOSTSVCCOMMAS - As BBHOSTSVC, but dots in the hostname replaced with commas
# BBNUMERIC - A 22-digit number made by BBSVCNUM, MACHIP and ACKCODE.
# RECOVERED - Is "1" if the service has recovered.
# DOWNSECS - Number of seconds the service has been down.
# DOWNSECSMSG - When recovered, holds the text "Event duration : N" where
# N is the DOWNSECS value.
use strict;
use Net::XMPP;
## Username and Password of Account
## an account specificlly for alerts can be made at
## https://www.google.com/accounts/NewAccount
my $username = "USERNAME";
my $password = "PASSWORD";
## Get recipent from hobbit-alerts.cfg
my $to = $ENV{'RCPT'};
my $resource = "PerlBot";
## Google Talk Configuration
my $hostname = 'talk.google.com';
my $port = 5222;
my $componentname = 'gmail.com';
my $connectiontype = 'tcpip';
my $tls = 1;
## Setup and connect
my $Connection = new Net::XMPP::Client();
my $status = $Connection->Connect(
hostname => $hostname, port => $port,
componentname => $componentname,
connectiontype => $connectiontype, tls => $tls);
## If connection failed- quit
if (!(defined($status))) {
print "ERROR: XMPP connection failed.\n";
print " ($!)\n";
exit(0);
}
my $sid = $Connection->{SESSION}->{id};
$Connection->{STREAM}->{SIDS}->{$sid}->{hostname} = $componentname;
## Authenticate with Google
my @result = $Connection->AuthSend(
username => $username, password => $password,
resource => $resource);
## If failure
if ($result[0] ne "ok") {
print "ERROR: Authorization failed: $result[0] - $result[1]\n";
exit(0);
}
## Send the message
$Connection->MessageSend(
to => "$to\@$componentname", body => $ENV{'BBALPHAMSG'},
resource => $resource);