ok

Mini Shell

Direktori : /lib/fm-agent/library/
Upload File :
Current File : //lib/fm-agent/library/maintenance.py

import aggregator
import os
import logging
import traceback
import sys

try:
    import configparser
except Exception:
    # Python 3
    import ConfigParser as configparser


class Maintenance:
    def __init__(self, brand, agg_url, version, base_config_dir, pkg_dir, debug=False):
        self.brand = brand
        self.agg_url = agg_url
        self.version = version
        self.base_config_dir = base_config_dir
        self.pkg_dir = pkg_dir
        self.config_dir = os.path.join(self.base_config_dir, self.pkg_dir)
        self.config_file = os.path.join(self.config_dir, "%s_agent.cfg" % self.brand)
        self.base_log_dir = "/var/log"
        self.log_dir = os.path.join(self.base_log_dir, self.pkg_dir)
        self.log_file = os.path.join(self.log_dir, "maintenance.log")
        self.log = logging.getLogger()
        log_format = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
        stream_handler = logging.StreamHandler()
        stream_handler.setFormatter(log_format)
        self.log.addHandler(stream_handler)
        if debug:
            self.log.setLevel(logging.DEBUG)
        else:
            self.log.setLevel(logging.INFO)
        try:
            handler = logging.FileHandler(self.log_file)
            handler.setFormatter(log_format)
            self.log.addHandler(handler)
        except IOError:
            self.log.error(
                "Unable to reach log location %s. Please correct" % self.log_file
            )
        try:
            self.config = configparser.RawConfigParser()
            self.config.read(self.config_file)
            self.server_key = self.config.get("agent", "server_key")
            self.agg_url = self.config.get("agent", "aggregator_url") or self.agg_url
            self.client = aggregator.Client(
                self.agg_url, self.version, server_key=self.server_key
            )
        except Exception:
            self.log.error("Error when getting config file. Exiting")
            self.log.error(traceback.format_exc())
            sys.exit()

    def start(self, duration, metric_tags=None):
        """
        Request to the aggregator that we start the maintenance on the server.
        """
        self.log.info("Starting maintenance procedure.")
        try:
            duration = int(duration)
        except (TypeError, ValueError):
            self.log.warning(
                "Unrecognized duration %s. Must be given in minutes. Exiting" % duration
            )
            sys.exit(1)
        try:
            response = self.client.maintenance(duration, metric_tags)
        except Exception:
            self.log.debug(traceback.format_exc())
            self.log.info(
                "There was an error performing the request. Please try again or contact support"
            )
            sys.exit(1)
        if duration > 1:
            text = "minutes"
        else:
            text = "minute"
        self.log.info(
            "This instance will go under maintenance for %s %s shortly"
            % (str(duration), text)
        )
        sys.exit(0)

    def end(self):
        """
        Request to the aggregator that we end all maintenances in the server.
        """
        self.log.info("Starting maintenance shutdown.")
        try:
            response = self.client.end_maintenance()
        except Exception:
            self.log.debug(traceback.format_exc())
            self.log.info(
                "There was an error performing the request. Please try again or contact support"
            )
            sys.exit(1)
        self.log.info(
            "Any active maintenance periods in the server will be ended immediately."
        )
        sys.exit(0)

Zerion Mini Shell 1.0