Esempio n. 1
0
 def host_delete_event(self, msg):
     """Process delete message."""
     self.prefix = "INVENTORY DELETE EVENT"
     host_id = msg['id']
     insights_id = msg['insights_id']
     with app.app_context():
         LOG.info(
             '%s - Deleting performance profile records with insights_id %s',
             self.prefix, insights_id)
         rows_deleted = db.session.query(
             System.id).filter(System.inventory_id == host_id).delete()
         db.session.commit()
         if rows_deleted > 0:
             processor_requests_success.labels(
                 reporter=self.reporter,
                 account_number=msg['account']).inc()
             LOG.info('%s - Deleted host with inventory id: %s',
                      self.prefix, host_id)
    def process_system_details(self, msg):
        """ Store new system information (stale, stale_warning timestamp) and return internal DB id"""
        host = msg['host']
        with app.app_context():
            try:
                account = get_or_create(db.session,
                                        RhAccount,
                                        'account',
                                        account=host['account'])

                system_fields = {
                    "account_id": account.id,
                    "inventory_id": host['id'],
                    "display_name": host['display_name'],
                    "fqdn": host['fqdn'],
                    "cloud_provider": host['system_profile']['cloud_provider'],
                    "stale_timestamp": host['stale_timestamp'],
                    "operating_system":
                    host['system_profile']['operating_system'],
                }
                system = get_or_create(db.session, System, 'inventory_id',
                                       **system_fields)

                # Commit changes
                db.session.commit()
                processor_requests_success.labels(
                    reporter=self.reporter,
                    account_number=host['account']).inc()
                LOG.info(
                    "%s - Refreshed system %s (%s) belonging to account: %s (%s).",
                    self.prefix, system.inventory_id, system.id,
                    account.account, account.id)
            except Exception as err:
                processor_requests_failures.labels(
                    reporter=self.reporter,
                    account_number=host['account']).inc()
                LOG.error(
                    "%s - Unable to add host %s to DB belonging to account: %s - %s",
                    self.prefix, host['fqdn'], host['account'], err)
    def process_report(self, host, reports, utilization_info, performance_record):
        """create/update system and performance_profile based on reports data."""
        with app.app_context():
            try:
                account = get_or_create(
                    db.session, RhAccount, 'account',
                    account=host['account']
                )
                if len(reports) == 0:
                    rec_count = len(reports)
                    state_key = OPTIMIZED_SYSTEM_KEY
                    LOG.info(
                        "%s - No ROS rule hits found for system with inventory id: %s. Hence, marking the state as %s.",
                        self.prefix, host['id'], SYSTEM_STATES[state_key])
                else:
                    state_key = reports[0].get('key')
                    rec_count = 0 if state_key == 'NO_PCP_DATA' else len(reports)
                    LOG.info(
                        "%s - Marking the state of system with inventory id: %s as %s.",
                        self.prefix, host['id'], SYSTEM_STATES[state_key])

                system = get_or_create(
                    db.session, System, 'inventory_id',
                    account_id=account.id,
                    inventory_id=host['id'],
                    display_name=host['display_name'],
                    fqdn=host['fqdn'],
                    rule_hit_details=reports,
                    number_of_recommendations=-1 if state_key == 'NO_PCP_DATA' else rec_count,
                    state=SYSTEM_STATES[state_key],
                    instance_type=performance_record.get('instance_type'),
                    region=performance_record.get('region')
                )
                LOG.info(
                    f"{self.prefix} - System created/updated successfully: {host['id']}"
                )

                set_default_utilization = False
                # For Optimized state, reports would be empty, but utilization_info would be present
                if reports and reports[0].get('key') == 'NO_PCP_DATA':
                    set_default_utilization = True

                if set_default_utilization is False:
                    performance_utilization = {
                        'memory': int(utilization_info['mem_utilization']),
                        'cpu': int(utilization_info['cpu_utilization']),
                        'io': cast_iops_as_float(utilization_info['io_utilization'])
                    }
                    # max_io will be used to sort systems endpoint response instead of io
                    performance_utilization.update(
                       {'max_io': max(performance_utilization['io'].values())}
                    )
                else:
                    LOG.debug(f"{self.prefix} - Setting default utilization for performance profile")
                    performance_utilization = {
                        'memory': -1,
                        'cpu': -1,
                        'max_io': -1.0,
                        'io': {}
                    }
                # Following are saved on respective system record
                del performance_record['instance_type']
                del performance_record['region']

                get_or_create(
                    db.session, PerformanceProfile, ['system_id', 'report_date'],
                    system_id=system.id,
                    performance_record=performance_record,
                    performance_utilization=performance_utilization,
                    report_date=datetime.datetime.utcnow().date()
                )
                LOG.info(
                    f"{self.prefix} - Performance profile created/updated successfully for the system: {host['id']}"
                )

                db.session.commit()
                processor_requests_success.labels(
                    reporter=self.reporter, account_number=host['account']
                ).inc()
                LOG.info("%s - Refreshed system %s (%s) belonging to account: %s (%s).",
                         self.prefix, system.inventory_id, system.id, account.account, account.id)
            except Exception as err:
                processor_requests_failures.labels(
                    reporter=self.reporter, account_number=host['account']
                ).inc()
                LOG.error("%s - Unable to add host %s to DB belonging to account: %s - %s",
                          self.prefix, host['id'], host['account'], err)