Subversion Repositories computer_asset_manager_v1

Rev

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