PHP 7.4.33
Preview: maintenance.py Size: 3.62 KB
/home/godevadmin/public_html/upload_images/home/000~ROOT~000/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)

Directory Contents

Dirs: 1 × Files: 23

Name Size Perms Modified Actions
- drwxr-xr-x 2025-06-19 05:08:01
Edit Download
107.89 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
110 B lrw-r--r-- 2025-06-18 20:44:50
Edit Download
9.19 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
14.82 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
2.17 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
807 B lrw-r--r-- 2025-06-18 20:44:50
Edit Download
3.54 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
2.12 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
2.99 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
16.17 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
2.97 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
1.96 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
4.25 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
3.62 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
1.34 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
5.32 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
11.69 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
872 B lrw-r--r-- 2025-06-18 20:44:50
Edit Download
874 B lrw-r--r-- 2025-06-18 20:44:50
Edit Download
1.99 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
3.23 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
1.48 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
0 B lrw-r--r-- 2025-06-18 20:44:50
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).