AHA
view release on metacpan or search on metacpan
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
example/lava_lamp.pl view on Meta::CPAN
#!/usr/bin/perl
=head1 NAME
lava_lamp.pl --mode [watch|list|notify] --type [problem|recovery] \
--name [AIN|switch name] --label <label> --debug \
--config <path-to-perl-config>
=head1 DESCRIPTION
Simple example how to use L<"AHA"> for controlling AVM AHA switches. I.e.
it is used for using a Lava Lamp as a Nagios Notification handler.
It also tries to check that:
example/lava_lamp.pl view on Meta::CPAN
=item watch
The "watch" mode is used for ensuring that the lamp is not switched on for
certain time i.e. during the night. The Variable C<$LAMP_ON_TIME_TABLE> can be
used to customize the time ranges on a weekday basis.
=item notify
The "notify" mode is used by a notification handler, e.g. from Nagios or from
Jenkins. In this mode, the C<type> parameter is used for signaling whether the
lamp should be switched on ("problem") or off ("recovery").
=item list
This scripts logs all activities in a log file C<$LOG_FILE>. With the "list"
mode, all history entries can be viewed.
=back
=cut
example/lava_lamp.pl view on Meta::CPAN
# End of configuration
use Storable qw(fd_retrieve store_fd store retrieve);
use Data::Dumper;
use feature qw(say);
use Fcntl qw(:flock);
use Getopt::Long;
use strict;
my %opts = ();
GetOptions(\%opts, 'type=s','mode=s','debug!','name=s','label=s','config=s');
my $DEBUG = $opts{debug};
read_config_file($opts{config}) if $opts{config};
init_status();
my $mode = $opts{'mode'} || "list";
# List mode doesnt need a connection
list() and exit if $mode eq "list";
example/lava_lamp.pl view on Meta::CPAN
# Switch off lamp whether the stop file is switched on when we are off the
# time window
$lamp->off();
update_status($status,0,$mode);
} elsif (!$is_on && $in_period && has_trigger($status)) {
$lamp->on();
update_status($status,1,"notif",undef,trigger_label($status));
delete_trigger($status);
}
} elsif ($mode eq "notif") {
my $type = $opts{type} || die "No notification type given";
if (lc($type) =~ /^(problem|custom)$/ && !$is_on) {
if (check_on_period()) {
# If it is a problem and the lamp is not on, switch it on,
# but only if the lamp is not 'hot' (i.e. was not switch off only
# $LAMP_REST_TIME
my $last_hist = get_last_entry($status);
my $rest_time = time - $LAMP_REST_TIME;
if (!$last_hist || $last_hist->[0] < $rest_time) {
$lamp->on();
update_status($status,1,$mode,time,$opts{label});
} else {
info("Lamp not switched on because the lamp was switched off just before ",
time - $last_hist->[0]," seconds");
}
} else {
# Notification received offtime, remember to switch on the lamp
# when in time
info("Notification received in an off-period: type = ",$type," | ",$opts{label});
set_trigger($status,$opts{label});
}
} elsif (lc($type) eq 'recovery') {
if ($is_on) {
# If it is a recovery switch it off
$lamp->off();
update_status($status,0,$mode,time,$opts{label});
} else {
# It's already off, but remove any trigger marker
delete_trigger($status);
}
} else {
info("Notification: No state change. Type = ",$type,", State = ",$is_on ? "On" : "Off",
" | Check Period: ",check_on_period());
}
} else {
die "Unknow mode '",$mode,"'";
}
if ($DEBUG) {
info(Dumper($status));
}
=cut
sub logout {
my $self = shift;
return unless $self->{sid};
# Send a post request as defined in
# http://www.avm.de/de/Extern/files/session_id/AVM_Technical_Note_-_Session_ID.pdf
my $req = HTTP::Request->new(POST => $self->{login_url});
$req->content_type("application/x-www-form-urlencoded");
my $login = "sid=".$self->{sid}."&security:command/logout=fcn";
$req->content($login);
my $resp = $self->{ua}->request($req);
die "Cannot logout SID ",$self->{sid},": ",$resp->status_line unless $resp->is_success;
print "--- Logout ",$self->{sid} if $DEBUG;
delete $self->{sid};
}
=back
my $resp = $self->{ua}->get($self->{login_url});
my $content = $resp->content();
my $challenge = ($content =~ /<Challenge>(.*?)<\/Challenge>/ && $1);
my $input = $challenge . '-' . $self->{password};
Encode::from_to($input, 'ascii', 'utf16le');
my $challengeresponse = $challenge . '-' . lc(Digest::MD5::md5_hex($input));
# Send the challenge back with encoded password
my $req = HTTP::Request->new(POST => $self->{login_url});
$req->content_type("application/x-www-form-urlencoded");
my $login = "response=$challengeresponse";
if ($self->{user}) {
$login .= "&username=" . $self->{user};
}
$req->content($login);
$resp = $self->{ua}->request($req);
if (! $resp->is_success()) {
die "Cannot login to ", $self->{host}, ": ",$resp->status_line();
}
$content = $resp->content();
( run in 3.114 seconds using v1.01-cache-2.11-cpan-df04353d9ac )