File: site-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,223 bytes parent folder | download | duplicates (2)
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 %sites;
my %sitegroups;
my %hostmap;
my %sitegroupmap;
my %opts;

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

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

for_all_hosts(\&handle_host);

print_summary();

sub handle_host {
    my $hostid = shift;
    #print "$hostid\n";
    for my $site (get_site($hostid)) {
        $site = "" unless defined $site;
        $sites{$site}++;
        my $sitegroup = get_sitegroup($hostid);
        if ($sitegroup) {
            $sitegroups{$site}{$sitegroup}++;
            if (exists $sitegroupmap{$site} && exists $sitegroupmap{$site}{$sitegroup}) {
                push @{$sitegroupmap{$site}{$sitegroup}}, $hostid ;
            } else {
                $sitegroupmap{$site}{$sitegroup} = [$hostid];
            }
        } else {
            if (exists $hostmap{$site}) {
                push @{$hostmap{$site}}, $hostid ;
            } else {
                $hostmap{$site} = [$hostid];
            }
        }
    }
}

sub print_summary {
    printf("  %-20s %5s\n", "site", "count");
    for my $site (sort keys %sites) {
        printf("  %-20s %5d\n", $site, $sites{$site});
        if (exists $opts{l}) {
            for my $hostid (sort @{$hostmap{$site}}) {
                my $hostname = get_hostname($hostid);
                my $site = get_site($hostid) || "";
                my $sitegroup = get_sitegroup($hostid) || "";
                printf "    %s %s/%s %s\n", $hostname, $site, $sitegroup, $hostid;
            }
        }
        for my $sitegroup (sort keys %{$sitegroups{$site}}) {
            printf("    %-18s %5d\n", $sitegroup, $sitegroups{$site}{$sitegroup});
            if (exists $opts{l}) {
                for my $hostid (sort @{$sitegroupmap{$site}{$sitegroup}}) {
                    my $hostname = get_hostname($hostid);
                    my $site = get_site($hostid) || "";
                    my $sitegroup = get_sitegroup($hostid) || "";
                    printf "      %s %s/%s %s\n", $hostname, $site, $sitegroup, $hostid;
                }
            }
        }
    }
}