Line 14... |
Line 14... |
14 |
* them to simpl eval() blocks.
|
14 |
* them to simpl eval() blocks.
|
15 |
*
|
15 |
*
|
16 |
* V0.9.2 20160217 RWR
|
16 |
* V0.9.2 20160217 RWR
|
17 |
* Fixed issue where if a device was marked as part of a Xen machine, it
|
17 |
* Fixed issue where if a device was marked as part of a Xen machine, it
|
18 |
* was being deleted by pci updates.
|
18 |
* was being deleted by pci updates.
|
- |
|
19 |
* Fixed issue where serial number was not being updated
|
19 |
*
|
20 |
*
|
20 |
*/
|
21 |
*/
|
21 |
|
22 |
|
22 |
require 'library.php';
|
23 |
require 'library.php';
|
23 |
$VERSION='0.9.2';
|
24 |
$VERSION='0.9.2';
|
Line 43... |
Line 44... |
43 |
private $errors = array( 1 => 'Could not process file, not xml, yaml or ini',
|
44 |
private $errors = array( 1 => 'Could not process file, not xml, yaml or ini',
|
44 |
2 => 'Invalid report, does not have one or more of report date, client name or computer name',
|
45 |
2 => 'Invalid report, does not have one or more of report date, client name or computer name',
|
45 |
3 => 'Invalid Report, invalid machine name',
|
46 |
3 => 'Invalid Report, invalid machine name',
|
46 |
4 => 'Duplicate Report',
|
47 |
4 => 'Duplicate Report',
|
47 |
5 => 'New Report, must be added to CAMP',
|
48 |
5 => 'New Report, must be added to CAMP',
|
48 |
6 => 'Report is in queue for addition after updates to CAMP'
|
49 |
6 => 'Report is in queue for addition after updates to CAMP',
|
- |
|
50 |
7 => 'Too many entries match criteria of name, client and serial number'
|
49 |
);
|
51 |
);
|
50 |
|
52 |
|
51 |
/*
|
53 |
/*
|
52 |
* $parsingArray contains one or more rows, each of which have three rows, starttag, endtag and eval
|
54 |
* $parsingArray contains one or more rows, each of which have three rows, starttag, endtag and eval
|
53 |
* starttag and endtag are regex's which match the beginning and end of a document (ie, --- and ... for yaml)
|
55 |
* starttag and endtag are regex's which match the beginning and end of a document (ie, --- and ... for yaml)
|
Line 65... |
Line 67... |
65 |
array_unshift( $this->messages,
|
67 |
array_unshift( $this->messages,
|
66 |
"Report on $this->reportDateSQL, client $this->clientName, system $this->machineName ($this->fileType)");
|
68 |
"Report on $this->reportDateSQL, client $this->clientName, system $this->machineName ($this->fileType)");
|
67 |
// now, we do every step in turn. If they set the FATAL flag, we exit.
|
69 |
// now, we do every step in turn. If they set the FATAL flag, we exit.
|
68 |
if ( $this->FATAL ) return $this->returnCode;
|
70 |
if ( $this->FATAL ) return $this->returnCode;
|
69 |
$this->getMachineID();
|
71 |
$this->getMachineID();
|
70 |
if ( empty( $this->machineID ) ) return $this->returnCode;
|
72 |
if ( empty( $this->machineID ) or $this->FATAL ) return $this->returnCode;
|
71 |
$this->checkDuplicate();
|
73 |
$this->checkDuplicate();
|
72 |
if ( $this->FATAL) return $this->returnCode;
|
74 |
if ( $this->FATAL) return $this->returnCode;
|
73 |
$this->updateComputerMakeup();
|
75 |
$this->updateComputerMakeup();
|
74 |
$this->updateOS();
|
76 |
$this->updateOS();
|
75 |
$this->updateBootTime();
|
77 |
$this->updateBootTime();
|
76 |
$this->doIPAddresses();
|
78 |
$this->doIPAddresses();
|
77 |
$this->processPCI();
|
79 |
$this->processPCI();
|
78 |
$this->processSoftwarePackages();
|
80 |
$this->processSoftwarePackages();
|
- |
|
81 |
$this->processXen();
|
79 |
// at this point, we don't really need the report title information
|
82 |
// at this point, we don't really need the report title information
|
80 |
// so we will remove it before recordReport has a chance to put it
|
83 |
// so we will remove it before recordReport has a chance to put it
|
81 |
// in database
|
84 |
// in database
|
82 |
array_shift( $this->messages );
|
85 |
array_shift( $this->messages );
|
83 |
// now record the report
|
86 |
// now record the report
|
Line 201... |
Line 204... |
201 |
} else {
|
204 |
} else {
|
202 |
$this->machineName = $this->report['system']['hostname'];
|
205 |
$this->machineName = $this->report['system']['hostname'];
|
203 |
}
|
206 |
}
|
204 |
$this->FATAL = ! empty( $this->messages );
|
207 |
$this->FATAL = ! empty( $this->messages );
|
205 |
|
208 |
|
206 |
/* removed for the time being.
|
- |
|
207 |
// serial number is fairly new, so it is not a critical error
|
209 |
// serial number is fairly new, so it is not a critical error
|
208 |
// it will be set in getMachineID if we have it here, but not there
|
210 |
// it will be set in getMachineID if we have it here, but not there
|
209 |
if ( empty ( $this->report['system']['serial'] ) ) {
|
211 |
if ( empty ( $this->report['system']['serial'] ) ) {
|
210 |
$this->messages[] = 'No Serial Number';
|
212 |
$this->messages[] = 'No Serial Number';
|
211 |
} else {
|
213 |
} else {
|
212 |
$this->serialNumber = $this->report['system']['serial'];
|
214 |
$this->serialNumber = $this->report['system']['serial'];
|
213 |
}
|
215 |
}
|
214 |
*/
|
- |
|
215 |
|
216 |
|
216 |
} // function validateReport
|
217 |
} // function validateReport
|
217 |
|
218 |
|
218 |
|
219 |
|
219 |
/*
|
220 |
/*
|
Line 225... |
Line 226... |
225 |
* machineID - index value of machine in database
|
226 |
* machineID - index value of machine in database
|
226 |
*/
|
227 |
*/
|
227 |
private function getMachineID () {
|
228 |
private function getMachineID () {
|
228 |
$this->clientID = getClientID( $this->clientName );
|
229 |
$this->clientID = getClientID( $this->clientName );
|
229 |
if ( $this->clientID ) {
|
230 |
if ( $this->clientID ) {
|
- |
|
231 |
try {
|
230 |
$this->machineID = getMachineID( $this->machineName, $this->clientID, $this->serialNumber );
|
232 |
$this->machineID = getMachineID( $this->machineName, $this->clientID, $this->serialNumber );
|
- |
|
233 |
}
|
- |
|
234 |
catch (Exception $e) {
|
- |
|
235 |
$this->messages[] = "Duplicate entries found when trying to process Machine [$this->machineName], Client [$this->clientID], Serial [$this->serialNumber]";
|
- |
|
236 |
$this->FATAL = true;
|
- |
|
237 |
$this->returnCode = 7;
|
- |
|
238 |
return false;
|
- |
|
239 |
}
|
231 |
}
|
240 |
}
|
232 |
if ( empty( $this->machineID ) ) {
|
241 |
if ( empty( $this->machineID ) ) {
|
233 |
$this->messages[] = $this->makeUnknown( $this->clientName, $this->machineName, $this->reportDateSQL );
|
242 |
$this->messages[] = $this->makeUnknown( $this->clientName, $this->machineName, $this->reportDateSQL );
|
234 |
return false;
|
243 |
return false;
|
235 |
}
|
244 |
}
|
236 |
if ( $this->machineID == -1 ) {
|
- |
|
237 |
$this->messages[] = "Duplicate serial number found for serial [$this->serialNumber], aborting";
|
- |
|
238 |
return false;
|
- |
|
239 |
}
|
- |
|
240 |
return true;
|
245 |
return true;
|
241 |
} // function getMachineID
|
246 |
} // function getMachineID
|
242 |
|
247 |
|
- |
|
248 |
|
- |
|
249 |
|
243 |
/*
|
250 |
/*
|
244 |
* checks for a duplicate report, ie one that has already been run.
|
251 |
* checks for a duplicate report, ie one that has already been run.
|
245 |
* if this computer has a report already for this date/time
|
252 |
* if this computer has a report already for this date/time
|
246 |
*/
|
253 |
*/
|
247 |
private function checkDuplicate() {
|
254 |
private function checkDuplicate() {
|
Line 294... |
Line 301... |
294 |
}
|
301 |
}
|
295 |
return $result;
|
302 |
return $result;
|
296 |
}
|
303 |
}
|
297 |
|
304 |
|
298 |
/*
|
305 |
/*
|
- |
|
306 |
* checks to values to see if they are equal
|
- |
|
307 |
* If they are both numeric, and $fuzzyValue is non-zero, will determine
|
- |
|
308 |
* if $value2 is within $value1 +/- percentage of $fuzzyValue.
|
- |
|
309 |
* Thus, if $fuzzyValue is 0.10, they are "equal" as long as $value1 is
|
- |
|
310 |
* within $value2 +/- 10%
|
- |
|
311 |
*/
|
- |
|
312 |
private function sloppyEqual ( $value1, $value2, $fuzzyValue ) {
|
- |
|
313 |
return
|
- |
|
314 |
( is_numeric( $value1 ) and is_numeric( $value2 ) and $fuzzyValue and
|
- |
|
315 |
$value1 <= $value2 + $value2 * $fuzzyValue and
|
- |
|
316 |
$value1 >= $value2 - $value2 * $fuzzyValue
|
- |
|
317 |
)
|
- |
|
318 |
or
|
- |
|
319 |
$value1 == $value2;
|
- |
|
320 |
}
|
- |
|
321 |
|
- |
|
322 |
|
- |
|
323 |
/*
|
299 |
* Checks an attribute from the device_attriburtes table. If the value has
|
324 |
* Checks an attribute from the device_attriburtes table. If the value has
|
300 |
* changed, sets the old one's removed_date to this report date, then adds
|
325 |
* changed, sets the old one's removed_date to this report date, then adds
|
301 |
* new record for new value.
|
326 |
* new record for new value.
|
- |
|
327 |
* If value is not off by more than $slop, will not update. Useful for memory and speed on
|
- |
|
328 |
* virtual devices where the values may be slightly different at different readings
|
302 |
* If the record does not exist, simply creates it
|
329 |
* If the record does not exist, simply creates it
|
303 |
*/
|
330 |
*/
|
304 |
public function checkAndUpdateAttribute( $attribute, $value ) {
|
331 |
public function checkAndUpdateAttribute( $attribute, $value, $slop = 0 ) {
|
305 |
if ( !isset($attribute) || !isset($value ) ) {
|
332 |
if ( !isset($attribute) || !isset($value ) ) {
|
306 |
$this->messages[] = "Error: attempt to use null value for [$attribute], value [$value] for ID $this->machineID in checkAndUPdateAttribute";
|
333 |
$this->messages[] = "Error: attempt to use null value for [$attribute], value [$value] for ID $this->machineID in checkAndUPdateAttribute";
|
307 |
return false;
|
334 |
return false;
|
308 |
}
|
335 |
}
|
309 |
$value = makeSafeSQLValue($value); # we want to always quote the value on this particular one
|
- |
|
310 |
//$attribute = makeSafeSQLValue( $attribute );
|
- |
|
311 |
$attrib_id = $this->getAttributeID( $attribute, $this->reportDateSQL );
|
336 |
$attrib_id = $this->getAttributeID( $attribute, $this->reportDateSQL );
|
312 |
$result = getOneDBValue( "select device_attrib.value
|
337 |
$result = getOneDBValue( "select device_attrib.value
|
313 |
from device_attrib join attrib using (attrib_id)
|
338 |
from device_attrib join attrib using (attrib_id)
|
314 |
where device_attrib.device_id = $this->machineID
|
339 |
where device_attrib.device_id = $this->machineID
|
315 |
and device_attrib.removed_date is null
|
340 |
and device_attrib.removed_date is null
|
316 |
and attrib.attrib_id = $attrib_id
|
341 |
and attrib.attrib_id = $attrib_id
|
317 |
");
|
342 |
");
|
318 |
$result = makeSafeSQLValue( $result ); # we must do this since we are comparing to $value which has had this done
|
- |
|
319 |
if ( isset( $result ) ) { # got it, now see if it compares ok
|
343 |
if ( isset( $result ) && ! $this->sloppyEqual( $value, $result, $slop ) ) { # got it, now see if it compares ok
|
320 |
if ( $value != $result ) { # nope, this has changed. Note, must use fixDatabaseValue for escapes already in $value
|
344 |
$this->messages[] = "New value [$value] for $attribute for device $this->machineID, voiding previous";
|
321 |
$this->messages[] = "New value [$value] for $attribute for device $this->machineID, voiding previous";
|
345 |
$value = makeSafeSQLValue($value); # we want to always quote the value on this particular one
|
322 |
queryDatabaseExtended( "update device_attrib
|
346 |
queryDatabaseExtended( "update device_attrib
|
323 |
set removed_date = '$this->reportDateSQL'
|
347 |
set removed_date = '$this->reportDateSQL'
|
324 |
where device_id = $this->machineID
|
348 |
where device_id = $this->machineID
|
325 |
and attrib_id = $attrib_id
|
349 |
and attrib_id = $attrib_id
|
326 |
and removed_date is null");
|
350 |
and removed_date is null");
|
327 |
unset( $result ); # this will force the insert in the next block of code
|
351 |
unset( $result ); # this will force the insert in the next block of code
|
328 |
} # if $result ne $value
|
- |
|
329 |
} # if ($result)
|
352 |
} # if ($result)
|
330 |
if ( ! isset( $result ) ) { # we have no valid entry for this attribute
|
353 |
if ( ! isset( $result ) ) { # we have no valid entry for this attribute
|
331 |
//$this->messages[] = "In checkAndUpdateAttribute, adding new record with insert into device_attrib(device_id,attrib_id,value,added_date) values ($this->machineID,$attrib_id,$value,$this->reportDateSQL)";
|
354 |
//$this->messages[] = "In checkAndUpdateAttribute, adding new record with insert into device_attrib(device_id,attrib_id,value,added_date) values ($this->machineID,$attrib_id,$value,$this->reportDateSQL)";
|
332 |
queryDatabaseExtended( "insert into device_attrib(device_id,attrib_id,value,added_date)
|
355 |
queryDatabaseExtended( "insert into device_attrib(device_id,attrib_id,value,added_date)
|
333 |
values ($this->machineID,$attrib_id,$value,'$this->reportDateSQL')");
|
356 |
values ($this->machineID,$attrib_id,$value,'$this->reportDateSQL')");
|
Line 336... |
Line 359... |
336 |
return false;
|
359 |
return false;
|
337 |
} // checkAndUpdateAttribute
|
360 |
} // checkAndUpdateAttribute
|
338 |
|
361 |
|
339 |
# simply verifies some attributes of the computer
|
362 |
# simply verifies some attributes of the computer
|
340 |
private function updateComputerMakeup() {
|
363 |
private function updateComputerMakeup() {
|
341 |
$this->checkAndUpdateAttribute('Memory', $this->report['system']['memory']);
|
364 |
$this->checkAndUpdateAttribute('Memory', $this->report['system']['memory'], 0.30); // memory can be plus/minus 30%
|
342 |
$this->checkAndUpdateAttribute('Number of CPUs',$this->report['system']['num_cpu']);
|
365 |
$this->checkAndUpdateAttribute('Number of CPUs',$this->report['system']['num_cpu']);
|
343 |
$this->checkAndUpdateAttribute('CPU Type', $this->report['system']['cpu_type']);
|
366 |
$this->checkAndUpdateAttribute('CPU Type', $this->report['system']['cpu_type']);
|
344 |
$this->checkAndUpdateAttribute('CPU SubType', $this->report['system']['cpu_sub']);
|
367 |
$this->checkAndUpdateAttribute('CPU SubType', $this->report['system']['cpu_sub']);
|
345 |
$this->checkAndUpdateAttribute('CPU Speed', $this->report['system']['cpu_speed']);
|
368 |
$this->checkAndUpdateAttribute('CPU Speed', $this->report['system']['cpu_speed'], 0.30 ); // cpu speed can be plus/minus 30%
|
- |
|
369 |
// validate serial number and update if necessary
|
- |
|
370 |
if ( $this->serialNumber ) {
|
- |
|
371 |
$dbSerial = getOneDBValue( "select serial from device where device_id = $this->machineID" );
|
- |
|
372 |
if ( ! isset ( $dbSerial ) or $dbSerial != $this->serialNumber ) {
|
- |
|
373 |
$this->messages[] = "Updating serial number to $this->serialNumber";
|
- |
|
374 |
queryDatabaseExtended( "update device set serial = '$this->serialNumber' where device_id = $this->machineID" );
|
- |
|
375 |
}
|
- |
|
376 |
}
|
346 |
} // updateComputerMakeup
|
377 |
} // updateComputerMakeup
|
347 |
|
378 |
|
- |
|
379 |
|
- |
|
380 |
|
348 |
// This is kind of funky, because column = null does not work, it must be column is null, so we have to look for it.
|
381 |
// This is kind of funky, because column = null does not work, it must be column is null, so we have to look for it.
|
349 |
function makeSQLEquals ( $field, $value ) {
|
382 |
function makeSQLEquals ( $field, $value ) {
|
350 |
return "$field " . ( $value == 'null' ? 'is null' : '=' . $value );
|
383 |
return "$field " . ( $value == 'null' ? 'is null' : '=' . $value );
|
351 |
}
|
384 |
}
|
352 |
|
385 |
|
Line 877... |
Line 910... |
877 |
} // foreach
|
910 |
} // foreach
|
878 |
if ( $count )
|
911 |
if ( $count )
|
879 |
$this->messages[] = "$count software packages added/updated";
|
912 |
$this->messages[] = "$count software packages added/updated";
|
880 |
}
|
913 |
}
|
881 |
} // processSoftwarePackages
|
914 |
} // processSoftwarePackages
|
- |
|
915 |
|
- |
|
916 |
|
- |
|
917 |
function processXenVirtual ( $name, $values ) {
|
- |
|
918 |
// Determine if the virtual is on a new machine or not
|
- |
|
919 |
$virtualID = getMachineID( $name ); // see if we can find the virtual
|
- |
|
920 |
|
- |
|
921 |
if ( $virtualID ) {
|
- |
|
922 |
if ( $this->machineID == getOneDBValue( "select part_of from device where device.device_id = $virtualID" ) ) {
|
- |
|
923 |
return;
|
- |
|
924 |
}
|
- |
|
925 |
} else { // we could not find the device
|
- |
|
926 |
$this->messages[] = "Could not locate id for virtual $name which is running on $this->machineName";
|
- |
|
927 |
return;
|
- |
|
928 |
}
|
- |
|
929 |
// we made it this far, which means we found the virtual's ID, but it does not have
|
- |
|
930 |
// this machine as its parent, so we need to update it.
|
- |
|
931 |
queryDatabaseExtended( "update device set part_of = $this->machineID where device_id = $virtualID" );
|
- |
|
932 |
$this->messages[] = "Virtual $name ($virtualID) now running on $this->machineName";
|
- |
|
933 |
} // processXenVirtual
|
- |
|
934 |
|
- |
|
935 |
function processXen ( ) {
|
- |
|
936 |
// Is this a Xen DOM0?
|
- |
|
937 |
if ( ! isset( $this->report['xen'] ) ) return;
|
- |
|
938 |
$machineID; // id in database
|
- |
|
939 |
$machineName;// name in report
|
- |
|
940 |
|
- |
|
941 |
foreach ( $this->report['xen']['virtual'] as $virtual => $data ) {
|
- |
|
942 |
$this->processXenVirtual( $virtual, $data );
|
- |
|
943 |
}
|
- |
|
944 |
} // processXen
|
882 |
|
945 |
|
883 |
} // class sysinfo
|
946 |
} // class sysinfo
|
884 |
|
947 |
|
885 |
|
948 |
|
886 |
|
949 |
|
Line 917... |
Line 980... |
917 |
}
|
980 |
}
|
918 |
if ( ! $result )
|
981 |
if ( ! $result )
|
919 |
exit ( 1 );
|
982 |
exit ( 1 );
|
920 |
$result = $thisReport->processReport();
|
983 |
$result = $thisReport->processReport();
|
921 |
print $thisReport->dumpMessages();
|
984 |
print $thisReport->dumpMessages();
|
922 |
//var_dump( $thisReport );
|
985 |
// var_dump( $thisReport );
|
923 |
exit ( $result );
|
986 |
exit ( $result );
|
924 |
|
987 |
|
925 |
?>
|
988 |
?>
|