Subversion Repositories computer_asset_manager_v1

Rev

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