| 1 | package Zabbix::ServerScript; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 19 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 19 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 19 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 19 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 19 | ▶ | use YAML; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 19 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 19 | ▶ | use Log::Log4perl; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 19 | ▶ | use Log::Log4perl::Level; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 19 | ▶ | use Proc::PID::File; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 19 | ▶ | use Proc::Daemon; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 19 | ▶ | use File::Basename; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 19 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 19 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 19 | ▶ | use Storable; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 19 | ▶ | use Term::ReadLine; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 19 | ▶ | use Term::UI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 19 | ▶ | use Getopt::Long qw(:config bundling); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 19 | ▶ | use List::MoreUtils qw(uniq); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||
| 23 | eval { | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 19 | require Zabbix::ServerScript::Config; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 19 | ▶ | } or eval { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=12.5
Branch: 1 branch
Condition33%1/3
eval { or eval {
MC/DC0%0/2
eval {
do {
require Zabbix::ServerScript::Config;
1
}
} or eval {
do {
require Zabbix::ServerScript::DefaultConfig;
1
}
}
eval {
do {
require Zabbix::ServerScript::Config;
1
}
}
eval {
do {
require Zabbix::ServerScript::DefaultConfig;
1
}
}
Truth table33%1/3
A or B
A= eval {
do {
require Zabbix::ServerScript::Config;
1
}
}B= eval {
do {
require Zabbix::ServerScript::DefaultConfig;
1
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 19 | require Zabbix::ServerScript::DefaultConfig; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 19 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 29 | } or die q(Either Zabbix::ServerScript::DefaultConfig or Zabbix::ServerScript::Config is required); | ||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 19 | our @ISA = q(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 33 | 19 | our @EXPORT = qw($config $logger $zx_api create_config); | |||||||||||||||||||||||||||||||||||||||||
| 34 | 19 | our $VERSION = q(0.13); | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 19 | our $config = {}; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 19 | our $logger; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 19 | our $zx_api; | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | sub _get_options { | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 20 | ▶ | my ($opt, @opt_specs) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_options: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 20 | my $default_opt = { | |||||||||||||||||||||||||||||||||||||||||
| 43 | daemon => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 44 | verbose => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 45 | debug => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 46 | unique => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 47 | debug => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 48 | console => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 20 | ▶ | if (defined $opt){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 13 | ▶ | croak q($opt must be hashref) unless ref $opt eq q(HASH); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 7 | $opt = {}; | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 19 | ▶ | map { $opt->{$_} = $default_opt->{$_} unless defined $opt->{$_} } keys %$default_opt; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 19 | my @default_opt_specs = qw( | |||||||||||||||||||||||||||||||||||||||||
| 60 | verbose|v+ | ||||||||||||||||||||||||||||||||||||||||||
| 61 | debug | ||||||||||||||||||||||||||||||||||||||||||
| 62 | daemon | ||||||||||||||||||||||||||||||||||||||||||
| 63 | console | ||||||||||||||||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 19 | @opt_specs = uniq (@opt_specs, @default_opt_specs); | |||||||||||||||||||||||||||||||||||||||||
| 66 | 19 | ▶ | GetOptions($opt, @opt_specs) or croak qq(Cannot get options); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 18 | return $opt; | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | sub _set_basename { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 10 | ▶ | my @caller = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_basename: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 10 | $ENV{BASENAME} = basename($caller[1]); | |||||||||||||||||||||||||||||||||||||||||
| 73 | 10 | $ENV{BASENAME} =~ s/\.pl$//; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 10 | $ENV{BASENAME} =~ s/[\0\/]//; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 10 | return; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | sub _set_binmode { | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | binmode(STDOUT, q(utf8:)); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_binmode: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | binmode(STDERR, q(utf8:)); | |||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | sub _set_id { | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 12 | ▶ | my ($id) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_id: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 12 | ▶ | if (defined $id){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | $ENV{ID} = $id; | |||||||||||||||||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 10 | $ENV{ID} = $ENV{BASENAME}; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 12 | return; | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | sub _set_logger { | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 78 | ▶ | my ($opt) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_logger: called CC=16 SCAR=27.7
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 78 | ▶ | $opt = {} unless defined $opt; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 78 | ▶ | croak qq(Couldn't find 'log_dir' section in Zabbix::ServerScript::Config) unless defined $Zabbix::ServerScript::Config->{log_dir}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 78 | ▶ | croak qq(Environment variables BASENAME and ID are not set) unless (defined $ENV{BASENAME} and $ENV{ID}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ENV{"BASENAME"} and $ENV{"ID"}
MC/DC50%1/2
defined $ENV{"BASENAME"} and $ENV{"ID"}
defined $ENV{"BASENAME"}
$ENV{"ID"}
Truth table66%2/3
A and B
A= defined $ENV{"BASENAME"}B= $ENV{"ID"}
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 75 | ▶ | if (defined $opt->{log_filename}){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 31 | ▶ | if ($opt->{log_filename} ne q()){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 28 | $ENV{LOG_FILENAME} = $opt->{log_filename}; | |||||||||||||||||||||||||||||||||||||||||
| 103 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 3 | $logger->logdie(q(Cannot log to empty filename)); | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 44 | $ENV{LOG_FILENAME} = qq($Zabbix::ServerScript::Config->{log_dir}/$ENV{BASENAME}.log); | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 72 | ▶ | croak qq(Couldn't find 'log' section in Zabbix::ServerScript::Config) unless defined $Zabbix::ServerScript::Config->{log}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 69 | Log::Log4perl->init($Zabbix::ServerScript::Config->{log}); | |||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 66 | my $log_category; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 66 | ▶ | if (defined $opt->{logger}){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 15 | ▶ | if ($opt->{logger} eq q()){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 12 | $log_category = q(Zabbix.ServerScript.nolog); | |||||||||||||||||||||||||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 3 | $log_category = $opt->{logger}; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 51 | ▶ | if (defined $opt->{console} && $opt->{console} == 1){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $opt->{"console"} and $opt->{"console"} == 1
MC/DC100%2/2
defined $opt->{"console"} and $opt->{"console"} == 1
defined $opt->{"console"}
$opt->{"console"} == 1
Truth table100%3/3
A and B
A= defined $opt->{"console"}B= $opt->{"console"} == 1
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 16 | $log_category = q(Zabbix.ServerScript.console); | |||||||||||||||||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 35 | $log_category = q(Zabbix.ServerScript); | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 66 | $logger = Log::Log4perl::get_logger($log_category); | |||||||||||||||||||||||||||||||||||||||||
| 128 | 66 | $ENV{LOG_CATEGORY} = $log_category; | |||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 66 | ▶ | if (defined $opt->{verbose} && $opt->{verbose}){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $opt->{"verbose"} and $opt->{"verbose"}
MC/DC100%2/2
defined $opt->{"verbose"} and $opt->{"verbose"}
defined $opt->{"verbose"}
$opt->{"verbose"}
Truth table100%3/3
A and B
A= defined $opt->{"verbose"}B= $opt->{"verbose"}
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 7 | $logger->more_logging($opt->{verbose}); | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 66 | ▶ | if (defined $opt->{debug} && $opt->{debug} == 1){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $opt->{"debug"} and $opt->{"debug"} == 1
MC/DC100%2/2
defined $opt->{"debug"} and $opt->{"debug"} == 1
defined $opt->{"debug"}
$opt->{"debug"} == 1
Truth table100%3/3
A and B
A= defined $opt->{"debug"}B= $opt->{"debug"} == 1
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 5 | $logger->level($DEBUG); | |||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | $SIG{__DIE__} = sub { | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 57 | ▶ | my ($message) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 57 | ▶ | if($^S and not (defined $ENV{ZBX_TESTING} and $ENV{ZBX_TESTING} == 1)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$^S and not defined $ENV{"ZBX_TESTING"} && $ENV{"ZBX_TESTING"} == 1
defined $ENV{'ZBX_TESTING'} && $ENV{'ZBX_TESTING'} == 1
MC/DC100%3/3
$^S and not defined $ENV{"ZBX_TESTING"} && $ENV{"ZBX_TESTING"} == 1
$^S
defined $ENV{'ZBX_TESTING'}
$ENV{'ZBX_TESTING'} == 1
Truth table100%4/4
A and not(B && C)
A= $^SB= defined $ENV{'ZBX_TESTING'}C= $ENV{'ZBX_TESTING'} == 1
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | # We're in an eval {} and don't want log | ||||||||||||||||||||||||||||||||||||||||||
| 141 | # this message but catch it later | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 36 | return; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 21 | $Log::Log4perl::caller_depth++; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 21 | $logger->fatal($message); | |||||||||||||||||||||||||||||||||||||||||
| 146 | 66 | }; | |||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | $SIG{__WARN__} = sub { | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 6 | ▶ | my ($message) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 6 | local $Log::Log4perl::caller_depth; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 6 | $Log::Log4perl::caller_depth++; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 6 | $logger->warn($message); | |||||||||||||||||||||||||||||||||||||||||
| 153 | 66 | }; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 66 | return; | |||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | sub _set_config { | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 14 | ▶ | my ($config_filename) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_config: called CC=8 SCAR=21.1
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 14 | ▶ | $logger->logcroak(qq(Environment variables BASENAME and ID are not set)) unless (defined $ENV{BASENAME} and $ENV{ID}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ENV{"BASENAME"} and $ENV{"ID"}
MC/DC50%1/2
defined $ENV{"BASENAME"} and $ENV{"ID"}
defined $ENV{"BASENAME"}
$ENV{"ID"}
Truth table66%2/3
A and B
A= defined $ENV{"BASENAME"}B= $ENV{"ID"}
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 13 | ▶ | if (not defined $config_filename){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 11 | $config_filename = qq($Zabbix::ServerScript::Config->{config_dir}/$ENV{BASENAME}.yaml); | |||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 13 | ▶ | if ($config_filename ne q()){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 13 | ▶ | if (-f $config_filename){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 2 | $logger->debug(qq(Loading local config from file $config_filename)); | |||||||||||||||||||||||||||||||||||||||||
| 168 | 2 | ▶ | $config = YAML::LoadFile($config_filename) or $logger->logdie(qq(Cannot load config from $config_filename)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 11 | ▶ | $logger->debug(qq(Local config $config_filename was not found.)) unless $config_filename eq q(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 12 | $config->{global} = $Zabbix::ServerScript::Config; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 12 | return; | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | sub _set_api { | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 17 | ▶ | my ($api) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_api: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 17 | my $api_config; | |||||||||||||||||||||||||||||||||||||||||
| 180 | 17 | ▶ | if (defined $api){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 8 | require Zabbix::ServerScript::API; | |||||||||||||||||||||||||||||||||||||||||
| 182 | 8 | $zx_api = Zabbix::ServerScript::API::init($api); | |||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | sub _get_pid { | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 8 | ▶ | my ($id) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_pid: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 8 | my $name = $ENV{BASENAME}; | |||||||||||||||||||||||||||||||||||||||||
| 189 | 8 | ▶ | $name .= qq(_$id) if defined $id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 8 | $name =~ s/[\0\/]/_/g; | |||||||||||||||||||||||||||||||||||||||||
| 191 | my $pid = { | ||||||||||||||||||||||||||||||||||||||||||
| 192 | name => $name, | ||||||||||||||||||||||||||||||||||||||||||
| 193 | dir => $Zabbix::ServerScript::Config->{pid_dir}, | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 8 | }; | |||||||||||||||||||||||||||||||||||||||||
| 195 | 8 | $logger->debug(qq(Using PID file $pid->{dir}/$pid->{name}.pid)); | |||||||||||||||||||||||||||||||||||||||||
| 196 | 8 | return $pid; | |||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | sub _set_unique { | ||||||||||||||||||||||||||||||||||||||||||
| 200 | 11 | ▶ | my ($unique, $id) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_unique: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 11 | ▶ | if (defined $unique && $unique){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $unique and $unique
MC/DC50%1/2
defined $unique and $unique
defined $unique
$unique
Truth table66%2/3
A and B
A= defined $uniqueB= $unique
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 2 | my $pid = _get_pid($id); | |||||||||||||||||||||||||||||||||||||||||
| 203 | 2 | ▶ | if (Proc::PID::File->running($pid)){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | croak(qq($pid->{name} is already running)); | |||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | sub _set_daemon { | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 11 | ▶ | my ($daemon) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_daemon: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 11 | ▶ | return Proc::Daemon::Init() if $daemon; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 9 | return; | |||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | sub retrieve_cache { | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 4 | ▶ | my ($cache_filename) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
retrieve_cache: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 4 | ▶ | if (not defined $cache_filename){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 1 | $logger->debug(q(Cache filename is not specified, using default filename)); | |||||||||||||||||||||||||||||||||||||||||
| 219 | 1 | $cache_filename = qq($Zabbix::ServerScript::Config->{cache_dir}/$ENV{BASENAME}.cache) | |||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | 4 | my $cache; | |||||||||||||||||||||||||||||||||||||||||
| 222 | 4 | ▶ | if (-f $cache_filename){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 3 | $logger->debug(qq(Loading cache from "$cache_filename")); | |||||||||||||||||||||||||||||||||||||||||
| 224 | eval { | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 3 | $cache = retrieve $cache_filename; | |||||||||||||||||||||||||||||||||||||||||
| 226 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 227 | 3 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 1 | $logger->error(qq(Cannot retrieve cache from "$cache_filename": $@)); | |||||||||||||||||||||||||||||||||||||||||
| 229 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 230 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 1 | $logger->info(qq(Cache file "$cache_filename" was not found)); | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | 4 | return $cache; | |||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | sub store_cache { | ||||||||||||||||||||||||||||||||||||||||||
| 237 | 3 | ▶ | my ($cache, $cache_filename) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
store_cache: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | 3 | ▶ | if (not defined $cache_filename){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 1 | $logger->debug(q(Cache filename is not specified, using default filename)); | |||||||||||||||||||||||||||||||||||||||||
| 240 | 1 | $cache_filename = qq($Zabbix::ServerScript::Config->{cache_dir}/$ENV{BASENAME}.cache) | |||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 3 | $logger->debug(qq(Storing cache to $cache_filename)); | |||||||||||||||||||||||||||||||||||||||||
| 243 | eval { | ||||||||||||||||||||||||||||||||||||||||||
| 244 | 3 | store $cache, $cache_filename; | |||||||||||||||||||||||||||||||||||||||||
| 245 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 246 | 3 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 1 | $logger->error(qq(Cannot store cache to "$cache_filename")); | |||||||||||||||||||||||||||||||||||||||||
| 248 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 249 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 250 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | sub init { | ||||||||||||||||||||||||||||||||||||||||||
| 254 | 9 | ▶ | my ($opt, @opt_specs) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | 9 | _get_options($opt, @opt_specs); | |||||||||||||||||||||||||||||||||||||||||
| 257 | 9 | _set_basename(caller); | |||||||||||||||||||||||||||||||||||||||||
| 258 | 9 | _set_id($opt->{id}); | |||||||||||||||||||||||||||||||||||||||||
| 259 | 9 | _set_daemon($opt->{daemon}); | |||||||||||||||||||||||||||||||||||||||||
| 260 | 9 | _set_logger($opt); | |||||||||||||||||||||||||||||||||||||||||
| 261 | 9 | _set_unique($opt->{unique}, $opt->{id}); | |||||||||||||||||||||||||||||||||||||||||
| 262 | 9 | _set_config($opt->{config}); | |||||||||||||||||||||||||||||||||||||||||
| 263 | 9 | _set_api($opt->{api}); | |||||||||||||||||||||||||||||||||||||||||
| 264 | 9 | $logger->debug(q(Initialized Zabbix::ServerScript)); | |||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | sub return_value { | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 3 | ▶ | my ($value) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
return_value: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 269 | 3 | ▶ | if (defined $value){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | 1 | $logger->debug(qq(Return value: $value)); | |||||||||||||||||||||||||||||||||||||||||
| 271 | 1 | chomp $value; | |||||||||||||||||||||||||||||||||||||||||
| 272 | 1 | print qq($value\n); | |||||||||||||||||||||||||||||||||||||||||
| 273 | 1 | exit; | |||||||||||||||||||||||||||||||||||||||||
| 274 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 2 | $logger->logcroak(q(Return value is not defined)); | |||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | sub connect_to_db { | ||||||||||||||||||||||||||||||||||||||||||
| 280 | 3 | ▶ | my ($dbname, $user, $password, $mode) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
connect_to_db: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 3 | ▶ | $logger->logcroak(q(dbname is not defined)) unless defined $dbname; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | 2 | my $dbh; | |||||||||||||||||||||||||||||||||||||||||
| 283 | 2 | $logger->debug(qq(Trying to connect to $dbname via ODBC)); | |||||||||||||||||||||||||||||||||||||||||
| 284 | 2 | ▶ | $dbh = DBI->connect( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | qq(dbi:ODBC:DSN=$dbname), | ||||||||||||||||||||||||||||||||||||||||||
| 286 | $user, | ||||||||||||||||||||||||||||||||||||||||||
| 287 | $password, | ||||||||||||||||||||||||||||||||||||||||||
| 288 | ) or $logger->logcroak(qq(Failed to connect to $dbname: $DBI::errstr)); | ||||||||||||||||||||||||||||||||||||||||||
| 289 | 1 | $logger->debug(qq(Connected to $dbname)); | |||||||||||||||||||||||||||||||||||||||||
| 290 | 1 | return $dbh; | |||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | sub _prepare_sender_data { | ||||||||||||||||||||||||||||||||||||||||||
| 294 | 8 | ▶ | my ($request_data) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_prepare_sender_data: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 8 | ▶ | if (ref($request_data) eq q(HASH)){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 6 | $request_data = [ $request_data ]; | |||||||||||||||||||||||||||||||||||||||||
| 297 | } elsif (ref($request_data) ne q(ARRAY)){ | ||||||||||||||||||||||||||||||||||||||||||
| 298 | 1 | croak(qq(Request is neither arrayref nor hashref: ) . Dumper($request_data)); | |||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||
| 300 | $request_data = { | ||||||||||||||||||||||||||||||||||||||||||
| 301 | 7 | request => q(sender data), | |||||||||||||||||||||||||||||||||||||||||
| 302 | data => $request_data, | ||||||||||||||||||||||||||||||||||||||||||
| 303 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 304 | # encode_json throws an exception itself, if it cannot encode json. | ||||||||||||||||||||||||||||||||||||||||||
| 305 | # This 'croak' stands here just in case encode_json implementation will be changed. | ||||||||||||||||||||||||||||||||||||||||||
| 306 | 7 | ▶ | my $request_json = encode_json($request_data) or croak(qq(Cannot encode to JSON: ) . Dumper($request_data)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | sub _proceed_sender_response { | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 2 | ▶ | my ($response_json) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_proceed_sender_response: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 2 | $response_json =~ s/^.+(?={)//; | |||||||||||||||||||||||||||||||||||||||||
| 312 | 2 | ▶ | my $response_data = decode_json($response_json) or croak(qq(Cannon decode JSON)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | 1 | return $response_data; | |||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | sub send { | ||||||||||||||||||||||||||||||||||||||||||
| 317 | 12 | ▶ | my ($request_data, $sender_host, $sender_port) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
send: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 318 | 12 | ▶ | $sender_host = q(localhost) if not defined $sender_host; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | 12 | ▶ | $sender_port = q(10051) if not defined $sender_port; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 12 | $logger->debug(qq(Opening sender socket to $sender_host:$sender_port)); | |||||||||||||||||||||||||||||||||||||||||
| 321 | 12 | require IO::Socket::INET; | |||||||||||||||||||||||||||||||||||||||||
| 322 | 12 | ▶ | my $socket = IO::Socket::INET->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | PeerAddr => $sender_host, | ||||||||||||||||||||||||||||||||||||||||||
| 324 | PeerPort => $sender_port, | ||||||||||||||||||||||||||||||||||||||||||
| 325 | Proto => q(tcp), | ||||||||||||||||||||||||||||||||||||||||||
| 326 | Timeout => 10, | ||||||||||||||||||||||||||||||||||||||||||
| 327 | ) or croak(qq(Cannot open socket for zabbix sender to "$sender_host:$sender_port": $?)); | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | 8 | my $request_json = _prepare_sender_data($request_data); | |||||||||||||||||||||||||||||||||||||||||
| 330 | 6 | my $request_length = length($request_json); | |||||||||||||||||||||||||||||||||||||||||
| 331 | 6 | my $response_json; | |||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | 6 | $logger->debug(qq(Writing $request_length of data to sender socket: $request_json)); | |||||||||||||||||||||||||||||||||||||||||
| 334 | 6 | ▶ | $socket->write($request_json, $request_length) or croak(qq(Cannot write to socket: $!)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 335 | 4 | ▶ | $socket->read($response_json, 2048) or croak(qq(Cannot read from socket: $!)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | 3 | ▶ | $socket->close or croak(qq(Cannot close socket: $!)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | 2 | $logger->debug(qq(Server answered to sender: $response_json)); | |||||||||||||||||||||||||||||||||||||||||
| 338 | 2 | my $response_data = _proceed_sender_response($response_json); | |||||||||||||||||||||||||||||||||||||||||
| 339 | 1 | return $response_data; | |||||||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | sub create_config { | ||||||||||||||||||||||||||||||||||||||||||
| 343 | 4 | ▶ | require Zabbix::ServerScript::DefaultConfig; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_config: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | 4 | my ($opt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 346 | 4 | ▶ | $opt = { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 347 | console => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 348 | verbose => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 349 | (defined $opt ? %$opt : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 350 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 351 | 4 | print Dumper($opt); | |||||||||||||||||||||||||||||||||||||||||
| 352 | 4 | init($opt); | |||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | 4 | my $term = Term::ReadLine->new('Zabbix::ServerScript'); | |||||||||||||||||||||||||||||||||||||||||
| 355 | 4 | (my $module_dir = dirname($INC{q(Zabbix/ServerScript/DefaultConfig.pm)})) =~ s|//|/|g; | |||||||||||||||||||||||||||||||||||||||||
| 356 | 4 | $module_dir = $term->get_reply( | |||||||||||||||||||||||||||||||||||||||||
| 357 | prompt => q(Directory to store Config.pm), | ||||||||||||||||||||||||||||||||||||||||||
| 358 | default => $module_dir, | ||||||||||||||||||||||||||||||||||||||||||
| 359 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 360 | 4 | ▶ | die(qq(Wrong directory: $module_dir)) unless (-d $module_dir and -w $module_dir); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-d $module_dir and -w $module_dir
MC/DC50%1/2
-d $module_dir and -w $module_dir
-d $module_dir
-w $module_dir
Truth table66%2/3
A and B
A= -d $module_dirB= -w $module_dir
| |||||||||||||||||||||||||||||||||||||||||||
| 361 | 3 | $logger->debug(qq(Will store Config.pm in $module_dir)); | |||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||
| 363 | 3 | my $module_filename = qq($module_dir/Config.pm); | |||||||||||||||||||||||||||||||||||||||||
| 364 | 3 | ▶ | if (-f $module_filename){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 365 | 2 | ▶ | $term->ask_yn( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 366 | prompt => qq(\n$module_filename exists.\nOverwrite?), | ||||||||||||||||||||||||||||||||||||||||||
| 367 | default => q(n), | ||||||||||||||||||||||||||||||||||||||||||
| 368 | ) or exit 0; | ||||||||||||||||||||||||||||||||||||||||||
| 369 | 1 | $logger->info(q(Overwrite has been requested)); | |||||||||||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | 2 | for my $section (qw(config_dir pid_dir log_dir)){ | |||||||||||||||||||||||||||||||||||||||||
| 373 | $Zabbix::ServerScript::Config->{$section} = $term->get_reply( | ||||||||||||||||||||||||||||||||||||||||||
| 374 | prompt => $section, | ||||||||||||||||||||||||||||||||||||||||||
| 375 | 6 | default => $Zabbix::ServerScript::Config->{$section}, | |||||||||||||||||||||||||||||||||||||||||
| 376 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | 2 | ▶ | open my $fh, q(>), $module_filename or die(qq(Cannot open file $module_filename: $!)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 380 | 2 | print $fh Data::Dumper->Dump([$Zabbix::ServerScript::Config], [q($Zabbix::ServerScript::Config)]); | |||||||||||||||||||||||||||||||||||||||||
| 381 | 2 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | 2 | ▶ | require $module_filename or die(qq(Cannot load module: $!)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 384 | 2 | $logger->info(qq($module_filename has been created successfully)); | |||||||||||||||||||||||||||||||||||||||||
| 385 | 2 | exit 0; | |||||||||||||||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||
| 388 | 19 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | =encoding utf-8 | ||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||
| 396 | Zabbix::ServerScript - Simplify your Zabbix server scripts' environment. | ||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||
| 398 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||
| 402 | use strict; | ||||||||||||||||||||||||||||||||||||||||||
| 403 | use warnings; | ||||||||||||||||||||||||||||||||||||||||||
| 404 | use utf8; | ||||||||||||||||||||||||||||||||||||||||||
| 405 | use Zabbix::ServerScript; | ||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||
| 407 | my $opt = { | ||||||||||||||||||||||||||||||||||||||||||
| 408 | id => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 409 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||
| 411 | my @opt_specs = qw( | ||||||||||||||||||||||||||||||||||||||||||
| 412 | id=i | ||||||||||||||||||||||||||||||||||||||||||
| 413 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||
| 415 | sub main { | ||||||||||||||||||||||||||||||||||||||||||
| 416 | Zabbix::ServerScript::init($opt, @opt_specs); | ||||||||||||||||||||||||||||||||||||||||||
| 417 | Zabbix::ServerScript::return_value(1); | ||||||||||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | main(); | ||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | Zabbix::ServerScript is a module to simplify writing new scripts for Zabbix server: external scripts, alert scripts, utils, etc. | ||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | =head1 SUBROUTINES | ||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | =head2 init($opt, @opt_specs) | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | Initializes variables, sets logger, API, etc. | ||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | If specified, the first argument must be hashref, which can have the following keys: | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | $opt = { | ||||||||||||||||||||||||||||||||||||||||||
| 435 | config => q(path/to/local/config.yaml), | ||||||||||||||||||||||||||||||||||||||||||
| 436 | console => 0, # should the script log to STDERR or not | ||||||||||||||||||||||||||||||||||||||||||
| 437 | verbose => 0, # increase verbosity. By default, script will log only WARN messages and above. | ||||||||||||||||||||||||||||||||||||||||||
| 438 | debug => 0, # Enable debug mode. | ||||||||||||||||||||||||||||||||||||||||||
| 439 | logger => q(Zabbix.ServerScript), # Log4perl logger name | ||||||||||||||||||||||||||||||||||||||||||
| 440 | api => q(), # name of Zabbix API instance in global config | ||||||||||||||||||||||||||||||||||||||||||
| 441 | id => q(), # unique identifier of what is being done, e.g.: database being checked | ||||||||||||||||||||||||||||||||||||||||||
| 442 | unique => 0, # only one instance for each $opt->{id} is allowed | ||||||||||||||||||||||||||||||||||||||||||
| 443 | daemon => 0, # daemonize during initialization. See Proc::Damon for details | ||||||||||||||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||
| 446 | If specified, the 2nd argument must be array of options descriptions, as for Getopt::Long::GetOptions. | ||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||
| 448 | The following options descrtiptions are included by default (see their meanings above): | ||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||
| 450 | verbose|v+ # --verbose (supports bundling, e.g. -vvv) | ||||||||||||||||||||||||||||||||||||||||||
| 451 | debug | ||||||||||||||||||||||||||||||||||||||||||
| 452 | daemon | ||||||||||||||||||||||||||||||||||||||||||
| 453 | console | ||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | Initializes the following global variables: | ||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||
| 460 | =item $logger | ||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||
| 462 | Log4perl instance | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | =item $config | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | hashref contais both local (script-specific) and global config data. | ||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | Default global config is located at Zabbix/ServerScript/DefaultConfig.pm. | ||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | User can generate its own global config and store it into Zabbix/ServerScript/Config.pm. Config.pm is preferred over DefaultConfig.pm. | ||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | Global config data can be accessed through $Zabbix::ServerScript::Config and $config->{global} variables. | ||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||
| 474 | Script-specific config is searched within $Zabbix::ServerScript::Config->{config_dir} path. Only YAML is currently supported for script-specific configs. | ||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||
| 476 | $config = { | ||||||||||||||||||||||||||||||||||||||||||
| 477 | global => { | ||||||||||||||||||||||||||||||||||||||||||
| 478 | config_dir => q(/path/to/local/config/dir), | ||||||||||||||||||||||||||||||||||||||||||
| 479 | log_dir => q(/tmp), | ||||||||||||||||||||||||||||||||||||||||||
| 480 | ..., | ||||||||||||||||||||||||||||||||||||||||||
| 481 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 482 | local_item1 => ..., | ||||||||||||||||||||||||||||||||||||||||||
| 483 | local_item2 => ..., | ||||||||||||||||||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||
| 486 | =item $zx_api | ||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||
| 488 | Zabbix::ServerScript::API object | ||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||
| 490 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||
| 492 | =head2 return_value($value) | ||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||
| 494 | Prints $value to STDOUT and exits. Throws an exception if $value is not defined. | ||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | =head2 store_cache($cache, $cache_filename) | ||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||
| 498 | Stores cache to file using Storable module. $cache_filename is optional. | ||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||
| 500 | =head2 retrieve_cache($cache_filename) | ||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | Retrieves cache from file using Storable module. $cache_filename is optional. | ||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||
| 504 | =head2 connect_to_db($dsn, $user, $password) | ||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||
| 506 | Connects to database via unixODBC. $dsn is mandatory. | ||||||||||||||||||||||||||||||||||||||||||
| 507 | Returns database handle or throws an exception on failure. | ||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||
| 509 | =head2 send($data_structure) | ||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | Send data to Zabbix trapper like zabbix_sender does. $data_structure is mandatory. | ||||||||||||||||||||||||||||||||||||||||||
| 512 | Returns server response on success or throws an exception on failure. | ||||||||||||||||||||||||||||||||||||||||||
| 513 | $data_structure must be either hashref or arrayref of hashrefs. | ||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||
| 515 | Each of hashref must be like: | ||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||
| 517 | { | ||||||||||||||||||||||||||||||||||||||||||
| 518 | host => q(Linux host), # hostname as in Zabbix frontend | ||||||||||||||||||||||||||||||||||||||||||
| 519 | key => q(item_key), | ||||||||||||||||||||||||||||||||||||||||||
| 520 | value => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 521 | clock => time, # unix timestamp, optional | ||||||||||||||||||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||
| 524 | =head2 create_config | ||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||
| 526 | Creates Config.pm from DefaultConfig.pm. | ||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||
| 528 | Usage: | ||||||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||||||
| 530 | perl -MZabbix::ServerScript -e create_config | ||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||
| 532 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||
| 534 | Copyright (C) Anton Alekseyev. | ||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||
| 536 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 537 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||
| 539 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||||||
| 541 | Anton Alekseyev E<lt>akint.wr+github@gmail.comE<gt> | ||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||
| 543 | =cut | ||||||||||||||||||||||||||||||||||||||||||