File Coverage

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