aboutsummaryrefslogtreecommitdiff
path: root/examples/dbload
blob: f3e9ec204dbeac042ed4736f5ad94a11aab4071d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/bin/sh
#! -*-perl-*-
eval 'exec perl -x -S $0 ${1+"$@"}'
    if 0;

=head1 NAME

dbload - Load IP addresses from database into Ping903

=head1 SYNOPSIS

B<dbload>
[B<-D> I<DRIVER>]
[B<-P> I<DBPORT>]
[B<-U> I<URL>]
[B<-c> I<COLUMN>]
[B<-d> I<DBNAME>]
[B<-h> I<DBHOST>]
[B<-p> I<DBPASS>]
[B<-q> I<QUERY>]
[B<-t> I<TABLE>]
[B<-u> I<DBUSER>]
[B<--column>=I<COLUMN>]
[B<--database>=I<DBNAME>]
[B<--defaults-file>=I<FILE>]
[B<--driver>=I<DRIVER>]
[B<--host>=I<DBHOST>]
[B<--params>=I<DBPARAM>]
[B<--password>=I<DBPASS>]
[B<--port>=I<DBPORT>]
[B<--query>=I<QUERY>]
[B<--table>=I<TABLE>]
[B<--url>=I<URL>]
[B<--user>=I<DBUSER>]

=head1 DESCRIPTION

Reads IP addresses from the database and adds them to the list of monitored
hosts of L<ping903>.

Most parameters are configurable.  You need to supply at least the database
connection information and the query to use.  For the latter, use the
B<--table> and B<--column> options together.  For really complex queries,
use the B<--query> option instead.  See below for details.

On success, prints on standard output the number of IP addresses loaded and
exits with status 0.  On error, displays on stabdard error the detailed 
diagnostic information as obtained from the server and exits with status 1.

=head1 OPTIONS

=head2 General options

=over 4

=item B<-U>, B<--url>=I<URL>

URL of the running L<ping903> daemon.  Default is C<http://localhost:8080>.

=back

=head2 Database connection options

=over 4

=item B<-D>, B<--driver>=I<DRIVER>

Sets L<DBI> database driver name.  Default is C<mysql>.

=item B<-h>, B<--host>=I<NAME>

Name or IP address of the database server.

=item B<-P>, B<--port>=I<PORT>

Port number the database server listens on.

=item B<-d>, B<--database>=I<NAME>

Name of the database to use.

=item B<-u>, B<--user>=I<USER>

Database user name.

=item B<-p>, B<--password>=I<PASS>

Database password.

=item B<--defaults-file>=I<FILE>

Name of the MySQL defaults file to use.  This option is used only if DBI
driver B<mysql> is used.  By default, the file F<.my.cnf> in the home
directory is used, if it exists.

=item B<--params>=I<STRING>

Additional parameters for the DBI driver.  For detailed information, refer
to the documentation of the driver in use.

=back

=head2 Query to extract IP addresses from the database

Three options are provided:

=over 4

=item B<-t>, B<--table>=I<TABLE>

Name of the table which holds IP addresses.

=item B<-c>, B<--column>=I<NAME>

Name of the column in I<TABLE> where IP address is stored.  If need
be, you can use SQL expression as well.

=item B<-q>, B<--query>=I<STRING>

SQL query to use in order to obtain IP addresses.  This overrides the two
options above.

=back

Normally you would use a combination of B<--table> and B<--column> options.
For example, if you have a database table C<hosts>, which has a column C<ip>,
that holds IP address in dotted-quad form, you would use the following options:

    --table=hosts --column=ip

If the table holds IP addresses in numeric form, use the following instead
(MySQL is assumed):

    --table=hosts --column='INET_NTOA(ip)'

Finally, if a complex query is needed to extract IP addresses (e.g. joining
several tables, etc.), supply it in full, via the B<--query> option.

=head2 Informative options

=over 4

=item B<-?>

Display short help summary.

=item B<--usage>

Display command line usage summary.

=item B<--help>

Display a detailed program manual.

=back

=head1 SEE ALSO

L<ping903>,
L<DBI>.

=cut
    
use strict;
use warnings;
use JSON;
use LWP::Ping903;
use DBI;
use Getopt::Long qw(:config gnu_getopt no_ignore_case);
use File::Spec;
use Pod::Usage;
use Pod::Man;

my $baseurl = 'http://localhost:8080';

my $driver = 'mysql';
my @connarg;
my $dbuser;
my $dbpass;
my $dbparams;
my $deffile;
my $table = 'hosts';
my $column = 'ip';
my $query;

GetOptions(
    'U|url=s' => \$baseurl,
    'D|driver=s' => \$driver,
    'd|database=s' => sub { push @connarg, "database=$_[1]" },
    'h|host=s' => sub { push @connarg, "host=$_[1]" },
    'P|port=s' => sub { push @connarg, "port=$_[1]" },
    'u|user=s' => \$dbuser,
    'p|password=s' => \$dbpass,
    'params=s' => \$dbparams,
    'defaults-file=s' => \$deffile,
    't|table=s' => \$table,
    'c|column=s' => \$column,
    'q|query=s' => \$query,
    'help' => sub {
	pod2usage(-exitstatus => 0, -verbose => 2);
    },
    'usage' => sub {
	pod2usage(-exitstatus => 0, -verbose => 0);
    },
    'hh|?' => sub {
	pod2usage(-message => "dbload - load IP addresses to ping903",
		  -exitstatus => 0);
    },
) or exit(1);

unless ($query) {
    $query = qq{SELECT $column FROM $table};
}

push @connarg, $dbparams if $dbparams;
if ($driver eq 'mysql') {
    if (!$deffile) {
	my $f = File::Spec->catfile($ENV{HOME}, '.my.cnf');
	if (-f $f) {
	    $deffile = $f;
        }
    }
    push @connarg, ";mysql_read_default_file=$deffile";
}

my $arg = join(':', ('DBI',$driver,@connarg));

my $dbh = DBI->connect($arg, $dbuser, $dbpass,
		       { RaiseError => 0, PrintError => 1, AutoCommit => 1})
    or pod2usage(-output => \*STDERR, -exitstatus => 1, -verbose => 0);

my $res = $dbh->selectall_arrayref($query,
				   { RaiseError => 0, PrintError => 1 })
    or pod2usage(-output => \*STDERR, -exitstatus => 1, -verbose => 0);

my $json_text = JSON->new->encode({
	'mode' => 'replace',	
	'ip-list' => [ map { $_->[0] } @$res ]
  });

my $ua = new LWP::Ping903;

my $response = $ua->post("$baseurl/config/ip-list",
			 'Content-Type' => 'application/json',
			 'Content' => $json_text);

if ($response->is_success) {
    my $n = @$res;
    print "Loaded $n addresses\n";
} else {
    print STDERR $response->status_line,"\n";
    my $ctype = $response->header('Content-Type');
    if ($ctype && $ctype eq 'application/json') {
	my $expl = JSON->new->decode($response->decoded_content);
	if (exists($expl->{index})) {
	    print STDERR "Item $expl->{index}: ";
	}
	print STDERR "$expl->{message}\n";
    }
    exit(1);
}

Return to:

Send suggestions and report system problems to the System administrator.