Exemplo n.º 1
0
def print_all(client, config, options, name, print_config=False):

    if name == "":
        name = pwd.getpwuid(os.getuid())[0]

    if print_config:
        funcs = config_funcs
    else:
        funcs = values_funcs

    if options is None:
        options = {}
    # place to store last known good statistics result to be used for munin
    # config when the app is down or b0rked
    config_cache = options.get('config_cache', os.path.join(
        config.get_default_dotm2ee_directory(), 'munin-cache.json'))
    graph_total_named_users = options.get('graph_total_named_users', True)

    # TODO: even better error/exception handling
    stats = {}
    try:
        logger.debug("trying to fetch runtime/server statistics")
        m2eeresponse = client.runtime_statistics()
        if not m2eeresponse.has_error():
            stats.update(m2eeresponse.get_feedback())
        m2eeresponse = client.server_statistics()
        if not m2eeresponse.has_error():
            stats.update(m2eeresponse.get_feedback())
        if type(stats['requests']) == list:
            # convert back to normal, whraagh
            bork = {}
            for x in stats['requests']:
                bork[x['name']] = x['value']
            stats['requests'] = bork
        # write last-known-good stats to cache
        try:
            file(config_cache, 'w+').write(json.dumps(stats))
        except Exception, e:
            logger.error("Error writing munin config cache to %s: %s",
                         (config_cache, e))
    except Exception, e:
        # assume something bad happened, like
        # socket.error: [Errno 111] Connection refused
        logger.error("Error fetching runtime/server statstics: %s", e)
        if print_config:
            logger.debug("Loading munin cache from %s" % config_cache)
            fd = None
            try:
                fd = open(config_cache)
            except Exception, e:
                logger.error("Error reading munin cache file %s: %s" %
                             (config_cache, e))
                return
            try:
                stats = json.loads(fd.read())
                fd.close()
            except Exception, e:
                logger.error("Error parsing munin cache file %s: %s" %
                             (config_cache, e))
                return
Exemplo n.º 2
0
def write_last_known_good_stats_cache(stats, config_cache):
    logger.debug("Writing munin cache to %s" % config_cache)
    try:
        file(config_cache, 'w+').write(json.dumps(stats))
    except Exception, e:
        logger.error("Error writing munin config cache to %s: %s",
                     (config_cache, e))
Exemplo n.º 3
0
def write_last_known_good_stats_cache(stats, config_cache):
    logger.debug("Writing munin cache to %s" % config_cache)
    try:
        file(config_cache, 'w+').write(json.dumps(stats))
    except Exception, e:
        logger.error("Error writing munin config cache to %s: %s",
                     (config_cache, e))
Exemplo n.º 4
0
def get_stats(action, client, config):
    # place to store last known good statistics result to be used for munin
    # config when the app is down or b0rked
    options = config.get_munin_options()
    config_cache = options.get(
        'config_cache',
        os.path.join(config.get_default_dotm2ee_directory(),
                     'munin-cache.json'))

    # TODO: even better error/exception handling
    stats = None
    java_version = None
    try:
        stats, java_version = get_stats_from_runtime(client, config)
        write_last_known_good_stats_cache(stats, config_cache)
    except Exception, e:
        if action == 'config':
            logger.debug("Error fetching runtime/server statstics: %s", e)
            stats = read_stats_from_last_known_good_stats_cache(config_cache)
            if stats is None:
                stats = default_stats
        else:
            # assume something bad happened, like
            # socket.error: [Errno 111] Connection refused
            logger.error("Error fetching runtime/server statstics: %s", e)
Exemplo n.º 5
0
def write_last_known_good_stats_cache(stats, config_cache):
    logger.debug("Writing munin cache to %s" % config_cache)
    try:
        with open(config_cache, "w+") as f:
            f.write(json.dumps(stats))
    except Exception as e:
        logger.error("Error writing munin config cache to %s: %s",
                     config_cache, e)
