Check/Disk.pm

filter: Statementstmt 20.2% 29 / 143 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 56.2% 9 / 16 Podpod 0.0% 0 / 3 totaltotal 13.8% 38 / 274 CC 49 SCAR 49.3
CC
49
Cov
12%
analyzeStats
306.0
disks
210.0
_bps
42.0
CRAP 139.0
↑ Summary Check/Systemd.pm »
1 package Zabbix::Check::Disk;
2 =head1 NAME
3  
4 Zabbix::Check::Disk - Zabbix check for disk
5  
6 =head1 VERSION
7  
8 version 1.10
9  
10 =head1 SYNOPSIS
11  
12 Zabbix check for disk
13  
14 UserParameter=cpan.zabbix.check.disk.discovery,/usr/bin/perl -MZabbix::Check::Disk -e_discovery
15 UserParameter=cpan.zabbix.check.disk.bps[*],/usr/bin/perl -MZabbix::Check::Disk -e_bps -- $1 $2
16 UserParameter=cpan.zabbix.check.disk.iops[*],/usr/bin/perl -MZabbix::Check::Disk -e_iops -- $1 $2
17 UserParameter=cpan.zabbix.check.disk.ioutil[*],/usr/bin/perl -MZabbix::Check::Disk -e_ioutil -- $1
18  
19 =head3 discovery
20  
21 discovers disks
22  
23 =head3 bps $1 $2
24  
25 gets disk I/O traffic in bytes per second
26  
27 $1: I<device name, eg: sda, sdb1, dm-3, ...>
28  
29 $2: I<type: read|write|total>
30  
31 =head3 iops $1 $2
32  
33 gets disk I/O transaction speed in transactions per second
34  
35 $1: I<device name, eg: sda, sdb1, dm-3, ...>
36  
37 $2: I<type: read|write|total>
38  
39 =head3 ioutil $1 $2
40  
41 gets disk I/O utilization in percentage
42  
43 $1: I<device name, eg: sda, sdb1, dm-3, ...>
44  
45 =cut
46 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶no warnings qw(qw utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 1 ▶use Zabbix::Check;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56  
57 BEGIN
58 {
59 1 ▶ require Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 # set the version for version checking
61 1 our $VERSION = '1.10';
62 # Inherit from Exporter to export functions and variables
63 1 our @ISA = qw(Exporter);
64 # Functions and variables which are exported by default
65 1 our @EXPORT = qw(_discovery _bps _iops _ioutil);
66 # Functions and variables which can be optionally exported
67 1 our @EXPORT_OK = qw();
68 }
69  
70  
71 sub disks
72 {
73 0 ▶ my $result = {};
Subroutine disks: not called CC=14 SCAR=53.5
Pod undocumented
74 0 for my $blockpath (glob("/sys/dev/block/*"))
75 {
76 0 ▶ next unless -f "$blockpath/uevent";
Branch: 1 branch
BranchTrueFalseTotal
unless -f "$blockpath/uevent" 0 0 0
77 0 my $uevent = read_file("$blockpath/uevent", { err_mode => "quiet" });
78 0 my ($major) = $uevent =~ /^\QMAJOR=\E(.*)/m;
79 0 my ($minor) = $uevent =~ /^\QMINOR=\E(.*)/m;
80 0 my ($devname) = $uevent =~ /^\QDEVNAME=\E(.*)/m;
81 0 my ($devtype) = $uevent =~ /^\QDEVTYPE=\E(.*)/m;
82 0 my $devpath = "/dev/$devname";
83 0 ▶ my $disk = {
Branch: 3 branches
BranchTrueFalseTotal
-f "$blockpath/size" && ($_ = read_file("$blockpath/size", {'err_mode', 'quiet'})) ? : 0 0 0
-f "$blockpath/removable" && ($_ = read_file("$blockpath/removable", {'err_mode', 'quiet'})) ? : 0 0 0
-f "$blockpath/partition" && ($_ = read_file("$blockpath/partition", {'err_mode', 'quiet'})) ? : 0 0 0
Condition0%0/9
-f "$blockpath/size" and $_ = read_file("$blockpath/size", {"err_mode", "quiet"})
!ll&&!rl&&r
000
-f "$blockpath/removable" and $_ = read_file("$blockpath/removable", {"err_mode", "quiet"})
!ll&&!rl&&r
000
-f "$blockpath/partition" and $_ = read_file("$blockpath/partition", {"err_mode", "quiet"})
!ll&&!rl&&r
000
MC/DC0%0/2
-f "$blockpath/size" and $_ = read_file("$blockpath/size", {"err_mode", "quiet"})
-f "$blockpath/size" $_ = read_file("$blockpath/size", {"err_mode", "quiet"})
MC/DC0%0/2
-f "$blockpath/removable" and $_ = read_file("$blockpath/removable", {"err_mode", "quiet"})
-f "$blockpath/removable" $_ = read_file("$blockpath/removable", {"err_mode", "quiet"})
MC/DC0%0/2
-f "$blockpath/partition" and $_ = read_file("$blockpath/partition", {"err_mode", "quiet"})
-f "$blockpath/partition" $_ = read_file("$blockpath/partition", {"err_mode", "quiet"})
Truth table0%0/3
A and B
A= -f "$blockpath/size"B= $_ = read_file("$blockpath/size", {"err_mode", "quiet"})
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= -f "$blockpath/removable"B= $_ = read_file("$blockpath/removable", {"err_mode", "quiet"})
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= -f "$blockpath/partition"B= $_ = read_file("$blockpath/partition", {"err_mode", "quiet"})
ABresult
0X0
100
111
84 blockpath => $blockpath,
85 devname => $devname,
86 devtype => $devtype,
87 devpath => $devpath,
88 major => $major,
89 minor => $minor,
90 size => (-f "$blockpath/size" and $_ = read_file("$blockpath/size", { err_mode => "quiet" }))? int(s/^\s+|\s+$//gr)*512: undef,
91 removable => (-f "$blockpath/removable" and $_ = read_file("$blockpath/removable", { err_mode => "quiet" }))? s/^\s+|\s+$//gr: undef,
92 partition => (-f "$blockpath/partition" and $_ = read_file("$blockpath/partition", { err_mode => "quiet" }))? s/^\s+|\s+$//gr: undef,
93 dmname => undef,
94 dmpath => undef,
95 };
96 0 ▶ if (-f "$blockpath/dm/name" and my $dmname = read_file("$blockpath/dm/name", { err_mode => "quiet" }))
Branch: 1 branch
BranchTrueFalseTotal
if (-f "$blockpath/dm/name" and my $dmname = read_file("$blockpath/dm/name", {"err_mode", "quiet"})) 0 0 0
Condition0%0/3
-f "$blockpath/dm/name" and my $dmname = read_file("$blockpath/dm/name", {"err_mode", "quiet"})
!ll&&!rl&&r
000
MC/DC0%0/2
-f "$blockpath/dm/name" and my $dmname = read_file("$blockpath/dm/name", {"err_mode", "quiet"})
-f "$blockpath/dm/name" my $dmname = read_file("$blockpath/dm/name", {"err_mode", "quiet"})
Truth table0%0/3
A and B
A= -f "$blockpath/dm/name"B= my $dmname = read_file("$blockpath/dm/name", {"err_mode", "quiet"})
ABresult
0X0
100
111
97 {
98 0 chomp $dmname;
99 0 $disk->{dmname} = $dmname;
100 0 $disk->{dmpath} = "/dev/mapper/$dmname";
101 }
102 0 ▶ my $dmpath = $disk->{dmpath}? $disk->{dmpath}: "";
Branch: 1 branch
BranchTrueFalseTotal
$disk->{'dmpath'} ? : 0 0 0
103 0 ▶ for my $mount (grep(/^(\Q$disk->{devpath}\E|\Q$dmpath\E)\s+/, (-f "/proc/mounts")? read_file("/proc/mounts", { err_mode => "quiet" }): ()))
Branch: 1 branch
BranchTrueFalseTotal
-f '/proc/mounts' ? : 0 0 0
104 {
105 0 chomp $mount;
106 0 my ($devpath, $mountpoint, $fstype) = $mount =~ /^(\S+)\s+(\S+)\s+(\S+)\s+/;
107 0 $disk->{fstype} = $fstype;
108 }
109 0 $result->{$devname} = $disk;
110 }
111 0 return $result;
112 }
113  
114 sub stats
115 {
116 0 ▶ my $result = {};
Subroutine stats: not called CC=4 SCAR=30.0
Pod undocumented
117 0 my $disks = disks();
118 0 for my $devname (keys %$disks)
119 {
120 0 my $disk = $disks->{$devname};
121 0 ▶ next unless -f "$disk->{blockpath}/stat";
Branch: 1 branch
BranchTrueFalseTotal
unless -f qq[$disk->{"blockpath"}/stat] 0 0 0
122 0 my $statLine = read_file("$disk->{blockpath}/stat", { err_mode => "quiet" });
123 0 ▶ next unless $statLine;
Branch: 1 branch
BranchTrueFalseTotal
unless $statLine 0 0 0
124 0 chomp $statLine;
125 0 my $stat = { 'epoch' => time() };
126 (
127 $stat->{readIOs},
128 $stat->{readsMerges},
129 $stat->{readSectors},
130 $stat->{readWaits},
131 $stat->{writeIOs},
132 $stat->{writesMerges},
133 $stat->{writeSectors},
134 $stat->{writeWaits},
135 $stat->{inFlight},
136 $stat->{IOTicks},
137 $stat->{totalWaits},
138 0 ) = $statLine =~ /^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/;
139 0 $result->{$devname} = $stat;
140 }
141 0 return $result;
142 }
143  
144 sub analyzeStats
145 {
146 0 ▶ my $now = time();
Subroutine analyzeStats: not called CC=17 SCAR=57.2
Pod undocumented
147 0 my $stats;
148 my $oldStats;
149 0 my $tmpPrefix = "/tmp/".(caller(0))[3] =~ s/\Q::\E/-/gr.",stats,";
150 0 for my $tmpPath (sort {$b cmp $a} glob("$tmpPrefix*"))
151 {
152 0 ▶ if (my ($epoch, $pid) = $tmpPath =~ /^\Q$tmpPrefix\E(\d*)\.(\d*)/)
Branch: 1 branch
BranchTrueFalseTotal
if (() = $tmpPath =~ /^\Q$tmpPrefix\E(\d*)\.(\d*)/u) 0 0 0
153 {
154 0 ▶ if ($now-$epoch < 1*60)
Branch: 1 branch
BranchTrueFalseTotal
if ($now - $epoch < 60) 0 0 0
155 {
156 0 ▶ if (not $stats)
Branch: 1 branch
BranchTrueFalseTotal
unless ($stats) 0 0 0
157 {
158 0 my $tmp = read_file($tmpPath, { err_mode => "quiet" });
159 0 ▶ eval { $stats = from_json($tmp) } if $tmp;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp 0 0 0
160 }
161 0 next;
162 }
163 0 ▶ if (not $oldStats)
Branch: 1 branch
BranchTrueFalseTotal
unless ($oldStats) 0 0 0
164 {
165 0 my $tmp = read_file($tmpPath, { err_mode => "quiet" });
166 0 ▶ eval { $oldStats = from_json($tmp) } if $tmp;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp 0 0 0
167 0 ▶ next unless not $tmp or $@;
Branch: 1 branch
BranchTrueFalseTotal
unless not $tmp or $@ 0 0 0
Condition0%0/3
not $tmp or $@
l!l&&r!l&&!r
000
MC/DC0%0/2
not $tmp or $@
not $tmp $@
Truth table0%0/3
not(A) or B
A= not $tmpB= $@
ABresult
1X1
011
000
168 }
169 0 ▶ next unless $now-$epoch > 2*60;
Branch: 1 branch
BranchTrueFalseTotal
unless $now - $epoch > 120 0 0 0
170 }
171 0 unlink($tmpPath);
172 }
173 0 ▶ unless ($stats)
Branch: 1 branch
BranchTrueFalseTotal
unless ($stats) 0 0 0
174 {
175 0 $stats = stats();
176 0 my $tmp;
177 0 eval { $tmp = to_json($stats, {pretty => 1}) };
178 0 ▶ write_file("$tmpPrefix$now.$$", { err_mode => "quiet" }, $tmp) if $tmp;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp 0 0 0
179 }
180 0 ▶ return unless $oldStats;
Branch: 1 branch
BranchTrueFalseTotal
unless $oldStats 0 0 0
181 0 my $result = {};
182 0 for my $devname (keys %$stats)
183 {
184 0 my $stat = $stats->{$devname};
185 0 my $oldStat = $oldStats->{$devname};
186 0 ▶ next unless defined $oldStat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $oldStat 0 0 0
187 0 my $diff = $stat->{epoch} - $oldStat->{epoch};
188 0 ▶ next unless $diff;
Branch: 1 branch
BranchTrueFalseTotal
unless $diff 0 0 0
189 0 $result->{$devname} = {};
190  
191 0 my $sector;
192 my $io;
193  
194 0 $sector = $stat->{readSectors} - $oldStat->{readSectors};
195 0 $io = $stat->{readIOs} - $oldStat->{readIOs};
196 0 $result->{$devname}->{bps_read} = 512*$sector/$diff;
197 0 $result->{$devname}->{iops_read} = $io/$diff;
198  
199 0 $sector = $stat->{writeSectors} - $oldStat->{writeSectors};
200 0 $io = $stat->{writeIOs} - $oldStat->{writeIOs};
201 0 $result->{$devname}->{bps_write} = 512*$sector/$diff;
202 0 $result->{$devname}->{iops_write} = $io/$diff;
203  
204 0 $sector = $stat->{readSectors} - $oldStat->{readSectors} + $stat->{writeSectors} - $oldStat->{writeSectors};
205 0 $io = $stat->{readIOs} - $oldStat->{readIOs} + $stat->{writeIOs} - $oldStat->{writeIOs};
206 0 $result->{$devname}->{bps_total} = 512*$sector/$diff;
207 0 $result->{$devname}->{iops_total} = $io/$diff;
208  
209 0 $result->{$devname}->{ioutil} = 100*($stat->{IOTicks} - $oldStat->{IOTicks})/(1000*$diff);
210 }
211 0 return $result;
212 }
213  
214 sub _discovery
215 {
216 0 ▶ my ($removable) = @_;
Subroutine _discovery: not called CC=4 SCAR=30.0
217 0 my @items;
218 0 my $disks = disks();
219 0 for my $devname (keys %$disks)
220 {
221 0 my $disk = $disks->{$devname};
222 0 ▶ next if not $removable and $disk->{removable};
Branch: 1 branch
BranchTrueFalseTotal
if not $removable and $disk->{"removable"} 0 0 0
Condition0%0/3
not $removable and $disk->{"removable"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $removable and $disk->{"removable"}
not $removable $disk->{"removable"}
Truth table0%0/3
not(A) and B
A= not $removableB= $disk->{"removable"}
ABresult
0X0
100
111
223 0 push @items, $disk;
224 }
225 0 return printDiscovery(@items);
226 }
227  
228 sub _bps
229 {
230 0 ▶ my ($devname, $type) = map(zbxDecode($_), @ARGV);
Subroutine _bps: not called CC=6 SCAR=37.4
231 0 ▶ return unless $devname and $type and $type =~ /^read|write|total$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $devname and $type and $type =~ /^read|write|total$/u 0 0 0
Condition0%0/6
$devname and $type and $type =~ /^read|write|total$/u
!ll&&!rl&&r
000
$devname and $type
!ll&&!rl&&r
000
MC/DC0%0/3
$devname and $type and $type =~ /^read|write|total$/u
$devname $type $type =~ /^read|write|total$/u
Truth table0%0/4
A and B and C
A= $devnameB= $typeC= $type =~ /^read|write|total$/u
ABCresult
0XX0
10X0
1100
1111
232 0 my $result = 0;
233 0 my $analyzed = analyzeStats();
234 0 ▶ my $status = $analyzed->{$devname} if $analyzed;
Branch: 1 branch
BranchTrueFalseTotal
if $analyzed 0 0 0
235 0 ▶ $result = sprintf("%.2f", $status->{"bps_$type"}) if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 0 0 0
236 0 print $result;
237 0 return $result;
238 }
239  
240 sub _iops
241 {
242 0 ▶ my ($devname, $type) = map(zbxDecode($_), @ARGV);
Subroutine _iops: not called CC=6 SCAR=37.4
243 0 ▶ return unless $devname and $type and $type =~ /^read|write|total$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $devname and $type and $type =~ /^read|write|total$/u 0 0 0
Condition0%0/6
$devname and $type and $type =~ /^read|write|total$/u
!ll&&!rl&&r
000
$devname and $type
!ll&&!rl&&r
000
MC/DC0%0/3
$devname and $type and $type =~ /^read|write|total$/u
$devname $type $type =~ /^read|write|total$/u
Truth table0%0/4
A and B and C
A= $devnameB= $typeC= $type =~ /^read|write|total$/u
ABCresult
0XX0
10X0
1100
1111
244 0 my $result = 0;
245 0 my $analyzed = analyzeStats();
246 0 ▶ my $status = $analyzed->{$devname} if $analyzed;
Branch: 1 branch
BranchTrueFalseTotal
if $analyzed 0 0 0
247 0 ▶ $result = sprintf("%.2f", $status->{"iops_$type"}) if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 0 0 0
248 0 print $result;
249 0 return $result;
250 }
251  
252 sub _ioutil
253 {
254 0 ▶ my ($devname) = map(zbxDecode($_), @ARGV);
Subroutine _ioutil: not called CC=4 SCAR=30.0
255 0 ▶ return unless $devname;
Branch: 1 branch
BranchTrueFalseTotal
unless $devname 0 0 0
256 0 my $result = 0;
257 0 my $analyzed = analyzeStats();
258 0 ▶ my $status = $analyzed->{$devname} if $analyzed;
Branch: 1 branch
BranchTrueFalseTotal
if $analyzed 0 0 0
259 0 ▶ $result = sprintf("%.2f", $status->{"ioutil"}) if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 0 0 0
260 0 print $result;
261 0 return $result;
262 }
263  
264  
265 1 1;
266 __END__
↑ Summary Check/Systemd.pm »