Line 1... |
Line 1... |
1 |
<?php
|
1 |
<?php
|
2 |
|
2 |
|
3 |
|
3 |
/*
|
- |
|
4 |
* Trims a value, then compares it to several 'null' possibilities
|
- |
|
5 |
* if it is a null, returns empty string, otherwise returns value
|
- |
|
6 |
*/
|
4 |
function cleanUpDMIValue( $value ) {
|
7 |
function cleanUpDMIValue( $value ) {
|
5 |
$value = trim( $value );
|
8 |
$value = trim( $value );
|
6 |
switch (strtolower( $value )) {
|
9 |
switch (strtolower( $value )) {
|
7 |
case 'unknown':
|
10 |
case 'unknown':
|
- |
|
11 |
case 'unspecified' :
|
8 |
case 'not available':
|
12 |
case 'not available':
|
- |
|
13 |
case 'to be filled by o.e.m.':
|
9 |
case 'not specified': return '';
|
14 |
case 'not specified': return '';
|
10 |
}
|
15 |
}
|
11 |
return $value;
|
16 |
return $value;
|
12 |
}
|
17 |
}
|
13 |
|
18 |
|
14 |
/*
|
19 |
/*
|
15 |
* Read value from hash, return $outputKey\tvalue from hash
|
20 |
* Read value from array of hash, return array of "$outputKey\tvalue" for each entry found
|
- |
|
21 |
* If nothing found, returns empty array
|
- |
|
22 |
* if $unique is true, returns first non-empty value found
|
16 |
* Or, empty string if not found
|
23 |
* if $outputKey is not defined, uses $key
|
17 |
*/
|
24 |
*/
|
18 |
function getDMIInformation( $data, $type, $handle, $key, $outputKey = '' ) {
|
25 |
function getDMIInformation( $data, $type, $key, $outputKey = '', $unique = true ) {
|
- |
|
26 |
$return = array();
|
- |
|
27 |
foreach ( $data[$type] as $handle ) {
|
19 |
if ( isset( $data[$type][$handle][$key]) ) {
|
28 |
if ( isset( $handle[$key]) ) {
|
20 |
$a = cleanUpDMIValue( $data[$type][$handle][$key] );
|
29 |
$a = cleanUpDMIValue( $handle[$key] );
|
21 |
if ( $a )
|
30 |
if ( $a ) {
|
22 |
return ( $outputKey ? $outputKey : $key ) . "\t" . $a;
|
31 |
$return[] = ( $outputKey ? $outputKey : $key ) . "\t" . $a;
|
- |
|
32 |
if ( $unique ) {
|
- |
|
33 |
return $return;
|
- |
|
34 |
}
|
- |
|
35 |
}
|
- |
|
36 |
}
|
23 |
}
|
37 |
}
|
24 |
return '';
|
38 |
return $return;
|
25 |
}
|
39 |
}
|
26 |
|
40 |
|
- |
|
41 |
/*
|
- |
|
42 |
* adds $value to array
|
- |
|
43 |
* if $value is scalar, simply adds it
|
- |
|
44 |
* if $value is array, adds every row
|
- |
|
45 |
* returns modified array
|
- |
|
46 |
*/
|
- |
|
47 |
function addData( $array, $value ) {
|
- |
|
48 |
if ( $value ) {
|
- |
|
49 |
switch (gettype( $value ) ) {
|
- |
|
50 |
case 'array' : foreach ( $value as $thisVal ) {
|
- |
|
51 |
$array[] = $thisVal;
|
- |
|
52 |
}
|
- |
|
53 |
break;
|
- |
|
54 |
case 'boolean' :
|
- |
|
55 |
case 'integer' :
|
- |
|
56 |
case 'double' :
|
- |
|
57 |
case 'string' : $array[] = $value;
|
- |
|
58 |
} // switch
|
- |
|
59 |
}
|
- |
|
60 |
return $array;
|
- |
|
61 |
} //addData
|
27 |
|
62 |
|
28 |
/*
|
63 |
/*
|
29 |
* Function will take an array of lines representing the output of a dmidecode file
|
64 |
* Function will take an array of lines representing the output of a dmidecode file
|
30 |
* and pick and choose the values we want to put into CAMP attributes.
|
65 |
* and pick and choose the values we want to put into CAMP attributes.
|
31 |
* It will return an array of key/value pairs to be added into the attributes table
|
66 |
* It will return an array of key/value pairs to be added into the attributes table
|
- |
|
67 |
*
|
- |
|
68 |
* NOTE: this is a summary of values we decided to store. dmidecode files have a lot of
|
- |
|
69 |
* additional information we chose not to include
|
32 |
*
|
70 |
*
|
33 |
*/
|
71 |
*/
|
34 |
|
72 |
|
35 |
function dmidecode2array( $contents ) {
|
73 |
function dmidecode2array( $contents ) {
|
36 |
|
74 |
|
37 |
// allows us to group the dmi types by function
|
75 |
// allows us to group the dmi types by function. not implemented
|
38 |
$dmiGroups = array(
|
76 |
$dmiGroups = array(
|
39 |
"bios" => "0,13",
|
77 |
"bios" => "0,13",
|
40 |
"system" => "1,12,15,23,32",
|
78 |
"system" => "1,12,15,23,32",
|
41 |
"baseboard" => "2,10,41",
|
79 |
"baseboard" => "2,10,41",
|
42 |
"chassis" => "3",
|
80 |
"chassis" => "3",
|
Line 45... |
Line 83... |
45 |
"cache" => "7",
|
83 |
"cache" => "7",
|
46 |
"connector" => "8",
|
84 |
"connector" => "8",
|
47 |
"slot" => "9"
|
85 |
"slot" => "9"
|
48 |
);
|
86 |
);
|
49 |
|
87 |
|
50 |
// This contains the standard DMI types, ie no OEM stuff
|
88 |
// This contains the standard DMI types, ie no OEM stuff. For reference, not used in code
|
51 |
|
89 |
|
52 |
$standardDMITypes = array(
|
90 |
$standardDMITypes = array(
|
53 |
"0" => "BIOS",
|
91 |
"0" => "BIOS",
|
54 |
"1" => "System",
|
92 |
"1" => "System",
|
55 |
"2" => "Baseboard",
|
93 |
"2" => "Baseboard",
|
Line 93... |
Line 131... |
93 |
"40" => "Additional Information",
|
131 |
"40" => "Additional Information",
|
94 |
"41" => "Onboard Devices Extended Information",
|
132 |
"41" => "Onboard Devices Extended Information",
|
95 |
"42" => "Management Controller Host Interface"
|
133 |
"42" => "Management Controller Host Interface"
|
96 |
);
|
134 |
);
|
97 |
|
135 |
|
98 |
// verify this is something we can work with
|
136 |
// verify this is something we can work with, First line should be the version of dmidecode found
|
99 |
preg_match('/dmidecode ([0-9.]+)/', $contents[0], $results);
|
137 |
if ( preg_match('/dmidecode ([0-9.]+)/', $contents[0], $results) ) {
|
100 |
#if ( $results[1] != "3.0" ) {
|
138 |
$dmidecodeVersion = $results[1];
|
- |
|
139 |
} else {
|
101 |
# return "This is only verified with dmidecode v3.0, v$results[1] found\n";
|
140 |
return "Not a valid dmidecode file";
|
102 |
#}
|
141 |
}
|
103 |
// first, let's parse it into a hash
|
142 |
// first, let's parse it into a hash
|
104 |
$currentHandle = '';
|
143 |
$currentHandle = '';
|
105 |
$currentType;
|
144 |
$currentType;
|
106 |
$data = array();
|
145 |
$data = array();
|
107 |
for ( $i = 0; $i < count($contents); $i++ ) {
|
146 |
for ( $i = 0; $i < count($contents); $i++ ) {
|
Line 121... |
Line 160... |
121 |
$data[$currentType][$currentHandle][$outputArray[1]] = $outputArray[2];
|
160 |
$data[$currentType][$currentHandle][$outputArray[1]] = $outputArray[2];
|
122 |
}
|
161 |
}
|
123 |
}
|
162 |
}
|
124 |
# well, we have at least one entry, so let's go
|
163 |
# well, we have at least one entry, so let's go
|
125 |
$return = array();
|
164 |
$return = array();
|
126 |
// get info on the actual machine
|
165 |
// manufacturer info
|
127 |
$temp = &getDMIInformation( $data, '1','0x0100','Manufacturer' );
|
166 |
$return = addData( $return, getDMIInformation( $data, '1','Manufacturer' ) );
|
128 |
if ( $temp ) $return[] = $temp;
|
- |
|
129 |
$temp = &getDMIInformation( $data, '1','0x0100','Product Name', 'Model' );
|
167 |
$return = addData( $return, getDMIInformation( $data, '1','Product Name', 'Model' ) );
|
130 |
if ( $temp ) $return[] = $temp;
|
- |
|
131 |
$temp = &getDMIInformation( $data, '1','0x0100','Serial Number' );
|
168 |
$return = addData( $return, getDMIInformation( $data, '1','Serial Number' ) );
|
132 |
if ( $temp ) $return[] = $temp;
|
- |
|
133 |
$temp = &getDMIInformation( $data, '1','0x0100','UUID' );
|
169 |
$return = addData( $return, getDMIInformation( $data, '1','UUID' ) );
|
134 |
if ( $temp ) $return[] = $temp;
|
- |
|
135 |
// firmware information
|
170 |
// physical machine
|
136 |
$temp = &getDMIInformation( $data, '0','0x0000','Vendor', 'Firmware Vendor' );
|
171 |
$return = addData( $return, getDMIInformation( $data, '3','Type', 'Enclosure Type' ) );
|
137 |
if ( $temp ) $return[] = $temp;
|
- |
|
138 |
$temp = &getDMIInformation( $data, '0','0x0000','Release Date', 'Firmware Release' );
|
172 |
$return = addData( $return, getDMIInformation( $data, '3','Asset Tag', 'Enclosure Asset Tag' ) );
|
139 |
if ( $temp ) $return[] = $temp;
|
- |
|
140 |
$temp = &getDMIInformation( $data, '0','0x0000','Version', 'Firmware Version' );
|
173 |
$return = addData( $return, getDMIInformation( $data, '3','Height', 'Enclosure Height' ) );
|
- |
|
174 |
|
141 |
if ( $temp ) $return[] = $temp;
|
175 |
// firmware version
|
142 |
$temp = &getDMIInformation( $data, '0','0x0000','Firmware Revision', 'Firmware Revision' );
|
176 |
$return = addData( $return, getDMIInformation( $data, '0','Vendor', 'Firmware Vendor' ) );
|
143 |
if ( $temp ) $return[] = $temp;
|
- |
|
144 |
// physical size
|
- |
|
145 |
$temp = &getDMIInformation( $data, '3','0x0300','Type', 'Enclosure Type' );
|
177 |
$return = addData( $return, getDMIInformation( $data, '0','Release Date', 'Firmware Release' ) );
|
146 |
if ( $temp ) $return[] = $temp;
|
- |
|
147 |
$temp = &getDMIInformation( $data, '3','0x0300','Asset Tag', 'Enclosure Asset Tag' );
|
178 |
$return = addData( $return, getDMIInformation( $data, '0','Version', 'Firmware Version' ) );
|
148 |
if ( $temp ) $return[] = $temp;
|
- |
|
149 |
$temp = &getDMIInformation( $data, '3','0x0300','Height', 'Enclosure Height' );
|
179 |
$return = addData( $return, getDMIInformation( $data, '0','Firmware Revision', 'Firmware Revision' ) );
|
150 |
if ( $temp ) $return[] = $temp;
|
- |
|
151 |
// processor information
|
180 |
// processor information. NOTE: assumes all sockets populated and all processors the saem
|
152 |
if ( isset( $data['4'] ) )
|
181 |
if ( isset( $data['4'] ) ) // count the number of sockets
|
153 |
$return[] = "CPU Count\t" . count( $data['4'] ); // fails if there are unpopulated slots or disabled ones
|
182 |
$return[] = "CPU Count\t" . count( $data['4'] );
|
154 |
|
- |
|
155 |
$temp = &getDMIInformation( $data, '4','0x0400','Family', 'CPU Family' );
|
183 |
$return = addData( $return, getDMIInformation( $data, '4','Family', 'CPU Family' ) );
|
156 |
if ( $temp ) $return[] = $temp;
|
- |
|
157 |
$temp = &getDMIInformation( $data, '4','0x0400','Manufacturer', 'CPU Manufacturer' );
|
184 |
$return = addData( $return, getDMIInformation( $data, '4','Manufacturer', 'CPU Manufacturer' ) );
|
158 |
if ( $temp ) $return[] = $temp;
|
- |
|
159 |
$temp = &getDMIInformation( $data, '4','0x0400','Version', 'CPU Version' );
|
185 |
$return = addData( $return, getDMIInformation( $data, '4','Version', 'CPU Version' ) );
|
160 |
if ( $temp ) $return[] = $temp;
|
- |
|
161 |
$temp = &getDMIInformation( $data, '4','0x0400','Current Speed', 'CPU Speed' );
|
186 |
$return = addData( $return, getDMIInformation( $data, '4','Current Speed', 'CPU Speed' ) );
|
162 |
if ( $temp ) $return[] = $temp;
|
- |
|
163 |
$temp = &getDMIInformation( $data, '4','0x0400','Core Count', 'CPU Cores (ea)' );
|
187 |
$return = addData( $return, getDMIInformation( $data, '4','Core Count', 'CPU Cores (ea)' ) );
|
164 |
if ( $temp ) $return[] = $temp;
|
- |
|
165 |
$temp = &getDMIInformation( $data, '4','0x0400','Thread Count', 'CPU Threads (ea)' );
|
188 |
$return = addData( $return, getDMIInformation( $data, '4','Thread Count', 'CPU Threads (ea)' ) );
|
166 |
if ( $temp ) $return[] = $temp;
|
- |
|
167 |
|
189 |
|
- |
|
190 |
/*
|
- |
|
191 |
* memory information. We want details on each slot for this, so we have to go through each entry, one at a time.
|
- |
|
192 |
* For readability, we also want to take several values and turn them into one human
|
- |
|
193 |
* readable string for each socket. So, we go through each entry in the memory socket, determine if it is populated
|
168 |
// memory information
|
194 |
* then concat it together for a pretty output
|
- |
|
195 |
*/
|
- |
|
196 |
|
169 |
if ( isset( $data['17'] ) ) {
|
197 |
if ( isset( $data['17'] ) ) {
|
170 |
$return[] = "Memory Slots\t" . count( $data['17'] );
|
198 |
$return[] = "Memory Slots\t" . count( $data['17'] );
|
171 |
$totalRAM = 0;
|
199 |
$totalRAM = 0; // we'll accumulate the total size as we find slots populated
|
172 |
foreach ( $data['17'] as $entry ) {
|
200 |
foreach ( $data['17'] as $entry ) { // type 17 has one entry for every DIMM slot
|
173 |
$mem = array();
|
201 |
$mem = array(); // fill this up with the values we want to turn into a string, then we'll implode it into the string
|
174 |
$mem[] = trim($entry['Locator'] ? $entry['Locator'] : ($entry['Bank Locator'] ? $entry['Bank Locator'] : '' ));
|
202 |
$mem[] = trim($entry['Locator'] ? $entry['Locator'] : ($entry['Bank Locator'] ? $entry['Bank Locator'] : '' ));
|
175 |
if ( $entry['Size'] == 'No Module Installed' ) {
|
203 |
if ( $entry['Size'] == 'No Module Installed' ) {
|
176 |
$mem[] = 'Unpopulated';
|
204 |
$mem[] = 'Unpopulated';
|
177 |
} else {
|
205 |
} else {
|
178 |
$mem[] = trim($entry['Size']);
|
206 |
$mem[] = trim($entry['Size']); // size can be kilo, meg, gig, tera, so convert it to mega
|
179 |
if ( preg_match('/(\d+)\s+(GB|MB|TB|kb)/i', $entry['Size'] , $outputArray) ) {
|
207 |
if ( preg_match('/(\d+)\s+(GB|MB|TB|kb)/i', $entry['Size'] , $outputArray) ) {
|
180 |
switch ( strtolower( $outputArray[2] ) ) {
|
208 |
switch ( strtolower( $outputArray[2] ) ) {
|
181 |
case 'gb' :
|
209 |
case 'gb' :
|
182 |
$outputArray[1] *= 1024;
|
210 |
$outputArray[1] *= 1024;
|
183 |
break;
|
211 |
break;
|
Line 205... |
Line 233... |
205 |
$return[] = "Memory\t" . implode( ', ', $mem );
|
233 |
$return[] = "Memory\t" . implode( ', ', $mem );
|
206 |
}
|
234 |
}
|
207 |
$return[] = "Memory Total\t$totalRAM MB";
|
235 |
$return[] = "Memory Total\t$totalRAM MB";
|
208 |
}
|
236 |
}
|
209 |
|
237 |
|
- |
|
238 |
/*
|
- |
|
239 |
* power supplies. Most workstations will not even have an entry here, but for servers we will have details on each individual
|
- |
|
240 |
* power supply, including the part number, serial number, etc..., so like memory, we want a detailed listing with a human
|
210 |
// power supplies
|
241 |
* readable string for each psu
|
- |
|
242 |
*/
|
211 |
if ( isset( $data['39'] ) ) {
|
243 |
if ( isset( $data['39'] ) ) {
|
212 |
$return[] = "Power Supply Count\t" . count( $data['39'] );
|
244 |
$return[] = "Power Supply Count\t" . count( $data['39'] );
|
213 |
foreach ( $data['39'] as $entry ) {
|
245 |
foreach ( $data['39'] as $entry ) {
|
214 |
if ( preg_match('/^Present/i', $entry['Status'] ) ) {
|
246 |
if ( preg_match('/^Present/i', $entry['Status'] ) ) {
|
215 |
$psu = array();
|
247 |
$psu = array();
|
Line 221... |
Line 253... |
221 |
$return[] = "Power Supply\t" . implode( ', ', $psu );
|
253 |
$return[] = "Power Supply\t" . implode( ', ', $psu );
|
222 |
}
|
254 |
}
|
223 |
}
|
255 |
}
|
224 |
}
|
256 |
}
|
225 |
return array( $return );
|
257 |
return array( $return );
|
- |
|
258 |
} // dmidecode2array
|
226 |
}
|
259 |
|
227 |
|
260 |
|
228 |
$filename = $argv[1];
|
261 |
$filename = $argv[1];
|
229 |
if ( $filename ) {
|
262 |
if ( $filename ) {
|
230 |
$contents = file( $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
|
263 |
$contents = file( $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
|
231 |
$out = dmidecode2array( $contents );
|
264 |
$out = dmidecode2array( $contents );
|