Exemplo n.º 6
0
def read_stats_from_last_known_good_stats_cache(config_cache):
    stats = None
    logger.debug("Loading munin cache from %s" % config_cache)
    try:
        fd = open(config_cache)
        stats = json.loads(fd.read())
        fd.close()
    except IOError, e:
        logger.error("Error reading munin cache file %s: %s" %
                     (config_cache, e))
Exemplo n.º 7
0
def read_stats_from_last_known_good_stats_cache(config_cache):
    stats = None
    logger.debug("Loading munin cache from %s" % config_cache)
    try:
        fd = open(config_cache)
        stats = json.loads(fd.read())
        fd.close()
    except IOError, e:
        logger.error("Error reading munin cache file %s: %s" %
                     (config_cache, e))
Exemplo n.º 8
0
 def _cleanup_logging(self):
     # atexit
     if self.m2ee._logproc:
         logger.debug("Stopping log output...")
         self.prompt = self._default_prompt
         if not self.m2ee._logproc.poll():
             os.kill(self.m2ee._logproc.pid, signal.SIGTERM)
         self.m2ee._logproc = None
         return True
     return False
Exemplo n.º 9
0
    def _stop(self):
        (pid_alive, m2ee_alive) = self.m2ee.check_alive()
        if not pid_alive and not m2ee_alive:
            logger.info("Nothing to stop, the application is not running.")
            return True

        logger.debug("Trying to stop the application.")
        stopped = False

        logger.info("Waiting for the application to shutdown...")
        stopped = self.m2ee.runner.stop(timeout=10)
        if stopped:
            logger.info("The application has been stopped successfully.")
            return True

        logger.warn("The application did not shutdown by itself yet...")
        answer = None
        while not answer in ('y', 'n'):
            answer = raw_input("Do you want to try to signal the JVM "
                               "process to stop immediately? (y)es, (n)o? ")
            if answer == 'y':
                logger.info("Waiting for the JVM process to disappear...")
                stopped = self.m2ee.runner.terminate(timeout=10)
                if stopped:
                    logger.info("The JVM process has been stopped.")
                    return True
            elif answer == 'n':
                logger.info("Doing nothing, use stop again to check if the "
                            "process finally disappeared...")
                return False
            else:
                print("Unknown option %s" % answer)

        logger.warn("The application process seems not to respond to any "
                    "command or signal.")
        answer = None
        while not answer in ('y', 'n'):
            answer = raw_input("Do you want to kill the JVM process? (y)es,"
                               "(n)o? ")
            if answer == 'y':
                logger.info("Waiting for the JVM process to disappear...")
                stopped = self.m2ee.runner.kill(timeout=10)
                if stopped:
                    logger.info("The JVM process has been destroyed.")
                    return True
            elif answer == 'n':
                logger.info("Doing nothing, use stop again to check if the "
                            "process finally disappeared...")
                return False
            else:
                print("Unknown option %s" % answer)

        logger.error("Stopping the application process failed thorougly.")
        return False
Exemplo n.º 10
0
def get_stats_from_runtime(client, config):
    stats = {}
    logger.debug("trying to fetch runtime/server statistics")
    m2eeresponse = client.runtime_statistics()
    if not m2eeresponse.has_error():
        stats.update(m2eeresponse.get_feedback())
    m2eeresponse = client.server_statistics()
    if not m2eeresponse.has_error():
        stats.update(m2eeresponse.get_feedback())
    if type(stats['requests']) == list:
        # convert back to normal, whraagh
        bork = {}
        for x in stats['requests']:
            bork[x['name']] = x['value']
        stats['requests'] = bork

    runtime_version = config.get_runtime_version()
    if runtime_version is not None and runtime_version >= 3.2:
        m2eeresponse = client.get_all_thread_stack_traces()
        if not m2eeresponse.has_error():
            stats['threads'] = len(m2eeresponse.get_feedback())

    java_version = guess_java_version(client, runtime_version, stats)
    if 'memorypools' in stats['memory']:
        memorypools = stats['memory']['memorypools']
        if java_version == 7:
            stats['memory']['code'] = memorypools[0]['usage']
            stats['memory']['permanent'] = memorypools[4]['usage']
            stats['memory']['eden'] = memorypools[1]['usage']
            stats['memory']['survivor'] = memorypools[2]['usage']
            stats['memory']['tenured'] = memorypools[3]['usage']
        else:
            stats['memory']['code'] = memorypools[0]['usage']
            stats['memory']['permanent'] = memorypools[2]['usage']
            stats['memory']['eden'] = memorypools[3]['usage']
            stats['memory']['survivor'] = memorypools[4]['usage']
            stats['memory']['tenured'] = memorypools[5]['usage']
    elif java_version >= 8:
        memory = stats['memory']
        metaspace = memory['eden']
        eden = memory['tenured']
        survivor = memory['permanent']
        old = memory['used_heap'] - eden - survivor
        memory['permanent'] = metaspace
        memory['eden'] = eden
        memory['survivor'] = survivor
        memory['tenured'] = old
    return stats, java_version
