aboutsummaryrefslogtreecommitdiff
path: root/examples/inspect
blob: af3d3de76e414875bc20b6e364a78f0d9afb2467 (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
#!/bin/sh
#! -*-perl-*-
eval 'exec perl -x -S $0 ${1+"$@"}'
    if 0;
# This file is part of Ping903
# Copyright (C) 2020 Sergey Poznyakoff
# 
# Ping903 is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
# 
# Ping903 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Ping903.  If not, see <http://www.gnu.org/licenses/>.

=head1 NAME

inspect - inspect the Ping903 daemon configuration

=head1 SYNOPSIS

B<inspect>
[B<-U> I<URL>]
[B<--url>=I<URL>]

=head1 DESCRIPTION

Queries the running B<ping903> instance and displays its configuration on
standard output.  The output is formatted as a valid B<ping903> configuration
file.

=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 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 LWP::Ping903;
use HTTP::Status qw(:constants);
use JSON;
use Getopt::Long qw(:config gnu_getopt no_ignore_case);
use Pod::Usage;
use Pod::Man;

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

GetOptions(
    'U|url=s' => \$baseurl,
    '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);
die "too many arguments; try `$0 --help' for more info\n" if @ARGV;

my $ua = new LWP::Ping903;

my $response = $ua->get("$baseurl/config");
unless ($response->is_success) {
    if ($response->code eq HTTP_UNAUTHORIZED) {
	my $s = $response->header('WWW-Authenticate');
	$s =~ s/Basic realm=//;
	$s =~ s/^"(.*)"$/$1/;
	$s =~ s/\\([\\\"])/$1/g;
	die "$s: not authorized";
    }
    die $response->status_line;
}

my $resp = JSON->new->decode($response->decoded_content);
foreach my $kw (sort keys %$resp) {
    my $val = $resp->{$kw} or next;
    if ($kw eq 'auth') {
	my $delim = '';
	foreach my $acl (@$val) {
	    print "${delim}auth $acl->{type} $acl->{method} $acl->{url}";
	    if (exists($acl->{'passwd-file'})) {
		print " $acl->{'passwd-file'}";
		if (exists($acl->{realm})) {
		    print " $acl->{realm}";
		}
	    }
	    $delim = "\n";
	}
    } else {
	print "$kw ";

	if (ref($val) eq 'ARRAY') {
	    print "<<EOF\n";
	    foreach my $ip (@$val) {
		print "    $ip\n";
	    }
	    print "EOF";
        } elsif (JSON::is_bool($val)) {
	    print $val ? "on" : "off";
        } else {
	    print $val
	}
    }
    print "\n";
}

Return to:

Send suggestions and report system problems to the System administrator.