File: SiteSummary.pm

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 (294 lines) | stat: -rw-r--r-- 7,289 bytes parent folder | download
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#
# Support library for scripts parsing the sitesummary files.
#

package SiteSummary;
require Exporter;

our $VERSION = 0.01;
our @ISA     = qw(Exporter);
our @EXPORT  = qw(
                  for_all_hosts
                  get_filepath_current
                  get_hostclass
                  get_site
                  get_sitegroup
                  get_hostname
                  get_dns_address
                  get_macaddresses
                  get_primary_macaddress
                  get_primary_ip_address
                  get_linux_kernel_ver
                  get_debian_edu_profile
                  get_debian_edu_ver
                  get_hardware_info
                  );

my $pwd = "/var/lib/sitesummary/entries";  # Path to the entries

# File for debian-edu configuration
my $debian_edu_config = "/debian-edu/config";

sub get_filepath_current {
    my ($hostid, $file) = @_;
    return "$pwd/$hostid$file";
}

#
# Return the value string from a file, ignoring comments
#
sub get_file_string {
    my ($hostid, $filename) = @_;
    my $path = get_filepath_current($hostid, $filename);
    my $string;
    if (open (FILE, $path)) {
        while(<FILE>) {
            chomp;
            s/\#.+$//;
            next if (/^\s*$/);
            $string = $_;
        }
        close(FILE);
        return $string;
    } else {
        return undef;
    }
}

#
# Return the site string
#
sub get_site {
    my $hostid = shift;
    return get_file_string($hostid, "/siteinfo/site");
}

#
# Return the sitegroup string
#
sub get_sitegroup {
    my $hostid = shift;
    return get_file_string($hostid, "/siteinfo/sitegroup");
}

#
# Return the hostclass string
#
sub get_hostclass {
    my $hostid = shift;
    return get_file_string($hostid, "/siteinfo/hostclass");
}

#
# Return the IP address on the primary network interface
#
sub get_primary_ip_address {
    my $hostid = shift;
    my $path = get_filepath_current($hostid, "/system/ifconfig-a");
    # XXX Not properly implemented, just pick the first IP
    my $ip;
    if (open (FILE, $path)) {
        while(<FILE>) {
            chomp;
            if (m/inet addr:(\S+)\s+/) {
                $ip = $1;
                last;
            }
        }
        close(FILE);
        return $ip;
    } else {
        return undef;
    }
}

#
# Return all MAC addresses
sub get_macaddresses {
    my $hostid = shift;
    my $path = get_filepath_current($hostid, "/system/ifconfig-a");
    if (open (FILE, $path)) {
        my @macs;
        while(<FILE>) {
            chomp;
            if (m/Link encap:Ethernet\s+HWaddr (\S+)\s+/) {
                push(@macs, $1);
            }
        }
        close(FILE);
        return @macs;
    } else {
        return undef;
    }
}

#
# Return the IP address on the primary network interface
#
sub get_primary_macaddress {
    my $hostid = shift;
    my $path = get_filepath_current($hostid, "/system/ifconfig-a");
    # XXX Not properly implemented, just pick the first MAC after
    # sorting alphabetically.
    if (open (FILE, $path)) {
        my @macs;
        while(<FILE>) {
            chomp;
            if (m/Link encap:Ethernet\s+HWaddr (\S+)\s+/) {
                push(@macs, $1);
            }
        }
        close(FILE);
        return (sort @macs)[0];
    } else {
        return undef;
    }
}

#
# Return the hostname string
#
sub get_hostname {
    my $hostid = shift;
    return get_file_string($hostid, "/system/hostname");
}

#
# Return an address that can be used to contact the host.  Prefer DNS,
# but fall back to the IP address if the IP address do not resolve in
# DNS.
#
sub in_dns {
    my $hostname = shift;
    my $packed_ip = gethostbyname($hostname);
    return defined $packed_ip;
}
sub get_dns_address {
    my $hostid = shift;
    my $hostname = get_hostname($hostid);
    # Use IP address as hostname if the provided hostname is bogus or
    # missing in DNS.
    $hostname = get_primary_ip_address($hostid)
        if (! in_dns($hostname) || "localhost" eq $hostname);
    return $hostname;
}

#
# Return Linux kernel version for the machines using Linux.
#
sub get_linux_kernel_ver {
    my $hostid = shift;
    my $path = get_filepath_current($hostid, "/system/uname-smr");
    my $kver;
    if (open (FILE, $path)) {
        while(<FILE>) {
            chomp;
            s/\#.+$//;
            next if (/^\s*$/);
            my @f = (split(/\s+/, $_));
            $kver = $f[1] if ("Linux" eq $f[0]);
        }
        close(FILE);
        return $kver;
    } else {
        return undef;
    }
}

sub get_debian_edu_profile {
    my $hostid = shift;
    my $path = get_filepath_current($hostid, $debian_edu_config);
    if ( ! -e $path ) {
        return undef;
    }
    if (open (FILE, $path)) {
        while (<FILE>) {
                chomp;
                s/\#.+$//;
                next if not (/PROFILE/);
                s/^PROFILE=//;
                return $_;
        }
    }
    close(FILE);
}

sub get_debian_edu_ver {
    my $hostid = shift;
    my $path = get_filepath_current($hostid, $debian_edu_config);
    if ( ! -e $path ) {
        return undef;
    }
    if (open (FILE, $path)) {
        while (<FILE>) {
                chomp;
                s/\#.+$//;
                next if not (/VERSION/);
                s/^VERSION=//;
                return $_;
        }
    }
}

sub get_hardware_info {
    my $hostid = shift;
    my $path = get_filepath_current($hostid, "/system/dmidecode");
    if (open(FILE, "<", $path)) {
        my $sysinfo = 0;
        my ($vendor, $model, $version, $serial);
        while (<FILE>) {
            chomp;
            next unless ($sysinfo || m/^System Information/);
            $sysinfo = 1;
            $vendor = $1 if (m/Manufacturer: (.+\S)\s*$/);
            $model = $1 if (m/Product Name: (.+\S)\s*$/);
            $version = $1 if (m/Version: (.+\S)\s*$/);
            $serial = $1 if (m/Serial Number: (.+\S)\s*$/);
            last if (m/^Handle /);
        }
        close(FILE);

        # Append version string to get for example the thinkpad model
        # name, but ignore bogus entries.
        $model = "$model $version" if ($version
                                       && $version ne "Not Specified"
                                       && $version ne "Not Available"
                                       && $version ne "System Version"
                                       && $version ne "None");

        return ($vendor, $model, $serial);
    } else {
        return undef;
    }
}

sub for_all_hosts {
    my $callback = shift;

    if ( ! -d $pwd ) {
        print STDERR "error: Missing $pwd directory.\n";
        return undef;
    }
    opendir(DIR, $pwd) or die "$!: $pwd\nDied";
    my $count = 0;
    foreach (readdir(DIR)) {
        chomp;
        next if m/^$/ || m/^.$/ || m/^..$/;
        my $hostid = $_;
        if ( -d "$pwd/$hostid" ) {
            $count ++ if ($callback->($hostid));
        } else {
            print STDERR "warning: Junk in filelog: $pwd/$hostid\n";
        }
    }
    closedir(DIR);
    return $count;
}

1;

########################################################################
# Local Variables:
# mode: perl
# End:
########################################################################