def test_parse(self):
        """This test is only possible because there is a working config in the test container"""
        phpfpm_obj = PHPFPMObject(
            local_id=123,
            pid=2,
            cmd='php-fpm: master process (/etc/php5/fpm/php-fpm.conf)',
            conf_path='/etc/php5/fpm/php-fpm.conf',
            workers=[3, 4])
        assert_that(phpfpm_obj, not_none())

        parsed_conf = phpfpm_obj.parse()
        assert_that(parsed_conf, not_none())
        assert_that(
            parsed_conf,
            equal_to({
                'pools': [{
                    'status_path': '/status',
                    'name': 'www',
                    'file': '/etc/php5/fpm/pool.d/www.conf',
                    'listen': '/run/php/php7.0-fpm.sock'
                }, {
                    'status_path': '/status',
                    'name': 'www2',
                    'file': '/etc/php5/fpm/pool.d/www2.conf',
                    'listen': '127.0.0.1:51'
                }],
                'include': ['/etc/php5/fpm/pool.d/*.conf'],
                'file':
                '/etc/php5/fpm/php-fpm.conf'
            }))
    def test_init(self):
        phpfpm_obj = PHPFPMObject(
            local_id=123,
            pid=2,
            cmd='php-fpm: master process (/etc/php5/fpm/php-fpm.conf)',
            conf_path='/etc/php5/fpm/php-fpm.conf',
            workers=[3, 4])
        assert_that(phpfpm_obj, not_none())

        assert_that(
            phpfpm_obj.local_id_args,
            equal_to(('php-fpm: master process (/etc/php5/fpm/php-fpm.conf)',
                      '/etc/php5/fpm/php-fpm.conf')))
        assert_that(phpfpm_obj.local_id, equal_to(123))
        assert_that(
            phpfpm_obj.definition,
            equal_to({
                'local_id': 123,
                'type': 'phpfpm',
                'root_uuid': None
            }  # root_uuid is None because there is no root obj
                     ))
        assert_that(
            phpfpm_obj.definition_hash,
            equal_to(
                'c1e5fecff97b99af2ed2fccf3c168f883f3910f191a5378f8f6903b0014b6368'
            ))
        assert_that(phpfpm_obj.collectors, has_length(2))
Exemple #3
0
    def setup_method(self, method):
        super(PHPFPMPoolMetricsCollectorTestCase, self).setup_method(method)
        context._setup_object_tank()

        self.phpfpm_obj = PHPFPMObject(
            local_id=123,
            pid=2,
            cmd='php-fpm: master process (/etc/php5/fpm/php-fpm.conf)',
            conf_path='/etc/php5/fpm/php-fpm.conf',
            workers=[3, 4])

        context.objects.register(self.phpfpm_obj)

        pool_data = {
            'status_path': '/status',
            'name': 'www',
            'file': '/etc/php5/fpm/pool.d/www.conf',
            'listen': '/run/php/php7.0-fpm.sock'
        }

        self.phpfpm_pool_obj = PHPFPMPoolObject(local_id=124,
                                                parent_local_id=123,
                                                **pool_data)

        context.objects.register(self.phpfpm_pool_obj,
                                 parent_obj=self.phpfpm_obj)
    def test_properties(self):
        """
        This test is meant to test some properties that have had intermittent
        user bug reports.
        """
        phpfpm_obj = PHPFPMObject(
            pid=2,
            cmd='php-fpm: master process (/etc/php5/fpm/php-fpm.conf)',
            conf_path='/etc/php5/fpm/php-fpm.conf',
            workers=[3, 4])
        assert_that(phpfpm_obj, not_none())

        assert_that(
            phpfpm_obj.local_id_args,
            equal_to(('php-fpm: master process (/etc/php5/fpm/php-fpm.conf)',
                      '/etc/php5/fpm/php-fpm.conf')))
        assert_that(
            phpfpm_obj.local_id,
            equal_to(
                'e5942daaa5bf35af722bac3b9582b17c07515f0f77936fb5c7f771c7736cc157'
            ))
        assert_that(
            phpfpm_obj.definition,
            equal_to({
                'local_id':
                'e5942daaa5bf35af722bac3b9582b17c07515f0f77936fb5c7f771c7736cc157',
                'type': 'phpfpm',
                'root_uuid': DEFAULT_UUID
            }))
        assert_that(
            phpfpm_obj.definition_hash,
            equal_to(
                '6ee51f6b649782e5dd04db052e7a018372645756378a7a3de3356c2ae6ff3bd7'
            ))
    def test_init(self):
        phpfpm_obj = PHPFPMObject(
            local_id=123,
            pid=2,
            cmd='php-fpm: master process (/etc/php5/fpm/php-fpm.conf)',
            conf_path='/etc/php5/fpm/php-fpm.conf',
            workers=[3, 4])
        assert_that(phpfpm_obj, not_none())

        assert_that(
            phpfpm_obj.local_id_args,
            equal_to(('php-fpm: master process (/etc/php5/fpm/php-fpm.conf)',
                      '/etc/php5/fpm/php-fpm.conf')))
        assert_that(phpfpm_obj.local_id, equal_to(123))
        assert_that(
            phpfpm_obj.definition,
            equal_to({
                'local_id': 123,
                'type': 'phpfpm',
                'root_uuid': DEFAULT_UUID
            }))
        assert_that(
            phpfpm_obj.definition_hash,
            equal_to(
                '32e8faf1747e8fa5778388b2db268941abeba7140cd83c52712ef97eb571e6d2'
            ))
        assert_that(phpfpm_obj.collectors, has_length(2))
    def setup_method(self, method):
        super(PHPFPMMetricsCollectorTestCase, self).setup_method(method)
        context._setup_object_tank()

        self.phpfpm_obj = PHPFPMObject(
            local_id=123,
            pid=2,
            cmd='php-fpm: master process (/etc/php5/fpm/php-fpm.conf)',
            conf_path='/etc/php5/fpm/php-fpm.conf',
            workers=[3, 4])

        context.objects.register(self.phpfpm_obj)
    def test_properties(self):
        """
        This test is meant to test some properties that have had intermittent
        user bug reports.
        """
        phpfpm_obj = PHPFPMObject(
            pid=2,
            cmd='php-fpm: master process (/etc/php5/fpm/php-fpm.conf)',
            conf_path='/etc/php5/fpm/php-fpm.conf',
            workers=[3, 4])
        assert_that(phpfpm_obj, not_none())

        assert_that(
            phpfpm_obj.local_id_args,
            equal_to(('php-fpm: master process (/etc/php5/fpm/php-fpm.conf)',
                      '/etc/php5/fpm/php-fpm.conf')))
        assert_that(
            phpfpm_obj.local_id,
            equal_to(
                'e5942daaa5bf35af722bac3b9582b17c07515f0f77936fb5c7f771c7736cc157'
            ))
        assert_that(
            phpfpm_obj.definition,
            equal_to({
                'local_id':
                'e5942daaa5bf35af722bac3b9582b17c07515f0f77936fb5c'
                '7f771c7736cc157',
                'type':
                'phpfpm',
                'root_uuid':
                None
            }  # root_uuid is None because there is no root obj
                     ))
        assert_that(
            phpfpm_obj.definition_hash,
            equal_to(
                'c2e304622d5d31cac924f6eb97564fc30605df72a9ccdbfafe2dfbb69057f08e'
            ))
