* Added Get->switches to read in command line switches.

* Added Get->host_uuid that reads the host's UUID from dmidecode.
* Added Log->_adjust_log_level to automatically change the active log level when -V (0), -v (1), -vv (2), -vvv (3) or -vvvv (4) are passed.

Signed-off-by: Digimer <digimer@alteeve.ca>
main
Digimer 8 years ago
parent 421d970f22
commit 0a712f4a9c
  1. 13
      AN/Tools.pm
  2. 143
      AN/Tools/Get.pm
  3. 56
      AN/Tools/Log.pm

@ -107,14 +107,13 @@ sub new
WORDS => AN::Tools::Words->new(), WORDS => AN::Tools::Words->new(),
}, },
DATA => {}, DATA => {},
ERROR_COUNT => 0,
ERROR_LIMIT => 10000,
DEFAULT => {
LANGUAGE => 'en_CA',
},
ENV_VALUES => { ENV_VALUES => {
ENVIRONMENT => 'cli', ENVIRONMENT => 'cli',
}, },
HOST => {
# This is the host's UUID. It should never be manually set.
UUID => "",
},
}; };
# Bless you! # Bless you!
@ -152,6 +151,9 @@ sub new
$an->Storage->read_config({file => "./tools.conf"}); $an->Storage->read_config({file => "./tools.conf"});
} }
# Read in any command line switches.
$an->Get->switches;
# Set passed parameters if needed. # Set passed parameters if needed.
if (ref($parameter) eq "HASH") if (ref($parameter) eq "HASH")
{ {
@ -481,6 +483,7 @@ sub _set_paths
# Executables # Executables
$an->data->{path} = { $an->data->{path} = {
exe => { exe => {
dmidecode => "/usr/sbin/dmidecode",
gethostip => "/usr/bin/gethostip", gethostip => "/usr/bin/gethostip",
hostname => "/bin/hostname", hostname => "/bin/hostname",
logger => "/usr/bin/logger", logger => "/usr/bin/logger",

@ -12,6 +12,8 @@ my $THIS_FILE = "Get.pm";
### Methods; ### Methods;
# date_and_time # date_and_time
# host_uuid
# switches
=pod =pod
@ -43,7 +45,11 @@ Methods in this module;
sub new sub new
{ {
my $class = shift; my $class = shift;
my $self = {}; my $self = {
HOST => {
UUID => "",
},
};
bless $self, $class; bless $self, $class;
@ -161,3 +167,138 @@ sub date_and_time
return($return_string); return($return_string);
} }
=head2 host_uuid
This returns the local host's system UUID (as reported by 'dmidecode').
print "This host's UUID: [".$an->Get->host_uuid."]\n";
It is possible to override the local UUID, though it is not recommended.
$an->Get->host_uuid({set => "720a0509-533d-406b-8fc1-03aca3e75fa7"})
=cut
sub host_uuid
{
my $self = shift;
my $parameter = shift;
my $an = $self->parent;
my $set = defined $parameter->{set} ? $parameter->{set} : "";
if ($set)
{
$an->data->{HOST}{UUID} = $set;
}
elsif (not $an->data->{HOST}{UUID})
{
# Read dmidecode
my $uuid = "";
my $shell_call = $an->data->{path}{exe}{dmidecode}." --string system-uuid";
#print $THIS_FILE." ".__LINE__."; [ Debug ] - shell_call: [$shell_call]\n";
open(my $file_handle, $shell_call." 2>&1 |") or warn $THIS_FILE." ".__LINE__."; [ Warning ] - Failed to call: [".$shell_call."], the error was: $!\n";
while(<$file_handle>)
{
# This should never be hit...
chomp;
$uuid = lc($_);
}
close $file_handle;
if ($uuid)
{
$an->data->{HOST}{UUID} = $uuid;
}
}
return($an->data->{HOST}{UUID});
}
=head2
This reads in the command line switches used to invoke the parent program.
It takes no arguments, and data is stored in 'C<< $an->data->{switches}{x} >>', where 'x' is the switch used.
Switches in the form 'C<< -x >>' and 'C<< --x >>' are treated the same and the corresponding 'C<< $an->data->{switches}{x} >>' will contain '#!set!#'.
Switches in the form 'C<< -x foo >>', 'C<< --x foo >>', 'C<< -x=foo >>' and 'C<< --x=foo >>' are treated the same and the corresponding 'C<< $an->data->{switches}{x} >>' will contain 'foo'.
The switches 'C<< -v >>', 'C<< -vv >>', 'C<< -vvv >>' and 'C<< -vvvv >>' will cause the active log level to automatically change to 1, 2, 3 or 4 respectively. Passing 'C<< -V >>' will set the log level to '0'.
Anything after 'C<< -- >>' is treated as a raw string and is not processed.
=cut
sub switches
{
my $self = shift;
my $an = $self->parent;
my $last_argument = "";
foreach my $argument (@ARGV)
{
if ($last_argument eq "raw")
{
# Don't process anything.
$an->data->{switches}{raw} .= " $argument";
}
elsif ($argument =~ /^-/)
{
# If the argument is just '--', appeand everything after it to 'raw'.
if ($argument eq "--")
{
$last_argument = "raw";
$an->data->{switches}{raw} = "";
}
else
{
($last_argument) = ($argument =~ /^-{1,2}(.*)/)[0];
if ($last_argument =~ /=/)
{
# Break up the variable/value.
($last_argument, my $value) = (split /=/, $last_argument, 2);
$an->data->{switches}{$last_argument} = $value;
}
else
{
$an->data->{switches}{$last_argument} = "#!SET!#";
}
}
}
else
{
if ($last_argument)
{
$an->data->{switches}{$last_argument} = $argument;
$last_argument = "";
}
else
{
# Got a value without an argument.
$an->data->{switches}{error} = 1;
}
}
}
# Clean up the initial space added to 'raw'.
if ($an->data->{switches}{raw})
{
$an->data->{switches}{raw} =~ s/^ //;
}
# Adjust the log level if requested.
$an->Log->_adjust_log_level();
return(0);
}
# =head3
#
# Private Functions;
#
# =cut
#############################################################################################################
# Private functions #
#############################################################################################################

@ -14,6 +14,7 @@ my $THIS_FILE = "Log.pm";
# entry # entry
# level # level
# secure # secure
# _adjust_log_level
=pod =pod
@ -256,17 +257,16 @@ sub entry
my $string = ""; my $string = "";
if (($source) && ($line)) if (($source) && ($line))
{ {
$string .= "$source:$line"; $string .= "$source:$line; ";
} }
elsif ($source) elsif ($source)
{ {
$string .= "$source"; $string .= "$source; ";
} }
elsif ($line) elsif ($line)
{ {
$string .= "$line"; $string .= "$line; ";
} }
$string .= "; ";
# If I have a raw string, do no more processing. # If I have a raw string, do no more processing.
if ($raw) if ($raw)
@ -294,9 +294,9 @@ sub entry
$shell_call .= " --server ".$server; $shell_call .= " --server ".$server;
} }
$shell_call .= " -- \"".$string."\""; $shell_call .= " -- \"".$string."\"";
#print $THIS_FILE." ".__LINE__."; [ Debug ] - shell_call: [$shell_call]\n";
# Record it! # Record it!
#print $THIS_FILE." ".__LINE__."; [ Debug ] - shell_call: [$shell_call]\n";
open(my $file_handle, $shell_call." 2>&1 |") or warn $THIS_FILE." ".__LINE__."; [ Warning ] - Failed to call: [".$shell_call."], the error was: $!\n"; open(my $file_handle, $shell_call." 2>&1 |") or warn $THIS_FILE." ".__LINE__."; [ Warning ] - Failed to call: [".$shell_call."], the error was: $!\n";
while(<$file_handle>) while(<$file_handle>)
{ {
@ -393,3 +393,49 @@ sub secure
return($an->data->{defaults}{'log'}{secure}); return($an->data->{defaults}{'log'}{secure});
} }
# =head3
#
# Private Functions;
#
# =cut
#############################################################################################################
# Private functions #
#############################################################################################################
=head2 _adjust_log_level
This is a private method used by 'C<< $an->Get->switches >>' that automatically adjusts the active log level to 0 ~ 4. See 'C<< perldoc AN::Tools::Get >>' for more information.
=cut
sub _adjust_log_level
{
my $self = shift;
my $parameter = shift;
my $an = $self->parent;
if ($an->data->{switches}{V})
{
$an->Log->level(0);
}
elsif ($an->data->{switches}{v})
{
$an->Log->level(1);
}
elsif ($an->data->{switches}{vv})
{
$an->Log->level(2);
}
elsif ($an->data->{switches}{vvv})
{
$an->Log->level(3);
}
elsif ($an->data->{switches}{vvvv})
{
$an->Log->level(4);
}
return(0);
}

Loading…
Cancel
Save