File Coverage

blib/lib/Uninets/Check/Modules/MongoDB.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package Uninets::Check::Modules::MongoDB;
2              
3 1     1   35258 use 5.10.0;
  1         4  
  1         59  
4 1     1   7 use strict;
  1         2  
  1         40  
5 1     1   6 use warnings FATAL => 'all';
  1         13  
  1         47  
6 1     1   1942 use Moo;
  1         19789  
  1         7  
7 1     1   5554 use Getopt::Long qw(GetOptionsFromArray);
  1         14553  
  1         7  
8 1     1   8997 use Try::Tiny;
  1         1141  
  1         52  
9 1     1   432 use MongoDB;
  0            
  0            
10             use JSON;
11              
12             =head1 NAME
13              
14             Uninets::Check::Modules::MongoDB - Uninets::Check module to check mongodb 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::MongoDB can check mongod reachability.
28              
29             # to show available information on parameters run
30             unicheck --info MongoDB
31              
32             =cut
33              
34             sub run {
35             my ($self, $action, @params) = @_;
36              
37             $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:27017
47             unicheck MongoDB reachable
48              
49             # check specific host:port
50             unicheck MongoDB reachable --host example.com --port 1234
51              
52             =cut
53              
54             sub reachable {
55             my ($self, @params) = @_;
56              
57             my $host = 'localhost';
58             my $port = 27017;
59             my $format = 'num';
60              
61             GetOptionsFromArray([@params],
62             'port=i' => \$port,
63             'host=s' => \$host,
64             'format=s' => \$format,
65             );
66              
67             my $retval;
68             try {
69             MongoDB::MongoClient->new(host => $host, port => $port);
70             $retval = $self->_return(1, 'Connection successful', $format);
71             } catch {
72             $retval = $self->_return(0, $_, $format);
73             };
74              
75             $retval;
76             }
77              
78             sub _return {
79             my ($self, $status, $value, $format) = @_;
80              
81             return JSON->new->encode(
82             {
83             message => $value,
84             status => $status,
85             }
86             ) if $format eq 'json';
87             # default last in case some non supported format was given
88             return $status; # if $format eq 'num'
89             }
90              
91             sub help {
92             {
93             description => 'Check mongoDB status',
94             actions => {
95             reachable => {
96             description => 'Check if mongodb server is reachable',
97             params => {
98             '--host' => 'Default: localhost',
99             '--port' => 'Default: 27017',
100             '--format' => 'Default: num'
101             },
102             formats => {
103             'num' => 'Returns the status code',
104             'json' => 'Returns a JSON structure',
105             },
106             },
107             },
108             }
109             }
110              
111             =head1 AUTHOR
112              
113             Matthias Krull, C<< <<m.krull at uninets.eu>> >>
114              
115             =head1 BUGS
116              
117             Please report any bugs or feature requests to C<bug-uninets-check-modules-mongodb at rt.cpan.org>, or through
118             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Uninets-Check-Modules-MongoDB>. I will be notified, and then you'll
119             automatically be notified of progress on your bug as I make changes.
120              
121              
122              
123              
124             =head1 SUPPORT
125              
126             You can find documentation for this module with the perldoc command.
127              
128             perldoc Uninets::Check::Modules::MongoDB
129              
130              
131             You can also look for information at:
132              
133             =over 4
134              
135             =item * RT: CPAN's request tracker (report bugs here)
136              
137             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Uninets-Check-Modules-MongoDB>
138              
139             =item * AnnoCPAN: Annotated CPAN documentation
140              
141             L<http://annocpan.org/dist/Uninets-Check-Modules-MongoDB>
142              
143             =item * CPAN Ratings
144              
145             L<http://cpanratings.perl.org/d/Uninets-Check-Modules-MongoDB>
146              
147             =item * Search CPAN
148              
149             L<http://search.cpan.org/dist/Uninets-Check-Modules-MongoDB/>
150              
151             =back
152              
153              
154             =head1 ACKNOWLEDGEMENTS
155              
156              
157             =head1 LICENSE AND COPYRIGHT
158              
159             Copyright 2013 Matthias Krull.
160              
161             This program is free software; you can redistribute it and/or modify it
162             under the terms of the the Artistic License (2.0). You may obtain a
163             copy of the full license at:
164              
165             L<http://www.perlfoundation.org/artistic_license_2_0>
166              
167             Any use, modification, and distribution of the Standard or Modified
168             Versions is governed by this Artistic License. By using, modifying or
169             distributing the Package, you accept this license. Do not use, modify,
170             or distribute the Package, if you do not accept this license.
171              
172             If your Modified Version has been derived from a Modified Version made
173             by someone other than you, you are nevertheless required to ensure that
174             your Modified Version complies with the requirements of this license.
175              
176             This license does not grant you the right to use any trademark, service
177             mark, tradename, or logo of the Copyright Holder.
178              
179             This license includes the non-exclusive, worldwide, free-of-charge
180             patent license to make, have made, use, offer to sell, sell, import and
181             otherwise transfer the Package with respect to any patent claims
182             licensable by the Copyright Holder that are necessarily infringed by the
183             Package. If you institute patent litigation (including a cross-claim or
184             counterclaim) against any party alleging that the Package constitutes
185             direct or contributory patent infringement, then this Artistic License
186             to you shall terminate on the date that such litigation is filed.
187              
188             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
189             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
190             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
191             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
192             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
193             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
194             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
195             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
196              
197              
198             =cut
199              
200             1; # End of Uninets::Check::Modules::MongoDB