Subversion Repositories computer_asset_manager_v1

Rev

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
 
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();
102 rodolico 200
      $return[] = "attribute\tvalue";
97 rodolico 201
 
100 rodolico 202
      /*
203
       * Grab the information we want from the file. Note tht we are renaming some of the fields from the dmidecode output
204
       * to match the keyfields used by our system (4th parameter in getDMIInformation).
205
       *
206
       * First, we'll get the easy stuff; stuff which only has one value we care about.
207
       * 
208
       */
209
      // manufacturer info
210
      $return = mergeArrays( $return, getDMIInformation( $data, '1','Manufacturer' ) );
211
      $return = mergeArrays( $return, getDMIInformation( $data, '1','Product Name', 'Model' ) );
212
      $return = mergeArrays( $return, getDMIInformation( $data, '1','Serial Number' ) );
213
      $return = mergeArrays( $return, getDMIInformation( $data, '1','UUID' ) );
214
      // physical machine
215
      $return = mergeArrays( $return, getDMIInformation( $data, '3','Type', 'Enclosure Type' ) );
216
      $return = mergeArrays( $return, getDMIInformation( $data, '3','Asset Tag', 'Enclosure Asset Tag' ) );
217
      $return = mergeArrays( $return, getDMIInformation( $data, '3','Height', 'Enclosure Height' ) );
218
      // firmware version
219
      $return = mergeArrays( $return, getDMIInformation( $data, '0','Vendor', 'Firmware Vendor' ) );
220
      $return = mergeArrays( $return, getDMIInformation( $data, '0','Release Date', 'Firmware Release' ) );
221
      $return = mergeArrays( $return, getDMIInformation( $data, '0','Version', 'Firmware Version' ) );
222
      $return = mergeArrays( $return, getDMIInformation( $data, '0','Firmware Revision', 'Firmware Revision' ) );
223
      // processor information. NOTE: assumes all sockets populated and all processors the same. If this is not the case
224
      // we will need to modify the following to dump each individual socket, similar to the meory information below.
225
      if ( isset( $data['4'] ) ) // count the number of sockets
226
         $return[] = "CPU Count\t" . count( $data['4'] );
227
      $return = mergeArrays( $return, getDMIInformation( $data, '4','Family', 'CPU Family' ) );
228
      $return = mergeArrays( $return, getDMIInformation( $data, '4','Manufacturer', 'CPU Manufacturer' ) );
229
      $return = mergeArrays( $return, getDMIInformation( $data, '4','Version', 'CPU Version' ) );
230
      $return = mergeArrays( $return, getDMIInformation( $data, '4','Current Speed', 'CPU Speed' ) );
231
      $return = mergeArrays( $return, getDMIInformation( $data, '4','Core Count', 'CPU Cores (ea)' ) );
232
      $return = mergeArrays( $return, getDMIInformation( $data, '4','Thread Count', 'CPU Threads (ea)' ) );
99 rodolico 233
 
100 rodolico 234
      /*
235
       * memory information. We want details on each slot for this, so we have to go through each entry, one at a time.
236
       * For readability, we also want to take several values and turn them into one human
237
       * readable string for each socket. So, we go through each entry in the memory socket, determine if it is populated
238
       * then concat it together for a pretty output
239
       */
240
 
