File Coverage

blib/lib/App/Unicheck/Modules/MySQL.pm
Criterion Covered Total %
statement 24 45 53.3
branch 0 2 0.0
condition n/a
subroutine 8 14 57.1
pod 1 3 33.3
total 33 64 51.5


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