File: hardware-model-summary

package info (click to toggle)
sitesummary 0.0.47
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 264 kB
  • ctags: 71
  • sloc: perl: 931; sh: 386; makefile: 63
file content (78 lines) | stat: -rwxr-xr-x 2,220 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/perl

use strict;
use warnings;

use SiteSummary;
use Getopt::Std;

my %vendors;
my %models;
my %hostmap;
my %modelmap;
my %opts;

sub usage {
    my $retval = shift;
    print <<EOF;
Usage: $0 [-l]
   -l   list hosts with the given vendor/model
EOF
    exit $retval;
}

getopt("l", \%opts) || usage(1);

for_all_hosts(\&handle_host);

print_summary();

sub handle_host {
    my $hostid = shift;
    #print "$hostid\n";
    my ($vendor, $model, undef) = get_hardware_info($hostid);
    $vendor = "[no-dmi-info]" unless defined $vendor;
    $vendors{$vendor}++;
    if ($model) {
        $models{$vendor}{$model}++;
        if (exists $modelmap{$vendor} && exists $modelmap{$vendor}{$model}) {
            push @{$modelmap{$vendor}{$model}}, $hostid ;
        } else {
            $modelmap{$vendor}{$model} = [$hostid];
        }
    } else {
        if (exists $hostmap{$vendor}) {
            push @{$hostmap{$vendor}}, $hostid ;
        } else {
            $hostmap{$vendor} = [$hostid];
        }
    }
}

sub print_summary {
    printf("  %-25s %5s\n", "vendor", "count");
    for my $vendor (sort keys %vendors) {
        printf("  %-25s %5d\n", $vendor, $vendors{$vendor});
        if (exists $opts{l}) {
            if (exists $hostmap{$vendor}) {
                for my $hostid (sort @{$hostmap{$vendor}}) {
                    my $hostname = get_hostname($hostid);
                    my ($vendor, $model, undef) = get_hardware_info($hostid);
                    $vendor = "[unknown]" unless defined $vendor;
                    $model = "" unless defined $model;
                    printf "    %s %s/%s %s\n", $hostname, $vendor, $model, $hostid;
                }
            }
        }
        for my $model (sort keys %{$models{$vendor}}) {
            printf("    %-23s %5d\n", $model, $models{$vendor}{$model});
            if (exists $opts{l}) {
                for my $hostid (sort @{$modelmap{$vendor}{$model}}) {
                    my $hostname = get_hostname($hostid);
                    my ($vendor, $model, undef) = get_hardware_info($hostid);
                    printf "      %s %s/%s %s\n", $hostname, $vendor, $model, $hostid;
                }
            }
        }
    }
}