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.11 |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 SYNOPSIS |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Zabbix check for Systemd services |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
15
|
1
|
|
|
1
|
|
1689
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
44
|
|
16
|
1
|
|
|
1
|
|
10
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
44
|
|
17
|
1
|
|
|
1
|
|
19
|
use v5.10.1; |
|
1
|
|
|
|
|
6
|
|
18
|
1
|
|
|
1
|
|
12
|
use Lazy::Utils; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
168
|
|
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
1
|
|
8
|
use Zabbix::Check; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
157
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
BEGIN |
24
|
|
|
|
|
|
|
{ |
25
|
1
|
|
|
1
|
|
9
|
require Exporter; |
26
|
1
|
|
|
|
|
3
|
our $VERSION = '1.11'; |
27
|
1
|
|
|
|
|
13
|
our @ISA = qw(Exporter); |
28
|
1
|
|
|
|
|
4
|
our @EXPORT = qw(_installed _system_status _service_discovery _service_status); |
29
|
1
|
|
|
|
|
939
|
our @EXPORT_OK = qw(); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
our ($systemctl) = whereis('systemctl'); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub get_unit_files |
37
|
|
|
|
|
|
|
{ |
38
|
0
|
0
|
|
0
|
0
|
|
return unless $systemctl; |
39
|
0
|
|
|
|
|
|
my ($type) = @_; |
40
|
0
|
|
|
|
|
|
my $result = {}; |
41
|
0
|
|
|
|
|
|
for (`$systemctl --no-legend list-unit-files 2>/dev/null`) |
42
|
|
|
|
|
|
|
{ |
43
|
0
|
|
|
|
|
|
$_ = trim($_); |
44
|
0
|
0
|
|
|
|
|
last unless $_; |
45
|
0
|
|
|
|
|
|
my ($unit, $state) = /^(\S+)\s+(\S+)/; |
46
|
0
|
|
|
|
|
|
my $info = { |
47
|
|
|
|
|
|
|
unit => $unit, |
48
|
|
|
|
|
|
|
state => $state, |
49
|
|
|
|
|
|
|
}; |
50
|
0
|
|
|
|
|
|
($info->{name}, $info->{type}) = $unit =~ /^([^\.]*)\.(.*)/; |
51
|
0
|
0
|
0
|
|
|
|
$result->{$unit} = $info if not $type or $type eq $info->{type}; |
52
|
|
|
|
|
|
|
} |
53
|
0
|
|
|
|
|
|
return $result; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub get_units |
57
|
|
|
|
|
|
|
{ |
58
|
0
|
0
|
|
0
|
0
|
|
return unless $systemctl; |
59
|
0
|
|
|
|
|
|
my ($type) = @_; |
60
|
0
|
|
|
|
|
|
my $result = {}; |
61
|
0
|
|
|
|
|
|
my $first = 1; |
62
|
0
|
|
|
|
|
|
for (`$systemctl --no-legend -a list-units 2>/dev/null`) |
63
|
|
|
|
|
|
|
{ |
64
|
0
|
|
|
|
|
|
$_ = trim($_); |
65
|
0
|
0
|
|
|
|
|
last unless $_; |
66
|
0
|
|
|
|
|
|
my ($unit, $load, $active, $sub, $desc) = /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)/; |
67
|
0
|
|
|
|
|
|
my $info = { |
68
|
|
|
|
|
|
|
unit => $unit, |
69
|
|
|
|
|
|
|
load => $load, |
70
|
|
|
|
|
|
|
active => $active, |
71
|
|
|
|
|
|
|
sub => $sub, |
72
|
|
|
|
|
|
|
desc => $desc, |
73
|
|
|
|
|
|
|
}; |
74
|
0
|
|
|
|
|
|
($info->{name}, $info->{type}) = $unit =~ /^([^\.]*)\.(.*)/; |
75
|
0
|
0
|
0
|
|
|
|
$result->{$unit} = $info if not $type or $type eq $info->{type}; |
76
|
|
|
|
|
|
|
} |
77
|
0
|
|
|
|
|
|
return $result; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub _installed |
81
|
|
|
|
|
|
|
{ |
82
|
0
|
0
|
|
0
|
|
|
my $result = $systemctl? 1: 0; |
83
|
0
|
|
|
|
|
|
print $result; |
84
|
0
|
|
|
|
|
|
return $result; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub _system_status |
88
|
|
|
|
|
|
|
{ |
89
|
0
|
|
|
0
|
|
|
my $result = ""; |
90
|
0
|
0
|
|
|
|
|
my $line = `$systemctl is-system-running 2>/dev/null` if $systemctl; |
91
|
0
|
0
|
|
|
|
|
if ($line) |
92
|
|
|
|
|
|
|
{ |
93
|
0
|
|
|
|
|
|
chomp $line; |
94
|
0
|
|
|
|
|
|
$result = $line; |
95
|
|
|
|
|
|
|
} |
96
|
0
|
|
|
|
|
|
print $result; |
97
|
0
|
|
|
|
|
|
return $result; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub _service_discovery |
101
|
|
|
|
|
|
|
{ |
102
|
0
|
|
|
0
|
|
|
my ($name_rgx) = map(zbx_decode($_), @ARGV); |
103
|
0
|
|
|
|
|
|
my @items; |
104
|
0
|
|
|
|
|
|
my $units = get_units('service'); |
105
|
0
|
0
|
|
|
|
|
@items = map($units->{$_}, grep({ not defined($name_rgx) or $units->{$_}->{name} =~ /$name_rgx/ } keys %$units)) if $units; |
|
0
|
0
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
return print_discovery(@items); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub _service_status |
110
|
|
|
|
|
|
|
{ |
111
|
0
|
|
|
0
|
|
|
my ($name) = map(zbx_decode($_), @ARGV); |
112
|
0
|
0
|
|
|
|
|
return "" unless $name; |
113
|
0
|
|
|
|
|
|
my $name_s = shellmeta($name); |
114
|
0
|
|
|
|
|
|
my $result = ""; |
115
|
0
|
0
|
|
|
|
|
my $line = `$systemctl is-active \"$name_s.service\" 2>/dev/null` if $systemctl; |
116
|
0
|
0
|
|
|
|
|
if ($line) |
117
|
|
|
|
|
|
|
{ |
118
|
0
|
|
|
|
|
|
chomp $line; |
119
|
0
|
|
|
|
|
|
$result = $line; |
120
|
|
|
|
|
|
|
} |
121
|
0
|
|
|
|
|
|
print $result; |
122
|
0
|
|
|
|
|
|
return $result; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
1; |
127
|
|
|
|
|
|
|
__END__ |