File Coverage

blib/lib/Sys/RevoBackup/Plugin/Zabbix.pm
Criterion Covered Total %
statement 11 13 84.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 16 18 88.8


line stmt bran cond sub pod time code
1             package Sys::RevoBackup::Plugin::Zabbix;
2             {
3             $Sys::RevoBackup::Plugin::Zabbix::VERSION = '0.27';
4             }
5             BEGIN {
6 1     1   2279 $Sys::RevoBackup::Plugin::Zabbix::AUTHORITY = 'cpan:TEX';
7             }
8             # ABSTRACT: revobackup plugin for Zabbix::Sender
9              
10 1     1   21 use 5.010_000;
  1         3  
  1         40  
11 1     1   6 use mro 'c3';
  1         2  
  1         6  
12 1     1   26 use feature ':5.10';
  1         2  
  1         88  
13              
14 1     1   514 use Moose;
  0            
  0            
15             use namespace::autoclean;
16              
17             # use IO::Handle;
18             # use autodie;
19             # use MooseX::Params::Validate;
20             # use Carp;
21             # use English qw( -no_match_vars );
22             use Try::Tiny;
23             use Zabbix::Sender;
24             use Sys::Hostname::FQDN ();
25              
26             # extends ...
27             extends 'Sys::RevoBackup::Plugin';
28             # has ...
29             # with ...
30             # initializers ...
31             sub _init_priority { return 10; }
32              
33             # your code here ...
34             sub run_prepare_hook {
35             my $self = shift;
36              
37             my $hostname = Sys::Hostname::FQDN::fqdn();
38             return $self->zabbix_report(time(),$hostname,'revobackup.started');
39             }
40              
41             sub run_cleanup_hook {
42             my $self = shift;
43             my $ok = shift;
44              
45             my $hostname = Sys::Hostname::FQDN::fqdn();
46             $self->zabbix_report(time(),$hostname,'revobackup.finished');
47              
48             return $self->zabbix_report($ok,$hostname,'revobackup.status');
49             }
50              
51             sub zabbix_report {
52             my $self = shift;
53             my $status = shift;
54             my $hostname = shift;
55             my $item = shift || 'revobackup.status';
56              
57             if ( my $zabbix_server = $self->config()->get('Zabbix::Server') ) {
58             $self->logger()->log( message => 'Using Zabbix Server at '.$zabbix_server, level => 'debug', );
59             my $arg_ref = {
60             'server' => $zabbix_server,
61             'port' => $self->config()->get('Zabbix::Port') || 10_051,
62             };
63             $arg_ref->{'hostname'} = $hostname if $hostname;
64             try {
65             my $Zabbix = Zabbix::Sender::->new($arg_ref);
66             $Zabbix->send( $item, $status );
67             $Zabbix = undef;
68             }
69             catch {
70             $self->logger()->log( message => 'Zabbix::Sender failed w/ error: '.$_, level => 'error', );
71             };
72             return 1;
73             }
74             else {
75             $self->logger()->log( message => 'No Zabbix Server configured.', level => 'debug', );
76             return;
77             }
78             }
79              
80             no Moose;
81             __PACKAGE__->meta->make_immutable;
82              
83             1;
84              
85             __END__
86              
87             =pod
88              
89             =encoding UTF-8
90              
91             =head1 NAME
92              
93             Sys::RevoBackup::Plugin::Zabbix - revobackup plugin for Zabbix::Sender
94              
95             =head1 METHODS
96              
97             =head2 run_cleanup_hook
98              
99             Report the backup status to zabbix.
100              
101             =head2 run_prepare_hook
102              
103             Report the start time to zabbix.
104              
105             =head2 zabbix_report
106              
107             Report to zabbix.
108              
109             =head1 NAME
110              
111             Sys::RevoBackup::Plugin::Zabbix - Report backup status to Zabbix
112              
113             =head1 AUTHOR
114              
115             Dominik Schulz <dominik.schulz@gauner.org>
116              
117             =head1 COPYRIGHT AND LICENSE
118              
119             This software is copyright (c) 2012 by Dominik Schulz.
120              
121             This is free software; you can redistribute it and/or modify it under
122             the same terms as the Perl 5 programming language system itself.
123              
124             =cut