Пример #1
0
    def _validate_informations(self, inventory_number, request_host):
        try:
            status = 7
            _info = self._search_by_inventory_number(inventory_number)
            try:
                _glpi_info = self._search_in_glpi_by_inventory_number(
                    inventory_number)
            except:
                raise AssertionError('GLPI server is not responding')

            count = 0
            for computer in _glpi_info.values():
                if computer['computer_ipaddress'] == request_host:
                    count += 1

            sesp_id = _info[1]['computer_id']
            if count == 0:
                query = f"insert into computers_logs(`type_id`, `computer_id`, `body`) values (4, {sesp_id}, 'IP information does not match')"
                Database().commit_without_return(query)
                status = 4

            if _glpi_info[1]['computer_name'] != _info[1]['computer_name']:
                status = 1

            if _info[1]['status_id'] != status and _info[1]['status_id'] != 2:
                query = f'UPDATE `computers` SET `status_id` = {status} WHERE `inventory_number`= {inventory_number};'
                Database().commit_without_return(query)

        except Exception as e:
            raise e

        return status, _glpi_info
Пример #2
0
    def _new_computer(self, computer_name, inventory_number,
                      last_request_host):
        try:
            query = Gera_query().inserir_na_tabela('computers', [
                'computer_name', 'inventory_number', 'status_id',
                'last_request_host'
            ], [computer_name, inventory_number, 6, last_request_host])
            Database().commit_without_return(query)

            status, computer_info = self._validate_informations(
                inventory_number, last_request_host)

            glpi_id = computer_info[1]['computer_id']
            glpi_name = computer_info[1]['computer_name']

            query = f'UPDATE `computers` SET `glpi_id` = {glpi_id}, `glpi_name` = "{glpi_name}" WHERE `inventory_number`= {inventory_number};'
            Database().commit_without_return(query)

            computer_info = self._search_by_inventory_number(inventory_number)
            self._force_next_inventory(inventory_number,
                                       computer_ipaddress=last_request_host)

        except Exception as e:
            raise e

        return status, computer_info
Пример #3
0
    def _search_by_inventory_number(self, inventory_number):
        try:
            header = [
                'computer_id', 'computer_name', 'status_id',
                'next_fusion_inventory', 'next_reboot', 'next_shutdown',
                'wallpaper', 'glpi_id', 'glpi_name'
            ]
            query = Gera_query().buscar_dados_da_tabela(
                'computers',
                where=True,
                coluna_verificacao='inventory_number',
                valor_where=inventory_number,
                returns=header)
            results = Database().commit_with_return(query)

            return_results = {}

            if len(results) == 0:
                raise NoResultsFoundError
            else:
                c = 0
                for result in results:
                    info_results = {}
                    x = 0
                    for dado in result:
                        info_results[header[x]] = dado
                        x += 1
                    c += 1
                    return_results[c] = info_results
        except Exception as e:
            raise e

        return return_results
Пример #4
0
    def _update_status_of_computer(self, inventory_number, status):
        try:
            query = f'UPDATE `computers` SET `status_id` = {status} WHERE `inventory_number`= {inventory_number};'
            Database().commit_without_return(query)
        except Exception as e:
            raise e

        return True
Пример #5
0
    def _update_computer(self, request_header, computer_host):
        try:
            inventory_number = request_header['Inventory_Number']
            computer_name = request_header['Computer_Name']
            last_request_host = computer_host

            try:
                computer = self._search_by_inventory_number(inventory_number)
                query = f'UPDATE `computers` SET `last_request_host` = "{computer_host}" WHERE `inventory_number`= {inventory_number};'
                Database().commit_without_return(query)

                if computer[1]['computer_name'] != computer_name:
                    query = f'UPDATE `computers` SET `computer_name` = "{computer_name}" WHERE `inventory_number`= {inventory_number};'
                    Database().commit_without_return(query)

                status, computer_info = self._validate_informations(
                    inventory_number, computer_host)

                glpi_id = computer_info[1]['computer_id']
                glpi_name = computer_info[1]['computer_name']

                query = f'UPDATE `computers` SET `glpi_id` = {glpi_id}, `glpi_name` = "{glpi_name}" WHERE `inventory_number`= {inventory_number};'
                Database().commit_without_return(query)

                if status != 1 and status != 2 and status != 6:
                    if computer[1]['next_fusion_inventory'] is None:
                        try:
                            self._force_next_inventory(
                                inventory_number,
                                computer_ipaddress=last_request_host)
                        except:
                            query = f'UPDATE `computers` SET `status_id` = 2 WHERE `inventory_number`= {inventory_number};'
                            Database().commit_without_return(query)

            except NoResultsFoundError:
                status, computer = self._new_computer(computer_name,
                                                      inventory_number,
                                                      last_request_host)

        except Exception as e:
            raise e

        return status, computer
