Author: arkurth
Date: Wed Dec 14 17:12:39 2011
New Revision: 1214346

URL: http://svn.apache.org/viewvc?rev=1214346&view=rev
Log:
VCL-545
Updated Linux.pm::get_file_size to be able to accept an array of files.

Updated Linux.pm::find_files to allow an argument to be passed specifying the 
type of search to perform. This allows a regex file pattern to be passed.

Added get_copy_speed_info_string to utils.pm. This displays various file copy 
performance statistics. It was copied out of VMware.pm and added to utils.pm 
since it can be used elsewhere. 

Added get_random_mac_address to utils.pm.

Updated utils.pm::format_data to detect an XML string. If XML, a hash is 
constructed using XML::Simple::XMLin and then displayed.


Other
Updated section of post_load in Linux.pm which attempts to run the 
vcl_post_load script to check if the script exists on the computer being loaded 
before trying to execute it.  This is done to prevent unnecessary warning 
messages from appearing in the vcld.log file.

Modified:
    incubator/vcl/trunk/managementnode/lib/VCL/Module/OS/Linux.pm
    incubator/vcl/trunk/managementnode/lib/VCL/utils.pm

Modified: incubator/vcl/trunk/managementnode/lib/VCL/Module/OS/Linux.pm
URL: 
http://svn.apache.org/viewvc/incubator/vcl/trunk/managementnode/lib/VCL/Module/OS/Linux.pm?rev=1214346&r1=1214345&r2=1214346&view=diff
==============================================================================
--- incubator/vcl/trunk/managementnode/lib/VCL/Module/OS/Linux.pm (original)
+++ incubator/vcl/trunk/managementnode/lib/VCL/Module/OS/Linux.pm Wed Dec 14 
17:12:39 2011
@@ -300,15 +300,17 @@ sub post_load {
        
        # Run the vcl_post_load script if it exists in the image
        my $script_path = '/etc/init.d/vcl_post_load';
-       my $result = $self->run_script($script_path);
-       if (!defined($result)) {
-               notify($ERRORS{'WARNING'}, 0, "error occurred running 
$script_path");
-       }
-       elsif ($result == 0) {
-               notify($ERRORS{'DEBUG'}, 0, "$script_path does not exist in 
image: $image_name");
-       }
-       else {
-               notify($ERRORS{'DEBUG'}, 0, "ran $script_path");
+       if ($self->file_exists($script_path)) {
+               my $result = $self->run_script($script_path);
+               if (!defined($result)) {
+                       notify($ERRORS{'WARNING'}, 0, "error occurred running 
$script_path");
+               }
+               elsif ($result == 0) {
+                       notify($ERRORS{'DEBUG'}, 0, "$script_path does not 
exist in image: $image_name");
+               }
+               else {
+                       notify($ERRORS{'DEBUG'}, 0, "ran $script_path");
+               }
        }
        
        if($self->enable_firewall_port("tcp", "any", $mn_private_ip, 1) ){
@@ -1381,7 +1383,7 @@ sub file_exists {
                return;
        }
        elsif (grep(/no such file/i, @$output)) {
-               notify($ERRORS{'DEBUG'}, 0, "file or directory does not exist 
on $computer_short_name: '$path'");
+               #notify($ERRORS{'DEBUG'}, 0, "file or directory does not exist 
on $computer_short_name: '$path'");
                return 0;
        }
        elsif (grep(/stat: /i, @$output)) {
@@ -1464,7 +1466,7 @@ sub delete_file {
                return;
        }
        else {
-               notify($ERRORS{'DEBUG'}, 0, "confirmed file does not exist on 
$computer_short_name: '$path'");
+               #notify($ERRORS{'DEBUG'}, 0, "confirmed file does not exist on 
$computer_short_name: '$path'");
                return 1;
        }
 }
@@ -1928,85 +1930,88 @@ sub get_file_size {
        my $calling_sub = (caller(1))[3] || '';
        
        # Get the path argument
-       my $file_path = shift;
-       if (!$file_path) {
-               notify($ERRORS{'WARNING'}, 0, "path argument was not 
specified");
+       my @file_paths = @_;
+       if (!@file_paths) {
+               notify($ERRORS{'WARNING'}, 0, "file paths argument was not 
specified");
                return;
        }
        
-       # Normalize the file path
-       $file_path = normalize_file_path($file_path);
-       
-       # Escape all spaces in the path
-       my $escaped_file_path = escape_file_path($file_path);
-       
        # Get the computer name
        my $computer_node_name = $self->data->get_computer_node_name() || 
return;
        
-       # Run stat rather than du because du is not available on VMware ESX
-       # -L     Dereference links
-       # %F     File type
-       # %n     File name
-       # %b     Number of blocks allocated (see %B)
-       # %B     The size in bytes of each block reported by %b
-       # %s     Total size, in bytes
-       
-       my $command = 'stat -L -c "%F:%n:%s:%b:%B" ' . $escaped_file_path;
-       my ($exit_status, $output) = $self->execute($command);
-       if (!defined($output)) {
-               notify($ERRORS{'WARNING'}, 0, "failed to run command to 
determine file size on $computer_node_name: $file_path\ncommand: '$command'");
-               return;
-       }
-       elsif (grep(/no such file/i, @$output)) {
-               if ($calling_sub !~ /get_file_size/) {
-                       notify($ERRORS{'DEBUG'}, 0, "unable to determine size 
of file on $computer_node_name because it does not exist: $file_path\ncommand: 
'$command'");
-               }
-               return;
-       }
-       elsif (grep(/^stat:/i, @$output)) {
-               notify($ERRORS{'WARNING'}, 0, "error occurred attempting to 
determine file size on $computer_node_name: $file_path\ncommand: 
$command\noutput:\n" . join("\n", @$output));
-               return;
-       }
-       
-       # Loop through the stat output lines
        my $file_count = 0;
        my $total_bytes_reserved = 0;
        my $total_bytes_used = 0;
-       for my $line (@$output) {
-               # Take the stat output line apart
-               my ($type, $path, $file_bytes, $file_blocks, $block_size) = 
split(/:/, $line);
-               if (!defined($type) || !defined($file_bytes) || 
!defined($file_blocks) || !defined($block_size) || !defined($path)) {
-                       notify($ERRORS{'WARNING'}, 0, "unexpected output 
returned from stat, line: $line\ncommand: $command\noutput:\n" . join("\n", 
@$output));
+       
+       for my $file_path (@file_paths) {
+               # Normalize the file path
+               $file_path = normalize_file_path($file_path);
+               
+               # Escape all spaces in the path
+               my $escaped_file_path = escape_file_path($file_path);
+               
+               # Run stat rather than du because du is not available on VMware 
ESX
+               # -L     Dereference links
+               # %F     File type
+               # %n     File name
+               # %b     Number of blocks allocated (see %B)
+               # %B     The size in bytes of each block reported by %b
+               # %s     Total size, in bytes
+               
+               my $command = 'stat -L -c "%F:%n:%s:%b:%B" ' . 
$escaped_file_path;
+               my ($exit_status, $output) = $self->execute($command);
+               if (!defined($output)) {
+                       notify($ERRORS{'WARNING'}, 0, "failed to run command to 
determine file size on $computer_node_name: $file_path\ncommand: '$command'");
                        return;
                }
-               
-               # Add the size to the total if the type is file
-               if ($type =~ /file/) {
-                       $file_count++;
-                       
-                       my $file_bytes_allocated = ($file_blocks * $block_size);
-                       
-                       $total_bytes_used += $file_bytes_allocated;
-                       $total_bytes_reserved += $file_bytes;
+               elsif (grep(/no such file/i, @$output)) {
+                       if ($calling_sub !~ /get_file_size/) {
+                               notify($ERRORS{'DEBUG'}, 0, "unable to 
determine size of file on $computer_node_name because it does not exist: 
$file_path\ncommand: '$command'");
+                       }
+                       return;
                }
-               elsif ($type =~ /directory/) {
-                       $path =~ s/[\\\/\*]+$//g;
-                       #notify($ERRORS{'DEBUG'}, 0, "recursively retrieving 
size of files under directory: '$path'");
-                       my ($subdirectory_bytes_allocated, 
$subdirectory_bytes_used, $subdirectory_file_count) = 
$self->get_file_size("$path/*");
-                       
-                       # Values will be null if there are no files under the 
subdirectory
-                       if (!defined($subdirectory_bytes_allocated)) {
-                               next;
+               elsif (grep(/^stat:/i, @$output)) {
+                       notify($ERRORS{'WARNING'}, 0, "error occurred 
attempting to determine file size on $computer_node_name: $file_path\ncommand: 
$command\noutput:\n" . join("\n", @$output));
+                       return;
+               }
+               
+               # Loop through the stat output lines
+               for my $line (@$output) {
+                       # Take the stat output line apart
+                       my ($type, $path, $file_bytes, $file_blocks, 
$block_size) = split(/:/, $line);
+                       if (!defined($type) || !defined($file_bytes) || 
!defined($file_blocks) || !defined($block_size) || !defined($path)) {
+                               notify($ERRORS{'WARNING'}, 0, "unexpected 
output returned from stat, line: $line\ncommand: $command\noutput:\n" . 
join("\n", @$output));
+                               return;
                        }
                        
-                       $file_count += $subdirectory_file_count;
-                       $total_bytes_reserved += $subdirectory_bytes_used;
-                       $total_bytes_used += $subdirectory_bytes_allocated;
+                       # Add the size to the total if the type is file
+                       if ($type =~ /file/) {
+                               $file_count++;
+                               
+                               my $file_bytes_allocated = ($file_blocks * 
$block_size);
+                               
+                               $total_bytes_used += $file_bytes_allocated;
+                               $total_bytes_reserved += $file_bytes;
+                       }
+                       elsif ($type =~ /directory/) {
+                               $path =~ s/[\\\/\*]+$//g;
+                               #notify($ERRORS{'DEBUG'}, 0, "recursively 
retrieving size of files under directory: '$path'");
+                               my ($subdirectory_bytes_allocated, 
$subdirectory_bytes_used, $subdirectory_file_count) = 
$self->get_file_size("$path/*");
+                               
+                               # Values will be null if there are no files 
under the subdirectory
+                               if (!defined($subdirectory_bytes_allocated)) {
+                                       next;
+                               }
+                               
+                               $file_count += $subdirectory_file_count;
+                               $total_bytes_reserved += 
$subdirectory_bytes_used;
+                               $total_bytes_used += 
$subdirectory_bytes_allocated;
+                       }
                }
        }
        
        if ($calling_sub !~ /get_file_size/) {
-               notify($ERRORS{'DEBUG'}, 0, "size of '$file_path' on 
$computer_node_name:\n" .
+               notify($ERRORS{'DEBUG'}, 0, "size of " . join(", ", 
@file_paths) . " on $computer_node_name:\n" .
                                 "file count: $file_count\n" .
                                 "reserved: " . 
get_file_size_info_string($total_bytes_reserved) . "\n" .
                                 "used: " . 
get_file_size_info_string($total_bytes_used));
@@ -2024,11 +2029,16 @@ sub get_file_size {
 
 =head2 find_files
 
- Parameters  : $base_directory_path, $file_pattern
+ Parameters  : $base_directory_path, $file_pattern, $search_type (optional)
  Returns     : array
  Description : Finds files under the base directory and any subdirectories path
                matching the file pattern. The search is not case sensitive. An
                array is returned containing matching file paths.
+               
+               A third argument can be supplied specifying the search type.
+               'regex' is currently the only supported type. If supplied, the
+               $file_pattern argument is assumed to be a regular expression.
+               Otherwise it is assumed to be a normal search pattern.
 
 =cut
 
@@ -2040,7 +2050,7 @@ sub find_files {
        }
        
        # Get the arguments
-       my ($base_directory_path, $file_pattern) = @_;
+       my ($base_directory_path, $file_pattern, $search_type) = @_;
        if (!$base_directory_path || !$file_pattern) {
                notify($ERRORS{'WARNING'}, 0, "base directory path and file 
pattern arguments were not specified");
                return;
@@ -2057,7 +2067,13 @@ sub find_files {
        my $computer_node_name = $self->data->get_computer_node_name() || 
return;
        
        # Run the find command
-       my $command = "find \"$base_directory_path\" -iname \"$file_pattern\"";
+       my $command;
+       if ($search_type && $search_type =~ /regex/i) {
+               $command = "find \"$base_directory_path\" -iregex 
\"$file_pattern\"";
+       }
+       else {
+               $command = "find \"$base_directory_path\" -iname 
\"$file_pattern\"";
+       }
        notify($ERRORS{'DEBUG'}, 0, "attempting to find files on 
$computer_node_name, base directory path: '$base_directory_path', pattern: 
$file_pattern, command: $command");
        
        my ($exit_status, $output) = $self->execute($command);

Modified: incubator/vcl/trunk/managementnode/lib/VCL/utils.pm
URL: 
http://svn.apache.org/viewvc/incubator/vcl/trunk/managementnode/lib/VCL/utils.pm?rev=1214346&r1=1214345&r2=1214346&view=diff
==============================================================================
--- incubator/vcl/trunk/managementnode/lib/VCL/utils.pm (original)
+++ incubator/vcl/trunk/managementnode/lib/VCL/utils.pm Wed Dec 14 17:12:39 2011
@@ -73,6 +73,7 @@ use Scalar::Util 'blessed';
 use Data::Dumper;
 use Cwd;
 use Sys::Hostname;
+use XML::Simple;
 
 #use Date::Calc qw(Delta_DHMS Time_to_Date Date_to_Time);
 
@@ -117,6 +118,7 @@ our @EXPORT = qw(
   get_computer_info
   get_computers_controlled_by_MN
   get_connect_method_info
+  get_copy_speed_info_string
   get_current_file_name
   get_current_package_name
   get_current_subroutine_name
@@ -136,6 +138,7 @@ our @EXPORT = qw(
   get_next_image_default
   get_os_info
   get_production_imagerevision_info
+  get_random_mac_address
   get_request_by_computerid
   get_request_end
   get_request_info
@@ -8920,6 +8923,12 @@ sub format_data {
                return '<undefined>';
        }
        
+       # If a string was passed which appears to be XML, convert it to a hash 
using XML::Simple
+       if (scalar(@data) == 1 && !ref($data[0]) && $data[0] =~ /^</) {
+               my $xml = XMLin($data[0], 'ForceArray' => 0, 'KeyAttr' => []);
+               return format_data($xml);
+       }
+       
        $Data::Dumper::Indent    = 1;
        $Data::Dumper::Purity    = 1;
        $Data::Dumper::Useqq     = 1;      # Use double quotes for representing 
string values
@@ -10187,7 +10196,7 @@ sub format_number {
 
 sub get_file_size_info_string {
        my ($size_bytes, $separator) = @_;
-       $separator = ", " if !$separator;
+       $separator = " - " if !$separator;
        
        my $size_mb = format_number(($size_bytes / 1024 / 1024), 1);
        my $size_gb = format_number(($size_bytes / 1024 / 1024 / 1024), 2);
@@ -10203,6 +10212,79 @@ sub get_file_size_info_string {
 
 #/////////////////////////////////////////////////////////////////////////////
 
+=head2 get_copy_speed_info_string
+
+ Parameters  : $copied_bytes, $duration_seconds
+ Returns     : string
+ Description : Calculates various copy rates based on the amount of data copied
+               and time the copy took.
+
+=cut
+
+sub get_copy_speed_info_string {
+       my ($copied_bytes, $duration_seconds) = @_;
+       
+       my $minutes = ($duration_seconds / 60);
+       $minutes =~ s/\..*//g;
+       my $seconds = ($duration_seconds - ($minutes * 60));
+       if (length($seconds) == 0) {
+               $seconds = "00";
+       }
+       elsif (length($seconds) == 1) {
+               $seconds = "0$seconds";
+       }
+       
+       my $copied_bits = ($copied_bytes * 8);
+       
+       my $copied_kb = ($copied_bytes / 1024);
+       my $copied_mb = ($copied_bytes / 1024 / 1024);
+       my $copied_gb = ($copied_bytes / 1024 / 1024 / 1024);
+       
+       my $copied_kbit = ($copied_bits / 1024);
+       my $copied_mbit = ($copied_bits / 1024 / 1024);
+       my $copied_gbit = ($copied_bits / 1024 / 1024 / 1024);
+       
+       my $bytes_per_second = ($copied_bytes / $duration_seconds);
+       my $kb_per_second = ($copied_kb / $duration_seconds);
+       my $mb_per_second = ($copied_mb / $duration_seconds);
+       my $gb_per_second = ($copied_gb / $duration_seconds);
+       
+       my $bits_per_second = ($copied_bits / $duration_seconds);
+       my $kbit_per_second = ($copied_kbit / $duration_seconds);
+       my $mbit_per_second = ($copied_mbit / $duration_seconds);
+       my $gbit_per_second = ($copied_gbit / $duration_seconds);
+       
+       my $bytes_per_minute = ($copied_bytes / $duration_seconds * 60);
+       my $kb_per_minute = ($copied_kb / $duration_seconds * 60);
+       my $mb_per_minute = ($copied_mb / $duration_seconds * 60);
+       my $gb_per_minute = ($copied_gb / $duration_seconds * 60);
+       
+       my $info_string;
+       
+       $info_string .= "data copied: " . 
get_file_size_info_string($copied_bytes) . "\n";
+       $info_string .= "time to copy: $minutes:$seconds (" . 
format_number($duration_seconds) . " seconds)\n";
+       $info_string .= "---\n";
+       $info_string .= "bits copied:  " . format_number($copied_bits) . " 
($copied_bits)\n";
+       $info_string .= "bytes copied: " . format_number($copied_bytes) . " 
($copied_bytes)\n";
+       $info_string .= "MB copied:    " . format_number($copied_mb, 1) . "\n";
+       $info_string .= "GB copied:    " . format_number($copied_gb, 2) . "\n";
+       $info_string .= "---\n";
+       $info_string .= "B/m:    " . format_number($bytes_per_minute) . "\n";
+       $info_string .= "MB/m:   " . format_number($mb_per_minute, 1) . "\n";
+       $info_string .= "GB/m:   " . format_number($gb_per_minute, 2) . "\n";
+       $info_string .= "---\n";
+       $info_string .= "B/s:    " . format_number($bytes_per_second) . "\n";
+       $info_string .= "MB/s:   " . format_number($mb_per_second, 1) . "\n";
+       $info_string .= "GB/s:   " . format_number($gb_per_second, 2) . "\n";
+       $info_string .= "---\n";
+       $info_string .= "Mbit/s: " . format_number($mbit_per_second, 1) . "\n";
+       $info_string .= "Gbit/s: " . format_number($gbit_per_second, 2);
+       
+       return $info_string;
+}
+
+#/////////////////////////////////////////////////////////////////////////////
+
 =head2 create_management_node_directory
 
  Parameters  : $directory_path
@@ -10736,6 +10818,52 @@ EOF
 
 #/////////////////////////////////////////////////////////////////////////////
 
+=head2 get_random_mac_address
+
+ Parameters  : $prefix (optional)
+ Returns     : string
+ Description : Generates a random MAC address.
+
+=cut
+
+sub get_random_mac_address {
+       my ($prefix) = @_;
+       
+       if ($prefix) {
+               # Add a trailing colon if not supplied in the argument
+               $prefix =~ s/:+$/:/;
+               
+               if ($prefix !~ /([a-f0-9]{2}:)+/i) {
+                       notify($ERRORS{'WARNING'}, 0, "invalid MAC address 
prefix argument: $prefix");
+                       return;
+               }
+               elsif (length($prefix) % 3 != 0) {
+                       notify($ERRORS{'WARNING'}, 0, "invalid MAC address 
prefix length: '$prefix'");
+                       return;
+               }
+               
+       }
+       else {
+               $prefix = '52:54:';
+       }
+       
+       my $random_octet_count = (6 - (length($prefix) / 3));
+       
+       # Remove the trailing colon
+       $prefix =~ s/:+$//;
+       
+       my $mac_address = uc($prefix);
+       
+       for (my $i=0; $i<$random_octet_count; $i++) {
+               $mac_address .= ":" . sprintf("%02X",int(rand(255)));
+       }
+       
+       notify($ERRORS{'DEBUG'}, 0, "generated random MAC address: 
'$mac_address'");
+       return $mac_address;
+}
+
+#/////////////////////////////////////////////////////////////////////////////
+
 1;
 __END__
 


Reply via email to