From bbad058b3379d04111fe8ba4471262be8feeb18e Mon Sep 17 00:00:00 2001 From: Digimer Date: Wed, 26 May 2021 20:24:05 -0400 Subject: [PATCH] * Created a new tool, anvil-watch-bonds, which is a live monitor of bonds and interfaces designed to be run from the command line on a given host. * Created Words->center_text that takes a string (or string key) and centers it to a given string length, padding white spaces on either side of the string as needed. Signed-off-by: Digimer --- Anvil/Tools.pm | 1 + Anvil/Tools/Convert.pm | 2 +- Anvil/Tools/Words.pm | 87 ++++++ share/words.xml | 9 + tools/Makefile.am | 1 + tools/anvil-watch-bonds | 673 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 772 insertions(+), 1 deletion(-) create mode 100755 tools/anvil-watch-bonds diff --git a/Anvil/Tools.pm b/Anvil/Tools.pm index 42b6191e..c60f16da 100644 --- a/Anvil/Tools.pm +++ b/Anvil/Tools.pm @@ -1062,6 +1062,7 @@ sub _set_paths alert_emails => "/var/spool/anvil", anvil => "/etc/anvil", backups => "/root/anvil-backups", + bonds => "/proc/net/bonding", 'cgi-bin' => "/var/www/cgi-bin", drbd_resources => "/etc/drbd.d/", fence_agents => "/usr/sbin", diff --git a/Anvil/Tools/Convert.pm b/Anvil/Tools/Convert.pm index b1589a36..0423f89f 100644 --- a/Anvil/Tools/Convert.pm +++ b/Anvil/Tools/Convert.pm @@ -137,7 +137,7 @@ sub add_commas $whole = $_; # Put it together - $number = $decimal ? "$whole.$decimal" : $whole; + $number = $decimal ? $whole.".".$decimal : $whole; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { number => $number }}); return ($number); diff --git a/Anvil/Tools/Words.pm b/Anvil/Tools/Words.pm index adc38e2d..672ebd15 100644 --- a/Anvil/Tools/Words.pm +++ b/Anvil/Tools/Words.pm @@ -18,6 +18,7 @@ my $THIS_FILE = "Words.pm"; # $ENV{'PERL_UNICODE'} = 1; ### Methods; +# center_text # clean_spaces # key # language @@ -93,6 +94,92 @@ sub parent # Public methods # ############################################################################################################# + +=head2 center_text + +This takes a string and an integer and pads the string with spaces on either side until the length is that of the integer. For uneven splits, the smaller number of spaces will be on the left. + +Paramters; + +=head3 string + +This is the string being centered. If not given, and empty string is returned. + +B<< Note >>: This can be C<< #!string!x!# >>. If this is passed, the string will be translated before being centered. + +=head3 width + +This is an integer of how width the centered string should be. + +=cut +sub center_text +{ + my $self = shift; + my $parameter = shift; + my $anvil = $self->parent; + my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0125", variables => { method => "Words->center_text()" }}); + + # Pick up the parameters. + my $string = defined $parameter->{string} ? $parameter->{string} : ""; + my $width = defined $parameter->{width} ? $parameter->{width} : 0; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + string => $string, + width => $width, + }}); + + return($string) if $width eq ""; + return("") if $parameter->{string} eq ""; + + ### NOTE: If a '#!string!x!#' is passed, the Log->entry method will translate it in the log itself, + ### so you won't see that string. + if ($string =~ /#!string!(.*?)!#/) + { + $string = $anvil->Words->string({key => $1}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { string => $string }}); + } + + my $current_length = length($string); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { current_length => $current_length }}); + if ($current_length < $width) + { + my $difference = $width - $current_length; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { difference => $difference }}); + if ($difference == 1) + { + $string .= " "; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { string => $string }}); + } + else + { + my $remainder = $difference % 2; + $difference -= $remainder; + my $spaces = $difference / 2; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + remainder => $remainder, + spaces => $spaces, + }}); + for (1..$spaces) + { + $string = " ".$string." "; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { string => $string }}); + } + if ($remainder) + { + $string .= " "; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { string => $string }}); + } + } + } + + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + string => $string, + 'length' => length($string), + }}); + return($string); +} + + =head2 clean_spaces This methid takes a string via a 'C<< line >>' parameter and strips leading and trailing spaces, plus compresses multiple spaces into single spaces. It is designed primarily for use by code parsing text coming in from a shell command. diff --git a/share/words.xml b/share/words.xml index 3673aa94..9c8de6e1 100644 --- a/share/words.xml +++ b/share/words.xml @@ -710,6 +710,14 @@ sys::manage::firewall = 1 Synced Remove #!string!brand_0006!# Name + Active Interface + Bond Mode + Active Interface + Link State + Duplex + Link Drops + + Configure Network @@ -2574,6 +2582,7 @@ Here we will inject 't_0006', which injects 't_0001' which has a variable: [#!st Fahrenheit % Amps + Going Back