Subversion Repositories computer_asset_manager_v1

Rev

Rev 96 | Rev 98 | 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
 
3
 
97 rodolico 4
/*
5
 * Read value from hash, return $outputKey\tvalue from hash
6
 * Or, empty string if not found
7
 */
8
function getDMIInformation( $data, $type, $handle, $key, $outputKey = '' ) {
9
   if ( isset( $data[$type][$handle][$key] ) ) {
10
      return ( $outputKey ? $outputKey : $key ) . "\t" . $data[$type][$handle][$key];
11
   } else {
12
      return '';
13
   }
14
}
96 rodolico 15
 
97 rodolico 16
 
96 rodolico 17
/*
18
 * Function will take an array of lines representing the output of a dmidecode file
19
 * and pick and choose the values we want to put into CAMP attributes.
20
 * It will return an array of key/value pairs to be added into the attributes table
21
 * 
22
 */
23
 
24
function dmidecode2array( $contents ) {
25
 
26
// allows us to group the dmi types by function
27
$dmiGroups = array(
28
      "bios" => "0,13",
29
      "system" => "1,12,15,23,32",
30
      "baseboard" => "2,10,41",
31
      "chassis" => "3",
32
      "processor" => "4",
33
      "memory" => "5,6,16,17",
34
      "cache" => "7",
35
      "connector" => "8",
36
      "slot" => "9"
37
);
38
 
39
// This contains the standard DMI types, ie no OEM stuff
40
 
41
$standardDMITypes = array( 
42
      "0" => "BIOS",
43
      "1" => "System",
44
      "2" => "Baseboard",
45
      "3" => "Chassis",
46
      "4" => "Processor",
47
      "5" => "Memory Controller",
48
      "6" => "Memory Module",
49
      "7" => "Cache",
50
      "8" => "Port Connector",
51
      "9" => "System Slots",
52
      "10" => "On Board Devices",
53
      "11" => "OEM Strings",
54
      "12" => "System Configuration Options",
55
      "13" => "BIOS Language",
56
      "14" => "Group Associations",
57
      "15" => "System Event Log",
58
      "16" => "Physical Memory Array",
59
      "17" => "Memory Device",
60
      "18" => "32-bit Memory Error",
61
      "19" => "Memory Array Mapped Address",
62
      "20" => "Memory Device Mapped Address",
63
      "21" => "Built-in Pointing Device",
64
      "22" => "Portable Battery",
65
      "23" => "System Reset",
66
      "24" => "Hardware Security",
67
      "25" => "System Power Controls",
68
      "26" => "Voltage Probe",
69
      "27" => "Cooling Device",
70
      "28" => "Temperature Probe",
71
      "29" => "Electrical Current Probe",
72
      "30" => "Out-of-band Remote Access",
73
      "31" => "Boot Integrity Services",
74
      "32" => "System Boot",
75
      "33" => "64-bit Memory Error",
76
      "34" => "Management Device",
77
      "35" => "Management Device Component",
78
      "36" => "Management Device Threshold Data",
79
      "37" => "Memory Channel",
80
      "38" => "IPMI Device",
81
      "39" => "Power Supply",
82
      "40" => "Additional Information",
83
      "41" => "Onboard Devices Extended Information",
84
      "42" => "Management Controller Host Interface"
85
);
86
 
87
   // verify this is something we can work with
88
   preg_match('/dmidecode ([0-9.]+)/', $contents[0], $results);
97 rodolico 89
   #if ( $results[1] != "3.0" ) {
90
   #   return "This is only verified with dmidecode v3.0, v$results[1] found\n";
91
   #}
96 rodolico 92
   // first, let's parse it into a hash
93
   $currentHandle = '';
94
   $currentType;
97 rodolico 95
   $data = array();
96 rodolico 96
   for ( $i = 0; $i < count($contents); $i++ ) {
97
      if ( preg_match('/^Handle ([0-9a-zx]+).*DMI type[^0-9]*(\d+),/i', $contents[$i], $outputArray) ) {
98
         //print "matched\n"; print_r($outputArray); die;
99
         $currentType = $outputArray[2];
100
         $currentHandle = $outputArray[1];
101
         if ( isset( $standardDMITypes[$currentType] ) || array_key_exists( $currentType,$standardDMITypes ) ) {
102
            $name = $contents[$i+1];
103
            $data[$currentType][$currentHandle]['name'] = $name;
104
            $i += 2; // skip the line with the name, and go to the next line
105
         } else {
106
            $currentType = $currentHandle = '';
107
         }
108
      }
109
      if ( $currentHandle &&  preg_match('/^\s+(.*):\s+(.*)$/i', $contents[$i], $outputArray) ) {
110
         $data[$currentType][$currentHandle][$outputArray[1]] = $outputArray[2];
111
      }
112
   }
97 rodolico 113
   # well, we have at least one entry, so let's go
114
   $return = array();
115
   // get info on the actual machine
116
   $temp = &getDMIInformation( $data, '1','0x0100','Manufacturer' );
117
   if ( $temp ) $return[] = $temp;
118
   $temp = &getDMIInformation( $data, '1','0x0100','Product Name', 'Model' );
119
   if ( $temp ) $return[] = $temp;
120
   $temp = &getDMIInformation( $data, '1','0x0100','Serial Number' );
121
   if ( $temp ) $return[] = $temp;
122
   $temp = &getDMIInformation( $data, '1','0x0100','UUID' );
123
   if ( $temp ) $return[] = $temp;
124
   // firmware information
125
   $temp = &getDMIInformation( $data, '0','0x0000','Vendor', 'Firmware Vendor' );
126
   if ( $temp ) $return[] = $temp;
127
   $temp = &getDMIInformation( $data, '0','0x0000','Release Date', 'Firmware Release' );
128
   if ( $temp ) $return[] = $temp;
129
   $temp = &getDMIInformation( $data, '0','0x0000','Version', 'Firmware Version' );
130
   if ( $temp ) $return[] = $temp;
131
   $temp = &getDMIInformation( $data, '0','0x0000','Firmware Revision', 'Firmware Revision' );
132
   if ( $temp ) $return[] = $temp;
133
   $temp = &getDMIInformation( $data, '3','0x0300','Firmware Revision', 'Enclosure Type' );
134
   if ( $temp ) $return[] = $temp;
135
   $temp = &getDMIInformation( $data, '3','0x3000','Asset Tag' );
136
   if ( $temp ) $return[] = $temp;
137
   // processor information
138
   if ( isset( $data['4'] ) )
139
      $return[] = "CPU Count\t" . count( $data['4'] ); // fails if there are unpopulated slots or disabled ones
140
 
141
   $temp = &getDMIInformation( $data, '4','0x0400','Family', 'CPU Family' );
142
   if ( $temp ) $return[] = $temp;
143
   $temp = &getDMIInformation( $data, '4','0x0400','Manufacturer', 'CPU Manufacturer' );
144
   if ( $temp ) $return[] = $temp;
145
   $temp = &getDMIInformation( $data, '4','0x0400','Version', 'CPU Version' );
146
   if ( $temp ) $return[] = $temp;
147
   $temp = &getDMIInformation( $data, '4','0x0400','Current Speed', 'CPU Speed' );
148
   if ( $temp ) $return[] = $temp;
149
   $temp = &getDMIInformation( $data, '4','0x0400','Core Count', 'CPU Cores (ea)' );
150
   if ( $temp ) $return[] = $temp;
151
   $temp = &getDMIInformation( $data, '4','0x0400','Thread Count', 'CPU Threads (ea)' );
152
   if ( $temp ) $return[] = $temp;
153
 
154
   // memory information
155
   if ( isset( $data['17'] ) ) {
156
      $return[] = "Memory Slots\t" . count( $data['17'] );
157
      $totalRAM = 0;
158
      foreach ( $data['17'] as $entry ) {
159
         $mem = array();
160
         $mem[] = trim($entry['Locator'] ? $entry['Locator'] : ($entry['Bank Locator'] ? $entry['Bank Locator'] : '' ));
161
         if ( $entry['Size'] == 'No Module Installed' ) {
162
            $mem[] = 'Unpopulated';
163
         } else {
164
            $mem[] = trim($entry['Size']);
165
            if ( preg_match('/(\d+)\s+(GB|MB|TB|kb)/i', $entry['Size'] , $outputArray) ) {
166
               switch ( strtolower( $outputArray[2] ) ) {
167
                  case 'gb' :
168
                           $outputArray[1] *= 1024;
169
                           break;
170
                  case 'tb' : 
171
                           $outputArray[1] *= 1024 * 1024;
172
                           break;
173
                  case 'kb' : 
174
                           $outputArray[1] /= 1024;
175
                           break;
176
               }
177
               $totalRAM += $outputArray[1];
178
            }
179
            $mem[] = trim($entry['Form Factor']);
180
            $mem[] = trim($entry['Type']);
181
            $mem[] = trim($entry['Data Width']);
182
            if ( isset( $entry['Configured Clock Speed']) && isset($entry['Speed']) && cleanUpDMIValue($entry['Configured Clock Speed']) && cleanUpDMIValue($entry['Speed']) )
183
               $mem[] = cleanUpDMIValue($entry['Configured Clock Speed'] . '/' . cleanUpDMIValue($entry['Speed']) );
184
            if ( isset($entry['Manufacturer']) && cleanUpDMIValue($entry['Manufacturer']) )
185
               $mem[] = cleanUpDMIValue($entry['Manufacturer']);
186
            if ( isset($entry['Part Number']) && cleanUpDMIValue( $entry['Part Number'] ) )
187
               $mem[] = 'p/n ' . cleanUpDMIValue($entry['Part Number']);
188
            if ( isset($entry['Serial Number']) && trim($entry['Serial Number']) != 'Not Specified' )
189
               $mem[] = 's/n ' . cleanUpDMIValue($entry['Serial Number']);
190
         }
191
         $return[] = "Memory\t" . implode( ', ', $mem );
192
      }
193
      $return[] = "Memory Total\t$totalRAM MB";
194
   }
195
 
196
   // power supplies
197
   if ( isset( $data['39'] ) ) {
198
      $return[] = "Power Supply Count\t" . count( $data['39'] );
199
      foreach ( $data['39'] as $entry ) {
200
         if ( preg_match('/^Present/i', $entry['Status'] ) ) {
201
            $psu = array();
202
            $psu[] = trim($entry['Name']);
203
            $psu[] = trim($entry['Max Power Capacity']);
204
            $psu[] = trim($entry['Manufacturer']);
205
            $psu[] = 'p/n ' . trim($entry['Model Part Number']);
206
            $psu[] = 's/n ' . trim($entry['Serial Number']);
207
            $return[] = "Power Supply\t" . implode( ', ', $psu );
208
         }
209
      }
210
   }
211
   return array( $return );
96 rodolico 212
}
97 rodolico 213
 
214
function cleanUpDMIValue( $value ) {
215
   $value = trim( $value );
216
   switch (strtolower( $value )) {
217
      case 'unknown':
218
      case 'not available':
219
      case 'not specified': return '';
220
   }
221
   return $value;
222
}
223
 
224
$filename = $argv[1];
225
if ( $filename ) {
226
   $contents = file( $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
227
   $out = dmidecode2array( $contents );
228
   print_r( $out );
229
} else {
230
   print "You must pass the file as the first command line parameter\n";
231
}