Mini Shell
# vim: set ts=4 sw=4 expandtab syntax=python:
"""
ngxstats.util
Realtime Nginx stats aggregation tool
Utility and helper functions
Copyright (c) 2019-2020 InMotion Hosting, Inc.
https://www.inmotionhosting.com/
@author J. Hipps <jacobh@inmotionhosting.com>
"""
import os
import logging
import logging.handlers
import traceback
import yaml
from yaml import CLoader
from ngxstats import gconf_defaults
logger = logging.getLogger('ngxstats')
class GConf:
"""
Global configuration
"""
_conf = {}
def __init__(self, cpath='/opt/ngxstats/config.yaml'):
self.configpath = cpath
def __getattr__(self, val):
if val in self._conf:
return self._conf.get(val)
raise KeyError(val)
def __getitem__(self, val):
return self._conf.get(val)
def default(self, val):
"""Get default value"""
gconf_defaults.get(val)
def parse_config(self, cpath='/opt/ngxstats/config.yaml'):
"""
Parse ngxstats config
"""
try:
with open(cpath, encoding='utf-8') as f:
conf = yaml.load(f, Loader=CLoader)
logger.debug("Loaded global config from %s", cpath)
except Exception as e:
logger.warning("Failed to load configuration file: %s", str(e))
conf = gconf_defaults
gconf_defaults.update(conf)
self._conf = gconf_defaults
return gconf_defaults
def __repr__(self):
return "<GConf: " + str(self._conf) + ">"
gconf = GConf()
def excepthook(etype, evalue, etraceback):
"""
Default exception hook
"""
logger.critical(
"Unhandled exception caught:\n%s",
'\n'.join(traceback.format_exception(etype, evalue, etraceback)),
)
def check_alive(pid):
"""
Check if process is still alive
"""
try:
os.kill(pid, 0)
return True
except Exception as e:
logger.debug("check_alive(%s): %s", pid, str(e))
return False
Zerion Mini Shell 1.0