Check/Systemd.pm

filter: Statementstmt 35.5% 27 / 76 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 57.1% 8 / 14 Podpod 0.0% 0 / 2 totaltotal 26.9% 35 / 130 CC 19 SCAR 30.5
CC
19
Cov
25%
getUnitFiles
42.0
getUnits
42.0
_service_status
20.0
CRAP 21.1
« Check/Disk.pm ↑ Summary Check/RabbitMQ.pm »
1 package Zabbix::Check::Systemd;
2 =head1 NAME
3  
4 Zabbix::Check::Systemd - Zabbix check for Systemd services
5  
6 =head1 VERSION
7  
8 version 1.10
9  
10 =head1 SYNOPSIS
11  
12 Zabbix check for Systemd services
13  
14 UserParameter=cpan.zabbix.check.systemd.installed,/usr/bin/perl -MZabbix::Check::Systemd -e_installed
15 UserParameter=cpan.zabbix.check.systemd.system_status,/usr/bin/perl -MZabbix::Check::Systemd -e_system_status
16 UserParameter=cpan.zabbix.check.systemd.service_discovery[*],/usr/bin/perl -MZabbix::Check::Systemd -e_service_discovery -- $1
17 UserParameter=cpan.zabbix.check.systemd.service_status[*],/usr/bin/perl -MZabbix::Check::Systemd -e_service_status -- $1
18  
19 =head3 installed
20  
21 checks Systemd is installed: 0 | 1
22  
23 =head3 system_status
24  
25 gets Systemd system status: initializing | starting | running | degraded | maintenance | stopping | offline | unknown
26  
27 =head3 service_discovery
28  
29 discovers Systemd enabled services
30  
31 $1: I<regex of service name, by default undefined>
32  
33 =head3 service_status $1
34  
35 gets Systemd enabled service status: active | inactive | failed | unknown | ...
36  
37 $1: I<service name>
38  
39 =cut
40 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶no warnings qw(qw utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use Lazy::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 1 ▶use Zabbix::Check;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49  
50 BEGIN
51 {
52 1 ▶ require Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 # set the version for version checking
54 1 our $VERSION = '1.10';
55 # Inherit from Exporter to export functions and variables
56 1 our @ISA = qw(Exporter);
57 # Functions and variables which are exported by default
58 1 our @EXPORT = qw(_installed _system_status _service_discovery _service_status);
59 # Functions and variables which can be optionally exported
60 1 our @EXPORT_OK = qw();
61 }
62  
63  
64 1 our ($systemctl) = whereisBin('systemctl');
65  
66  
67 sub getUnitFiles
68 {
69 0 ▶ return unless $systemctl;
Subroutine getUnitFiles: not called CC=6 SCAR=37.4
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $Zabbix::Check::Systemd::systemctl 0 0 0
70 0 my ($type) = @_;
71 0 my $result = {};
72 0 for (`$systemctl --no-legend list-unit-files 2>/dev/null`)
73 {
74 0 chomp;
75 0 ▶ last unless s/^\s+|\s+$//gr;
Branch: 1 branch
BranchTrueFalseTotal
unless s/^\s+|\s+$//gru 0 0 0
76 0 my ($unit, $state) = /^(\S+)\s+(\S+)/;
77 0 my $info = {
78 unit => $unit,
79 state => $state,
80 };
81 0 ($info->{name}, $info->{type}) = $unit =~ /^([^\.]*)\.(.*)/;
82 0 ▶ $result->{$unit} = $info if not $type or $type eq $info->{type};
Branch: 1 branch
BranchTrueFalseTotal
if not $type or $type eq $info->{"type"} 0 0 0
Condition0%0/3
not $type or $type eq $info->{"type"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $type or $type eq $info->{"type"}
not $type $type eq $info->{"type"}
Truth table0%0/3
not(A) or B
A= not $typeB= $type eq $info->{"type"}
ABresult
1X1
011
000
83 }
84 0 return $result;
85 }
86  
87 sub getUnits
88 {
89 0 ▶ return unless $systemctl;
Subroutine getUnits: not called CC=6 SCAR=37.4
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $Zabbix::Check::Systemd::systemctl 0 0 0
90 0 my ($type) = @_;
91 0 my $result = {};
92 0 my $first = 1;
93 0 for (`$systemctl --no-legend -a list-units 2>/dev/null`)
94 {
95 0 chomp;
96 0 ▶ last unless s/^\s+|\s+$//gr;
Branch: 1 branch
BranchTrueFalseTotal
unless s/^\s+|\s+$//gru 0 0 0
97 0 my ($unit, $load, $active, $sub, $desc) = /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)/;
98 0 my $info = {
99 unit => $unit,
100 load => $load,
101 active => $active,
102 sub => $sub,
103 desc => $desc,
104 };
105 0 ($info->{name}, $info->{type}) = $unit =~ /^([^\.]*)\.(.*)/;
106 0 ▶ $result->{$unit} = $info if not $type or $type eq $info->{type};
Branch: 1 branch
BranchTrueFalseTotal
if not $type or $type eq $info->{"type"} 0 0 0
Condition0%0/3
not $type or $type eq $info->{"type"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $type or $type eq $info->{"type"}
not $type $type eq $info->{"type"}
Truth table0%0/3
not(A) or B
A= not $typeB= $type eq $info->{"type"}
ABresult
1X1
011
000
107 }
108 0 return $result;
109 }
110  
111 sub _installed
112 {
113 0 ▶ my $result = $systemctl? 1: 0;
Subroutine _installed: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$Zabbix::Check::Systemd::systemctl ? : 0 0 0
114 0 print $result;
115 0 return $result;
116 }
117  
118 sub _system_status
119 {
120 0 ▶ my $result = "";
Subroutine _system_status: not called CC=3 SCAR=24.8
121 0 ▶ my $line = `$systemctl is-system-running 2>/dev/null` if $systemctl;
Branch: 1 branch
BranchTrueFalseTotal
if $Zabbix::Check::Systemd::systemctl 0 0 0
122 0 ▶ if ($line)
Branch: 1 branch
BranchTrueFalseTotal
if ($line) 0 0 0
123 {
124 0 chomp $line;
125 0 $result = $line;
126 }
127 0 print $result;
128 0 return $result;
129 }
130  
131 sub _service_discovery
132 {
133 0 ▶ my ($nameRgx) = map(zbxDecode($_), @ARGV);
Subroutine _service_discovery: not called CC=3 SCAR=24.8
134 0 my @items;
135 0 my $units = getUnits('service');
136 0 ▶ @items = map($units->{$_}, grep({ not defined($nameRgx) or $units->{$_}->{name} =~ /$nameRgx/ } keys %$units)) if $units;
Branch: 2 branches
BranchTrueFalseTotal
if $units 0 0 0
not defined $nameRgx or $units->{$_}{"name"} =~ /$nameRgx/u 0 0 0
137 0 return printDiscovery(@items);
138 }
139  
140 sub _service_status
141 {
142 0 ▶ my ($name) = map(zbxDecode($_), @ARGV);
Subroutine _service_status: not called CC=4 SCAR=30.0
143 0 ▶ return unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
144 0 my $nameS = shellmeta($name);
145 0 my $result = "";
146 0 ▶ my $line = `$systemctl is-active \"$nameS.service\" 2>/dev/null` if $systemctl;
Branch: 1 branch
BranchTrueFalseTotal
if $Zabbix::Check::Systemd::systemctl 0 0 0
147 0 ▶ if ($line)
Branch: 1 branch
BranchTrueFalseTotal
if ($line) 0 0 0
148 {
149 0 chomp $line;
150 0 $result = $line;
151 }
152 0 print $result;
153 0 return $result;
154 }
155  
156  
157 1 1;
158 __END__
« Check/Disk.pm ↑ Summary Check/RabbitMQ.pm »