File Coverage

blib/lib/Uninets/Check/Modules/Redis.pm
Criterion Covered Total %
statement 24 53 45.2
branch 0 2 0.0
condition n/a
subroutine 8 17 47.0
pod 2 4 50.0
total 34 76 44.7


line stmt bran cond sub pod time code
1             package Uninets::Check::Modules::Redis;
2              
3 1     1   20661 use 5.10.1;
  1         4  
  1         43  
4 1     1   5 use strict;
  1         2  
  1         35  
5 1     1   5 use warnings FATAL => 'all';
  1         10  
  1         39  
6 1     1   980 use Moo;
  1         16134  
  1         7  
7 1     1   2798 use Getopt::Long qw(GetOptionsFromArray);
  1         14496  
  1         7  
8 1     1   1096 use Redis;
  1         75790  
  1         38  
9 1     1   12 use Try::Tiny;
  1         2  
  1         70  
10 1     1   1098 use JSON;
  1         15023  
  1         7  
11              
12             =head1 NAME
13              
14             Uninets::Check::Modules::Redis - Uninets::Check module to check redis servers.
15              
16             =head1 VERSION
17              
18             Version 0.01
19              
20             =cut
21              
22             our $VERSION = '0.02';
23              
24              
25             =head1 SYNOPSIS
26              
27             Uninets::Check::Modules::Redis can check used memory and reachability of redis servers.
28              
29             # to show available information on parameters run
30             unicheck --info Redis
31              
32             =cut
33              
34             sub run {
35 0     0 0   my ($self, $action, @params) = @_;
36              
37 0           $self->$action(@params);
38             }
39              
40             =head1 ACTIONS
41              
42             =head2 reachable
43              
44             Check if the server is reachable.
45              
46             # check default localhost:6379
47             unicheck Redis reachable
48              
49             # check specific host:port
50             unicheck Redis reachable --host example.com --port 1234
51              
52             =cut
53              
54             sub reachable {
55 0     0 1   my ($self, @params) = @_;
56              
57 0           my $host = '127.0.0.1';
58 0           my $port = 6379;
59 0           my $format = 'num';
60              
61 0           GetOptionsFromArray([@params],
62             'port=i' => \$port,
63             'host=s' => \$host,
64             'format=s' => \$format,
65             );
66              
67 0           my $retval;
68             try {
69 0     0     Redis->new(server => "$host:$port");
70 0           $retval = $self->_return(1, 'Connection successful', $format);
71             } catch {
72 0     0     $retval = $self->_return(0, $_, $format);
73 0           };
74              
75 0           $retval;
76             }
77              
78             =head2 size
79              
80             Get the size redis uses in memory.
81              
82             unicheck Redis size
83              
84             =cut
85              
86             sub size {
87 0     0 1   my ($self, @params) = @_;
88              
89 0           my $host = '127.0.0.1';
90 0           my $port = 6379;
91 0           my $format = 'num';
92              
93 0           GetOptionsFromArray([@params],
94             'port=i' => \$port,
95             'host=s' => \$host,
96             'format=s' => \$format,
97             );
98              
99 0           my $size = 0;
100 0           my $retval;
101             try {
102 0     0     $size = Redis->new(server => "$host:$port")->info->{used_memory};
103 0           $retval = $self->_return($size, $size, $format);
104             } catch {
105 0     0     $retval = $self->_return(-1, $_, $format);
106 0           };
107              
108 0           $retval;
109             }
110              
111             sub _return {
112 0     0     my ($self, $status, $value, $format) = @_;
113              
114 0 0         return JSON->new->encode(
115             {
116             message => $value,
117             status => $status,
118             }
119             ) if $format eq 'json';
120             # default last in case some non supported format was given
121 0           return $status; # if $format eq 'num'
122             }
123              
124             sub help {
125             {
126 0     0 0   description => 'Check redis status',
127             actions => {
128             reachable => {
129             description => 'Check if redis server is reachable',
130             params => {
131             '--host' => 'Default: 127.0.0.1',
132             '--port' => 'Default: 6379',
133             '--format' => 'Default: num',
134             },
135             formats => {
136             'num' => 'Returns the status code',
137             'json' => 'Returns a JSON structure',
138             },
139             },
140             size => {
141             description => 'Check redis RAM consumption',
142             params => {
143             '--host' => 'Default: 127.0.0.1',
144             '--port' => 'Default: 6379',
145             '--format' => 'Default: num',
146             },
147             formats => {
148             'num' => 'Returns the size in bytes',
149             'json' => 'Returns a JSON structure',
150             },
151             },
152             },
153             }
154             }
155              
156             =head1 AUTHOR
157              
158             Matthias Krull, C<< <> >>
159              
160             =head1 BUGS
161              
162             Please report any bugs or feature requests to C, or through
163             the web interface at L. I will be notified, and then you'll
164             automatically be notified of progress on your bug as I make changes.
165              
166              
167              
168              
169             =head1 SUPPORT
170              
171             You can find documentation for this module with the perldoc command.
172              
173             perldoc Uninets::Check::Modules::Redis
174              
175              
176             You can also look for information at:
177              
178             =over 4
179              
180             =item * RT: CPAN's request tracker (report bugs here)
181              
182             L
183              
184             =item * AnnoCPAN: Annotated CPAN documentation
185              
186             L
187              
188             =item * CPAN Ratings
189              
190             L
191              
192             =item * Search CPAN
193              
194             L
195              
196             =back
197              
198              
199             =head1 ACKNOWLEDGEMENTS
200              
201              
202             =head1 LICENSE AND COPYRIGHT
203              
204             Copyright 2013 Matthias Krull.
205              
206             This program is free software; you can redistribute it and/or modify it
207             under the terms of the the Artistic License (2.0). You may obtain a
208             copy of the full license at:
209              
210             L
211              
212             Any use, modification, and distribution of the Standard or Modified
213             Versions is governed by this Artistic License. By using, modifying or
214             distributing the Package, you accept this license. Do not use, modify,
215             or distribute the Package, if you do not accept this license.
216              
217             If your Modified Version has been derived from a Modified Version made
218             by someone other than you, you are nevertheless required to ensure that
219             your Modified Version complies with the requirements of this license.
220              
221             This license does not grant you the right to use any trademark, service
222             mark, tradename, or logo of the Copyright Holder.
223              
224             This license includes the non-exclusive, worldwide, free-of-charge
225             patent license to make, have made, use, offer to sell, sell, import and
226             otherwise transfer the Package with respect to any patent claims
227             licensable by the Copyright Holder that are necessarily infringed by the
228             Package. If you institute patent litigation (including a cross-claim or
229             counterclaim) against any party alleging that the Package constitutes
230             direct or contributory patent infringement, then this Artistic License
231             to you shall terminate on the date that such litigation is filed.
232              
233             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
234             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
235             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
236             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
237             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
238             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
239             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
240             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
241              
242              
243             =cut
244              
245             1; # End of Uninets::Check::Modules::Redis