Sunday, January 27, 2008

Using Google Calendar to Monitor Software, part 3

I use Google Calendar to monitor automatic software execution in cron jobs. Here's the code.

----

#!/usr/bin/perl -w
# $Id: cal_event 5853 2007-04-02 16:46:01Z jhaemer $

use strict;

# get proprietary information from the accompanying file
# "cal_event_proprietary.pm"

my $app_dir = `dirname $0`;
chomp $app_dir;
push @INC, $app_dir;
require cal_event_proprietary; # defines %url, username, passwords
our %url; # the calendar_name => url hash

# get calendar and event from command line
@ARGV > 1 || die "usage: $0 calendar_name description\n";
my $calname = shift;
my $url = $url{$calname} || die "$calname: no such calendar\n";
my $title = "@ARGV";

# login
our ( $login, $passwd );
use Net::Google::Calendar;
my $cal = Net::Google::Calendar->new( url => $url );
$cal->login( $login, $passwd );

# create entry
my $entry = Net::Google::Calendar::Entry->new();
$entry->title($title);
$entry->when( DateTime->now,
DateTime->now() + DateTime::Duration->new( minutes => 1 ) );

my $tmp = $cal->add_entry($entry);
die "Couldn't add event: $@\n" unless defined $tmp;

# $Id: cal_event 5853 2007-04-02 16:46:01Z jhaemer $

__END__

=head1 NAME

cal_event - announce an event in a Google calendar

=head1 SYNOPSIS

cal_event calendar_name description

=head1 DESCRIPTION

Makes an entry in the named calendar.

=head1 EXAMPLES

This program makes it easy to track status of cron jobs from a simple, calendar
interface, just by calling the program from the cron job.

if [ $? -eq 0 ]
then
cal_event ${0##*/} SUCCEEDED
else
cal_event ${0##*/} FAILED
fi

=head1 BUGS and CAVEATS

=over

=item *

Expects username, password, and calendar info in F.
See F for details.

=item *

All calendars need to be in the same account.

=back

=head1 FILES

cal_event_proprietary.pm

=head1 SEE ALSO

Net::Google::Calendar

=head1 AUTHOR

Jeffrey S. Haemer

No comments: