ok
Direktori : /usr/share/lve-stats/ |
Current File : //usr/share/lve-stats/lve-bursting-cleanup.py |
# coding=utf-8 # # Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2023 All Rights Reserved # # Licensed under CLOUD LINUX LICENSE AGREEMENT # http://cloudlinux.com/docs/LICENSE.TXT import argparse import logging import sys from dataclasses import dataclass from datetime import datetime from typing import Self import sqlalchemy as sa import lvestats.lib.commons.decorators from lvestats.lib.bursting.history import TableDoesNotExistError from lvestats.lib.commons.argparse_utils import ParseDatetime from lvestats.lib.commons.dateutil import local_to_gm from lvestats.lib.commons.logsetup import setup_logging from lvestats.lib.config import ConfigError, read_config from lvestats.lib.dbengine import make_db_engine, MakeDbException from lvestats.plugins.generic.burster.config import is_bursting_supported from lvestats.plugins.generic.burster.common import Timestamp from lvestats.plugins.generic.burster.storage.cleanup import cleanup_old_events _log = setup_logging( {}, caller_name='lve-bursting-cleanup', file_level=logging.WARNING, console_level=logging.ERROR, ) @lvestats.lib.commons.decorators.no_sigpipe def _main() -> None: if not is_bursting_supported(): print('Bursting Limits feature is not supported in current environment') sys.exit(1) try: cfg = read_config() dbengine = make_db_engine(cfg=cfg) params = _Params.from_args_and_config(cfg, sys.argv[1:]) cleanup_old_events(dbengine, params.server_id, params.cutoff) except TableDoesNotExistError as e: print(e.message) print('Enable Bursting Limits feature to create the table') sys.exit(1) except (sa.exc.OperationalError, sa.exc.ProgrammingError, ConfigError, MakeDbException) as e: _log.error('Error occurred while executing the "lve-bursting-cleanup" utility', exc_info=e) sys.exit(1) @dataclass(frozen=True) class _Params: @classmethod def from_args_and_config(cls, config: dict[str, str], argv: list[str]) -> Self: parser = argparse.ArgumentParser( description="Utility for trimming bursting limits history", ) parser.add_argument( '--server-id', type=str, default=config.get('server_id', 'localhost'), help='used with central database for multiple servers, default "%(default)s"', ) parser.add_argument( '-t', '--to', action=ParseDatetime, default=datetime.now(), nargs='+', help='cleanup records up to the specified date and time', metavar='YYYY-MM-DD[ HH:MM]', dest='cutoff', ) namespace = parser.parse_args(argv) cutoff = local_to_gm(namespace.cutoff).timestamp() return cls( server_id=namespace.server_id, cutoff=Timestamp(int(cutoff)), ) server_id: str cutoff: Timestamp if __name__ == '__main__': _main()