summaryrefslogtreecommitdiffabout
path: root/lib/App/Mangemanche.pm
blob: 4d4379635622490f85d068a35bced1c5b81396ca (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package App::Mangemanche;
use strict;
use warnings;
use Getopt::Long qw(:config gnu_getopt no_ignore_case require_order);
use Pod::Man;
use Pod::Usage;
use Pod::Find qw(pod_where);
use Net::Ping903;
use App::Mangemanche::Command ':exit_codes';
use File::Basename;

our $VERSION = '0.4.90';

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

my $LICENSE = <<'EOT'
Copyright (C) 2020 Sergey Poznyakoff

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
EOT
;    
    
sub new {
    my $class = shift;
    my $config_file;
    my $url;
    my $config = {};

    GetOptions(
	'shorthelp|?' => sub {
	    pod2usage(-input => pod_where({-inc => 1}, $class),
		      -verbose => 99,
		      -sections => [qw(NAME SYNOPSIS COMMANDS)],
		      -exitstatus => 'NOEXIT');
	    $class->list_commands;
	    exit(EX_OK);
	},
	'help' => sub {
	    App::Mangemanche::Command::mandoc($class, basename($0)),
	},
	'usage' => sub {
	    pod2usage(-exitstatus => EX_OK,
		      -input => pod_where({-inc => 1}, $class),
		      -verbose => 0)
	},
	'version' => sub {
	    print "$0 version $VERSION\n";
	    print $LICENSE,"\n";
	    exit(EX_OK);
	},
	'config|c=s' => \$config_file,
	'url|u=s' => \$url
    ) or exit(EX_USAGE);

    if ($config_file) {
	if (-f $config_file) {
	    $config = $class->readconfig($config_file);
	} else {
	    die "configuration file $config_file does not exists\n";
	}
    } else {
	$config_file = '/etc/ping903.conf';
	if (-f $config_file) {
	    $config = $class->readconfig($config_file);
	}
    }

    if ($url) {
	$config->{baseurl} = $url;
    } elsif (!$config->{baseurl}) {
	$config->{baseurl} = $DEFAULT_URL;
    }

    unless ($config->{baseurl} =~ m{^https?://}) {
	$config->{baseurl} = "http://$url";
    }

    my $agent = new Net::Ping903($config->{baseurl});

    my $com = shift @ARGV;
    die "no command name\n" unless $com;

    my $modname = __PACKAGE__ . '::Command::' . $com;
    my $modpath = $modname;
    $modpath =~ s{::}{/}g;
    $modpath .= '.pm';
    my $cmd;
    eval {
	require $modpath;
	$cmd = $modname->new($com, $agent);
    };
    if ($@) {
	if ($@ =~ /Can't locate $modpath/) {
	    die "unknown command: $com\n"
	}
	die $@;
    }
    return $cmd;
}

sub readconfig {
    my ($class, $file) = @_;
    my $config = {};
    if (open(my $fh, '<', $file)) {
	while (<$fh>) {
	    chomp;
	    s/^\s+//;
	    s/\s+$//;
	    next if /^(#.*)?$/;
	    if (m{^listen\s+(.+)$}) {
		$config->{baseurl} = $1;
		last;
	    }
	}
	close $fh;
    } else {
	die "$file: file doesn't exist\n";
    }
    return $config;
}

sub list_commands {
    my $class = shift;
    my @classpath = (split(/::/, $class), 'Command');

    print "\nAvailable commands are:\n";
    foreach my $mod (sort
		     map {
			 my $name = basename($_);
			 if (exists($INC{File::Spec->catfile(@classpath,$name)})) {
			     ()
			 } else {
			     eval {
				     require $_;
			     };
			     $name =~ s/\.pm$//;
			     $@ ? () : [$name, $_];
			}
		     }
		     map {
			   glob File::Spec->catfile($_, @classpath, '*.pm')
		     } @INC) {

	my $s;
	open(my $fh, '>', \$s);
	pod2usage(-input => $mod->[1],
		  -output => $fh,
		      -verbose => 99,
		      -sections => ['NAME'],
		      -exitstatus => 'NOEXIT');
	close $fh;
	my (undef, $descr) = split("\n", $s||'');
	unless ($descr) {
	    $descr = '    ' . $mod->[0]
	}
	print "$descr\n";
    }
}

1;

=head1 NAME

mangemanche - remote management interface to B<ping903> daemon.

=head1 SYNOPSIS

B<mangemanche>
[B<-c> I<FILE>]
[B<-u> I<FILE>]
[B<--config=>I<FILE>]
[B<--url=>I<FILE>]
I<COMMAND>
I<ARG>...

B<mangemanche>
[B<-?>]
[B<--help>]
[B<--usage>]
[B<--version>]

=head1 DESCRIPTION

Mangemanche provides remote management facilities for the running B<ping903>
daemon.  First non-optional argument specifies the name of the I<command> to
run.  Arguments following it supply options and arguments to that command.

Mangemanche reads the IP address and port number of the B<ping903> management
socket from its configuration file, F</etc/ping903.conf>.  If the file does
not exist, the default port (B<localhost:8080>) is used.  The location of
both can be overridden in the command line.

If the server requests HTTP authorization, the program will look up the
authorization credentials in the B<.ping903.cred> file located in the
home directory.  Refer to L<ping903.cred>(5) for a detailed description.

=head1 OPTIONS

=over 4

=item B<-c>, B<--config=>I<FILE>

Specifies alternative configuration file to use instead of
F</etc/ping903.conf>.

=item B<-u>, B<--url=>I<FILE>

Sets the URL of the B<ping903> management socket.

=back

=head2 Informative options

Each of these options causes the program to display a particular piece of
information and exit with the zero status.  Any other options and arguments
are ignored.

=over 4

=item B<-?>

Displays a short option reminder and lists the available commands with
their short descriptions.

=item B<--help>

Displays this manual page.

=item B<--usage>

Displays a terse command line usage summary.

=item B<--version>

Displays the program version and copyright information.

=back

=head1 COMMANDS

This section summarizes the basic B<mangemanche> commands.  Your installation
may provide more.  Use B<mangemanche -?> to obtain the list of available
command.

To see the manual for each particular command, run

    mangemanche COMMAND --help

=head2 ident

Displays B<ping903> server identification: package name, version number,
and main process PID.

=head2 inspect

Displays the actual B<ping903> server configuration.

=head2 ipadd

Adds a single IP to the mutable list of monitored IP addresses.

=head2 ipdel

Deletes a single IP from the mutable list of monitored IP addresses.

=head2 nagios

Extracts the list of monitored IP addresses from the Nagios configuration
and installs them in B<ping903>.

=head2 dbload

Load IP addresses from SQL database into B<ping903>.

=head1 SEE ALSO

L<ping903>(1),
L<ping903.conf>(5),
L<ping903.cred>(5).

=cut

Return to:

Send suggestions and report system problems to the System administrator.