Subversion Repositories computer_asset_manager_v1

Rev

Rev 97 | Rev 99 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

<?php


function cleanUpDMIValue( $value ) {
   $value = trim( $value );
   switch (strtolower( $value )) {
      case 'unknown':
      case 'not available':
      case 'not specified': return '';
   }
   return $value;
}

/*
 * Read value from hash, return $outputKey\tvalue from hash
 * Or, empty string if not found
 */
function getDMIInformation( $data, $type, $handle, $key, $outputKey = '' ) {
   if ( isset( $data[$type][$handle][$key]) ) {
      $a = cleanUpDMIValue( $data[$type][$handle][$key] );
      if ( $a )
         return ( $outputKey ? $outputKey : $key ) . "\t" . $a;
   }
   return '';
}


/*
 * Function will take an array of lines representing the output of a dmidecode file
 * and pick and choose the values we want to put into CAMP attributes.
 * It will return an array of key/value pairs to be added into the attributes table
 * 
 */

function dmidecode2array( $contents ) {

// allows us to group the dmi types by function
$dmiGroups = array(
      "bios" => "0,13",
      "system" => "1,12,15,23,32",
      "baseboard" => "2,10,41",
      "chassis" => "3",
      "processor" => "4",
      "memory" => "5,6,16,17",
      "cache" => "7",
      "connector" => "8",
      "slot" => "9"
);

// This contains the standard DMI types, ie no OEM stuff

$standardDMITypes = array( 
      "0" => "BIOS",
      "1" => "System",
      "2" => "Baseboard",
      "3" => "Chassis",
      "4" => "Processor",
      "5" => "Memory Controller",
      "6" => "Memory Module",
      "7" => "Cache",
      "8" => "Port Connector",
      "9" => "System Slots",
      "10" => "On Board Devices",
      "11" => "OEM Strings",
      "12" => "System Configuration Options",
      "13" => "BIOS Language",
      "14" => "Group Associations",
      "15" => "System Event Log",
      "16" => "Physical Memory Array",
      "17" => "Memory Device",
      "18" => "32-bit Memory Error",
      "19" => "Memory Array Mapped Address",
      "20" => "Memory Device Mapped Address",
      "21" => "Built-in Pointing Device",
      "22" => "Portable Battery",
      "23" => "System Reset",
      "24" => "Hardware Security",
      "25" => "System Power Controls",
      "26" => "Voltage Probe",
      "27" => "Cooling Device",
      "28" => "Temperature Probe",
      "29" => "Electrical Current Probe",
      "30" => "Out-of-band Remote Access",
      "31" => "Boot Integrity Services",
      "32" => "System Boot",
      "33" => "64-bit Memory Error",
      "34" => "Management Device",
      "35" => "Management Device Component",
      "36" => "Management Device Threshold Data",
      "37" => "Memory Channel",
      "38" => "IPMI Device",
      "39" => "Power Supply",
      "40" => "Additional Information",
      "41" => "Onboard Devices Extended Information",
      "42" => "Management Controller Host Interface"
);
   
   // verify this is something we can work with
   preg_match('/dmidecode ([0-9.]+)/', $contents[0], $results);
   #if ( $results[1] != "3.0" ) {
   #   return "This is only verified with dmidecode v3.0, v$results[1] found\n";
   #}
   // first, let's parse it into a hash
   $currentHandle = '';
   $currentType;
   $data = array();
   for ( $i = 0; $i < count($contents); $i++ ) {
      if ( preg_match('/^Handle ([0-9a-zx]+).*DMI type[^0-9]*(\d+),/i', $contents[$i], $outputArray) ) {
         //print "matched\n"; print_r($outputArray); die;
         $currentType = $outputArray[2];
         $currentHandle = $outputArray[1];
         if ( isset( $standardDMITypes[$currentType] ) || array_key_exists( $currentType,$standardDMITypes ) ) {
            $name = $contents[$i+1];
            $data[$currentType][$currentHandle]['name'] = $name;
            $i += 2; // skip the line with the name, and go to the next line
         } else {
            $currentType = $currentHandle = '';
         }
      }
      if ( $currentHandle &&  preg_match('/^\s+(.*):\s+(.*)$/i', $contents[$i], $outputArray) ) {
         $data[$currentType][$currentHandle][$outputArray[1]] = $outputArray[2];
      }
   }
   # well, we have at least one entry, so let's go
   $return = array();
   // get info on the actual machine
   $temp = &getDMIInformation( $data, '1','0x0100','Manufacturer' );
   if ( $temp ) $return[] = $temp;
   $temp = &getDMIInformation( $data, '1','0x0100','Product Name', 'Model' );
   if ( $temp ) $return[] = $temp;
   $temp = &getDMIInformation( $data, '1','0x0100','Serial Number' );
   if ( $temp ) $return[] = $temp;
   $temp = &getDMIInformation( $data, '1','0x0100','UUID' );
   if ( $temp ) $return[] = $temp;
   // firmware information
   $temp = &getDMIInformation( $data, '0','0x0000','Vendor', 'Firmware Vendor' );
   if ( $temp ) $return[] = $temp;
   $temp = &getDMIInformation( $data, '0','0x0000','Release Date', 'Firmware Release' );
   if ( $temp ) $return[] = $temp;
   $temp = &getDMIInformation( $data, '0','0x0000','Version', 'Firmware Version' );
   if ( $temp ) $return[] = $temp;
   $temp = &getDMIInformation( $data, '0','0x0000','Firmware Revision', 'Firmware Revision' );
   if ( $temp ) $return[] = $temp;
   // physical size
   $temp = &getDMIInformation( $data, '3','0x0300','Type', 'Enclosure Type' );
   if ( $temp ) $return[] = $temp;
   $temp = &getDMIInformation( $data, '3','0x0300','Asset Tag', 'Enclosure Asset Tag' );
   if ( $temp ) $return[] = $temp;
   $temp = &getDMIInformation( $data, '3','0x0300','Height', 'Enclosure Height' );
   if ( $temp ) $return[] = $temp;
   // processor information
   if ( isset( $data['4'] ) )
      $return[] = "CPU Count\t" . count( $data['4'] ); // fails if there are unpopulated slots or disabled ones
      
   $temp = &getDMIInformation( $data, '4','0x0400','Family', 'CPU Family' );
   if ( $temp ) $return[] = $temp;
   $temp = &getDMIInformation( $data, '4','0x0400','Manufacturer', 'CPU Manufacturer' );
   if ( $temp ) $return[] = $temp;
   $temp = &getDMIInformation( $data, '4','0x0400','Version', 'CPU Version' );
   if ( $temp ) $return[] = $temp;
   $temp = &getDMIInformation( $data, '4','0x0400','Current Speed', 'CPU Speed' );
   if ( $temp ) $return[] = $temp;
   $temp = &getDMIInformation( $data, '4','0x0400','Core Count', 'CPU Cores (ea)' );
   if ( $temp ) $return[] = $temp;
   $temp = &getDMIInformation( $data, '4','0x0400','Thread Count', 'CPU Threads (ea)' );
   if ( $temp ) $return[] = $temp;

   // memory information
   if ( isset( $data['17'] ) ) {
      $return[] = "Memory Slots\t" . count( $data['17'] );
      $totalRAM = 0;
      foreach ( $data['17'] as $entry ) {
         $mem = array();
         $mem[] = trim($entry['Locator'] ? $entry['Locator'] : ($entry['Bank Locator'] ? $entry['Bank Locator'] : '' ));
         if ( $entry['Size'] == 'No Module Installed' ) {
            $mem[] = 'Unpopulated';
         } else {
            $mem[] = trim($entry['Size']);
            if ( preg_match('/(\d+)\s+(GB|MB|TB|kb)/i', $entry['Size'] , $outputArray) ) {
               switch ( strtolower( $outputArray[2] ) ) {
                  case 'gb' :
                           $outputArray[1] *= 1024;
                           break;
                  case 'tb' : 
                           $outputArray[1] *= 1024 * 1024;
                           break;
                  case 'kb' : 
                           $outputArray[1] /= 1024;
                           break;
               }
               $totalRAM += $outputArray[1];
            }
            $mem[] = trim($entry['Form Factor']);
            $mem[] = trim($entry['Type']);
            $mem[] = trim($entry['Data Width']);
            if ( isset( $entry['Configured Clock Speed']) && isset($entry['Speed']) && cleanUpDMIValue($entry['Configured Clock Speed']) && cleanUpDMIValue($entry['Speed']) )
               $mem[] = cleanUpDMIValue($entry['Configured Clock Speed'] . '/' . cleanUpDMIValue($entry['Speed']) );
            if ( isset($entry['Manufacturer']) && cleanUpDMIValue($entry['Manufacturer']) )
               $mem[] = cleanUpDMIValue($entry['Manufacturer']);
            if ( isset($entry['Part Number']) && cleanUpDMIValue( $entry['Part Number'] ) )
               $mem[] = 'p/n ' . cleanUpDMIValue($entry['Part Number']);
            if ( isset($entry['Serial Number']) && trim($entry['Serial Number']) != 'Not Specified' )
               $mem[] = 's/n ' . cleanUpDMIValue($entry['Serial Number']);
         }
         $return[] = "Memory\t" . implode( ', ', $mem );
      }
      $return[] = "Memory Total\t$totalRAM MB";
   }

   // power supplies
   if ( isset( $data['39'] ) ) {
      $return[] = "Power Supply Count\t" . count( $data['39'] );
      foreach ( $data['39'] as $entry ) {
         if ( preg_match('/^Present/i', $entry['Status'] ) ) {
            $psu = array();
            $psu[] = trim($entry['Name']);
            $psu[] = trim($entry['Max Power Capacity']);
            $psu[] = trim($entry['Manufacturer']);
            $psu[] = 'p/n ' . trim($entry['Model Part Number']);
            $psu[] = 's/n ' . trim($entry['Serial Number']);
            $return[] = "Power Supply\t" . implode( ', ', $psu );
         }
      }
   }
   return array( $return );
}

$filename = $argv[1];
if ( $filename ) {
   $contents = file( $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
   $out = dmidecode2array( $contents );
   print_r( $out );
} else {
   print "You must pass the file as the first command line parameter\n";
}