blib/lib/Zabbix/ServerScript.pm

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