Subversion Repositories computer_asset_manager_v1

Rev

Rev 110 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 rodolico 1
<?php
2
 
3
/*
4
 * Requires php-pear and libyaml under Debian Wheezy
11 rodolico 5
 *
6
 * apt-get install php-pear php5-dev libyaml-dev libyaml-0-2 libyaml-0-2-dbg
3 rodolico 7
 * pecl install yaml
11 rodolico 8
 * echo 'extension=yaml.so' >> /etc/php5/cli/php.ini
9
 * echo 'extension=yaml.so' >> /etc/apache2/cli/php.ini
5 rodolico 10
 *
11
 * V0.9 20160203 RWR
12
 * Changed to use yaml for the configuration file. Had to take the
13
 * anonymous functions which parsed the different file types and convert
14
 * them to simpl eval() blocks.
15
 * 
16 rodolico 16
 * V0.9.2 20160217 RWR
17
 * Fixed issue where if a device was marked as part of a Xen machine, it
18
 * was being deleted by pci updates.
36 rodolico 19
 * Fixed issue where serial number was not being updated
110 rodolico 20
 *
21
 * v0.9.3 20200218 RWR
22
 * Fixed invalid table name (attrib_device vs device_attrib) and set up to read a new section, attributes
16 rodolico 23
 * 
3 rodolico 24
*/
25
 
26
require 'library.php';
110 rodolico 27
$VERSION='0.9.3';
3 rodolico 28
 
29
 
