line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Zabbix::Check::Redis; |
2
|
|
|
|
|
|
|
=head1 NAME |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
Zabbix::Check::Redis - Zabbix check for Redis service |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 VERSION |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
version 1.12 |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 SYNOPSIS |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Zabbix check for Redis service |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
15
|
1
|
|
|
1
|
|
1489
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
46
|
|
16
|
1
|
|
|
1
|
|
8
|
use warnings; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
40
|
|
17
|
1
|
|
|
1
|
|
17
|
use v5.10.1; |
|
1
|
|
|
|
|
6
|
|
18
|
1
|
|
|
1
|
|
8
|
use Time::HiRes; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
12
|
|
19
|
1
|
|
|
1
|
|
115
|
use Lazy::Utils; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
150
|
|
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
1
|
|
9
|
use Zabbix::Check; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
146
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
BEGIN |
25
|
|
|
|
|
|
|
{ |
26
|
1
|
|
|
1
|
|
8
|
require Exporter; |
27
|
1
|
|
|
|
|
3
|
our $VERSION = '1.12'; |
28
|
1
|
|
|
|
|
13
|
our @ISA = qw(Exporter); |
29
|
1
|
|
|
|
|
4
|
our @EXPORT = qw(_installed _discovery _running _info _resptime); |
30
|
1
|
|
|
|
|
1207
|
our @EXPORT_OK = qw(); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
our ($redis_server) = whereis('redis-server'); |
35
|
|
|
|
|
|
|
our ($redis_cli) = whereis('redis-cli'); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub bind_to_redis_cli_args |
39
|
|
|
|
|
|
|
{ |
40
|
0
|
|
|
0
|
0
|
|
my ($bind) = @_; |
41
|
0
|
0
|
|
|
|
|
return "" unless defined($bind); |
42
|
0
|
0
|
|
|
|
|
if ($bind =~ /^([^:]*):(\d*)$/) |
43
|
|
|
|
|
|
|
{ |
44
|
0
|
|
|
|
|
|
my ($host, $port); |
45
|
0
|
|
|
|
|
|
$host = "127.0.0.1"; |
46
|
0
|
0
|
|
|
|
|
$host = shellmeta($1, 1) if $1 ne ""; |
47
|
0
|
|
|
|
|
|
$port = "6379"; |
48
|
0
|
0
|
|
|
|
|
$port = shellmeta($2, 1) if $2 ne ""; |
49
|
0
|
|
|
|
|
|
return "-h $host -p $port"; |
50
|
|
|
|
|
|
|
} |
51
|
0
|
|
|
|
|
|
$bind = shellmeta($bind, 1); |
52
|
0
|
|
|
|
|
|
return "-s $bind"; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub get_info |
56
|
|
|
|
|
|
|
{ |
57
|
0
|
0
|
|
0
|
0
|
|
return unless $redis_cli; |
58
|
0
|
|
|
|
|
|
my ($bind) = @_; |
59
|
0
|
|
|
|
|
|
my $redis_cli_args = bind_to_redis_cli_args($bind); |
60
|
|
|
|
|
|
|
my $result = file_cache("all", 30, sub |
61
|
|
|
|
|
|
|
{ |
62
|
0
|
|
|
0
|
|
|
my $result = { 'epoch' => time() }; |
63
|
0
|
|
|
|
|
|
my $topic; |
64
|
0
|
|
|
|
|
|
for (`$redis_cli $redis_cli_args info 2>/dev/null`) |
65
|
|
|
|
|
|
|
{ |
66
|
0
|
|
|
|
|
|
chomp; |
67
|
0
|
0
|
|
|
|
|
if (/^#(.*)/) |
68
|
|
|
|
|
|
|
{ |
69
|
0
|
|
|
|
|
|
$topic = trim($1); |
70
|
0
|
|
|
|
|
|
next; |
71
|
|
|
|
|
|
|
} |
72
|
0
|
|
|
|
|
|
my ($key, $val) = split(":", $_, 2); |
73
|
0
|
0
|
0
|
|
|
|
next unless defined($topic) and defined($key) and defined($val); |
|
|
|
0
|
|
|
|
|
74
|
0
|
|
|
|
|
|
$key = "$topic:".trim($key); |
75
|
0
|
|
|
|
|
|
$val = trim($val); |
76
|
0
|
|
|
|
|
|
$result->{$key} = $val; |
77
|
|
|
|
|
|
|
} |
78
|
0
|
|
|
|
|
|
return $result; |
79
|
0
|
|
|
|
|
|
}); |
80
|
0
|
|
|
|
|
|
return $result; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub _installed |
84
|
|
|
|
|
|
|
{ |
85
|
0
|
0
|
|
0
|
|
|
my $result = $redis_server? 1: 0; |
86
|
0
|
|
|
|
|
|
print $result; |
87
|
0
|
|
|
|
|
|
return $result; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub _discovery |
91
|
|
|
|
|
|
|
{ |
92
|
0
|
|
|
0
|
|
|
my @items; |
93
|
0
|
|
|
|
|
|
for (`ps -C redis-server -o pid,cmd 2>/dev/null`) |
94
|
|
|
|
|
|
|
{ |
95
|
0
|
|
|
|
|
|
chomp; |
96
|
0
|
0
|
|
|
|
|
if (/^\s*(\d*)\s+\Q$redis_server\E\ (\S+)/) |
97
|
|
|
|
|
|
|
{ |
98
|
0
|
|
|
|
|
|
push @items, { bind => $2 }; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
0
|
|
|
|
|
|
return print_discovery(@items); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub _running |
105
|
|
|
|
|
|
|
{ |
106
|
0
|
|
|
0
|
|
|
my ($bind) = @_; |
107
|
0
|
|
|
|
|
|
my $result = 2; |
108
|
0
|
0
|
|
|
|
|
if ($redis_server) |
109
|
|
|
|
|
|
|
{ |
110
|
0
|
|
|
|
|
|
my $cmd = $redis_server; |
111
|
0
|
0
|
|
|
|
|
$cmd .= " $bind" if defined($bind); |
112
|
0
|
|
|
|
|
|
$cmd = shellmeta($cmd); |
113
|
0
|
|
|
|
|
|
system "pgrep -f \"$cmd\" >/dev/null 2>&1"; |
114
|
0
|
0
|
|
|
|
|
$result = ($? == 0)? 1: 0; |
115
|
|
|
|
|
|
|
} |
116
|
0
|
|
|
|
|
|
print $result; |
117
|
0
|
|
|
|
|
|
return $result; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub _info |
121
|
|
|
|
|
|
|
{ |
122
|
0
|
|
|
0
|
|
|
my ($key, $bind) = map(zbx_decode($_), @ARGV); |
123
|
0
|
0
|
|
|
|
|
return "" unless $key; |
124
|
0
|
|
|
|
|
|
my $result = ""; |
125
|
0
|
|
|
|
|
|
my $info = get_info($bind); |
126
|
0
|
0
|
|
|
|
|
$result = $info->{$key} if defined($info->{$key}); |
127
|
0
|
|
|
|
|
|
print $result; |
128
|
0
|
|
|
|
|
|
return $result; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub _resptime |
132
|
|
|
|
|
|
|
{ |
133
|
0
|
|
|
0
|
|
|
my ($bind) = map(zbx_decode($_), @ARGV); |
134
|
0
|
|
|
|
|
|
my $key = (caller(0))[3]; |
135
|
0
|
|
|
|
|
|
$key =~ s/\Q::\E/-/g; |
136
|
0
|
|
|
|
|
|
$key = shellmeta($key, 1); |
137
|
0
|
|
|
|
|
|
my $redis_cli_args = bind_to_redis_cli_args($bind); |
138
|
0
|
|
|
|
|
|
my $time = Time::HiRes::time(); |
139
|
0
|
|
|
|
|
|
`$redis_cli $redis_cli_args GET $key 2>/dev/null`; |
140
|
0
|
|
|
|
|
|
my $result = Time::HiRes::time()-$time; |
141
|
0
|
|
|
|
|
|
`$redis_cli $redis_cli_args INCR $key 2>/dev/null`; |
142
|
0
|
|
|
|
|
|
print $result; |
143
|
0
|
|
|
|
|
|
return $result; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
1; |
148
|
|
|
|
|
|
|
__END__ |