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
|
#
# 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_site
get_sitegroup
get_linux_kernel_ver
get_debian_edu_ver
);
my $pwd = "/var/lib/sitesummary/entries"; # Path to the entries
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 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_ver {
# my $hostid = shift;
my $debian_edu_config = "/etc/debian-edu/config";
if ( ! -e $debian_edu_config ) {
print STDERR "error: Missing $debian_edu_config file.\n";
return undef;
}
my $profiles = "Main-Server Workstation Thin-Client-Server Standalone";
my @profile = split(/ /, $profiles);
open(FILE, $debian_edu_config) or die "$!: $debian_edu_config\nDied";
while ( my @config = <FILE> ) {
if ( "@config" =~ "$profile[0]" ) {
return $profile[0];
}elsif ( "@config" =~ "$profile[1]" ) {
return $profile[1];
}elsif ( "@config" =~ "$profile[2]" ) {
return $profile[2];
}elsif ( "@config" =~ "$profile[3]" ) {
return $profile[3];
}else{
print STDERR "error: Can't determine debian-edu profile.\n";
return undef;
}
}
close(FILE);
}
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:
########################################################################
|