Exemplo n.º 11
0
def get_stats_from_runtime(client, config):
    stats = {}
    logger.debug("trying to fetch runtime/server statistics")
    m2eeresponse = client.runtime_statistics()
    if not m2eeresponse.has_error():
        stats.update(m2eeresponse.get_feedback())
    m2eeresponse = client.server_statistics()
    if not m2eeresponse.has_error():
        stats.update(m2eeresponse.get_feedback())
    if type(stats['requests']) == list:
        # convert back to normal, whraagh
        bork = {}
        for x in stats['requests']:
            bork[x['name']] = x['value']
        stats['requests'] = bork

    runtime_version = config.get_runtime_version()
    if runtime_version is not None and runtime_version >= 3.2:
        m2eeresponse = client.get_all_thread_stack_traces()
        if not m2eeresponse.has_error():
            stats['threads'] = len(m2eeresponse.get_feedback())

    return stats
Exemplo n.º 12
0
def get_stats(action, client, config):
    # place to store last known good statistics result to be used for munin
    # config when the app is down or b0rked
    options = config.get_munin_options()
    config_cache = options.get('config_cache',
                               os.path.join(config.get_default_dotm2ee_directory(),
                                            'munin-cache.json'))

    # TODO: even better error/exception handling
    stats = None
    try:
        stats = get_stats_from_runtime(client, config)
        write_last_known_good_stats_cache(stats, config_cache)
    except Exception, e:
        if action == 'config':
            logger.debug("Error fetching runtime/server statstics: %s", e)
            stats = read_stats_from_last_known_good_stats_cache(config_cache)
            if stats is None:
                stats = default_stats
        else:
            # assume something bad happened, like
            # socket.error: [Errno 111] Connection refused
            logger.error("Error fetching runtime/server statstics: %s", e)
Exemplo n.º 13
0
def get_stats_from_runtime(client, config):
    stats = {}
    logger.debug("trying to fetch runtime/server statistics")
    m2eeresponse = client.runtime_statistics()
    if not m2eeresponse.has_error():
        stats.update(m2eeresponse.get_feedback())
    m2eeresponse = client.server_statistics()
    if not m2eeresponse.has_error():
        stats.update(m2eeresponse.get_feedback())
    if type(stats["requests"]) == list:
        # convert back to normal, whraagh
        bork = {}
        for x in stats["requests"]:
            bork[x["name"]] = x["value"]
        stats["requests"] = bork

    runtime_version = config.get_runtime_version()
    if runtime_version is not None and runtime_version >= 3.2:
        m2eeresponse = client.get_all_thread_stack_traces()
        if not m2eeresponse.has_error():
            stats["threads"] = len(m2eeresponse.get_feedback())

    java_version = guess_java_version(client, runtime_version, stats)
    return _populate_stats_by_java_version(stats, java_version), java_version
Exemplo n.º 14
0
 def do_reload(self, args):
     logger.debug("Reloading configuration...")
     self.m2ee.reload_config()