Subversion Repositories computer_asset_manager_v1

Rev

Rev 98 | Rev 100 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
96 rodolico 1
<?php
2
 
99 rodolico 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
 */
98 rodolico 7
function cleanUpDMIValue( $value ) {
8
   $value = trim( $value );
9
   switch (strtolower( $value )) {
10
      case 'unknown':
99 rodolico 11
      case 'unspecified' :
98 rodolico 12
      case 'not available':
99 rodolico 13
      case 'to be filled by o.e.m.':
98 rodolico 14
      case 'not specified': return '';
15
   }
16
   return $value;
17
}
18
 
97 rodolico 19
/*
99 rodolico 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
23
 * if $outputKey is not defined, uses $key
97 rodolico 24
 */
99 rodolico 25
function getDMIInformation( $data, $type, $key, $outputKey = '', $unique = true ) {
26
   $return = array();
27
   foreach ( $data[$type] as $handle ) {
28
      if ( isset( $handle[$key]) ) {
29
         $a = cleanUpDMIValue( $handle[$key] );
30
         if ( $a ) {
31
            $return[] = ( $outputKey ? $outputKey : $key ) . "\t" . $a;
32
            if ( $unique ) {
33
               return $return;
34
            }
35
         }
36
      }
97 rodolico 37
   }
99 rodolico 38
   return $return;
97 rodolico 39
}
96 rodolico 40
 
99 rodolico 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
97 rodolico 62
 
96 rodolico 63
/*
64
 * Function will take an array of lines representing the output of a dmidecode file
65
 * and pick and choose the values we want to put into CAMP attributes.
66
 * It will return an array of key/value pairs to be added into the attributes table
99 rodolico 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
96 rodolico 70
 * 
71
 */
72
 