Exemple #8
0
    def _discover_objects(self):
        # save the current ids
        existing_hashes = [
            obj.definition_hash
            for obj in self.objects.find_all(types=self.types)
        ]

        phpfpm_masters = self._find_all()

        discovered_hashes = []
        while len(phpfpm_masters):
            try:
                data = phpfpm_masters.pop()
                root_uuid = context.objects.root_object.uuid \
                    if context.objects.root_object else None
                definition = {
                    'type': 'phpfpm',
                    'local_id': data['local_id'],
                    'root_uuid': root_uuid
                }
                definition_hash = PHPFPMObject.hash(definition)
                discovered_hashes.append(definition_hash)

                if definition_hash not in existing_hashes:
                    # New object -- create it
                    new_obj = PHPFPMObject(data=data)

                    # Send discover event.
                    new_obj.eventd.event(
                        level=INFO,
                        message='php-fpm master process found, pid %s' %
                        new_obj.pid)

                    self.objects.register(new_obj,
                                          parent_id=self.objects.root_id)
                elif definition_hash in existing_hashes:
                    for obj in self.objects.find_all(types=self.types):
                        if obj.definition_hash == definition_hash:
                            current_obj = obj
                            break

                    if current_obj.pid != data['pid']:
                        # PIDs changed...php-fpm must have been restarted
                        context.log.debug(
                            'php-fpm was restarted (pid was %s now %s)' %
                            (current_obj.pid, data['pid']))
                        new_obj = PHPFPMObject(data=data)

                        # send php-fpm restart event
                        new_obj.eventd.event(
                            level=INFO,
                            message='php-fpm master process was restarted, new'
                            ' pid %s, old pid %s' %
                            (new_obj.pid, current_obj.pid))

                        # stop and unregister children
                        for child_obj in self.objects.find_all(
                                obj_id=current_obj.id,
                                children=True,
                                include_self=False):
                            child_obj.stop()
                            self.objects.unregister(obj=child_obj)

                        # stop old object
                        current_obj.stop()

                        # unregister old object
                        self.objects.unregister(current_obj)

                        self.objects.register(new_obj,
                                              parent_id=self.objects.root_id)
            except psutil.NoSuchProcess:
                context.log.debug(
                    'phpfpm is restarting/reloading, pids are changing, agent '
                    'is waiting')

        # check if we left something in objects (phpfpm could be stopped or
        # something)
        dropped_hashes = filter(lambda x: x not in discovered_hashes,
                                existing_hashes)
        if len(dropped_hashes):
            for dropped_hash in dropped_hashes:
                for obj in self.objects.find_all(types=self.types):
                    if obj.definition_hash == dropped_hash:
                        dropped_obj = obj

                        context.log.debug('phpfpm was stopped (pid was %s)' %
                                          dropped_obj.pid)

                        for child_obj in self.objects.find_all(
                                obj_id=dropped_obj.id,
                                children=True,
                                include_self=False):
                            child_obj.stop()
                            self.objects.unregister(child_obj)

                        dropped_obj.stop()
                        self.objects.unregister(dropped_obj)