Subversion Repositories computer_asset_manager_v1

Rev

Rev 99 | Rev 102 | 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
 
100 rodolico 3
   /*
4
    * dmidecode2array.php
5
    * 
6
    * Set of routines which convert the output of dmidecode into an array containing key/value pairs.
7
    *
8
    * Use this by including the file in your script, load the output of a dmidecode run into an array of lines, then call
9
    * $array = dmidecode2array( $contentsOfFile ).
10
    * Sample code to read a file name from ARGV, process the file and dump the resulting array using print_r shown at
11
    * bottom of this file, commented out.
12
    * 
13
    * Copyright (c) 2020 by the Daily Data, Inc. All rights reserved. Redistribution and use in source and binary forms, with or
14
    * without modification, are permitted provided that the following conditions are met:
15
    *     Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
16
    *     Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
17
    *        disclaimer in the documentation and/or other materials provided with the distribution.
18
    *     Neither the name of the nor the names of its contributors may be used to endorse or promote products derived from this
19
    *       software without specific prior written permission.
20
    *
21
    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
22
    * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
23
    * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
    * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
    *
28
    * 20200113 rodo@dailydata.net v0.1
29
    * Initial build
30
    * 
31
    */
32
 
33
 
34
 
35
   /*
36
    * Trims a value, then compares it to several 'null' possibilities
37
    * if it is a null, returns empty string, otherwise returns value
38
    */
39
   function cleanUpDMIValue( $value ) {
40
      $value = trim( $value );
41
      switch (strtolower( $value )) {
42
         case 'unknown':
43
         case 'unspecified' :
44
         case 'not available':
45
         case 'to be filled by o.e.m.':
46
         case 'not specified': return '';
47
      }
48
      return $value;
98 rodolico 49
   }
50
 
100 rodolico 51
   /*
52
    * Read value from array of hash, return array of "$outputKey\tvalue" for each entry found
53
    * If nothing found, returns empty array
54
    * if $unique is true, returns first non-empty value found
55
    * if $outputKey is not defined, uses $key
56
    */
57
   function getDMIInformation( $data, $type, $key, $outputKey = '', $unique = true ) {
58
      $return = array();
59
      foreach ( $data[$type] as $handle ) {
60
         if ( isset( $handle[$key]) ) {
61
            $a = cleanUpDMIValue( $handle[$key] );
62
            if ( $a ) {
63
               $return[] = ( $outputKey ? $outputKey : $key ) . "\t" . $a;
64
               if ( $unique ) {
65
                  return $return;
66
               }
99 rodolico 67
            }
68
         }
69
      }
100 rodolico 70
      return $return;
97 rodolico 71
   }
96 rodolico 72
 
100 rodolico 73
   /*
74
    * adds $value to array
75
    * if $value is scalar, simply adds it
76
    * if $value is array, adds every row
77
    * returns modified array
78
    *
79
    * Can be done more efficiently with some PHP built-ins, but I forgot how.
80
    */
81
   function mergeArrays( $array, $value ) {
82
      if ( $value ) {
83
         switch (gettype( $value ) ) {
84
            case 'array' : foreach ( $value as $thisVal ) {
85
                              $array[] = $thisVal;
86
                           }
87
                           break;
88
            case 'boolean' :
89
            case 'integer' :
90
            case 'double'  :
91
            case 'string'  : $array[] = $value;
92
         } // switch
93
      }
94
      return $array;
95
   } //mergeArrays
97 rodolico 96
 
96 rodolico 97
 
100 rodolico 98
   /*
99
    * Function will take an array of lines representing the output of a dmidecode file
100
    * and pick and choose the values we want to put into CAMP attributes.
101
    * It will return an array of key/value pairs to be added into the attributes table
102
    *
103
    * NOTE: this is a summary of values we decided to store. dmidecode files have a lot of
104
    * additional information we chose not to include, but they can be added by simply modifying the 
105
    * 
106
    */
96 rodolico 107
 
