def get_mesos_task_count_by_slave(mesos_state, pool=None): """Get counts of running tasks per mesos slave. Also include separate count of chronos tasks :param mesos_state: mesos state dict :param pool: pool of slaves to return (None means all) :returns: dict of {<slave_id>:{'count': <count>, 'slave': <slave_object>, 'chronos_count':<chronos_count>}} """ all_mesos_tasks = get_running_tasks_from_active_frameworks('') # empty string matches all app ids if pool: slaves = { slave['id']: {'count': 0, 'slave': slave, 'chronos_count': 0} for slave in mesos_state.get('slaves', []) if slave['attributes'].get('pool', 'default') == pool } else: slaves = { slave['id']: {'count': 0, 'slave': slave, 'chronos_count': 0} for slave in mesos_state.get('slaves', []) } for task in all_mesos_tasks: if task.slave['id'] not in slaves: log.error("Task associated with %s but slave not in masters list of slaves" % task.slave['id']) continue else: slaves[task.slave['id']]['count'] += 1 log.debug("Task framework: {0}".format(task.framework.name)) if task.framework.name == 'chronos': slaves[task.slave['id']]['chronos_count'] += 1 slaves = {slave_pid: SlaveTaskCount(**slave_counts) for slave_pid, slave_counts in slaves.items()} for slave in slaves.values(): log.info("Slave: {0}, running {1} tasks, including {2} chronos tasks".format(slave.slave['pid'], slave.count, slave.chronos_count)) return slaves
def get_mesos_leader(): """Get the current mesos-master leader's hostname. Attempts to determine this by using mesos.cli to query ZooKeeper. :returns: The current mesos-master hostname""" try: url = master.CURRENT.host except MesosMasterConnectionError: log.debug('mesos.cli failed to provide the master host') raise log.debug("mesos.cli thinks the master host is: %s" % url) hostname = urlparse(url).hostname log.debug("The parsed master hostname is: %s" % hostname) # This check is necessary, as if we parse a value such as 'localhost:5050', # it won't have a hostname attribute if hostname: try: host = socket.gethostbyaddr(hostname)[0] fqdn = socket.getfqdn(host) except (socket.error, socket.herror, socket.gaierror, socket.timeout): log.debug("Failed to convert mesos leader hostname to fqdn!") raise log.debug("Mesos Leader: %s" % fqdn) return fqdn else: raise ValueError('Expected to receive a valid URL, got: %s' % url)
def get_mesos_task_count_by_slave(mesos_state, pool=None): """Get counts of running tasks per mesos slave. Also include separate count of chronos tasks :param mesos_state: mesos state dict :param pool: pool of slaves to return (None means all) :returns: dict of {<slave_id>:{'count': <count>, 'slave': <slave_object>, 'chronos_count':<chronos_count>}} """ all_mesos_tasks = get_running_tasks_from_active_frameworks( '') # empty string matches all app ids if pool: slaves = { slave['id']: { 'count': 0, 'slave': slave, 'chronos_count': 0 } for slave in mesos_state.get('slaves', []) if slave['attributes'].get('pool', 'default') == pool } else: slaves = { slave['id']: { 'count': 0, 'slave': slave, 'chronos_count': 0 } for slave in mesos_state.get('slaves', []) } for task in all_mesos_tasks: if task.slave['id'] not in slaves: log.debug( "Task associated with slave {0} not found in {1} pool".format( task.slave['id'], pool)) continue else: slaves[task.slave['id']]['count'] += 1 log.debug("Task framework: {0}".format(task.framework.name)) if task.framework.name == 'chronos': slaves[task.slave['id']]['chronos_count'] += 1 slaves = { slave_counts['slave']['hostname']: SlaveTaskCount(**slave_counts) for slave_counts in slaves.values() } for slave in slaves.values(): log.debug("Slave: {0}, running {1} tasks, including {2} chronos tasks". format(slave.slave['hostname'], slave.count, slave.chronos_count)) return slaves