Пример #6
0
    def _force_next_inventory(self,
                              inventory_number,
                              now=True,
                              computer_ipaddress=False):
        try:
            computer = self._search_in_glpi_by_inventory_number(
                str(inventory_number))
            sesp_id = self._search_by_inventory_number(
                str(inventory_number))[1]['computer_id']

            if now:
                if not computer_ipaddress:
                    for _computer in computer.values():
                        try:
                            ip = _computer['computer_ipaddress'].split('.')
                            if ip[2] == '0' or ip[2] == '1':
                                computer_ipaddress = _computer[
                                    'computer_ipaddress']
                                break
                        except:
                            pass
                try:
                    request = f'http://{computer_ipaddress}:62354/now'
                    response = requests.get(request)
                except:
                    query = f'UPDATE `computers` SET `status_id` = 3 WHERE `inventory_number`= {inventory_number};'
                    Database().commit_without_return(query)
                    raise
                else:
                    fusion_frequency = conf.fusion_inventory(
                    )['inventory_frequency']
                    next_fusion_inventory = datetime.now() + timedelta(
                        days=fusion_frequency)
                    next_fusion_inventory = datetime.strftime(
                        next_fusion_inventory, '%Y-%m-%d %H:%M')

                    query = f'UPDATE `computers` SET `next_fusion_inventory` = "{next_fusion_inventory}" WHERE `inventory_number`= {inventory_number};'
                    Database().commit_without_return(query)
                    query = f"insert into computers_logs(`type_id`, `computer_id`, `body`) values (8, {sesp_id}, 'New inventory has completed succesfully')"
                    Database().commit_without_return(query)

        except Exception as e:
            raise e
Пример #7
0
    def _schedule_next_inventory(self, inventory_number):
        try:
            computer = self._search_in_glpi_by_inventory_number(
                str(inventory_number))
            sesp_id = self._search_by_inventory_number(
                str(inventory_number))[1]['computer_id']

            fusion_frequency = conf.fusion_inventory()['inventory_frequency']
            next_fusion_inventory = datetime.now() + timedelta(
                days=fusion_frequency)
            next_fusion_inventory = datetime.strftime(next_fusion_inventory,
                                                      '%Y-%m-%d %H:%M')

            query = f'UPDATE `computers` SET `next_fusion_inventory` = "{next_fusion_inventory}" WHERE `inventory_number`= {inventory_number};'
            Database().commit_without_return(query)

            query = f"insert into computers_logs(`type_id`, `computer_id`, `body`) values (8, {sesp_id}, 'New inventory has completed succesfully')"
            Database().commit_without_return(query)
        except:
            raise
Пример #8
0
    def log_register(self):
        try:
            with open('resources/models/commons/errors/log.log', 'a') as log:
                log.write(
                    f'Route: {self.route}\t\tMethod:{self.method}\t\tApplicant:{self.applicant}\t\tError:{self.error}\t\tWhen:{str(datetime.now())}\n'
                )

            query = Gera_query().inserir_na_tabela(
                'api_logs',
                ['type_id', 'route', 'method', 'applicant', 'body'], [
                    '1', f'"{self.route}"', f'"{self.method}"',
                    f'"{self.applicant}"', self.error
                ])

            Database().commit_without_return(query)

        except Exception as e:
            with open('resources/models/commons/errors/log.log', 'a') as log:
                log.write(
                    f'Route: LogRegister\t\t\t\t\tMethod:\t\t\tApplicant:{self.applicant}\t\tError:{e}\t\tWhen:{str(datetime.now())}\n'
                )
            raise e

        raise self.error
Пример #9
0
 def __init__(self):
     self.database = Database()
     self.glpi = Glpi()