Subversion Repositories computer_asset_manager_v1

Rev

Rev 96 | Rev 98 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 96 Rev 97
Line 1... Line 1...
1
<?php
1
<?php
2
 
2
 
3
 
3
 
-
 
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
}
-
 
15
 
4
 
16
 
5
/*
17
/*
6
 * Function will take an array of lines representing the output of a dmidecode file
18
 * Function will take an array of lines representing the output of a dmidecode file
7
 * and pick and choose the values we want to put into CAMP attributes.
19
 * and pick and choose the values we want to put into CAMP attributes.
8
 * It will return an array of key/value pairs to be added into the attributes table
20
 * It will return an array of key/value pairs to be added into the attributes table
Line 72... Line 84...
72
      "42" => "Management Controller Host Interface"
84
      "42" => "Management Controller Host Interface"
73
);
85
);
74
   
86
   
75
   // verify this is something we can work with
87
   // verify this is something we can work with
76
   preg_match('/dmidecode ([0-9.]+)/', $contents[0], $results);
88
   preg_match('/dmidecode ([0-9.]+)/', $contents[0], $results);
77
   if ( $results[1] != "3.0" ) {
89
   #if ( $results[1] != "3.0" ) {
78
      return "This is only verified with dmidecode v3.0, v$results[1] found\n";
90
   #   return "This is only verified with dmidecode v3.0, v$results[1] found\n";
79
   }
91
   #}
80
   // first, let's parse it into a hash
92
   // first, let's parse it into a hash
81
   $currentHandle = '';
93
   $currentHandle = '';
82
   $currentType;
94
   $currentType;
83
   $data;
95
   $data = array();
84
   for ( $i = 0; $i < count($contents); $i++ ) {
96
   for ( $i = 0; $i < count($contents); $i++ ) {
85
      if ( preg_match('/^Handle ([0-9a-zx]+).*DMI type[^0-9]*(\d+),/i', $contents[$i], $outputArray) ) {
97
      if ( preg_match('/^Handle ([0-9a-zx]+).*DMI type[^0-9]*(\d+),/i', $contents[$i], $outputArray) ) {
86
         //print "matched\n"; print_r($outputArray); die;
98
         //print "matched\n"; print_r($outputArray); die;
87
         $currentType = $outputArray[2];
99
         $currentType = $outputArray[2];
88
         $currentHandle = $outputArray[1];
100
         $currentHandle = $outputArray[1];
Line 96... Line 108...
96
      }
108
      }
97
      if ( $currentHandle &&  preg_match('/^\s+(.*):\s+(.*)$/i', $contents[$i], $outputArray) ) {
109
      if ( $currentHandle &&  preg_match('/^\s+(.*):\s+(.*)$/i', $contents[$i], $outputArray) ) {
98
         $data[$currentType][$currentHandle][$outputArray[1]] = $outputArray[2];
110
         $data[$currentType][$currentHandle][$outputArray[1]] = $outputArray[2];
99
      }
111
      }
100
   }
112
   }
-
 
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 );
-
 
212
}
-
 
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
   }
101
   return $data;
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";
102
}
231
}
103
 
-
 
104
$contents = file( 'dmidecode/xen4.dmidecode', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
-
 
105
$out = dmidecode2array( $contents );
-
 
106
print_r( $out );
-