Mini Shell

Direktori : /proc/self/root/proc/thread-self/root/opt/imh-python/lib/python3.9/site-packages/fpmstatus/
Upload File :
Current File : //proc/self/root/proc/thread-self/root/opt/imh-python/lib/python3.9/site-packages/fpmstatus/cli.py

# vim: set ts=4 sw=4 expandtab syntax=python:
"""

fpmstatus.cli
Command-line functions & CLI entry-point

@author J. Hipps <jacobh@inmotionhosting.com>

"""

import os
import sys
import json
import logging
import logging.handlers
from argparse import ArgumentParser, Action

import yaml

from fpmstatus import fpm, report
from fpmstatus import __version__, __date__
from fpmstatus import cwp_fpm

logger = logging.getLogger('fpmstatus')

class SafeStoreAction(Action):
    def __init__(self, option_strings, dest, nargs=None, **kwargs):
        super(SafeStoreAction, self).__init__(option_strings, dest, **kwargs)

    def __call__(self, parser, namespace, values, option_string=None):
        is_safe = False
        if 'SUDO_UID' in os.environ:
            if os.environ['SUDO_UID'] == '0':
                is_safe = True
            else:
                is_safe = False
        else:
            is_safe = True
        if is_safe:
            setattr(namespace, self.dest, values)
        else:
            print("ERROR: Attempted to set dangerous option as a non-root sudoer! Aborting.")
            sys.exit(249)


def setup_logging(clevel=logging.INFO, flevel=logging.DEBUG, logfile=None):
    """
    Setup logging
    """
    logger.setLevel(logging.DEBUG)

    # Console
    con = logging.StreamHandler()
    con.setLevel(clevel)
    con_format = logging.Formatter("%(levelname)s: %(message)s")
    con.setFormatter(con_format)
    logger.addHandler(con)

    # File
    if logfile:
        try:
            flog = logging.handlers.WatchedFileHandler(logfile)
            flog.setLevel(flevel)
            flog_format = logging.Formatter("[%(asctime)s] %(name)s: %(levelname)s: %(message)s")
            flog.setFormatter(flog_format)
            logger.addHandler(flog)
        except Exception as e:
            logger.warning("Failed to open logfile: %s", str(e))

def parse_cli(show_help=False):
    """
    Parse CLI arguments
    """
    parser = ArgumentParser(description="PHP-FPM Pool Status tool")
    parser.set_defaults(user=None, domain=None, outfmt='report',
                        loglevel=logging.INFO, verbose=False)
    parser.add_argument('--user', '-u', action='store', metavar="USER",
                        help="Specify user")
    parser.add_argument('--domain', '-d', action='store', metavar="DOMAIN",
                        help="Specify domain")
    parser.add_argument('--pool', '-p', action='store', metavar="POOL",
                        help="Specify pool name")
    parser.add_argument('--sock', '-s', action='store', metavar="[SOCKPATH|HOST:PORT]",
                        help="Specify socket")
    parser.add_argument('--verbose', '-V', dest='verbose', action='store_true',
                        help="Enable detailed reporting output")
    parser.add_argument('--compact', '-c', dest='compact', action='store_true',
                        help="Prints the head of a verbose output, best used with -d")
    parser.add_argument('--debug', '-D', dest='loglevel', action='store_const', const=logging.DEBUG,
                        help="Enable debug output")
    parser.add_argument('--json', '-J', dest='outfmt', action='store_const', const='json',
                        help="Output format: JSON")
    parser.add_argument('--yaml', '-Y', dest='outfmt', action='store_const', const='yaml',
                        help="Output format: YAML")
    parser.add_argument('--version', '-v', action='version', version="%s (%s)" % (__version__, __date__))
    parser.add_argument(
        "--install-conf",
        action="store_true",
        dest="install",
        help="Only install php-fpm changes, do not fetch status. (cwp)",
    )
    if show_help:
        parser.print_help()
        sys.exit(1)

    return parser.parse_args()

def _main():
    """Entry point"""
    args = parse_cli()
    setup_logging(clevel=args.loglevel, flevel=logging.DEBUG)

    if cwp_fpm.is_cwp():
        full_support = cwp_fpm.check_full_support()
        if not full_support:
            if args.install:
                cwp_fpm.deploy_support()
                sys.exit(0)
            else:
                print("PHP-FPM status page not enabled. "
                      "Re-run with --install-conf then restart FPM.")
                sys.exit(1)

    # Perform command/filtering
    if args.domain:
        pd = [fpm.get_domain_pool(args.domain)]
    elif args.user:
        pd = fpm.get_user_pools(args.user)
    elif args.pool:
        pd = [fpm.get_pool_by_name(args.pool)]
    elif args.sock:
        pd = [fpm.get_pool_status(args.sock)]
    else:
        pd = fpm.get_all_pools()

    # Format the output
    if pd:
        if args.outfmt == 'json':
            print(json.dumps(pd, sort_keys=True, indent=4, separators=(',', ': ')))
        elif args.outfmt == 'yaml':
            print(yaml.dump(pd, Dumper=yaml.SafeDumper, default_flow_style=False))
        else:
            for tpool in pd:
                if isinstance(tpool, dict):
                    if not args.compact and not args.verbose:
                        report.repgen_pool_terse(tpool)
                    else:
                        report.repgen_pool(tpool, args.compact)

    logger.debug("Done")

if __name__ == '__main__':
    _main()

Zerion Mini Shell 1.0