73
function dmidecode2array( $contents ) {
74
 
99 rodolico 75
// allows us to group the dmi types by function. not implemented
96 rodolico 76
$dmiGroups = array(
77
      "bios" => "0,13",
78
      "system" => "1,12,15,23,32",
79
      "baseboard" => "2,10,41",
80
      "chassis" => "3",
81
      "processor" => "4",
82
      "memory" => "5,6,16,17",
83
      "cache" => "7",
84
      "connector" => "8",
85
      "slot" => "9"
86
);
87
 
99 rodolico 88
// This contains the standard DMI types, ie no OEM stuff. For reference, not used in code
96 rodolico 89
 
90
$standardDMITypes = array( 
91
      "0" => "BIOS",
92
      "1" => "System",
93
      "2" => "Baseboard",
94
      "3" => "Chassis",
95
      "4" => "Processor",
96
      "5" => "Memory Controller",
97
      "6" => "Memory Module",
98
      "7" => "Cache",
99
      "8" => "Port Connector",
100
      "9" => "System Slots",
101
      "10" => "On Board Devices",
102
      "11" => "OEM Strings",
103
      "12" => "System Configuration Options",
104
      "13" => "BIOS Language",
105
      "14" => "Group Associations",
106
      "15" => "System Event Log",
107
      "16" => "Physical Memory Array",
108
      "17" => "Memory Device",
109
      "18" => "32-bit Memory Error",
110
      "19" => "Memory Array Mapped Address",
111
      "20" => "Memory Device Mapped Address",
112
      "21" => "Built-in Pointing Device",
113
      "22" => "Portable Battery",
114
      "23" => "System Reset",
115
      "24" => "Hardware Security",
116
      "25" => "System Power Controls",
117
      "26" => "Voltage Probe",
118
      "27" => "Cooling Device",
119
      "28" => "Temperature Probe",
120
      "29" => "Electrical Current Probe",
121
      "30" => "Out-of-band Remote Access",
122
      "31" => "Boot Integrity Services",
123
      "32" => "System Boot",
124
      "33" => "64-bit Memory Error",
125
      "34" => "Management Device",
126
      "35" => "Management Device Component",
127
      "36" => "Management Device Threshold Data",
128
      "37" => "Memory Channel",
129
      "38" => "IPMI Device",
130
      "39" => "Power Supply",
131
      "40" => "Additional Information",
132
      "41" => "Onboard Devices Extended Information",
133
      "42" => "Management Controller Host Interface"
134
);
135
 
99 rodolico 136
   // verify this is something we can work with, First line should be the version of dmidecode found
137
   if ( preg_match('/dmidecode ([0-9.]+)/', $contents[0], $results) ) {
138
      $dmidecodeVersion = $results[1];
139
   } else {
140
      return "Not a valid dmidecode file";
141
   }
96 rodolico 142
   // first, let's parse it into a hash
143
   $currentHandle = '';
144
   $currentType;
97 rodolico 145
   $data = array();
96 rodolico 146
   for ( $i = 0; $i < count($contents); $i++ ) {
147
      if ( preg_match('/^Handle ([0-9a-zx]+).*DMI type[^0-9]*(\d+),/i', $contents[$i], $outputArray) ) {
148
         //print "matched\n"; print_r($outputArray); die;
149
         $currentType = $outputArray[2];
150
         $currentHandle = $outputArray[1];
151
         if ( isset( $standardDMITypes[$currentType] ) || array_key_exists( $currentType,$standardDMITypes ) ) {
152
            $name = $contents[$i+1];
153
            $data[$currentType][$currentHandle]['name'] = $name;
154
            $i += 2; // skip the line with the name, and go to the next line
155
         } else {
156
            $currentType = $currentHandle = '';
157
         }
158
      }
159
      if ( $currentHandle &&  preg_match('/^\s+(.*):\s+(.*)$/i', $contents[$i], $outputArray) ) {
160
         $data[$currentType][$currentHandle][$outputArray[1]] = $outputArray[2];
161
      }
162
   }
97 rodolico 163
   # well, we have at least one entry, so let's go
164
   $return = array();
99 rodolico 165
   // manufacturer info
166
   $return = addData( $return, getDMIInformation( $data, '1','Manufacturer' ) );
167
   $return = addData( $return, getDMIInformation( $data, '1','Product Name', 'Model' ) );
168
   $return = addData( $return, getDMIInformation( $data, '1','Serial Number' ) );
169
   $return = addData( $return, getDMIInformation( $data, '1','UUID' ) );
170
   // physical machine
171
   $return = addData( $return, getDMIInformation( $data, '3','Type', 'Enclosure Type' ) );
172
   $return = addData( $return, getDMIInformation( $data, '3','Asset Tag', 'Enclosure Asset Tag' ) );
173
   $return = addData( $return, getDMIInformation( $data, '3','Height', 'Enclosure Height' ) );
97 rodolico 174
 
99 rodolico 175
   // firmware version
176
   $return = addData( $return, getDMIInformation( $data, '0','Vendor', 'Firmware Vendor' ) );
177
   $return = addData( $return, getDMIInformation( $data, '0','Release Date', 'Firmware Release' ) );
178
   $return = addData( $return, getDMIInformation( $data, '0','Version', 'Firmware Version' ) );
179
   $return = addData( $return, getDMIInformation( $data, '0','Firmware Revision', 'Firmware Revision' ) );
180
   // processor information. NOTE: assumes all sockets populated and all processors the saem
181
   if ( isset( $data['4'] ) ) // count the number of sockets
182
      $return[] = "CPU Count\t" . count( $data['4'] );
183
   $return = addData( $return, getDMIInformation( $data, '4','Family', 'CPU Family' ) );
184
   $return = addData( $return, getDMIInformation( $data, '4','Manufacturer', 'CPU Manufacturer' ) );
185
   $return = addData( $return, getDMIInformation( $data, '4','Version', 'CPU Version' ) );
186
   $return = addData( $return, getDMIInformation( $data, '4','Current Speed', 'CPU Speed' ) );
187
   $return = addData( $return, getDMIInformation( $data, '4','Core Count', 'CPU Cores (ea)' ) );
188
   $return = addData( $return, getDMIInformation( $data, '4','Thread Count', 'CPU Threads (ea)' ) );
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
194
    * then concat it together for a pretty output
195
    */
196
 
97 rodolico 197
   if ( isset( $data['17'] ) ) {
198
      $return[] = "Memory Slots\t" . count( $data['17'] );
99 rodolico 199
      $totalRAM = 0; // we'll accumulate the total size as we find slots populated
200
      foreach ( $data['17'] as $entry ) { // type 17 has one entry for every DIMM slot
201
         $mem = array(); // fill this up with the values we want to turn into a string, then we'll implode it into the string
97 rodolico 202
         $mem[] = trim($entry['Locator'] ? $entry['Locator'] : ($entry['Bank Locator'] ? $entry['Bank Locator'] : '' ));
203
         if ( $entry['Size'] == 'No Module Installed' ) {
204
            $mem[] = 'Unpopulated';
205
         } else {
99 rodolico 206
            $mem[] = trim($entry['Size']); // size can be kilo, meg, gig, tera, so convert it to mega
97 rodolico 207
            if ( preg_match('/(\d+)\s+(GB|MB|TB|kb)/i', $entry['Size'] , $outputArray) ) {
208
               switch ( strtolower( $outputArray[2] ) ) {
209
                  case 'gb' :
210
                           $outputArray[1] *= 1024;
211
                           break;
212
                  case 'tb' : 
213
                           $outputArray[1] *= 1024 * 1024;
214
                           break;
215
                  case 'kb' : 
216
                           $outputArray[1] /= 1024;
217
                           break;
218
               }
219
               $totalRAM += $outputArray[1];
220
            }
221
            $mem[] = trim($entry['Form Factor']);
222
            $mem[] = trim($entry['Type']);
223
            $mem[] = trim($entry['Data Width']);
224
            if ( isset( $entry['Configured Clock Speed']) && isset($entry['Speed']) && cleanUpDMIValue($entry['Configured Clock Speed']) && cleanUpDMIValue($entry['Speed']) )
225
               $mem[] = cleanUpDMIValue($entry['Configured Clock Speed'] . '/' . cleanUpDMIValue($entry['Speed']) );
226
            if ( isset($entry['Manufacturer']) && cleanUpDMIValue($entry['Manufacturer']) )
227
               $mem[] = cleanUpDMIValue($entry['Manufacturer']);
228
            if ( isset($entry['Part Number']) && cleanUpDMIValue( $entry['Part Number'] ) )
229
               $mem[] = 'p/n ' . cleanUpDMIValue($entry['Part Number']);
230
            if ( isset($entry['Serial Number']) && trim($entry['Serial Number']) != 'Not Specified' )
231
               $mem[] = 's/n ' . cleanUpDMIValue($entry['Serial Number']);
232
         }
233
         $return[] = "Memory\t" . implode( ', ', $mem );
234
      }
235
      $return[] = "Memory Total\t$totalRAM MB";
236
   }
237
 
99 rodolico 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
241
    * readable string for each psu
242
    */
97 rodolico 243
   if ( isset( $data['39'] ) ) {
244
      $return[] = "Power Supply Count\t" . count( $data['39'] );
245
      foreach ( $data['39'] as $entry ) {
246
         if ( preg_match('/^Present/i', $entry['Status'] ) ) {
247
            $psu = array();
248
            $psu[] = trim($entry['Name']);
249
            $psu[] = trim($entry['Max Power Capacity']);
250
            $psu[] = trim($entry['Manufacturer']);
251
            $psu[] = 'p/n ' . trim($entry['Model Part Number']);
252
            $psu[] = 's/n ' . trim($entry['Serial Number']);
253
            $return[] = "Power Supply\t" . implode( ', ', $psu );
254
         }
255
      }
256
   }
257
   return array( $return );
99 rodolico 258
}  // dmidecode2array
97 rodolico 259
 
99 rodolico 260
 
97 rodolico 261
$filename = $argv[1];
262
if ( $filename ) {
263
   $contents = file( $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
264
   $out = dmidecode2array( $contents );
265
   print_r( $out );
266
} else {
267
   print "You must pass the file as the first command line parameter\n";
268
}