| 76 | rodolico | 1 | #! /usr/bin/env perl
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | # parse a Belarc Advisor file to get the values out of it
 | 
        
           |  |  | 4 | # there are many more values available, but we are only getting a few
 | 
        
           |  |  | 5 |   | 
        
           |  |  | 6 | use strict;
 | 
        
           |  |  | 7 | use warnings;
 | 
        
           |  |  | 8 |   | 
        
           |  |  | 9 | my $VERSION = 0.9;
 | 
        
           |  |  | 10 | my $DATE = '20180824';
 | 
        
           |  |  | 11 |   | 
        
           |  |  | 12 | my @out;
 | 
        
           |  |  | 13 |   | 
        
           |  |  | 14 | package HTMLStrip;
 | 
        
           |  |  | 15 | use base "HTML::Parser";
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 | sub cleanup {
 | 
        
           |  |  | 18 |    my $line = shift;
 | 
        
           |  |  | 19 |    $line =~ s/ / /g;
 | 
        
           |  |  | 20 |    #chomp $line;
 | 
        
           |  |  | 21 |    # ltrim only
 | 
        
           |  |  | 22 |    #$line =~ s/^\s+//;
 | 
        
           |  |  | 23 |    # rtrim only
 | 
        
           |  |  | 24 |    #$line =~ s/\s+$//;
 | 
        
           |  |  | 25 |    # trim
 | 
        
           |  |  | 26 |    $line =~ s/^\s+|\s+$//g;
 | 
        
           |  |  | 27 |    return $line;
 | 
        
           |  |  | 28 | }
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | sub text {
 | 
        
           |  |  | 32 |    my ( $self,$text) = @_;
 | 
        
           |  |  | 33 |    chomp $text;
 | 
        
           |  |  | 34 |    push @out, &cleanup( $text ) if $text !~ m/^\s*$/;
 | 
        
           |  |  | 35 | }
 | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 | my $client = shift;
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 | my $p = new HTMLStrip;
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 | while (<>) {
 | 
        
           |  |  | 42 |    $p->parse($_);
 | 
        
           |  |  | 43 | }
 | 
        
           |  |  | 44 | # flush and parse remaining unparsed HTML
 | 
        
           |  |  | 45 | $p->eof;
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 | my %definition;
 | 
        
           |  |  | 48 |   | 
        
           |  |  | 49 | #print join( "\n", @out );
 | 
        
           |  |  | 50 | my $i = 0;
 | 
        
           |  |  | 51 | while ( $i < @out ) {
 | 
        
           |  |  | 52 |    if ( $out[$i] eq 'Computer Name:' ) {
 | 
        
           |  |  | 53 |       $definition{'hostname'} = $out[++$i];
 | 
        
           |  |  | 54 |    } elsif ($out[$i] eq 'DNS Suffix:' ) {
 | 
        
           |  |  | 55 |       $definition{'dns'} = $out[++$i];
 | 
        
           |  |  | 56 |    } elsif ($out[$i] eq 'Profile Date:' ) {
 | 
        
           |  |  | 57 |       $definition{'reportdate'} = $out[++$i];
 | 
        
           |  |  | 58 |    } elsif ($out[$i] eq 'Operating System' ) {
 | 
        
           |  |  | 59 |       $definition{'os'} = $out[++$i];
 | 
        
           |  |  | 60 |    } elsif ($out[$i] eq 'Auto IP Address:' ) {
 | 
        
           |  |  | 61 |       $definition{'ip'} = $out[++$i];
 | 
        
           |  |  | 62 |       my $j = $i;
 | 
        
           |  |  | 63 |       for ( my $j = $i; $j - $i < 10; $j++ ) {
 | 
        
           |  |  | 64 |          if ( $out[$j] eq 'Physical Address:' ) {
 | 
        
           |  |  | 65 |             $definition{'mac'} = $out[$j+1];
 | 
        
           |  |  | 66 |             $i = $j;
 | 
        
           |  |  | 67 |             last;
 | 
        
           |  |  | 68 |          } # for
 | 
        
           |  |  | 69 |       } # foreach
 | 
        
           |  |  | 70 |    } elsif ($out[$i] =~ m/System Serial Number: (.*)/ ) {
 | 
        
           |  |  | 71 |       $definition{'serial'} = $1;
 | 
        
           |  |  | 72 |    } elsif ($out[$i] =~ m/Chassis Serial Number: (.*)/ ) {
 | 
        
           |  |  | 73 |       $definition{'tag'} = $1;
 | 
        
           |  |  | 74 |    } elsif ($out[$i] =~ m/Enclosure Type: (.*)/ ) {
 | 
        
           |  |  | 75 |       $definition{'enclosure'} = $1;
 | 
        
           |  |  | 76 |    } elsif ($out[$i] eq 'System Model' ) {
 | 
        
           |  |  | 77 |       $definition{'model'} = $out[++$i];
 | 
        
           |  |  | 78 |    } elsif ($out[$i] eq 'Processor' ) {
 | 
        
           |  |  | 79 |       $i += 2;
 | 
        
           |  |  | 80 |       $definition{'cpu'} = $out[$i];
 | 
        
           |  |  | 81 |    } elsif ($out[$i] eq 'Memory Modules' ) {
 | 
        
           |  |  | 82 |       $i += 2;
 | 
        
           |  |  | 83 |       $definition{'memory'} = $out[$i];
 | 
        
           |  |  | 84 |    } elsif ($out[$i] eq 'Software Licenses' ) {
 | 
        
           |  |  | 85 |       if ( $out[$i+1] eq '[' ) {
 | 
        
           |  |  | 86 |          $i += 4;
 | 
        
           |  |  | 87 |          while ( $i < @out && $out[$i] ne 'Find unused software and reduce licensing costs...' ) {
 | 
        
           |  |  | 88 |             $definition{'--' . $out[$i]} = $out[$i+1];
 | 
        
           |  |  | 89 |             $i += 2;
 | 
        
           |  |  | 90 |          }
 | 
        
           |  |  | 91 |       }
 | 
        
           |  |  | 92 |    }
 | 
        
           |  |  | 93 |    $i++;
 | 
        
           |  |  | 94 | }
 | 
        
           |  |  | 95 |   | 
        
           |  |  | 96 | # dump storage
 | 
        
           |  |  | 97 | #foreach my $key ( sort keys %definition ) {  print "$key\t$definition{$key}\n"; }
 | 
        
           |  |  | 98 |   | 
        
           |  |  | 99 | my $output;
 | 
        
           |  |  | 100 | my $temp;
 | 
        
           |  |  | 101 | my @temp;
 | 
        
           |  |  | 102 |   | 
        
           |  |  | 103 | # for several things, we need to double quote them, so we do it all at once
 | 
        
           |  |  | 104 | foreach my $toQuote ( 'os', 'os_installed', 'mac', 'serial', 'tag', 'reportdate' ) {
 | 
        
           |  |  | 105 |    $definition{$toQuote} = qq/$definition{$toQuote}/ if $definition{$toQuote};
 | 
        
           |  |  | 106 | }
 | 
        
           |  |  | 107 |   | 
        
           |  |  | 108 | # calculate system information
 | 
        
           |  |  | 109 | # clean hostname and add dns name if it exists
 | 
        
           |  |  | 110 | $definition{'hostname'} =~ m/^([a-z0-9.-]+)/;
 | 
        
           |  |  | 111 | $definition{'hostname'} = $1;
 | 
        
           |  |  | 112 | $definition{'hostname'} .= '.' . $definition{'dns'} if $definition{'dns'};
 | 
        
           |  |  | 113 | # clean memory and convert to kb
 | 
        
           |  |  | 114 | @temp = split( ' ', $definition{'memory'} );
 | 
        
           |  |  | 115 | $temp[0] *= 1024 if ( $temp[1] eq 'Megabytes' );
 | 
        
           |  |  | 116 | $definition{'memory'} = $temp[0];
 | 
        
           |  |  | 117 |   | 
        
           |  |  | 118 | # if serial not defined but tag is, set serial to tag
 | 
        
           |  |  | 119 | if ( $definition{'tag'} && ! $definition{'serial'} ) {
 | 
        
           |  |  | 120 |    $definition{'serial'} = $definition{'tag'};
 | 
        
           |  |  | 121 |    $definition{'tag'} = undef;
 | 
        
           |  |  | 122 | };
 | 
        
           |  |  | 123 |   | 
        
           |  |  | 124 | # parse cpu type, etc..
 | 
        
           |  |  | 125 | if ( $definition{'cpu'} =~ m/([0-9.]+) gigahertz (\w+) (.*)/ ) {
 | 
        
           |  |  | 126 |    $definition{'cpu_speed'} = $1 * 1024;
 | 
        
           |  |  | 127 |    $definition{'cpu_type'} = $2;
 | 
        
           |  |  | 128 |    $definition{'cpu_sub'} = $3;
 | 
        
           |  |  | 129 | }
 | 
        
           |  |  | 130 |   | 
        
           |  |  | 131 |   | 
        
           |  |  | 132 | # populate system information
 | 
        
           |  |  | 133 | $output = "system:\n";
 | 
        
           |  |  | 134 | $output .= "  hostname: $definition{hostname}\n";
 | 
        
           |  |  | 135 | $output .= "  serial: $definition{serial}\n" if $definition{'serial'};
 | 
        
           |  |  | 136 | $output .= "  tag: $definition{tag}\n" if $definition{'tag'};
 | 
        
           |  |  | 137 | $output .= "  memory: $definition{memory}\n" if $definition{'memory'};
 | 
        
           |  |  | 138 | $output .= "  cpu: $definition{cpu}\n" if $definition{'cpu'};
 | 
        
           |  |  | 139 | $output .= "  cpu_speed: $definition{cpu_speed}\n" if $definition{'cpu_speed'};
 | 
        
           |  |  | 140 | $output .= "  cpu_type: $definition{cpu_type}\n" if $definition{'cpu_type'};
 | 
        
           |  |  | 141 | $output .= "  cpu_sub: $definition{cpu_sub}\n" if $definition{'cpu_sub'};
 | 
        
           |  |  | 142 |   | 
        
           |  |  | 143 | # network information
 | 
        
           |  |  | 144 | @temp = split( '/', $definition{'ip'} );
 | 
        
           |  |  | 145 | $output .= "\nnetwork:\n  NIC:\n";
 | 
        
           |  |  | 146 | $output .= "    address: $temp[0]\n" if $temp[0];
 | 
        
           |  |  | 147 | $output .= "    netmask: $temp[1]\n" if $temp[1];
 | 
        
           |  |  | 148 | $output .= "    mac: $definition{mac}\n" if $definition{'mac'};
 | 
        
           |  |  | 149 |   | 
        
           |  |  | 150 | $output .= "\noperatingsystem:\n";
 | 
        
           |  |  | 151 | $output .= "  os_version:  \"$definition{os}\"\n" if $definition{'os'};
 | 
        
           |  |  | 152 | $output .= "  installed: $definition{os_installed}\n" if $definition{'os_installed'};
 | 
        
           |  |  | 153 |   | 
        
           |  |  | 154 | # report information
 | 
        
           |  |  | 155 | $output .= "\nreport:\n";
 | 
        
           |  |  | 156 | $output .= "  date: $definition{reportdate}\n" if $definition{'reportdate'};
 | 
        
           |  |  | 157 | $output .= "  version: 3.0.0\n";
 | 
        
           |  |  | 158 | $output .= "  client: $client\n";
 | 
        
           |  |  | 159 |   | 
        
           |  |  | 160 | # clean up YAML
 | 
        
           |  |  | 161 | $output = "#parseBelarc $VERSION YAML\n---\n" . $output . "---\n";
 | 
        
           |  |  | 162 | open REPORT,">$definition{hostname}.yaml" or die "Could not write to $definition{hostname}.yaml: $!\n";
 | 
        
           |  |  | 163 | print REPORT "$output";
 | 
        
           |  |  | 164 | close REPORT;
 | 
        
           |  |  | 165 |   | 
        
           |  |  | 166 | open SOFTWARE,">$definition{hostname}.csv" or die "Could not write to $definition{hostname}.csv: $!\n";
 | 
        
           |  |  | 167 | foreach my $key ( keys %definition ) {
 | 
        
           |  |  | 168 |    next if $key !~ m/^\-\-(.*)$/;
 | 
        
           |  |  | 169 |    my $product = $1;
 | 
        
           |  |  | 170 |    next if $key =~ m/Microsoft - PowerShell/;
 | 
        
           |  |  | 171 |    next if $key =~ m/Microsoft - Internet Explorer/;
 | 
        
           |  |  | 172 |    next if $key =~ m/Belarc - Advisor/;
 | 
        
           |  |  | 173 |    if ( $definition{$key} =~ m/\(Key: ([^)]+)/ ) { 
 | 
        
           |  |  | 174 |       $definition{$key} = $1;
 | 
        
           |  |  | 175 |    }
 | 
        
           |  |  | 176 |    print SOFTWARE "$client\t$product\t$definition{$key}\t$definition{hostname}\n";
 | 
        
           |  |  | 177 | }
 | 
        
           |  |  | 178 | close SOFTWARE;
 | 
        
           |  |  | 179 |   | 
        
           |  |  | 180 |   | 
        
           |  |  | 181 | #print '='x40 . "\n"; print join( "\n", @out );
 | 
        
           |  |  | 182 |   |