241
      if ( isset( $data['17'] ) ) {
242
         $return[] = "Memory Slots\t" . count( $data['17'] );
243
         $totalRAM = 0; // we'll accumulate the total size as we find slots populated
244
         foreach ( $data['17'] as $entry ) { // type 17 has one entry for every DIMM slot
245
            $mem = array(); // fill this up with the values we want to turn into a string, then we'll implode it into the string
246
            $mem[] = trim($entry['Locator'] ? $entry['Locator'] : ($entry['Bank Locator'] ? $entry['Bank Locator'] : '' ));
247
            if ( $entry['Size'] == 'No Module Installed' ) {
248
               $mem[] = 'Unpopulated';
249
            } else {
250
               $mem[] = trim($entry['Size']); // size can be kilo, meg, gig, tera, so convert it to mega
251
               if ( preg_match('/(\d+)\s+(GB|MB|TB|kb)/i', $entry['Size'] , $outputArray) ) {
252
                  switch ( strtolower( $outputArray[2] ) ) {
253
                     case 'gb' :
254
                              $outputArray[1] *= 1024;
255
                              break;
256
                     case 'tb' : 
257
                              $outputArray[1] *= 1024 * 1024;
258
                              break;
259
                     case 'kb' : 
260
                              $outputArray[1] /= 1024;
261
                              break;
262
                  }
263
                  $totalRAM += $outputArray[1];
97 rodolico 264
               }
100 rodolico 265
               $mem[] = trim($entry['Form Factor']);
266
               $mem[] = trim($entry['Type']);
267
               $mem[] = trim($entry['Data Width']);
268
               if ( isset( $entry['Configured Clock Speed']) && isset($entry['Speed']) && cleanUpDMIValue($entry['Configured Clock Speed']) && cleanUpDMIValue($entry['Speed']) )
269
                  $mem[] = cleanUpDMIValue($entry['Configured Clock Speed'] . '/' . cleanUpDMIValue($entry['Speed']) );
270
               if ( isset($entry['Manufacturer']) && cleanUpDMIValue($entry['Manufacturer']) )
271
                  $mem[] = cleanUpDMIValue($entry['Manufacturer']);
272
               if ( isset($entry['Part Number']) && cleanUpDMIValue( $entry['Part Number'] ) )
273
                  $mem[] = 'p/n ' . cleanUpDMIValue($entry['Part Number']);
274
               if ( isset($entry['Serial Number']) && trim($entry['Serial Number']) != 'Not Specified' )
275
                  $mem[] = 's/n ' . cleanUpDMIValue($entry['Serial Number']);
97 rodolico 276
            }
100 rodolico 277
            $return[] = "Memory\t" . implode( ', ', $mem );
97 rodolico 278
         }
100 rodolico 279
         $return[] = "Memory Total\t$totalRAM MB";
97 rodolico 280
      }
281
 
100 rodolico 282
      /*
283
       * power supplies. Most workstations will not even have an entry here, but for servers we will have details on each individual
284
       * power supply, including the part number, serial number, etc..., so like memory, we want a detailed listing with a human
285
       * readable string for each psu
286
       */
287
      if ( isset( $data['39'] ) ) {
288
         $return[] = "Power Supply Count\t" . count( $data['39'] );
289
         foreach ( $data['39'] as $entry ) {
290
            if ( preg_match('/^Present/i', $entry['Status'] ) ) {
291
               $psu = array();
292
               $psu[] = trim($entry['Name']);
293
               $psu[] = trim($entry['Max Power Capacity']);
294
               $psu[] = trim($entry['Manufacturer']);
295
               $psu[] = 'p/n ' . trim($entry['Model Part Number']);
296
               $psu[] = 's/n ' . trim($entry['Serial Number']);
297
               $return[] = "Power Supply\t" . implode( ', ', $psu );
298
            }
299
         }
300
      }
102 rodolico 301
      return $return;
100 rodolico 302
   }  // dmidecode2array
303
 
99 rodolico 304
   /*
100 rodolico 305
    * Test code for dmidecode input
306
    * call from cli as
307
    * php dmidecode2array FILENAME
308
    * the array will be dumped to STDOUT by print_r
309
    * 
99 rodolico 310
    */
100 rodolico 311
   /* remove this line for testing
312
 
313
   $filename = $argv[1]; // get first parameter from cli
314
   if ( $filename ) {
315
      // slurp file into array, ignore empty lines and remove line endings
316
      $contents = file( $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
317
      // $out will contain the key/value pairs
318
      $out = dmidecode2array( $contents );
319
      // dump it
320
      print_r( $out );
321
   } else {
322
      print "You must pass the file as the first command line parameter\n";
97 rodolico 323
   }
324
 
100 rodolico 325
   remove this line for testing */
99 rodolico 326
 
100 rodolico 327
?>