| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
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 |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head3 service_status $1 |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
gets Systemd enabled service status: active | inactive | failed | unknown | ... |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
$1: I |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=cut |
|
40
|
1
|
|
|
1
|
|
711
|
use strict; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
23
|
|
|
41
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
20
|
|
|
42
|
1
|
|
|
1
|
|
2
|
no warnings qw(qw utf8); |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
35
|
|
|
43
|
1
|
|
|
1
|
|
7
|
use v5.14; |
|
|
1
|
|
|
|
|
2
|
|
|
44
|
1
|
|
|
1
|
|
3
|
use utf8; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
4
|
|
|
45
|
1
|
|
|
1
|
|
34
|
use Lazy::Utils; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
78
|
|
|
46
|
|
|
|
|
|
|
|
|
47
|
1
|
|
|
1
|
|
4
|
use Zabbix::Check; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
112
|
|
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
BEGIN |
|
51
|
|
|
|
|
|
|
{ |
|
52
|
1
|
|
|
1
|
|
4
|
require Exporter; |
|
53
|
|
|
|
|
|
|
# set the version for version checking |
|
54
|
1
|
|
|
|
|
1
|
our $VERSION = '1.10'; |
|
55
|
|
|
|
|
|
|
# Inherit from Exporter to export functions and variables |
|
56
|
1
|
|
|
|
|
7
|
our @ISA = qw(Exporter); |
|
57
|
|
|
|
|
|
|
# Functions and variables which are exported by default |
|
58
|
1
|
|
|
|
|
2
|
our @EXPORT = qw(_installed _system_status _service_discovery _service_status); |
|
59
|
|
|
|
|
|
|
# Functions and variables which can be optionally exported |
|
60
|
1
|
|
|
|
|
680
|
our @EXPORT_OK = qw(); |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
our ($systemctl) = whereisBin('systemctl'); |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub getUnitFiles |
|
68
|
|
|
|
|
|
|
{ |
|
69
|
0
|
0
|
|
0
|
0
|
|
return unless $systemctl; |
|
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
|
0
|
|
|
|
|
last unless s/^\s+|\s+$//gr; |
|
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
|
0
|
0
|
|
|
|
$result->{$unit} = $info if not $type or $type eq $info->{type}; |
|
83
|
|
|
|
|
|
|
} |
|
84
|
0
|
|
|
|
|
|
return $result; |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub getUnits |
|
88
|
|
|
|
|
|
|
{ |
|
89
|
0
|
0
|
|
0
|
0
|
|
return unless $systemctl; |
|
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
|
0
|
|
|
|
|
last unless s/^\s+|\s+$//gr; |
|
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
|
0
|
0
|
|
|
|
$result->{$unit} = $info if not $type or $type eq $info->{type}; |
|
107
|
|
|
|
|
|
|
} |
|
108
|
0
|
|
|
|
|
|
return $result; |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub _installed |
|
112
|
|
|
|
|
|
|
{ |
|
113
|
0
|
0
|
|
0
|
|
|
my $result = $systemctl? 1: 0; |
|
114
|
0
|
|
|
|
|
|
print $result; |
|
115
|
0
|
|
|
|
|
|
return $result; |
|
116
|
|
|
|
|
|
|
} |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub _system_status |
|
119
|
|
|
|
|
|
|
{ |
|
120
|
0
|
|
|
0
|
|
|
my $result = ""; |
|
121
|
0
|
0
|
|
|
|
|
my $line = `$systemctl is-system-running 2>/dev/null` if $systemctl; |
|
122
|
0
|
0
|
|
|
|
|
if ($line) |
|
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
|
|
|
0
|
|
|
my ($nameRgx) = map(zbxDecode($_), @ARGV); |
|
134
|
0
|
|
|
|
|
|
my @items; |
|
135
|
0
|
|
|
|
|
|
my $units = getUnits('service'); |
|
136
|
0
|
0
|
|
|
|
|
@items = map($units->{$_}, grep({ not defined($nameRgx) or $units->{$_}->{name} =~ /$nameRgx/ } keys %$units)) if $units; |
|
|
0
|
0
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
return printDiscovery(@items); |
|
138
|
|
|
|
|
|
|
} |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub _service_status |
|
141
|
|
|
|
|
|
|
{ |
|
142
|
0
|
|
|
0
|
|
|
my ($name) = map(zbxDecode($_), @ARGV); |
|
143
|
0
|
0
|
|
|
|
|
return unless $name; |
|
144
|
0
|
|
|
|
|
|
my $nameS = shellmeta($name); |
|
145
|
0
|
|
|
|
|
|
my $result = ""; |
|
146
|
0
|
0
|
|
|
|
|
my $line = `$systemctl is-active \"$nameS.service\" 2>/dev/null` if $systemctl; |
|
147
|
0
|
0
|
|
|
|
|
if ($line) |
|
148
|
|
|
|
|
|
|
{ |
|
149
|
0
|
|
|
|
|
|
chomp $line; |
|
150
|
0
|
|
|
|
|
|
$result = $line; |
|
151
|
|
|
|
|
|
|
} |
|
152
|
0
|
|
|
|
|
|
print $result; |
|
153
|
0
|
|
|
|
|
|
return $result; |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
1; |
|
158
|
|
|
|
|
|
|
__END__ |