aboutsummaryrefslogtreecommitdiff
path: root/examples/dbload
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2020-02-25 10:15:51 +0200
committerSergey Poznyakoff <gray@gnu.org>2020-02-25 10:15:51 +0200
commite13c04e4a09b5c5ea34dd65f3a4f9cdaadd47079 (patch)
tree8bc8f57434aebfccab24fd4b185bb1c361c0fabc /examples/dbload
parent8ac571387454ab8c728402ccf7ebd1f166204fe6 (diff)
downloadping903-e13c04e4a09b5c5ea34dd65f3a4f9cdaadd47079.tar.gz
ping903-e13c04e4a09b5c5ea34dd65f3a4f9cdaadd47079.tar.bz2
Add example interface scripts
* examples/dbload: New file. * examples/inspect: New file. * examples/ipadd: New file. * examples/ipdel: New file.
Diffstat (limited to 'examples/dbload')
-rwxr-xr-xexamples/dbload262
1 files changed, 262 insertions, 0 deletions
diff --git a/examples/dbload b/examples/dbload
new file mode 100755
index 0000000..6b776e4
--- /dev/null
+++ b/examples/dbload
@@ -0,0 +1,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::UserAgent;
+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::UserAgent;
+
+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.