more docs done and env is all good

This commit is contained in:
Zane C. B-H 2020-02-04 02:20:39 -06:00
parent 861c55a9e1
commit ce6b4b97b3
2 changed files with 156 additions and 34 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env perl
#
#This software is Copyright (c) 2019 by Zane C. Bowers-Hadley.
#This software is Copyright (c) 2020 by Zane C. Bowers-Hadley.
#
#This is free software, licensed under:
#
@ -35,6 +35,7 @@ my $method;
my $print_cmd;
my $debug;
my $command = join( ' ', @ARGV );
my $env;
# get the commandline options
Getopt::Long::Configure('no_ignore_case');
@ -42,27 +43,143 @@ Getopt::Long::Configure('bundling');
Getopt::Long::Configure('pass_through');
Getopt::Long::Configure('require_order');
GetOptions(
'c=s' => \$config_file,
'h=s' => \$hosts_file,
'm=s' => \$method,
'p' => \$print_cmd,
'd' => \$debug,
);
'c=s' => \$config_file,
'h=s' => \$hosts_file,
'm=s' => \$method,
'e=s' => \$env,
'p' => \$print_cmd,
'd' => \$debug,
);
my $csh = Cluster::SSH::Helper->new_from_ini( $config_file, $hosts_file );
my $return = $csh->run(
{
command => \@ARGV,
method => $method,
print_cmd => $print_cmd,
debug => $debug,
command => \@ARGV,
method => $method,
print_cmd => $print_cmd,
debug => $debug,
env => $env,
}
);
if ($debug) {
print $return."\n";
exit;
}else {
exit $return;
print $return. "\n";
exit;
}
else {
exit $return;
}
=head1 NAME
cshelper - Find the best machine via SNMP to run a command on.
=head1 SYNOPSIS
cshelper [B<-d>] [B<-p>] [B<-e> <enviroment>] [B<-h> <hosts.ini>] [B<-c> <config.ini>] [B<-m> <method>] <command>
=head1 DESCRIPTION
This figures out the host to run the command on via polling the configured hosts via SNMP. The current methods
below are currently supported are as below.
load_1m - Find the machine with the lowest 1m load.
ram_used_percent - Find the machine with the lowest used percent of RAM.
=head1 ARGS
=head2 B<-d>
Print the command to run and then exit with out running it.
=head2 B<-p>
Print the command before running it.
=head2 B<-e> <enviroment>
Run with a altered enviroment.
This will used the specified section in the config INI file
to build a /usr/bin/env command with to use to run the
specified command.
=head2 B<-h> <hosts.ini>
The hosts INI config file to use.
=head2 B<-c> <config.ini>
The config INI file to use.
=head2 B<-m> <method>
Use this method instead of default method.
=head1 CONFIGURATION
=head2 CONFIG.INI
The default one is ~/.config/cluster-ssh-helper/config.ini .
Below is a basic example.
snmp=-v 2c -c public
=head3 snmp
This is the additional args to pass to snmpget for auth.
=head3 method
This is default method to use for selecting the what host to run the command on.
If not specified, 'load' is used.
load_1m , checks 1 minute load and uses the lowest
ram_used_percent , uses the host with the lowest used percent of RAM
=head3 ssh
The default command to use for SSH.
If not specified, just 'ssh' will be used.
This should not include either user or host.
=head3 ssh_user
If specified, this will be the default user to use for SSH.
If not specified, SSH is invoked with out specifying a user.
=head3 env
If specified, '/usr/bin/env' will be inserted before the command to be ran.
The name=values pairs for this will be built using the hash key specified by this.
=head3 warn_on_poll
Issue a warn() on SNMP timeouts or other polling issues. This won't be a automatic
failure. Simply timing out on SNMP means that host will be skipped and not considered.
This defaults to 1, true.
=head2 HOSTS.INI
The default one is ~/.config/cluster-ssh-helper/config.ini .
This contains the hosts to connect to. The same variables can be used
as from the config. Anything specified for a use will override the one in
config.
Below is a example configuration with 3 hosts in it, with the second
one having a different SNMP auth then the other two.
[127.0.0.1]
[a.foo.bar]
snmp=-v 2c -c k3k3kqwdq
[b.foo.bar]
=cut

View File

@ -9,7 +9,7 @@ use File::BaseDir qw/xdg_config_home/;
=head1 NAME
Cluster::SSH::Helper - The great new Cluster::SSH::Helper!
Cluster::SSH::Helper - Poll machines in a cluster via SNMP and determine which to run a command on.
=head1 VERSION
@ -21,14 +21,21 @@ our $VERSION = '0.1.0';
=head1 SYNOPSIS
Quick summary of what the module does.
Perhaps a little code snippet.
use Cluster::SSH::Helper;
my $csh = Cluster::SSH::Helper->new();
...
my $csh;
eval({
# config using...
# ~/.config/cluster-ssh-helper/hosts.ini
# ~/.config/cluster-ssh-helper/config.ini
$csh= Cluster::SSH::Helper->new_from_ini();
});
if ( $@ ){
die( 'Cluster::SSH::Helper->new_from_ini failed... '.$@ );
}
# Run the command on the machine with the lowest 1m load.
$csh->run({ command=>'uname -a' });
=head1 METHODS
@ -250,23 +257,24 @@ sub run {
}
# makes sure the section exists if one was specified
my $env_command;
if ( defined( $opts->{env} )
&& !defined( $self->{config}{_}{ $opts->{env} } ) ) {
&& !defined( $self->{config}{ $opts->{env} } ) ) {
die( '"'
. $opts->{env}
. '" is not a defined section in the config' );
}
# build the env section if needed.
my $env_command;
if ( defined( $opts->{env} ) ) {
# builds the env commant
$env_command = '/usr/bin/env';
foreach my $key ( keys( @{ $self->{config}{ $$opts->{env} } } ) ) {
$env_command =
$env_command . ' '
foreach my $key ( keys( %{ $self->{config}{'test'} } ) ) {
$env_command
= $env_command . ' '
. shell_quote( shell_quote($key) ) . '='
. shell_quote(
shell_quote( $self->{config}{ $opts->{env}{$key} } ) );
. shell_quote( shell_quote( $self->{config}{ $opts->{env} }{$key} ) );
}
}
# the initial command to use
@ -539,9 +547,6 @@ Please report any bugs or feature requests to C<bug-cluster-ssh-helper at rt.cpa
the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Cluster-SSH-Helper>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
@ -577,7 +582,7 @@ L<https://metacpan.org/release/Cluster-SSH-Helper>
=head1 LICENSE AND COPYRIGHT
This software is Copyright (c) 2019 by Zane C. Bowers-Hadley.
This software is Copyright (c) 2020 by Zane C. Bowers-Hadley.
This is free software, licensed under: