def _discover_objects(self): # save the current hashes existing_hashes = [ obj.definition_hash for obj in self.objects.find_all(types=self.types) ] discovered_pools = self._find_all() while len(discovered_pools): try: data = discovered_pools.pop() definition = { 'type': 'phpfpm_pool', 'local_id': data['local_id'], 'root_uuid': context.uuid } definition_hash = PHPFPMPoolObject.hash(definition) if definition_hash not in existing_hashes: # new object -- create it new_obj = PHPFPMPoolObject(data=data) # send discover event new_obj.eventd.event( level=INFO, message='php-fpm pool found, name "%s"' % new_obj.name) self.objects.register(new_obj, parent_id=data['parent_id']) # We don't need restart logic since the master process should # take care of child objects. except psutil.NoSuchProcess: context.log.debug( 'phpfpm is restarting/reloading, pids are changing, agent is waiting' )
def test_init(self): pool_data = { 'status_path': '/status', 'name': 'www', 'file': '/etc/php5/fpm/pool.d/www.conf', 'listen': '/run/php/php7.0-fpm.sock' } phpfpm_pool = PHPFPMPoolObject( local_id=124, parent_local_id=123, **pool_data ) assert_that(phpfpm_pool, not_none()) assert_that(phpfpm_pool.parent_local_id, equal_to(123)) assert_that(phpfpm_pool.local_id_args, equal_to((123, 'www'))) assert_that(phpfpm_pool.local_id, equal_to(124)) assert_that(phpfpm_pool.definition, equal_to( {'local_id': 124, 'type': 'phpfpm_pool', 'root_uuid': None} )) assert_that(phpfpm_pool.definition_hash, equal_to( '8c84685602a25812172c511118d0f93d705f2cd67c41d278f4ae5b8b8967a8bd' )) assert_that(phpfpm_pool.collectors, has_length(2))
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_init(self): pool_data = { 'status_path': '/status', 'name': 'www', 'file': '/etc/php5/fpm/pool.d/www.conf', 'listen': '/run/php/php7.0-fpm.sock' } phpfpm_pool = PHPFPMPoolObject(local_id=124, parent_local_id=123, **pool_data) assert_that(phpfpm_pool, not_none()) assert_that(phpfpm_pool.parent_local_id, equal_to(123)) assert_that(phpfpm_pool.local_id_args, equal_to((123, 'www'))) assert_that(phpfpm_pool.local_id, equal_to(124)) assert_that( phpfpm_pool.definition, equal_to({ 'local_id': 124, 'type': 'phpfpm_pool', 'root_uuid': DEFAULT_UUID })) assert_that( phpfpm_pool.definition_hash, equal_to( 'a2071ce31b031f8f163d464648a7c9cf5305a5be65ddb9ef7daf2d97c13520f6' )) assert_that(phpfpm_pool.collectors, has_length(2))
def setup_method(self, method): super(PHPFPMPoolMetaCollectorTestCase, self).setup_method(method) self.phpfpm_pool_obj = PHPFPMPoolObject( local_id=124, parent_local_id=123, status_path='/status', name='www', file='/etc/php5/fpm/pool.d/www.conf', listen='/run/php/php7.0-fpm.sock')
def _find_all(self): """ Go through the masters. For the masters go through the configured pools, update pooldata with parent_local_id and local_id hash data. Then add the new updated pool to results and return completed dicts. :return: List of Dict data representations of object meta data. """ phpfpm_masters = self.objects.find_all(types=('phpfpm',)) phpfpm_pools = [] for master in phpfpm_masters: master_config = master.parse() for pool_data in master_config['pools']: # if there are no "missing" (None) values... if not len(filter(lambda x: x is None, pool_data.values())): # ...proceed with creation of a pool object for management pool_data.update(parent_id=master.id) pool_data.update(parent_local_id=master.local_id) pool_local_id = ( pool_data['parent_local_id'], pool_data['name'] ) pool_data.update( local_id=PHPFPMPoolObject.hash_local(pool_local_id) ) phpfpm_pools.append(pool_data) else: # create list of keys with None values none_keys = map( lambda t: t[0], filter( lambda t: t[1] is None, [(k, v) for k, v in pool_data.iteritems()] ) ) # ...log debug and skip context.log.debug( 'found a pool "%s" with missing or commented ' 'directives %s %s' % ( pool_data['name'], ', '.join(['"%s"' % key for key in none_keys]), pool_data ) ) return phpfpm_pools
def test_collect_flisten(self): phpfpm_pool_obj = PHPFPMPoolObject( local_id=124, parent_local_id=123, status_path='/status', name='www', file='/etc/php5/fpm/pool.d/www.conf', listen='/run/php/$pool.sock') phpfpm_pool_meta_collector = PHPFPMPoolMetaCollector( object=phpfpm_pool_obj, interval=self.phpfpm_pool_obj.intervals['meta']) assert_that(phpfpm_pool_meta_collector, not_none()) assert_that(phpfpm_pool_meta_collector.meta, equal_to({})) # make sure meta is empty # collect and assert that meta is not empty phpfpm_pool_meta_collector.collect() # check value assert_that( phpfpm_pool_meta_collector.meta, equal_to({ 'listen': '/run/php/$pool.sock', 'flisten': '/run/php/www.sock', 'local_id': 124, 'name': 'www', 'display_name': 'phpfpm www @ hostname.nginx', 'parent_local_id': 123, 'status_path': '/status', 'type': 'phpfpm_pool', 'root_uuid': DEFAULT_UUID, 'can_have_children': False })) # check that it matches the object metad assert_that(phpfpm_pool_meta_collector.meta, equal_to(phpfpm_pool_obj.metad.current))