コード例 #1
0
    def test_ps_parser(self):
        ps, _ = subp.call(PS_CMD)
        assert_that(ps, not_none())

        parsed_lines = [PS_PARSER(line) for line in ps]
        assert_that(parsed_lines, not_none())
        assert_that(parsed_lines, has_length(6))

        # just for ease of testing, let's trim the None at the end
        parsed_lines = filter(lambda item: item is not None, parsed_lines)
        assert_that(parsed_lines, has_length(5))

        master_pid, master_ppid, master_command = parsed_lines[0]
        assert_that(master_pid, not_none())
        assert_that(master_ppid, equal_to(1))
        assert_that(
            master_command,
            string_contains_in_order('php-fpm:', 'master', 'process',
                                     'php-fpm.conf'))

        for child_id, child_ppid, child_command in parsed_lines[1:]:
            assert_that(child_id, not_none())
            assert_that(child_ppid, equal_to(master_pid))
            assert_that(child_command,
                        string_contains_in_order('php-fpm:', 'pool', 'www'))
コード例 #2
0
    def test_ls_cmd(self):
        ps, _ = subp.call(PS_CMD)
        parsed_lines = [PS_PARSER(line) for line in ps]
        master_pid, master_ppid, master_command = parsed_lines[0]

        ls, _ = subp.call(LS_CMD % master_pid)
        assert_that(ls, not_none())
コード例 #3
0
    def test_ls_parser(self):
        ps, _ = subp.call(PS_CMD)
        parsed_lines = [PS_PARSER(line) for line in ps]
        master_pid, master_ppid, master_command = parsed_lines[0]

        ls, _ = subp.call(LS_CMD % master_pid)
        assert_that(ls, not_none())

        parsed = LS_PARSER(ls[0])
        assert_that(parsed, not_none())
        assert_that(parsed, equal_to('/usr/sbin/php5-fpm'))
コード例 #4
0
    def test_ps_master_parser(self):
        ps, _ = subp.call(PS_CMD)
        assert_that(ps, not_none())

        parsed_lines = [PS_PARSER(line) for line in ps]
        assert_that(parsed_lines, not_none())
        assert_that(parsed_lines, has_length(6))

        master_pid, master_ppid, master_command = parsed_lines[0]

        parsed_master = MASTER_PARSER(master_command)
        assert_that(parsed_master, not_none())
        assert_that(parsed_master, equal_to('/etc/php5/fpm/php-fpm.conf'))
コード例 #5
0
    def test_version_parser(self):
        # get master_pid
        ps, _ = subp.call(PS_CMD)
        ps_parsed_lines = [PS_PARSER(line) for line in ps]
        master_pid, master_ppid, master_command = ps_parsed_lines[0]

        # get bin_path
        ls, _ = subp.call(LS_CMD % master_pid)
        bin_path = LS_PARSER(ls[0])

        version, raw_line = VERSION_PARSER(bin_path)
        assert_that(version, not_none())
        assert_that(raw_input, not_none)

        # these checks may be too specific...will definitely break on alternate
        # systems/versions
        assert_that(version, equal_to('5.5.9-1'))
        assert_that(raw_line, starts_with('PHP 5.5.9-1'))
コード例 #6
0
    def test_version_parser(self):
        # get master_pid
        ps, _ = subp.call(PS_CMD)
        ps_parsed_lines = [PS_PARSER(line) for line in ps]
        master_pid, master_ppid, master_command = ps_parsed_lines[0]

        # get bin_path
        ls, _ = subp.call(LS_CMD % master_pid)
        bin_path = LS_PARSER(ls[0])

        version, raw_line = VERSION_PARSER(bin_path)
        assert_that(version, not_none())
        assert_that(raw_input, not_none)

        # these checks may be too specific...will definitely break on alternate
        # systems/versions
        assert_that(version, equal_to('5.5.9-1'))
        assert_that(
            raw_line,
            any_of(
                'PHP 5.5.9-1ubuntu4.21 (fpm-fcgi) (built: Feb  9 2017 21:00:52)',
                'PHP 5.5.9-1ubuntu4.22 (fpm-fcgi) (built: Aug  4 2017 19:44:16)'
            ))
コード例 #7
0
    def _find_all(ps=None):
        """
        Tries to find a master process

        :param ps: List of Strings...used for debugging our parsing logic...
                        should be None most of the time
        :return: List of Dicts phpfpm object definitions
        """
        # get ps info
        try:
            # set ps output to passed param or call subp
            ps, _ = (ps, None) if ps is not None else subp.call(PS_CMD)
            context.log.debug('ps php-fpm output: %s' % ps)
        except Exception as e:
            # log error
            exception_name = e.__class__.__name__
            context.log.debug(
                'failed to find running php-fpm via "%s" due to %s' %
                (PS_CMD, exception_name))
            context.log.debug('additional info:', exc_info=True)

            # If there is a root_object defined, log an event to send to the
            # cloud.
            if context.objects.root_object:
                context.objects.root_object.eventd.event(
                    level=INFO, message='no php-fpm found')

            # break processing returning a fault-tolerant empty list
            return []

        if not any('master process' in line for line in ps):
            context.log.info('no php-fpm masters found')

            # break processing returning a fault-tolerant empty list
            return []

        # collect all info about processes
        masters = {}
        try:
            for line in ps:
                parsed = PS_PARSER(line)

                # if not parsed - go to the next line
                if parsed is None:
                    continue

                pid, ppid, cmd = parsed  # unpack values

                # match master process
                if 'master process' in cmd:

                    # if ppid isn't 1, then the master process must have been
                    # started with a launcher
                    if ppid != 1:
                        out, err = subp.call('ps o command %d' % ppid)
                        # take the second line because the first is a header
                        parent_command = out[1]
                        context.log.debug(
                            'launching php-fpm with "%s" is not currently '
                            'supported' % parent_command)
                        continue

                    try:
                        conf_path = MASTER_PARSER(cmd)
                    except:
                        context.log.error('failed to find conf_path for %s' %
                                          cmd)
                        context.log.debug('additional info:', exc_info=True)
                    else:
                        # calculate local_id
                        local_id = hashlib.sha256(
                            '%s_%s' % (cmd, conf_path)).hexdigest()

                        if pid not in masters:
                            masters[pid] = {'workers': []}

                        masters[pid].update({
                            'cmd': cmd.strip(),
                            'conf_path': conf_path,
                            'pid': pid,
                            'local_id': local_id
                        })

                # match pool process
                elif 'pool' in cmd:
                    if ppid in masters:
                        masters[ppid]['workers'].append(pid)
                    else:
                        masters[ppid] = dict(workers=[pid])

        except Exception as e:
            # log error
            exception_name = e.__class__.__name__
            context.log.error('failed to parse ps results due to %s' %
                              exception_name)
            context.log.debug('additional info:', exc_info=True)

        # format results
        results = []
        for payload in masters.itervalues():
            # only add payloads that have all the keys
            if 'cmd' in payload and \
                    'conf_path' in payload and \
                    'pid' in payload and \
                    'local_id' in payload and \
                    'workers' in payload:
                results.append(payload)
            else:
                context.log.debug(
                    'phpfpm master "_find_all()" found an incomplete entity %s'
                    % payload)
        return results