File: sysinfo.pl

package info (click to toggle)
pdbv 2.0.10.1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 436 kB
  • ctags: 52
  • sloc: perl: 1,004; sh: 291; makefile: 129
file content (226 lines) | stat: -rw-r--r-- 5,803 bytes parent folder | download | duplicates (3)
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
# (c) 2002-2005 Mathieu Roy <yeupou@gnu.org>
# sysinfo.pl: this file is part of package_db_view
#
#   Any system specific values.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#  take a look at http://savannah.nongnu.org/projects/pdbv
#  send comments at <pdbv-dev@nongnu.org>
#
#  $Id: sysinfo.pl,v 1.13 2005/02/23 10:42:20 yeupou Exp $

print "The purpose of this file is not being executed alone";
exit;

# We want to know the computer  with deal with.
# We can access directly /proc/cpuinfo and this kind of files that
# give much informations. But in fact, most of the time, we will use uname
# et caetera. 
# It is more portable, but less informative with older uname.

# Distro version?
sub PdbvSysinfoGetDistro {
    my $hdist = 0;
    my $ret;
    my $debian_rel = "/etc/debian_version";
    my $redhat_rel = "/etc/redhat-release";
    my $url;

    if (-e $redhat_rel) {
	$hdist = $redhat_rel;
    } elsif (-e $debian_rel) {
	$hdist = $debian_rel;
	$url = "http://www.debian.org";
	$ret = gettext("Debian GNU release ");
    }
    
    if ($hdist) {
	open(FILE, $hdist);
	while (<FILE>) {
	    $ret .= $_; 
	}
	close(FILE);
    } else {
	$ret = gettext("unknown distribution");
    }
    
    if ($url) {
	# This is XHTML specific, should be removed if we add another
	# ouput type.
	return "<a href=\"".$url."\">".$ret."</a>";
    } else {
	return $ret;
    }
}

# Kernel?
sub PdbvSysinfoGetUnameKernel {
    my $uname_s = `uname -s 2> /dev/null`;
    my $uname_mr = `uname -mr 2> /dev/null`;
    my $uname_v = `uname -v 2> /dev/null`;
    chomp($uname_s, $uname_mr, $uname_v);
    my $uname_smr = "$uname_s $uname_mr";

    return sprintf(gettext("kernel %s, compiled %s"), $uname_smr, $uname_v);
}

# Cpu?
sub PdbvSysinfoGetUnameCpu {
    # We cannot rely on uname, linux does not provide correct informations
    # with something else than /proc/cpuinfo.
    #
    # So we work on /proc/cpuinfo. If it's too much work to maintain it,
    # we'll drop it.
    #
    # If you get "Processor Unknown" or something inappropriate,
    # you can give us the content of/proc/cpuinfo at
    # http://savannah.nongnu.org/bugs/?func=detailbug&bug_id=3751&group_id=2348
    # or at <pdbv-dev@nongnu.org>.
    # If you system does not have /proc/cpuinfo (hurd), you can tell us
    # how to get these informations.

    my $cpuinfo = "/proc/cpuinfo";
    if (-r $cpuinfo) {
	# Look for raw data, ok for:
	# i*86, powerpc, s390, hppa, alpha, ia64, sparc
	my $smp;
	my $bogomips;
	my $vendor;
	my $model;
	my $mhz;
	my $cache;
	my $bogomips;

	# We need to reset the input separator (set by dpkg.pl)
	my $is = $/;
	$/ = "\n";

	open(CPUINFO, "< $cpuinfo");
	while (<CPUINFO>) {
	    # Number of processors
	    if (m/^(processor(.*: \d| \d)|CPU\d)/i) {
		$smp++;
	    }
	    
	    # Details
	    if (m/^vendor/i) {
		# Ex: GenuineIntel
		s/[vendor|machine].*: (.*)/$1/i;
		chomp;
		$vendor = $_;
	    next;
	    }
	    if (m/^(cpu family)/i && !$vendor) {
		# Ex: PA-RISC 2.0 
		s/cpu family.*: (.*)/$1/i;
		chomp;
		$vendor = $_;
		next;	    
	    }
	    if (m/^(model name|cpu model|family|processor.*: Intel|cpu.*:.*Sparc)/i) {
		# Ex: Intel(R) Pentium(R) 4 CPU 1.70GHz
		#     PCA56 
		#     Itanium
		#     Intel StrongARM-110 rev 3 (v4l) 
		#     TI UltraSparc II (BlackBird) 
		s/^[model name|cpu model|family|processor|cpu].*: (.*)/$1/i;
		chomp;
		$model = $_;
		next;
	    }
	    if (m/^(cpu Mhz|clock)/i) {
		# Ex: 1700.121
		s/^[cpu Mhz|clock].*: (.*)/$1/i;
		chomp;
		$mhz = $_;
		next;
	    }
	    if (m/^bogomips/i) {
		# Ex: 3381.65
		s/^bogomips.*: (.*)/$1/i;
		chomp;
		$bogomips = $_;
		next;
	    }
	}
	close(CPUINFO);
	
	# Get back the input separator
	$/ = $is;

	# There must be at least one CPU
	$smp = 1 unless $smp;
	
	# Prepare the final output
	my $cpudetails;

	if ($vendor|$model) {
	    $cpudetails .= "$vendor " if $vendor;
	    $cpudetails .= "$model " if $model;
	} else {
	    $cpudetails .= gettext("unknown");
	}
	if ($mhz|$bogomips) {
	    $cpudetails .= "(";
	    $cpudetails .= "$mhz MHz, " if $mhz;
	    $cpudetails .= "$bogomips bogomips" if $bogomips;
	    $cpudetails .= ")";
	}
	
	# Print
	return $smp." ".sprintf(gettext("processor %s"), $cpudetails);
    }
}


# Ram, swap?
sub PdbvSysinfoGetFree {
    # Free can be available only if there is a /proc/meminfo
    # If so, we try to use free.
    # (There is little chance that procps is not installed)
    if (-e "/proc/meminfo") {
	my $kb = gettext("kB");
	my $memtotal = `free | grep Mem | awk '{print \$2}'`;
	my $swaptotal = `free | grep Swap | awk '{print \$2}'`;
	chomp($memtotal, $swaptotal);
	
	return sprintf(gettext("%s $kb mem, %s $kb swap"), $memtotal, $swaptotal);
    }
}

# Information about the /usr partition. The result will be used frequently
# so the function frequently used should preferably only return a $var
# defined once.
# NOTE: this stuff is not used right, % relative to the whole installed-size 
# from the database seems more sensible.

my $usr_partition;
my $usr_partition_size;
sub PdbvSysinfoDetermineSystemPartitionInfo {
    # We use as possible df
    open(DF, "df /usr |");
    while (<DF>) {
	my $df;
	($df) = /^(\/.*)$/;
	($usr_partition, $usr_partition_size) = split(" ", $df);
    }
    close(DF);
}

sub PdbvSysinfoGetSystemPartition {
    return $usr_partition;
}

sub PdbvSysinfoGetSystemPartitionSize {
    return $usr_partition_size;
}


# end