Пример #1
0
    async def async_work(self, **kwargs):
        command = kwargs['command']
        args = kwargs.get('args', [])
        # print('LocalCommandWork', command, args)
        process = await asyncio.create_subprocess_exec(
            command,
            *args,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            cwd=kwargs.get('cwd'),
        )

        await process.wait()

        if process.returncode == 0:
            level = const.LEVEL_OK
        else:
            level = const.LEVEL_FAIL

        stdout = await process.stdout.read()

        if kwargs.get('is_adapted') and level == const.LEVEL_OK:
            parsed_stdout = json.loads(stdout.decode('utf-8'),
                                       object_hook=json_util.object_hook)
            check_result = TaskResult(**parsed_stdout)

        else:
            check_result = TaskResult(
                level=level,
                extra={'stdout': stdout.decode('utf-8')},
            )

        return check_result
Пример #2
0
    async def async_check(self, host: str, **kwargs):
        protocol = kwargs.get('protocol', 'http')
        port = kwargs.get('port', '80')
        path = kwargs.get('path', '/')
        url = '%s://%s:%s%s' % (protocol, host, port, path)
        # print('Start http', host, url)

        extra = {'url': url}
        level = const.LEVEL_OK
        async with aiohttp.get(url) as response:
            extra['is_http_status_ok'] = response.status == kwargs.get('http_status', 200)
            if not extra['is_http_status_ok']:
                extra['http_status'] = response.status
                level = const.LEVEL_FAIL

            if kwargs.get('contains'):
                content = await response.read()
                content = content.decode('utf-8')
                extra['is_contains_ok'] = kwargs['contains'] in content
                if not extra['is_contains_ok']:
                    extra['content'] = content
                    level = const.LEVEL_FAIL

        check_result = TaskResult(
            level=level,
            extra=extra,
        )

        return check_result
Пример #3
0
    async def async_check(self, host: str, **kwargs):
        # print('Start ping', host)
        process = await asyncio.create_subprocess_exec(
            'ping', host, '-c1',
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
        )

        await process.wait()

        if process.returncode == 0:
            level = const.LEVEL_OK
        else:
            level = const.LEVEL_FAIL

        stdout = await process.stdout.read()
        check_result = TaskResult(
            level=level,
            extra={'stdout': stdout.decode('utf-8')},
        )

        # import random
        # await asyncio.sleep(random.randint(5, 10))
        # print('End ping', host)
        return check_result
Пример #4
0
    def process_contacted(self, result, kwargs):
        warning_percent = int(kwargs.get('warning', 50))
        fail_percent = int(kwargs.get('fail', 90))

        contacted = self._get_contacted(result)
        host_data = self.get_host_data(contacted)

        total = host_data['swap']['total']
        free = host_data['swap']['free']

        extra = {
            'total': total,
            'free': free,
        }

        level = const.LEVEL_OK
        if total:
            free_percent = int((float(free) / float(total)) * 100)
            used_percent = 100 - free_percent
            extra['used_percent'] = used_percent

            if used_percent >= fail_percent:
                level = const.LEVEL_FAIL
            elif used_percent >= warning_percent:
                level = const.LEVEL_WARNING

        return TaskResult(
            level=level,
            extra=extra,
        )
Пример #5
0
 def process_contacted(self, result, kwargs):
     level = const.LEVEL_OK
     check_result = TaskResult(
         level=level,
         extra=self._correct_result(result),
     )
     return check_result
Пример #6
0
 def process_contacted(self, result, kwargs):
     if kwargs.get('is_adapted'):
         contacted = self._get_contacted(result)
         stdout = contacted['stdout']
         parsed_stdout = json.loads(stdout.decode('utf-8'), object_hook=json_util.object_hook)
         return TaskResult(**parsed_stdout)
     else:
         return super(RemoteCommandWork, self).process_contacted(result, kwargs)
Пример #7
0
 def process_contacted(self, result, kwargs):
     """
     method for override process data logic
     """
     contacted = self._get_contacted(result)
     host_data = self.get_host_data(contacted)
     return TaskResult(
         level=const.LEVEL_OK,
         extra=host_data,
     )
Пример #8
0
    def check(self, host, **kwargs):
        remote_user, remote_pass = self._get_user_pass(kwargs)
        inventory = self._create_inventory(host)

        result = self.run(host, remote_user, remote_pass, inventory, kwargs)

        if result['dark'].get(host) and result['dark'][host].get('failed'):
            return TaskResult(
                level=const.LEVEL_FAIL,
                extra=self._correct_result(result),
            )

        return self.process_contacted(result, kwargs)
Пример #9
0
    def process_contacted(self, result, kwargs):
        partitions = kwargs.get('partitions')
        warning_percent = int(kwargs.get('warning', 80))
        fail_percent = int(kwargs.get('fail', 95))

        contacted = self._get_contacted(result)
        host_data = self.get_host_data(contacted)

        extra = self._analyze_spaces(host_data['mounts'], partitions,
                                     warning_percent, fail_percent)

        if extra.get('fails'):
            level = const.LEVEL_FAIL
        elif extra.get('warnings'):
            level = const.LEVEL_WARNING
        else:
            level = const.LEVEL_OK

        return TaskResult(
            level=level,
            extra=extra,
        )