100 rodolico 108
   function dmidecode2array( $contents ) {
96 rodolico 109
 
100 rodolico 110
   // allows us to group the dmi types by function. not implemented
111
   $dmiGroups = array(
112
         "bios" => "0,13",
113
         "system" => "1,12,15,23,32",
114
         "baseboard" => "2,10,41",
115
         "chassis" => "3",
116
         "processor" => "4",
117
         "memory" => "5,6,16,17",
118
         "cache" => "7",
119
         "connector" => "8",
120
         "slot" => "9"
121
   );
96 rodolico 122
 
100 rodolico 123
   // This contains the standard DMI types, ie no OEM stuff. For reference, not used in code
124
 
125
   $standardDMITypes = array( 
126
         "0" => "BIOS",
127
         "1" => "System",
128
         "2" => "Baseboard",
129
         "3" => "Chassis",
130
         "4" => "Processor",
131
         "5" => "Memory Controller",
132
         "6" => "Memory Module",
133
         "7" => "Cache",
134
         "8" => "Port Connector",
135
         "9" => "System Slots",
136
         "10" => "On Board Devices",
137
         "11" => "OEM Strings",
138
         "12" => "System Configuration Options",
139
         "13" => "BIOS Language",
140
         "14" => "Group Associations",
141
         "15" => "System Event Log",
142
         "16" => "Physical Memory Array",
143
         "17" => "Memory Device",
144
         "18" => "32-bit Memory Error",
145
         "19" => "Memory Array Mapped Address",
146
         "20" => "Memory Device Mapped Address",
147
         "21" => "Built-in Pointing Device",
148
         "22" => "Portable Battery",
149
         "23" => "System Reset",
150
         "24" => "Hardware Security",
151
         "25" => "System Power Controls",
152
         "26" => "Voltage Probe",
153
         "27" => "Cooling Device",
154
         "28" => "Temperature Probe",
155
         "29" => "Electrical Current Probe",
156
         "30" => "Out-of-band Remote Access",
157
         "31" => "Boot Integrity Services",
158
         "32" => "System Boot",
159
         "33" => "64-bit Memory Error",
160
         "34" => "Management Device",
161
         "35" => "Management Device Component",
162
         "36" => "Management Device Threshold Data",
163
         "37" => "Memory Channel",
164
         "38" => "IPMI Device",
165
         "39" => "Power Supply",
166
         "40" => "Additional Information",
167
         "41" => "Onboard Devices Extended Information",
168
         "42" => "Management Controller Host Interface"
169
   );
170
 
171
      // verify this is something we can work with, First line should be the version of dmidecode found
172
      if ( preg_match('/dmidecode ([0-9.]+)/', $contents[0], $results) ) {
173
         $dmidecodeVersion = $results[1];
174
      } else {
175
         return "Not a valid dmidecode file";
176
      }
177
      // first, let's parse it into a hash
178
      $currentHandle = '';
179
      $currentType;
180
      $data = array();
181
      for ( $i = 0; $i < count($contents); $i++ ) {
182
         if ( preg_match('/^Handle ([0-9a-zx]+).*DMI type[^0-9]*(\d+),/i', $contents[$i], $outputArray) ) {
183
            //print "matched\n"; print_r($outputArray); die;
184
            $currentType = $outputArray[2];
185
            $currentHandle = $outputArray[1];
186
            if ( isset( $standardDMITypes[$currentType] ) || array_key_exists( $currentType,$standardDMITypes ) ) {
187
               $name = $contents[$i+1];
188
               $data[$currentType][$currentHandle]['name'] = $name;
189
               $i += 2; // skip the line with the name, and go to the next line
190
            } else {
191
               $currentType = $currentHandle = '';
192
            }
96 rodolico 193
         }
100 rodolico 194
         if ( $currentHandle &&  preg_match('/^\s+(.*):\s+(.*)$/i', $contents[$i], $outputArray) ) {
195
            $data[$currentType][$currentHandle][$outputArray[1]] = $outputArray[2];
196
         }
96 rodolico 197
      }
100 rodolico 198
      # well, we have at least one entry, so let's go
199
      $return = array();
97 rodolico 200
 
100 rodolico 201
      /*
202
       * Grab the information we want from the file. Note tht we are renaming some of the fields from the dmidecode output
203
       * to match the keyfields used by our system (4th parameter in getDMIInformation).
204
       *
205
       * First, we'll get the easy stuff; stuff which only has one value we care about.
206
       * 
207
       */
208
      // manufacturer info
209
      $return = mergeArrays( $return, getDMIInformation( $data, '1','Manufacturer' ) );
210
      $return = mergeArrays( $return, getDMIInformation( $data, '1','Product Name', 'Model' ) );
211
      $return = mergeArrays( $return, getDMIInformation( $data, '1','Serial Number' ) );
212
      $return = mergeArrays( $return, getDMIInformation( $data, '1','UUID' ) );
213
      // physical machine
214
      $return = mergeArrays( $return, getDMIInformation( $data, '3','Type', 'Enclosure Type' ) );
215
      $return = mergeArrays( $return, getDMIInformation( $data, '3','Asset Tag', 'Enclosure Asset Tag' ) );
216
      $return = mergeArrays( $return, getDMIInformation( $data, '3','Height', 'Enclosure Height' ) );
217
      // firmware version
218
      $return = mergeArrays( $return, getDMIInformation( $data, '0','Vendor', 'Firmware Vendor' ) );
219
      $return = mergeArrays( $return, getDMIInformation( $data, '0','Release Date', 'Firmware Release' ) );
220
      $return = mergeArrays( $return, getDMIInformation( $data, '0','Version', 'Firmware Version' ) );
221
      $return = mergeArrays( $return, getDMIInformation( $data, '0','Firmware Revision', 'Firmware Revision' ) );
222
      // processor information. NOTE: assumes all sockets populated and all processors the same. If this is not the case
223
      // we will need to modify the following to dump each individual socket, similar to the meory information below.
224
      if ( isset( $data['4'] ) ) // count the number of sockets
225
         $return[] = "CPU Count\t" . count( $data['4'] );
226
      $return = mergeArrays( $return, getDMIInformation( $data, '4','Family', 'CPU Family' ) );
227
      $return = mergeArrays( $return, getDMIInformation( $data, '4','Manufacturer', 'CPU Manufacturer' ) );
228
      $return = mergeArrays( $return, getDMIInformation( $data, '4','Version', 'CPU Version' ) );
229
      $return = mergeArrays( $return, getDMIInformation( $data, '4','Current Speed', 'CPU Speed' ) );
230
      $return = mergeArrays( $return, getDMIInformation( $data, '4','Core Count', 'CPU Cores (ea)' ) );
231
      $return = mergeArrays( $return, getDMIInformation( $data, '4','Thread Count', 'CPU Threads (ea)' ) );
99 rodolico 232
 
100 rodolico 233
      /*
234
       * memory information. We want details on each slot for this, so we have to go through each entry, one at a time.
235
       * For readability, we also want to take several values and turn them into one human
236
       * readable string for each socket. So, we go through each entry in the memory socket, determine if it is populated
237
       * then concat it together for a pretty output
238
       */
239
 
240
      if ( isset( $data['17'] ) ) {
241
         $return[] = "Memory Slots\t" . count( $data['17'] );
242
         $totalRAM = 0; // we'll accumulate the total size as we find slots populated
243
         foreach ( $data['17'] as $entry ) { // type 17 has one entry for every DIMM slot
244
            $mem = array(); // fill this up with the values we want to turn into a string, then we'll implode it into the string
245
            $mem[] = trim($entry['Locator'] ? $entry['Locator'] : ($entry['Bank Locator'] ? $entry['Bank Locator'] : '' ));
246
            if ( $entry['Size'] == 'No Module Installed' ) {
247
               $mem[] = 'Unpopulated';
248
            } else {
249
               $mem[] = trim($entry['Size']); // size can be kilo, meg, gig, tera, so convert it to mega
250
               if ( preg_match('/(\d+)\s+(GB|MB|TB|kb)/i', $entry['Size'] , $outputArray) ) {
251
                  switch ( strtolower( $outputArray[2] ) ) {
252
                     case 'gb' :
253
                              $outputArray[1] *= 1024;
254
                              break;
255
                     case 'tb' : 
256
                              $outputArray[1] *= 1024 * 1024;
257
                              break;
258
                     case 'kb' : 
259
                              $outputArray[1] /= 1024;
260
                              break;
261
                  }
262
                  $totalRAM += $outputArray[1];
97 rodolico 263
               }
100 rodolico 264
               $mem[] = trim($entry['Form Factor']);
265
               $mem[] = trim($entry['Type']);
266
               $mem[] = trim($entry['Data Width']);
267
               if ( isset( $entry['Configured Clock Speed']) && isset($entry['Speed']) && cleanUpDMIValue($entry['Configured Clock Speed']) && cleanUpDMIValue($entry['Speed']) )
268
                  $mem[] = cleanUpDMIValue($entry['Configured Clock Speed'] . '/' . cleanUpDMIValue($entry['Speed']) );
269
               if ( isset($entry['Manufacturer']) && cleanUpDMIValue($entry['Manufacturer']) )
270
                  $mem[] = cleanUpDMIValue($entry['Manufacturer']);
271
               if ( isset($entry['Part Number']) && cleanUpDMIValue( $entry['Part Number'] ) )
272
                  $mem[] = 'p/n ' . cleanUpDMIValue($entry['Part Number']);
273
               if ( isset($entry['Serial Number']) && trim($entry['Serial Number']) != 'Not Specified' )
274
                  $mem[] = 's/n ' . cleanUpDMIValue($entry['Serial Number']);
97 rodolico 275
            }
100 rodolico 276
            $return[] = "Memory\t" . implode( ', ', $mem );
97 rodolico 277
         }
100 rodolico 278
         $return[] = "Memory Total\t$totalRAM MB";
97 rodolico 279
      }
280
 
100 rodolico 281
      /*
282
       * power supplies. Most workstations will not even have an entry here, but for servers we will have details on each individual
283
       * power supply, including the part number, serial number, etc..., so like memory, we want a detailed listing with a human
284
       * readable string for each psu
285
       */
286
      if ( isset( $data['39'] ) ) {
287
         $return[] = "Power Supply Count\t" . count( $data['39'] );
288
         foreach ( $data['39'] as $entry ) {
289
            if ( preg_match('/^Present/i', $entry['Status'] ) ) {
290
               $psu = array();
291
               $psu[] = trim($entry['Name']);
292
               $psu[] = trim($entry['Max Power Capacity']);
293
               $psu[] = trim($entry['Manufacturer']);
294
               $psu[] = 'p/n ' . trim($entry['Model Part Number']);
295
               $psu[] = 's/n ' . trim($entry['Serial Number']);
296
               $return[] = "Power Supply\t" . implode( ', ', $psu );
297
            }
298
         }
299
      }
300
      return array( $return );
301
   }  // dmidecode2array
302
 
99 rodolico 303
   /*
100 rodolico 304
    * Test code for dmidecode input
305
    * call from cli as
306
    * php dmidecode2array FILENAME
307
    * the array will be dumped to STDOUT by print_r
308
    * 
99 rodolico 309
    */
100 rodolico 310
   /* remove this line for testing
311
 
312
   $filename = $argv[1]; // get first parameter from cli
313
   if ( $filename ) {
314
      // slurp file into array, ignore empty lines and remove line endings
315
      $contents = file( $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
316
      // $out will contain the key/value pairs
317
      $out = dmidecode2array( $contents );
318
      // dump it
319
      print_r( $out );
320
   } else {
321
      print "You must pass the file as the first command line parameter\n";
97 rodolico 322
   }
323
 
100 rodolico 324
   remove this line for testing */
99 rodolico 325
 
100 rodolico 326
?>