30
class sysinfo {
31
   private $report; // this will hold an entire report
32
   private $messages = array();
33
   private $FATAL = false;
34
   private $parsingArray;   // this is a required array that teaches how to properly parse the input
35
 
36
   // following are used internally to track important information
37
   private $clientID;   // id in database
38
   private $clientName; // name from report
39
   private $machineID;  // id in database
40
   private $machineName;// name in report
41
   private $reportDate; // unix timestamp of report
42
   private $reportDateSQL; // used for inserts and compares in the database
43
   private $serialNumber; // the serial number, which is assumed to be unique
44
   private $fileContents;
45
   private $processDuplicates = false; // used for testing; allows duplicate reports
46
   private $returnCode = 0; // how we returned from this
47
   private $errors = array( 1 => 'Could not process file, not xml, yaml or ini',
48
                            2 => 'Invalid report, does not have one or more of report date, client name or computer name',
49
                            3 => 'Invalid Report, invalid machine name',
50
                            4 => 'Duplicate Report',
51
                            5 => 'New Report, must be added to CAMP',
36 rodolico 52
                            6 => 'Report is in queue for addition after updates to CAMP',
53
                            7 => 'Too many entries match criteria of name, client and serial number'
3 rodolico 54
                          );
55
 
56
   /*
57
    * $parsingArray contains one or more rows, each of which have three rows, starttag, endtag and eval
58
    * starttag and endtag are regex's which match the beginning and end of a document (ie, --- and ... for yaml)
59
    * eval is an anonymous function which will evaluate a particular type of document, returning it as an
60
    * array which we store in $report
61
    */
62
 
63
   function __construct( $parsingArray ) {
64
      $this->parsingArray = $parsingArray;
65
   }
66
 
67
   public function processReport () {
68
      $this->validateReport(); // validate the report appears to be ok
69
      // prepend the name of the report to messages BEFORE any error messages that may have appeared
70
      array_unshift( $this->messages,
71
            "Report on $this->reportDateSQL, client $this->clientName, system $this->machineName ($this->fileType)");
72
      // now, we do every step in turn. If they set the FATAL flag, we exit.
73
      if ( $this->FATAL ) return $this->returnCode;
74
      $this->getMachineID();
36 rodolico 75
      if ( empty( $this->machineID ) or $this->FATAL ) return $this->returnCode;
9 rodolico 76
      $this->checkDuplicate();
3 rodolico 77
      if ( $this->FATAL) return $this->returnCode;
78
      $this->updateComputerMakeup();
79
      $this->updateOS();
80
      $this->updateBootTime();
81
      $this->doIPAddresses();
82
      $this->processPCI();
83
      $this->processSoftwarePackages();
36 rodolico 84
      $this->processXen();
110 rodolico 85
      $this->processAttributes();
9 rodolico 86
      // at this point, we don't really need the report title information
87
      // so we will remove it before recordReport has a chance to put it
88
      // in database
89
      array_shift( $this->messages );
90
      // now record the report
91
      $this->recordReport();
3 rodolico 92
      return $this->returnCode;;
93
   } // function processReport
94
 
95
   /* 
96
    * Name: loadFromFile
97
    * Parameters: filename (path to load file from)
98
    * Returns: true if on success, false if not
99
    * Modifies: 
100
    *    report - added messages on why the report is not valid
101
    *    FATAL    - set to true if any of the required values do not exist
102
    */
103
 
104
   public function loadFromFile ( $filename ) {
105
      // load file into memory. Additionally, remove any \r's from it (leaving \n's for newlines)
106
      $this->fileContents = str_replace("\r","", file_get_contents( $filename ) );
107
      // try to parse the body out
108
      $this->getBody();
109
      if ( isset( $this->report ) ) {
110
         if ( $this->fileType == 'xml' ) $this->fixXML();
111
         if ( $this->fileType == 'ini' ) {
112
            $this->messages[] = 'We do not process ini files, but storing it instead';
113
         }
114
         return $this->fileType;
115
      } else { // we don't know what it is
116
         $this->FATAL = true;
117
         $this->messages[] = "Unable to parse [$filename] using YAML, XML or INI";
118
         return false;
119
      }
120
      //unset( $this->fileContents ); // free up memory
121
   } // function loadFromFile
122
 
123
 
124
   // once the file has been loaded, we need to figure out what kind of file it is
125
   // then use the function embedded in $parsingArray to import it.
126
   private function getBody ( ) {
127
      foreach ( $this->parsingArray as $key => $regexes ) {
128
         $matches;
129
         $pattern = $regexes['startTag'] . '.*' . $regexes['endTag'];
130
         if ( preg_match( "/$pattern/ms", $this->fileContents, $matches ) ) {
131
            $this->fileType = $key;
132
            $this->fileContents = $matches[0];
5 rodolico 133
            $body = $this->fileContents;
134
            $this->report = eval( $regexes['eval'] );
135
            if ( $this->report ) return true;
3 rodolico 136
         } // if
137
      } // foreach
138
      return false;
139
   }
140
 
141
   /*
142
    * Name: fixXML
143
    * Parameters: None
144
    * Returns: nothing
145
    * Modifies: $this->report
146
    * YAML creates the entries as ['network']['interface name']
147
    * XML  creates the entries as ["network"][x][name]=>
148
    * We will simply copy the XML information into a duplicate
149
    * in YAML style
150
    */
151
   private function fixXML() {
152
       $oldInfo = $this->report['network'];
153
       unset ($this->report['network']);
154
       foreach ( $oldInfo as $entry ) {
155
          #$this->messages[] = "Duplicating network entry for $entry[name]";
156
          foreach ($entry as $key => $value ) {
157
             $this->report['network'][$entry['name']][$key] = $value;
158
             #$this->messages[] = "  Adding  $value as value for network.name.$key";
159
          } // inner foreach
160
       } // outer foreach
161
   } // fixXML
162
 
163
 
164
   private function processINI() {
165
      return false;
166
   }
167
 
168
   public function dumpMessages () {
9 rodolico 169
      $return = '';
170
      if ( $this->messages ) {
171
         $return = implode( "\n", $this->messages );
172
         $return .= "\n";
173
      }
3 rodolico 174
      return $return;
175
   }
176
 
177
   /* 
178
    * Name: validateReport
179
    * Parameters: none
180
    * Returns: true if valid, false if not
181
    * Modifies: 
182
    *    messages - added messages on why the report is not valid
183
    *    FATAL    - set to true if any of the required values do not exist
184
    */
185
 
186
      public function validateReport () {
187
      /*
188
       * A report is considered valid if it has a date, client name and hostname
189
       * No check is made at this time for validity of that data
190
       */
191
      if ( empty( $this->report['report']['date']) ) {
192
         $this->messages[] = 'No report date';
193
      } else {
194
         $this->reportDate = strtotime( $this->report['report']['date'] ); // store in Unix timestamp
195
         $this->reportDateSQL = strftime( '%F %T', $this->reportDate ); // save a copy of the date in SQL format
196
         if ( empty( $this->reportDate ) )
197
            $this->messages[] = "Unable to parse report date [$this->report['report']['date']]";
198
      }
199
 
200
      if ( empty( $this->report['report']['client'] ) ) {
201
         $this->messages[] = 'No client name';
202
      } else {
203
         $this->clientName = $this->report['report']['client'];
204
      }
205
 
206
      if ( empty ( $this->report['system']['hostname'] ) ) {
207
         $this->messages[] = 'No Computer Name';
208
      } else {
209
         $this->machineName = $this->report['system']['hostname'];
210
      }
211
      $this->FATAL = ! empty( $this->messages );
212
 
213
      // serial number is fairly new, so it is not a critical error
214
      // it will be set in getMachineID if we have it here, but not there
215
      if ( empty ( $this->report['system']['serial'] ) ) {
216
         $this->messages[] = 'No Serial Number';
217
      } else {
218
         $this->serialNumber = $this->report['system']['serial'];
219
      }
220
 
221
   } // function validateReport
222
 
223
 
224
   /* 
225
    * Name: getMachineID
226
    * Parameters: none
227
    * Returns: true if found, false if not
228
    * Modifies: 
229
    *    clientID - index value of client in database
230
    *    machineID - index value of machine in database
231
    */
232
   private function getMachineID () {
233
      $this->clientID = getClientID( $this->clientName );
234
      if ( $this->clientID ) {
36 rodolico 235
         try {
236
            $this->machineID = getMachineID( $this->machineName, $this->clientID, $this->serialNumber );
237
         }
238
         catch (Exception $e) {
239
            $this->messages[] = "Duplicate entries found when trying to process Machine [$this->machineName], Client [$this->clientID], Serial [$this->serialNumber]";
240
            $this->FATAL = true;
241
            $this->returnCode = 7;
242
            return false;
243
         }
3 rodolico 244
      }
245
      if ( empty( $this->machineID ) ) {
246
         $this->messages[] = $this->makeUnknown( $this->clientName, $this->machineName, $this->reportDateSQL );
247
         return false;
248
      }
249
      return true;
250
   } // function getMachineID
251
 
36 rodolico 252
 
253
 
3 rodolico 254
   /* 
255
    * checks for a duplicate report, ie one that has already been run.
256
    * if this computer has a report already for this date/time
257
    */ 
9 rodolico 258
   private function checkDuplicate() {
3 rodolico 259
      $count = getOneDBValue("select count(*) from sysinfo_report where device_id = $this->machineID and report_date = '$this->reportDateSQL'");
260
      if ( $this->processDuplicates ) { $count = 0; } // for testing
261
      if ( $count ) {
262
         $this->messages[] = "Duplicate Report for $this->machineName (id $this->machineID) on $this->reportDateSQL";
263
         $this->returnCode = 4;
264
         $this->FATAL = true;
265
         return false;
266
      }
9 rodolico 267
      return true;
268
   } // recordReport
269
 
270
 
271
   /* 
272
    * Creates an entry in the sysinfo_report table
273
    */ 
274
   private function recordReport() {
3 rodolico 275
      $version = $this->report['report']['version'];
9 rodolico 276
      $messages = makeSafeSQLValue( $this->dumpMessages() );
277
      // if we made it this far, we are ok, so just add the report id
278
      queryDatabaseExtended("insert into sysinfo_report(device_id,version,report_date, notes,added_date) values ($this->machineID ,'$version','$this->reportDateSQL',$messages,now())");
3 rodolico 279
      $this->reportID = getOneDBValue("select max( sysinfo_report_id ) from sysinfo_report where device_id = $this->machineID and report_date = '$this->reportDateSQL'");
280
      return true;
9 rodolico 281
   } // recordReport
3 rodolico 282
 
283
 
284
   function makeUnknown( $clientName, $machineName, $reportDate ) {
285
      $result = getOneDBValue("select report_date from unknown_entry where client_name = '$clientName' and device_name = '$machineName'");
286
      if ( empty( $result ) ) {
287
         queryDatabaseExtended( "insert into unknown_entry(client_name,device_name,report_date) values ('$clientName', '$machineName', '$reportDate')");
288
         $this->returnCode = 5;
289
         return "New entry, client=$clientName, device=$machineName. You must update Camp before this report can be processed";
290
      } else {
291
         $this->returnCode = 6;
292
         return "New entry detected, but entry already made. You must update Camp before this can be processed";
293
      }
294
   }
295
 
110 rodolico 296
   // simply used to get an attrib_id. If it does not exist, will create it
3 rodolico 297
 
298
   private function getAttributeID ( $attributeName, $reportDate ) {
299
      $attributeName = makeSafeSQLValue( $attributeName );
300
      $result = getOneDBValue( "select attrib_id from attrib where name = $attributeName and removed_date is null" );
301
      if ( !isset( $result) ) {
302
         $result = queryDatabaseExtended( "insert into attrib (name,added_date) values ($attributeName,'$reportDate')");
303
         $this->messages[] = "Added a new attribute type [$attributeName]";
304
         $result = $result['insert_id'];
305
      }
306
      return $result;
307
   }
308
 
309
   /*
36 rodolico 310
    * checks to values to see if they are equal
311
    * If they are both numeric, and $fuzzyValue is non-zero, will determine
312
    * if $value2 is within $value1 +/- percentage of $fuzzyValue.
313
    * Thus, if $fuzzyValue is 0.10, they are "equal" as long as $value1 is 
314
    * within $value2 +/- 10%
315
    */
316
   private function sloppyEqual ( $value1, $value2, $fuzzyValue ) {
317
      return 
318
         ( is_numeric( $value1 ) and is_numeric( $value2 ) and $fuzzyValue and
319
            $value1 <= $value2 + $value2 * $fuzzyValue and
320
            $value1 >= $value2 - $value2 * $fuzzyValue 
321
         )
322
         or
323
         $value1 == $value2;
324
   }
325
 
326
 
327
   /*
110 rodolico 328
    * Checks an attribute from the attrib_deviceurtes table. If the value has
3 rodolico 329
    * changed, sets the old one's removed_date to this report date, then adds
330
    * new record for new value.
36 rodolico 331
    * If value is not off by more than $slop, will not update. Useful for memory and speed on
332
    * virtual devices where the values may be slightly different at different readings
3 rodolico 333
    * If the record does not exist, simply creates it
334
    */
36 rodolico 335
   public function checkAndUpdateAttribute( $attribute, $value, $slop = 0 ) {
3 rodolico 336
      if ( !isset($attribute) || !isset($value ) ) {
337
         $this->messages[] = "Error: attempt to use null value for [$attribute], value [$value] for ID $this->machineID in checkAndUPdateAttribute";
338
         return false;
339
      }
340
      $attrib_id = $this->getAttributeID( $attribute, $this->reportDateSQL );
110 rodolico 341
      $result = getOneDBValue( "select attrib_device.value
342
         from attrib_device join attrib using (attrib_id)
343
         where attrib_device.device_id = $this->machineID
344
               and attrib_device.removed_date is null
3 rodolico 345
               and attrib.attrib_id = $attrib_id
346
         ");
36 rodolico 347
      if ( isset( $result ) && ! $this->sloppyEqual( $value, $result, $slop ) ) { # got it, now see if it compares ok
348
         $this->messages[] = "New value [$value] for $attribute for device $this->machineID, voiding previous";
349
         $value = makeSafeSQLValue($value); # we want to always quote the value on this particular one
110 rodolico 350
         queryDatabaseExtended( "update attrib_device
36 rodolico 351
                                    set removed_date = '$this->reportDateSQL'
352
                                 where device_id = $this->machineID
353
                                       and attrib_id = $attrib_id
354
                                       and removed_date is null");
355
         unset( $result ); # this will force the insert in the next block of code
3 rodolico 356
      } # if ($result)
357
      if ( ! isset( $result ) ) { # we have no valid entry for this attribute
110 rodolico 358
         //$this->messages[] = "In checkAndUpdateAttribute, adding new record with insert into attrib_device(device_id,attrib_id,value,added_date) values ($this->machineID,$attrib_id,$value,$this->reportDateSQL)";
359
         queryDatabaseExtended( "insert into attrib_device(device_id,attrib_id,value,added_date)
3 rodolico 360
                                 values ($this->machineID,$attrib_id,$value,'$this->reportDateSQL')");
361
         return true;
362
      }
363
      return false;
364
   } // checkAndUpdateAttribute
365
 
366
   # simply verifies some attributes of the computer
367
   private function updateComputerMakeup() {
111 rodolico 368
      if ( isset( $this->report['system']['memory'] ) ) {
369
         $this->checkAndUpdateAttribute('Memory',        $this->report['system']['memory'], 0.30); // memory can be plus/minus 30%
370
      }
371
      if ( isset( $this->report['system']['num_cpu'] ) ) {
372
         $this->checkAndUpdateAttribute('Number of CPUs',$this->report['system']['num_cpu']);
373
      }
374
      if ( isset( $this->report['system']['cpu_type'] ) ) {
375
         $this->checkAndUpdateAttribute('CPU Type',      $this->report['system']['cpu_type']);
376
      }
377
      if ( isset( $this->report['system']['cpu_sub'] ) ) {
378
         $this->checkAndUpdateAttribute('CPU SubType',   $this->report['system']['cpu_sub']);
379
      }
380
      if ( isset( $this->report['system']['cpu_speed'] ) ) {
381
         $this->checkAndUpdateAttribute('CPU Speed',     $this->report['system']['cpu_speed'], 0.30 ); // cpu speed can be plus/minus 30%
382
      }
36 rodolico 383
      // validate serial number and update if necessary
384
      if ( $this->serialNumber ) {
385
         $dbSerial = getOneDBValue( "select serial from device where device_id = $this->machineID" );
386
         if ( ! isset ( $dbSerial ) or $dbSerial != $this->serialNumber ) {
387
            $this->messages[] = "Updating serial number to $this->serialNumber";
388
            queryDatabaseExtended( "update device set serial = '$this->serialNumber' where device_id = $this->machineID" );
389
         }
390
      }
3 rodolico 391
   } // updateComputerMakeup
392
 
110 rodolico 393
   /* several report entries are simply designed to add/update the attributes table (attrib_device), so we'll just go through them
394
    * one by one
395
    */
396
   private function processAttributes() {
397
      foreach ( $this->report['attributes'] as $key => $value ) {
398
         $this->checkAndUpdateAttribute( $key, $value );
399
      }
400
   } // processAttributes
36 rodolico 401
 
402
 
3 rodolico 403
   // This is kind of funky, because column = null does not work, it must be column is null, so we have to look for it.
404
   function makeSQLEquals ( $field, $value ) {
405
      return "$field " . ( $value == 'null' ? 'is null' : '=' . $value );
406
   }
407
 
408
 
409
   private function updateOS () {
410
      // find os
411
      $osName =         makeSafeSQLValue( isset($this->report['operatingsystem']['os_name'])         ? $this->report['operatingsystem']['os_name'] : '');
412
      $kernel =         makeSafeSQLValue( isset($this->report['operatingsystem']['kernel'])          ? $this->report['operatingsystem']['kernel'] : '');
413
      $distro_name =    makeSafeSQLValue( isset($this->report['operatingsystem']['os_distribution']) ? $this->report['operatingsystem']['distribution'] : '');
414
      $release =        makeSafeSQLValue( isset($this->report['operatingsystem']['os_release'])      ? $this->report['operatingsystem']['release'] : '');
415
      $version =        makeSafeSQLValue( isset($this->report['operatingsystem']['os_version'])      ? $this->report['operatingsystem']['os_version'] : '');
416
      $description =    makeSafeSQLValue( isset($this->report['operatingsystem']['description'])     ? $this->report['operatingsystem']['description'] : '');
417
      $codename =       makeSafeSQLValue( isset($this->report['operatingsystem']['codename'])        ? $this->report['operatingsystem']['codename'] : '');
418
 
419
      $osID = getOneDBValue( "select operating_system_id from operating_system
420
               where " . $this->makeSQLEquals( 'name', $osName ) . "
421
                  and " . $this->makeSQLEquals( 'kernel', $kernel ) . "
422
                  and " . $this->makeSQLEquals( 'distro', $distro_name ) . "
423
                  and " . $this->makeSQLEquals( 'distro_release', $release ) );
424
      if ( !isset( $osID ) ) {
425
         $this->messages[] = "Creating a new OS with name = $osName, kernel = $kernel, distro = $distro_name,  distro_release = $release";
426
         $result = queryDatabaseExtended( "insert into operating_system (name,version,kernel,distro,distro_description,distro_release,distro_codename, added_date) values
427
                                          ($osName,$version,$kernel,$distro_name,$description,$release,$codename, '$this->reportDateSQL')" );
428
         $osID = $result['insert_id'];
429
      }
430
 
431
      # verify the operating system
432
      $registeredOS = getOneDBValue( "select operating_system_id from device_operating_system where device_id = $this->machineID and removed_date is null" );
433
      if ( ! $registeredOS || $registeredOS != $osID ) {
434
         if ( $registeredOS ) { #we have the same computer, but a new OS???
435
            queryDatabaseExtended( "update device_operating_system set removed_date = '$this->reportDateSQL' where device_id = $this->machineID and removed_date is null");
436
            $this->messages[] = "Computer $this->machineName has a new OS";
437
         }
438
         queryDatabaseExtended( "insert into device_operating_system( device_id,operating_system_id,added_date) values ($this->machineID,$osID,'$this->reportDateSQL')");
439
      }
440
   }
441
 
6 rodolico 442
   /* every time we get a report, we need to see if the computer was rebooted
443
    * There is some slop in the last reboot date, so we assume if it is less than
444
    * $fuzzyRebootTimes, it was not rebooted. $fuzzyRebootTimes is calculated in
445
    * seconds. Since most of our machines take a minimum of 5 minutes to reboot
446
    * we'll go with that (300), though it would be just as good to go with a day
447
    * (86400) since we only run this report daily
448
    * When one is found, we remove the old report and add a new one.
449
    */
3 rodolico 450
   private function updateBootTime() {
6 rodolico 451
      $fuzzyRebootTimes = 300; // this allows slop of this many seconds before we assume a machine has been rebooted, ie
3 rodolico 452
      $lastReboot;
453
      if ( isset( $this->report['system']['last_boot'] ) ) {
454
         $lastReboot = strftime( '%F %T',$this->report['system']['last_boot'] ); // convert unix timestamp to sql value
455
         if ( isset( $lastReboot ) ) {
6 rodolico 456
            //if ( ! getOneDBValue( "select computer_uptime_id from computer_uptime where device_id = $this->machineID and last_reboot = '$lastReboot'" ) ) {
457
            if ( ! getOneDBValue( "select computer_uptime_id from computer_uptime where abs(TIME_TO_SEC(timediff( last_reboot, '$lastReboot' ))) < 3600 and device_id = $this->machineID" ) ) {
3 rodolico 458
               $this->messages[] = "Computer was rebooted at $lastReboot";
459
               queryDatabaseExtended( "update computer_uptime set removed_date = '$this->reportDateSQL' where device_id = $this->machineID and removed_date is null" );
460
               queryDatabaseExtended( "insert into computer_uptime (device_id,added_date,last_reboot) values ($this->machineID,'$this->reportDateSQL','$lastReboot')");
461
            }
462
         } else {
463
            $this->messages[] = 'Invalid reboot time [' . $this->Report['system']['last_boot'] . ']';
464
         }
465
      } else {
466
         $this->messages[] = 'No Boot time given';
467
      }
468
   }
469
 
470
 
471
   // checks if the report and the database values are the same
472
   private function checkInterfaceEquals( $report, $database ) {
473
      //var_dump( $report );
474
      //var_dump( $database );
475
      return ( 
476
         $report['address'] == $database['address'] and
477
         $report['netmask'] == $database['netmask'] and
478
         $report['ip6address'] == $database['ip6'] and
479
         $report['ip6networkbits'] == $database['ip6net'] and
480
         $report['mtu'] == $database['mtu'] and
481
         $report['mac'] == $database['mac'] 
482
         );
483
   } // checkInterfaceEquals
484
 
485
 
486
   /*
487
    * routine will check for all IP addresses reported and check against those recorded in the
488
    * database. It will remove any no longer in the database, and add any new ones
489
    */
490
   private function doIPAddresses () {
491
      /*
492
       * get all interfaces from the database
493
       * remove any interfaces from database which are not in report
494
       * add any interfaces from report which are not in database or which have changed
495
       */
496
      $network = $this->report['network']; // make a copy so we don't modify the original report
497
      // we won't process lo
498
      unset ($network['lo']);
499
      // at this point, we could get rid of tun's also, if we wanted to
500
 
501
      // let's ensure that all rows in report have the minimum required data
502
      foreach ( array_keys( $network ) as $interface ) {
503
         foreach ( array( 'address','netmask','ip6address','ip6networkbits','mtu','mac' ) as $required ) {
504
            if ( ! isset( $network[$interface][$required] ) ) { 
505
               $network[$interface][$required]  = NULL; 
506
            }
507
         } // checking all keys
508
      } // checking all report rows
509
      // var_dump( $network );   
510
 
511
      // get current information on all active interfaces in the database
512
      $dbInterfaces = queryDatabaseExtended( "select network_id,interface,address,netmask,ip6,ip6net,mac,mtu from network where device_id = $this->machineID and removed_date is null" );
513
      //print "select network_id,interface,address,netmask,ip6,ip6net,mac,mtu from network where device_id = $this->machineID and removed_date is null\n";
514
      //var_dump($dbInterfaces);
515
 
516
      // now, get rid of any interfaces which are no longer in the database
517
      // get a list of interfaces being passed in by report as a comma delimited list for processing.
518
      foreach ( array_keys( $network) as $temp ) {
519
         $interfaces[] = "'$temp'";
520
      }
521
      $interfaces = implode( ',', $interfaces );
522
      if ( $interfaces ) { // are there any? Then simply remove anything not in their set.
523
         queryDatabaseExtended( "update network set removed_date = '$this->reportDateSQL' where device_id = $this->machineID and removed_date is null and interface not in ($interfaces)");
524
         // print "update network set removed_date = '$this->reportDateSQL' where device_id = $this->machineID and removed_date is null and interface not in ($interfaces)\n";
525
         // reload the database results to exclude the ones we removed above
526
         $dbInterfaces = queryDatabaseExtended( "select network_id,interface,address,netmask,ip6,ip6net,mac,mtu from network where device_id = $this->machineID and removed_date is null" );
527
         // print "select network_id,interface,address,netmask,ip6,ip6net,mac,mtu from network where device_id = $this->machineID and removed_date is null\n";
528
      } // if
529
      // at this point, the database is a subset of the report, ie there are no entries in the database which are not also in the report.
530
      // let's remove the ones which are the same from both sets.
531
      // we can take a shortcut since the database is a subset of the report, ie we can remove entries from the report as we process
532
      if ( $dbInterfaces ) { // do we have anything in the database?
533
         foreach ( $dbInterfaces['data'] as $row => $value ) {
534
            // print "checking if " . $network[$value['interface']] . " equals $value\n";
535
            if ( $this->checkInterfaceEquals( $network[$value['interface']], $value ) ) { // compare the two entries
536
               // print "\tIt Does, so deleting it from updates\n";
537
               unset( $network[$value['interface']] );
538
            } else { // they are not equal. We will simply void out the existing one and it will treat the report entry as a new entry.
539
               // print "\tit is an update, so setting to removed in database\n";
540
               queryDatabaseExtended( "update network set removed_date = '$this->reportDateSQL' where device_id = $this->machineID and removed_date is null and interface = '" . $value['interface'] . "'");
541
               // print "update network set removed_date = '$this->reportDateSQL' where device_id = $this->machineID and removed_date is null and interface = '" . $value['interface'] . "'\n";
542
            } // if..else
543
         } // foreach
544
      }
545
      // finally, we should have only new entries, or entries which have changed and have been voided
546
      // a new row should be created for anything left.
547
      foreach ( $network as $interface => $values ) {
548
         //var_dump( $values );
549
         $sql = implode ( ',', array( 
550
                              $this->machineID,
551
                              makeSafeSQLValue($this->reportDateSQL),
552
                              makeSafeSQLValue($interface),
553
                              makeSafeSQLValue($values['address']),
554
                              makeSafeSQLValue($values['netmask']),
555
                              makeSafeSQLValue($values['ip6address']),
556
                              makeSafeSQLValue($values['ip6networkbits']),
557
                              makeSafeSQLValue($values['mtu']),
558
                              makeSafeSQLValue($values['mac'])
559
                              )
560
                         );
561
         $sql = "insert into network (device_id,added_date,interface,address,netmask,ip6,ip6net,mtu,mac) values (" . $sql . ")";
562
         //$this->messages[] = $sql;
563
         queryDatabaseExtended( $sql );
564
         $this->messages[] = "Network Device $interface was added/modified";
565
      }
566
   } // doIPAddresses
567
 
568
 
569
   /*
570
    * function looks through every element of $report, for a subkey
571
    * named 'keyfield'. It turns those into a string appropriate for
572
    * inclusion in a query as 
573
    * where fieldname in ( $keylist )
574
    * For example, with values a, b, c, 54, q'
575
    * returns 'a','b','c','54','q\''
576
    * where $keylist is generated by this function
577
    * returns null if no values found
578
    */ 
579
   function getKeyList ( $report ) {
580
      $keys = array();
581
      $keyset = null;
582
      // every row should have a key 'keyfield'.Since we don't know 
583
      // what they are, be sure to make sure they are SQL Safe
584
      foreach ( $report as $key => $value ) {
585
         $keys[] = makeSafeSQLValue( $value['keyfield'] );
586
      }
587
      return $keys ? implode( ",\n", $keys ) : null;
588
   }
589
 
590
   /*
591
    * function checks all keys in $first and $second to see if they
592
    * match. If they do, returns true, else returns false
593
    * ignores any keys passed in as array $ignore
594
    */
595
   function rowMatches ( $first, $second, $ignore = array() ) {
596
      foreach ( $first as $fkey => $fValue ) {
597
         if ( in_array( $fkey, $ignore ) )
598
            continue;
599
         if ( $fValue == $second[$fkey] )
600
            continue;
601
         return false;
602
      }
603
      return true;
604
   } // rowMatches
605
 
606
   /* Generic function that will 
607
    * delete items in database not found in report
608
    * Clean up report to contain only those items which are to be added
609
    * Return a copy of the report.
610
    * 
611
    * NOTE: since we are deleting items not found in the report, we will then
612
    * subsequently add them. This is the update process since we never actually
613
    * "delete" anything from the database.
614
    * 
615
    * Requires the report, and report must have a key for each entry named
616
    * 'keyfield' which the query below may use (as special tag <keys>
617
    * Also uses the following queries. If a query does not exist, that
618
    * process is ignored
619
    * 'remove old' - should have an entry as <device_id> which will have the current
620
    *       machine ID placed in it
621
    * 'delete' - <ids> which will be replaced with a list of keys from the 
622
    *         report.
623
    * 'find' - should return a column named 'id' which is a primary key on the
624
    *          table (used by delete query), and a column named 'keyfield'
625
    *          which matches the same one in the report. Any other fields in
626
    *          the query are checked against the report, and the report and
627
    *          database entry are only considered matching if all query fields
628
    *          match a report entry.
629
    * 
630
    * Before passing in report, you should go through the entries and create
631
    * a new one called 'keyfield' for each entry which will be a unique key
632
    * for an entry. For example, software packages may be the package name
633
    * and the version.
634
    */
635
   private function AddDeleteUpdate ( $report, $queries, $reportLabel, $testing=false ) {
636
 
637
      if ( $testing ) {
638
         print "*****************Starting\n"; 
639
         print_r( $report ); 
640
         print "*****************Starting\n";
641
      }
642
      $database = null;
643
      $keySet = $this->getKeyList( $report );
644
      if ( is_null( $keySet ) ) // bail if we don't have any data
645
         return null;
646
 
647
      // find values which are in database but not in report and delete them from database
648
      if ( $queries['remove old'] ) {
649
         $queries['remove old'] = str_replace( '<keys>', $keySet, $queries['remove old'] );
650
         $queries['remove old'] = str_replace( '<device_id>', $this->machineID, $queries['remove old'] );
651
         if ( $testing )
652
            print "Remove Query***************************\n" . $queries['remove old'] . "\n***********************************\n";
653
         else {
654
            $database = queryDatabaseExtended( $queries['remove old'] );
655
            if ( $database and $database['affected_rows'] ) 
656
               $this->messages[] = $database['affected_rows'] . " $reportLabel removed from database because they were removed for superceded";
657
         } // else
658
      } // if
659
 
660
      // now, let's get all the values left in the database
661
      if ( $queries['find'] ) {
662
         $queries['find'] = str_replace( '<keys>', $keySet, $queries['find'] );
663
         $queries['find'] = str_replace( '<device_id>', $this->machineID, $queries['find'] );
664
         if ( $testing )
665
            print "Find Query***************************\n" . $queries['find'] . "\n***********************************\n";
666
         else
667
            $database = queryDatabaseExtended( $queries['find'] );
668
      }
669
      if ( $database ) { // there were entries in the database still
670
         $database = $database['data']; // get to the data
671
         $keysToDelete = array();
672
         // find values which are different in report, and delete the database entry
673
         foreach ( $database as $row => $entries ) {
674
            foreach ( $report as $key => $values ) {
675
               if ( $report[$key]['keyfield'] != $entries['keyfield'] )
676
                  continue;
677
               // if we made it here, our keyfields match, so we check
678
               // the contents of the other fields
679
               if ( $this->rowMatches( $entries, $values, array( 'keyfield', 'id' ) ) ) {
680
                  // they match, so remove it from the report
681
                  unset( $report[$key] );
682
               } else { // they are different in the report, so remove from database
683
                  $keysToDelete[] = $entries['id'];
684
               }
685
            } // inner foreach
686
         } // outer foreach
687
         if ( $keysToDelete and $queries['delete'] ) {
688
            $queries['delete'] = str_replace( '<ids>', implode( ',', $keysToDelete ), $queries['delete'] );
689
            if ( $testing )
690
               print "Delete Query***************************\n" . $queries['delete'] . "\n***********************************\n";
691
            else {
692
               $updates = queryDatabaseExtended( $queries['delete'] );
693
               $this->messages[] = $updates['affected_rows'] . " $reportLabel modified";
694
            }
695
         }
696
      } // if $database
697
      // return items to be added
698
      if ( $testing ) {
699
         print "*****************Ending\n"; 
700
         print_r( $report ); 
701
         print "*****************Ending\n";
702
      }
703
      return $report;
704
   } // function AddDeleteUpdate
705
 
706
 
707
   /*
708
    * routine to ensure the hardware returned as PCI hardware is in the attributes area
709
    */ 
710
   private function processPCI ( ) {
711
      // following are some of the synonyms we will use from the PCI
712
      // tables for the "name" of the device. They are listed in the
713
      // order of priority (ie, first one found is chosen).
714
      $nameSynomyms = array('name','device','sdevice','device0');
715
 
716
      // query which removes devices from database that are not in report
717
      // we assume if the report doesn't have it, it has been removed
718
      $queries['remove old'] = 
719
                    "update device
720
                        set removed_date = '$this->reportDateSQL'
721
                        where device_id in (
722
                           select device_id from (
723
                              select
724
                                    device_id
725
                                 from
726
                                    device join device_type using (device_type_id)
727
                                 where
728
                                    device.part_of = <device_id>
16 rodolico 729
                                    and device_type.show_as_system <> 'Y'
3 rodolico 730
                                    and device.removed_date is null
731
                                    and device.name not in ( <keys> )
732
                              ) as b
733
                              )";
734
 
735
 
736
      $queries['find'] = 
737
                    "select
738
                        device_id id,
739
                        device.name  'keyfield',
740
                        device.notes 'info'
741
                     from device join device_type using (device_type_id) 
742
                     where
743
                        device_type.name = 'PCI Card' 
744
                        and device.removed_date is null
745
                        and device.part_of = <device_id>";
746
 
747
      $queries['delete'] =
748
                    "update device
749
                        set removed_date = '$this->reportDateSQL'
750
                        where device_id in ( <ids> )";
751
 
752
 
753
      if ( ! isset( $this->report['pci'] ) ) return;
754
      // make a local copy so we can modify as needed
755
      $pciHash = $this->report['pci'];
756
      # normalize the data
757
      foreach ( array_keys( $pciHash ) as $key ) {
758
         if ( ! is_array( $pciHash[$key] ) ) {
759
            $this->messages[] = 'Invalid PCI format, not recording';
760
            return false;
761
         }
762
         if ( ! isset( $pciHash[$key]['slot'] ) ) { // doesn't have a slot field
763
            $slotField = '';
764
            foreach ( array_keys( $pciHash[$key] ) as $subkey ) { // scan through all keys and see if there is something with a "slot looking" value in it
765
               if ( preg_match ( '/^[0-9a-f:.]+$/' , $pciHash[$key][$subkey] ) )
766
                  $slotField = $subkey;
767
            }
768
            if ( $slotField ) {
769
               $pciHash[$key]['slot'] = $pciHash[$key][$slotField];
770
            } else {
771
               $pciHash[$key]['slot'] = 'Unknown';
772
            }
773
         }
774
         // Each entry must have a name. Use 'device' if it doesn't exist
775
         // Basically, we try each option in turn, with the first test having
776
         // priority.
777
 
778
         if ( ! isset( $pciHash[$key]['name'] ) ) {
779
            // if there is no name, try to use one of the synonyms
780
            foreach ( $nameSynomyms as $testName ) {
781
               if ( isset ( $pciHash[$key][$testName] ) ) {
782
                  $pciHash[$key]['name'] = $pciHash[$key][$testName];
783
                  break;
784
               } // if
785
            } // foreach
786
            if ( empty( $pciHash[$key]['name'] ) ) {
787
               $this->messages[] = "No name given for one or more PCI devices at normalize, Computer ID: [$this->machineID], Report Date: [$this->reportDate]";
788
               //print_r( $pciHash ); 
789
               //die;
790
               return;
791
            }
792
         } elseif ( $pciHash[$key]['name'] == $pciHash[$key]['slot'] ) {
793
            $pciHash[$key]['name'] = $pciHash[$key]['device'];
794
         } // if..else
795
         // Following is what will actually be put in the device table, ie device.name
796
         $pciHash[$key]['keyfield'] = $pciHash[$key]['slot'] . ' - ' . $pciHash[$key]['name'];
797
         ksort( $pciHash[$key] );
798
         $ignore = array( 'keyfield' );
799
         $info = '';
800
         foreach ( $pciHash[$key] as $subkey => $value ) {
801
            if ( in_array( $subkey, $ignore ) )
802
               continue;
803
            $info .= "[$subkey]=$value\n";
804
         }
805
         $pciHash[$key]['info'] = $info;
806
      } // foreach
807
      // at this point, we should have a slot and a name field in all pci devices
808
      $toAdd = $this->AddDeleteUpdate( $pciHash, $queries, 'PCI Devices' );
809
      if ( $toAdd ) {
810
         $pciCardID = getOneDBValue( "select device_type_id from device_type where name = 'PCI Card'");
811
         $count = 0;
812
         foreach ( $toAdd as $entry => $values ) {
813
            $keyfield = makeSafeSQLValue( $values['keyfield'] );
814
            $info = makeSafeSQLValue( $values['info'] );
815
            $query = "insert into device 
816
                        select 
817
                           null,
818
                           site_id,
819
                           $pciCardID,
820
                           $keyfield,
821
                           $info,
822
                           device_id,
823
                           '$this->reportDateSQL',
824
                           null,
825
                           null 
826
                        from device 
827
                        where device_id = $this->machineID";
828
            queryDatabaseExtended( $query );
829
            $count++;
830
         } // foreach
831
         $this->messages[] = "$count PCI devices added/modified";
832
      } // if
833
   } // processPCI
834
 
835
   function getSoftwareID ( $packageName,$versionInfo,$description ) {
836
      $packageName = makeSafeSQLValue( $packageName );
837
      $versionInfo = makeSafeSQLValue( $versionInfo );
838
      $description = makeSafeSQLValue( $description );
839
      // does the package exist?
840
      $packageID = getOneDBValue("select software_id from software where package_name = $packageName and removed_date is null");
841
      if ( !$packageID ) { # NO, package doesn't exist, so add it to the database
842
         $packageID = queryDatabaseExtended( "insert into software (package_name,description, added_date) values ($packageName,$description, '$this->reportDateSQL')");
843
         if ( $packageID['insert_id'] )
844
            $packageID = $packageID['insert_id'];
845
         else {
846
            $this->messages[] = "ERROR: Software Packages - Could not find version $packageName and could not add to database";
847
            $packageID = null;
848
         }
849
      }
850
      // does this version number exist?
851
      $versionID = getOneDBValue( "select software_version_id from software_version where version = $versionInfo and removed_date is null" );
852
      if ( ! $versionID ) { # nope, so add it
853
         $versionID = queryDatabaseExtended( "insert into software_version ( version,added_date ) values ($versionInfo,'$this->reportDateSQL')");
854
         if ( $versionID['insert_id'] )
855
            $versionID = $versionID['insert_id'];
856
         else {
857
            $this->messages[] = "ERROR: Software Packages - Could not find version $versionInfo and could not add to database";
858
            $versionID = null;
859
         }
860
      }
861
      return array( 'package id' => $packageID,'version id' => $versionID);
862
   } // getSoftwareID
863
 
864
 
865
   function processSoftwarePackages (  ) {
866
      // query which removes devices from database that are not in report
867
      // we assume if the report doesn't have it, it has been removed
868
      $queries['remove old'] = 
869
                    "update installed_packages
870
                        set removed_date = '$this->reportDateSQL'
871
                        where installed_packages_id in (
872
                           select installed_packages_id from (
873
                              select
874
                                    installed_packages_id
875
                              from 
876
                                 installed_packages 
877
                                 join software using ( software_id )
878
                                 join software_version using (software_version_id)
879
                              where
880
                                    installed_packages.device_id = <device_id>
881
                                    and installed_packages.removed_date is null
882
                                    and concat(software.package_name,software_version.version) not in ( <keys> )
883
                              ) as b
884
                              )";
885
      $queries['find'] = 
886
           "select 
887
               installed_packages.installed_packages_id 'id',
888
               concat(software.package_name,software_version.version) 'keyfield'
889
            from 
890
               installed_packages 
891
               join software using ( software_id )
892
               join software_version using (software_version_id)
893
            where 
894
               device_id = <device_id>
895
               and installed_packages.removed_date is null";
896
      $queries['delete'] =
897
                    "update installed_packages
898
                        set removed_date = '$this->reportDateSQL'
899
                        where installed_packages_id in ( <ids> )";
900
 
901
      if ( ! isset( $this->report['software'] ) ) return;
902
      // make a local copy so we can modify as needed
903
      $softwareHash = $this->report['software'];
904
      foreach ( array_keys( $softwareHash ) as $key ) {
905
         if ( ! isset( $softwareHash[$key]['name'] ) )
906
            $softwareHash[$key]['name'] = $key;
907
         if ( ! isset( $softwareHash[$key]['description'] ) )
908
            $softwareHash[$key]['description'] = '';
14 rodolico 909
         if ( isset( $softwareHash[$key]['release'] ) ) // some of them have a release number, so we just add that to the version
910
            $softwareHash[$key]['version'] = $softwareHash[$key]['version'] . '-r' . $softwareHash[$key]['release'];
3 rodolico 911
         // This is needed for matching
912
         $softwareHash[$key]['keyfield'] = $softwareHash[$key]['name'] . $softwareHash[$key]['version'];
913
      } // foreach
914
      // at this point, we should have a slot and a name field in all pci devices
915
      $toAdd = $this->AddDeleteUpdate( $softwareHash, $queries, 'Software Packages' );
916
      if ( $toAdd ) {
917
         $count = 0;
918
         foreach ( $toAdd as $key => $value ) {
919
            $ids = $this->getSoftwareID ( $value['name'],$value['version'],$value['description'] );
920
            if ( is_null( $ids['package id'] ) or is_null( $ids['version id'] ) ) {
921
               $this->messages[] = "Could not create entry for package " . $value['name'] . ", version " . $value['version'];
922
            } else {
923
               $insertValues = implode( ',', array( $this->machineID, $ids['package id'], $ids['version id'], "'$this->reportDateSQL'" ) );
924
               $query = "insert into installed_packages
925
                           (device_id,software_id,software_version_id,added_date) 
926
                           values 
927
                           ( $insertValues )";
928
               $database = queryDatabaseExtended( $query );
929
               if ( $database )
930
                  $count++;
931
            } // if..else
932
         } // foreach
933
         if ( $count )
934
            $this->messages[] = "$count software packages added/updated";
935
      }
936
   } // processSoftwarePackages
36 rodolico 937
 
938
 
939
   function processXenVirtual ( $name, $values ) {
940
      // Determine if the virtual is on a new machine or not
941
      $virtualID = getMachineID( $name ); // see if we can find the virtual
3 rodolico 942
 
36 rodolico 943
      if ( $virtualID ) {
944
         if ( $this->machineID == getOneDBValue( "select part_of from device where device.device_id = $virtualID" ) ) {
945
            return;
946
         }
947
      } else { // we could not find the device
948
         $this->messages[] = "Could not locate id for virtual $name which is running on $this->machineName";
949
         return;
950
      }
951
      // we made it this far, which means we found the virtual's ID, but it does not have
952
      // this machine as its parent, so we need to update it.
953
      queryDatabaseExtended( "update device set part_of = $this->machineID where device_id = $virtualID" );
954
      $this->messages[] = "Virtual $name ($virtualID) now running on $this->machineName";
955
   } // processXenVirtual
956
 
957
   function processXen ( ) {
958
      // Is this a Xen DOM0?
959
      if ( ! isset( $this->report['xen'] ) ) return;
960
      $machineID;  // id in database
961
      $machineName;// name in report
962
 
963
      foreach ( $this->report['xen']['virtual'] as $virtual => $data ) {
964
         $this->processXenVirtual( $virtual, $data );
965
      }
966
   } // processXen
967
 
3 rodolico 968
} // class sysinfo
969
 
970
 
971
 
5 rodolico 972
/*
973
 * we don't know where the configuration file will be, so we have a list
974
 * below (searchPaths) to look for it. It will load the first one it 
975
 * finds, then exit. THUS, you could have multiple configuration files
976
 * but only the first one in the list will be used
977
 */
3 rodolico 978
 
5 rodolico 979
$confFileName = "sysinfoRead.conf.yaml";
980
$searchPaths = array( '/etc/camp', '/opt/camp', '/opt/camp/sysinfo', $_SERVER['SCRIPT_FILENAME'], getcwd() );
981
$configuration = array();
982
foreach ( $searchPaths as $path ) {
983
   if ( file_exists( "$path/$confFileName" ) ) {
984
      #print "Found $path/$confFileName\n";
985
      $configuration = yaml_parse_file( "$path/$confFileName" );
986
      #require "$path/$confFileName";
987
      break; // exit out of the loop; we don't try to load it more than once
988
   } // if
989
} // foreach
3 rodolico 990
 
5 rodolico 991
mysql_connect( $configuration['database']['databaseServer'], $configuration['database']['databaseUsername'], $configuration['database']['databasePassword'] ) or die(mysql_error());
992
mysql_select_db( $configuration['database']['database'] ) or die(mysql_error());
993
 
994
 
995
$thisReport = new sysinfo( $configuration['bodyContents'] );
3 rodolico 996
$result; 
997
 
998
$result = $thisReport->loadFromFile( $argc > 1 ? $argv[1] : "php://stdin" );
999
if ( $result == 'ini' ) {
1000
   print $thisReport->dumpMessages();
1001
   exit(0);
1002
}
1003
if ( ! $result )
1004
   exit ( 1 );
1005
$result = $thisReport->processReport();
1006
print $thisReport->dumpMessages();
36 rodolico 1007
// var_dump( $thisReport );
3 rodolico 1008
exit ( $result );
1009
 
1010
?>