#!/usr/bin/perl
#
# Copyright 2012-2013 SPARTA, Inc.  All rights reserved.  See the COPYING
# file distributed with this software for details.
#
# owl-heartbeat						Owl Monitoring System
#
#       This script contacts an Owl manager to give a "heartbeat" notice.
#
# Revision History:
#	1.0	121201	Initial version.
#
#	2.0	Released as part of DNSSEC-Tools 2.0.		130301
#

use strict;

use FindBin;

use lib "$FindBin::Bin/../perllib";
use owlutils;

use Getopt::Long qw(:config no_ignore_case_always);

use LWP::Simple;

#
# Version information.
#
my $NAME   = "owl-heartbeat";
my $VERS   = "$NAME version: 2.0.0";
my $DTVERS = "DNSSEC-Tools version: 2.0";

#------------------------------------------------------------------------
# Defaults and some constants.

my $NSBASE = 'root-servers.net';		# Base name for root servers.

my $DEF_CONFIG	= $owlutils::DEF_CONFIG;	# Default config file nodename.
my $DEF_CONFDIR	= $owlutils::DEF_CONFDIR;	# Default config directory.
my $DEF_DATADIR	= $owlutils::DEF_DATADIR;	# Default data directory.
my $DEF_LOGDIR	= $owlutils::DEF_LOGDIR;	# Default log directory.


#------------------------------------------------------------------------

#
# Data required for command line options.
#
my %options = ();                       # Filled option array.
my @opts =
(
	"confdir|cd=s",				# Specify config directory.
	"config|cf=s",				# Specify config file.
	"datadir=s",				# Specify data directory.
	"logdir=s",				# Specify log directory.

	"help",					# Give help message.
	"Version",				# Give version info.
	"verbose",				# Give verbose output.
);

my $verbose = 0;
my $confdir;					# Config directory.
my $config;					# Config file.
my $datadir;					# Data directory.
my $logdir;					# Log directory.
my $logfile;					# Log file.

#------------------------------------------------------------------------
# Data and log message data.
#

#------------------------------------------------------------------------

main();
exit(0);

#------------------------------------------------------------------------
# Routine:	main()
#
sub main
{
	my @hburls;				# Heartbeat URLs.
	my $sname;				# Name of this sensor.
	my $resp;				# Response from manager.

	#
	# Check our options and read the configuration file.
	#
	doopts();
	owl_setup($NAME,$confdir,$datadir,$logdir);
	if(owl_readconfig($config,$datadir,$logdir) != 0)
	{
		exit(2);
	}

	#
	# Get some data set in our utilities.
	#
	@hburls = @owlutils::heartbeaturls;
	$sname = $owlutils::hostname;

	if(@hburls == 0)
	{
		print STDERR "undefined heartbeat URL\n";
		exit(1);
	}

	#
	# Go through the URL list and try to contact each.
	#
	foreach my $hburl (@hburls)
	{
		#
		# Add the sensor's name to the URL.
		#
		$hburl .= "?sensor=$sname";

# print "hburl - <$hburl>\n\n";

		#
		# We'll save the response, but for now we're also ignoring it.
		#
		$resp = get($hburl);
	}

}

#------------------------------------------------------------------------
# Routine:	doopts()
#
sub doopts
{
	#
	# Parse the options.
	#
	GetOptions(\%options,@opts) || usage();

	#
	# Handle a few immediate flags.
	#
	version()	if(defined($options{'Version'}));
	usage(1)	if(defined($options{'help'}));

	#
	# Set our option variables based on the parsed options.
	#
	$confdir    = $options{'confdir'}    || $DEF_CONFDIR;
	$config	    = $options{'config'}     || $DEF_CONFIG;
	$datadir    = $options{'datadir'};
	$logdir	    = $options{'logdir'};
	$verbose    = $options{'verbose'};

	#
	# Moosh together a few variables to build the config file.
	#
	$config = "$confdir/$config" if($config !~ /\//);

}

#-----------------------------------------------------------------------------
# Routine:      version()
#
# Purpose:      Print the version number(s) and exit.
#
sub version
{
	print STDERR "$VERS\n";
	print STDERR "$DTVERS\n";
	exit(0);
}

#-----------------------------------------------------------------------------
# Routine:      usage()
#
sub usage
{
	print STDERR "usage:  $0 [options]\n";

	print STDERR "\t\where options are:\n";
	print STDERR "\t\t-confdir config-directory\n";
	print STDERR "\t\t-cd config-directory\n";
	print STDERR "\t\t-config config-file\n";
	print STDERR "\t\t-cf config-file\n";
	print STDERR "\t\t-datadir data-directory\n";
	print STDERR "\t\t-logdir log-directory\n";
	print STDERR "\t\t-help\n";
	print STDERR "\t\t-verbose\n";
	print STDERR "\t\t-Version\n";


	exit(0);
}

#--------------------------------------------------------------------------

=pod

=head1 NAME

owl-heartbeat - Provides sensor heartbeat to Owl manager

=head1 SYNOPSIS

  owl-heartbeat [options]

=head1 DESCRIPTION

B<owl-heartbeat> provides a "heartbeat" to the Owl manager.  It touches
a webpage on the manager host and the manager records the contact's time.

The specific webpages are defined in the Owl configuration file with the
I<manager heartbeat> entry.  B<owl-heartbeat> will contact each URL listed,
so multiple I<manager heartbeat> entries will result in multiple heartbeats.

It is assumed B<owl-heartbeat> will be run from B<cron> rather than manually.

=head1 OPTIONS

=over 4

=item B<-confdir config-directory>

=item B<-cd config-directory>

Specifies the directory that holds the B<owl-heartbeat> configuration file.
If this is not given, then the default B<conf> name will be used.  If this
is a relative path, it will be relative from the point of execution.

=item B<-config config-file>

=item B<-cf config-file>

Specifies the B<owl-heartbeat> configuration file.  If I<config-file> does not
contain any directory nodes, then the specified name will be prefixed with the
configuration directory.  If it does contain directory nodes, the configuration
directory (default or option-specified) will be ignored.  If this is not
given, then the default B<owl.conf> name will be used.

=item B<-datadir data-directory>

Specifies the directory that will hold the B<owl-heartbeat> data files.  If
this is not given, then the default B<data> name will be used.  If this is a
relative path, it will be relative from the point of execution.  If this
directory doesn't exist, it will be created.

=item B<-logdir log-directory>

Specifies the directory that will hold the B<owl-heartbeat> log files.  If
this is not given, then the default B<log> name will be used.  If this is
a relative path, it will be relative from the point of execution.  If this
directory doesn't exist, it will be created.

=item B<-help>

Prints a help message.

=item B<-verbose>

Prints verbose output.

=item B<-Version>

Prints B<owl-heartbeat>'s version and exit.

=back

=head1 SEE ALSO

B<owl-dnstimer(1)>,
B<owl-sensord(1)>

B<owl-config(5)>

=head1 COPYRIGHT

Copyright 2012-2013 SPARTA, Inc.  All rights reserved.

=head1 AUTHOR

Wayne Morrison, tewok@tislabs.com

=cut

