#!/usr/bin/perl -w
#
# Created by Mathieu Gagné 2009
#

use strict;
use warnings;
use Net::Growl;
use Getopt::Long qw(:config no_ignore_case bundling);

# Default values
my $application = 'Nagios';
my $title = 'Alert';
my $message = '';
my $priority = 2;
my $sticky = 0;
my $destination = 'localhost';
my $password = '';

my $help = 0;

my $pod2usage = sub {
  # Load Pod::Usage only if needed.
  require "Pod/Usage.pm";
  import Pod::Usage;
  
	pod2usage(@_);
};


# Declare and retreive options
GetOptions(
  'h|help'          => \$help,
  'a|application=s' => \$application,
  't|title=s'       => \$title,
  'm|message=s'     => \$message,
  'P|priority=i'    => \$priority,
  's|sticky'        => \$sticky,
  'H|host=s'        => \$destination,
  'p|password=s'    => \$password,
) or $pod2usage->(1);


# Print help
$pod2usage->(1) if $help;


# Validate options
if ( $application eq '' ) {
  die "Error: Missing mandatory option: application\n";
}

if ( $title eq '' ) {
  die "Error: Missing mandatory option: title\n";
}

if ( $message eq '' ) {
  die "Error: Missing mandatory option: message\n";
}

if ( $priority eq '' ) {
  die "Error: Missing mandatory option: priority\n";
}

if ( $password eq '' ) {
  die "Error: Missing mandatory option: password\n";
}


#
# Main program
#


# Set up the Socket
my %addr = (
  PeerAddr => $destination,
  PeerPort => Net::Growl::GROWL_UDP_PORT,
  Proto    => 'udp',
);

my $s = IO::Socket::INET->new ( %addr ) || die "Could not create socket: $!\n";

# Register the application
my $p = Net::Growl::RegistrationPacket->new(
  application => $application,
  password    => $password,
);

$p->addNotification();

print $s $p->payload();

# Send a notification
$p = Net::Growl::NotificationPacket->new(
  application => $application,
  title       => $title,
  description => $message,
  priority    => $priority,
  sticky      => $sticky,
  password    => $password,
);

print $s $p->payload();

close($s);


=head1 NAME
 
 notify-growl

=head1 DESCRIPTION
 
 Allow Nagios to send Growl Notifications over the network.

=head1 SYNOPSIS

 notify-growl options

=head1 OPTIONS
 
=over 4

=item B<-h|--help>

 Print help message and exit.

=item B<-H <hostaddress>>
 
 Hostaddress of the destination accepting Growl Notifications over the network. (Default: localhost)

=item B<-a|--application>

 Application name used to register to Growl. (Default: Nagios)

=item B<-t|--title>
 
 Title of the Growl notification. (Default: Alert)

=item B<-m|--message>
 
 Message of the Growl notification.

=item B<-P|--priority>

 Priority of the Growl notification. (Default: 2)

=item B<-s|--sticky>
 
 Sticky Growl notification. (Default: no)

=item B<-p|--password>

 Password to connect to the destination accepting Growl Notifications over the network.

=back

=head1 EXAMPLES

 define command {
  command_name  notify-host-by-growl
  command_line  $USER1$/notify-growl -H localhost -p password -a Nagios -t Alert -m "$NOTIFICATIONTYPE$ Alert $HOSTNAME$[$HOSTADDRESS$] is $HOSTSTATE$"
 }

 define command {
  command_name  notify-service-by-growl
  command_line  $USER1$/notify-growl -H localhost -p password -a Nagios -t Alert -m "$NOTIFICATIONTYPE$ Alert $HOSTNAME$[$HOSTADDRESS$]/$SERVICEDESC$ is $SERVICESTATE$"
 }

 
=cut

