diff --git a/Products/ZenCollector/configcache/cli/__init__.py b/Products/ZenCollector/configcache/cli/__init__.py index f7409ce684..66cd5296d0 100644 --- a/Products/ZenCollector/configcache/cli/__init__.py +++ b/Products/ZenCollector/configcache/cli/__init__.py @@ -9,12 +9,8 @@ from __future__ import absolute_import +from .device import Device +from .oidmap import OidMap -from .expire import Expire -from .list import List_ -from .remove import Remove -from .show import Show -from .stats import Stats - -__all__ = ("Expire", "List_", "Remove", "Show", "Stats") +__all__ = ("Device", "OidMap") diff --git a/Products/ZenCollector/configcache/cli/args.py b/Products/ZenCollector/configcache/cli/args.py index dcdcf82e6d..7a56e2837c 100644 --- a/Products/ZenCollector/configcache/cli/args.py +++ b/Products/ZenCollector/configcache/cli/args.py @@ -54,14 +54,14 @@ def __iter__(self): return iter(self._choices) -_common_parser = None +_devargs_parser = None -def get_common_parser(): - global _common_parser - if _common_parser is None: - _common_parser = argparse.ArgumentParser(add_help=False) - _common_parser.add_argument( +def get_devargs_parser(): + global _devargs_parser + if _devargs_parser is None: + _devargs_parser = argparse.ArgumentParser(add_help=False) + _devargs_parser.add_argument( "-m", "--collector", type=str, @@ -69,7 +69,7 @@ def get_common_parser(): help="Name of the performance collector. Supports simple '*' " "wildcard comparisons. A lone '*' selects all collectors.", ) - _common_parser.add_argument( + _devargs_parser.add_argument( "-s", "--service", type=str, @@ -77,7 +77,7 @@ def get_common_parser(): help="Name of the configuration service. Supports simple '*' " "wildcard comparisons. A lone '*' selects all services.", ) - _common_parser.add_argument( + _devargs_parser.add_argument( "device", nargs="*", default=argparse.SUPPRESS, @@ -85,4 +85,4 @@ def get_common_parser(): "Supports simple '*' wildcard comparisons. Not specifying a " "device will select all devices.", ) - return _common_parser + return _devargs_parser diff --git a/Products/ZenCollector/configcache/cli/device.py b/Products/ZenCollector/configcache/cli/device.py new file mode 100644 index 0000000000..99a142bc31 --- /dev/null +++ b/Products/ZenCollector/configcache/cli/device.py @@ -0,0 +1,36 @@ +############################################################################## +# +# Copyright (C) Zenoss, Inc. 2024, all rights reserved. +# +# This content is made available according to terms specified in +# License.zenoss under the directory where your Zenoss product is installed. +# +############################################################################## + +from __future__ import absolute_import, print_function + +from ..app.args import get_subparser + +from .expire import ExpireDevice +from .list import ListDevice +from .remove import RemoveDevice +from .show import ShowDevice +from .stats import StatsDevice + + +class Device(object): + description = "Manage the device configuration cache" + + @staticmethod + def add_arguments(parser, subparsers): + devicep = get_subparser( + subparsers, + "device", + description=Device.description, + ) + device_subparsers = devicep.add_subparsers(title="Device Subcommands") + ExpireDevice.add_arguments(devicep, device_subparsers) + ListDevice.add_arguments(devicep, device_subparsers) + RemoveDevice.add_arguments(devicep, device_subparsers) + ShowDevice.add_arguments(devicep, device_subparsers) + StatsDevice.add_arguments(devicep, device_subparsers) diff --git a/Products/ZenCollector/configcache/cli/expire.py b/Products/ZenCollector/configcache/cli/expire.py index bce2b6fd39..97d7d798ee 100644 --- a/Products/ZenCollector/configcache/cli/expire.py +++ b/Products/ZenCollector/configcache/cli/expire.py @@ -20,34 +20,20 @@ from ..app.args import get_subparser from ..cache import ConfigStatus, DeviceQuery -from .args import get_common_parser +from .args import get_devargs_parser from ._selection import get_message, confirm -class Expire(object): - description = "Mark configurations as expired" - - @staticmethod - def add_arguments(parser, subparsers): - listp = get_subparser( - subparsers, - "expire", - description=Expire.description, - ) - expire_subparsers = listp.add_subparsers(title="Expire Subcommands") - ExpireDevices.add_arguments(listp, expire_subparsers) - ExpireOidMap.add_arguments(listp, expire_subparsers) - - class ExpireOidMap(object): - configs = (("expire.zcml", __name__),) + description = "Mark OID Map as expired" + configs = (("store.zcml", __name__),) @staticmethod def add_arguments(parser, subparsers): subp = get_subparser( subparsers, - "oidmap", - description="Expire oidmap configuration", + "expire", + description=ExpireOidMap.description, ) subp.set_defaults(factory=ExpireOidMap) @@ -66,18 +52,19 @@ def run(self): print("Oidmap configuration already expired") -class ExpireDevices(object): - configs = (("expire.zcml", __name__),) +class ExpireDevice(object): + description = "Mark device configurations as expired" + configs = (("store.zcml", __name__),) @staticmethod def add_arguments(parser, subparsers): subp = get_subparser( subparsers, - "device", - description="Expire device configurations", - parent=get_common_parser(), + "expire", + description=ExpireDevice.description, + parent=get_devargs_parser(), ) - subp.set_defaults(factory=ExpireDevices) + subp.set_defaults(factory=ExpireDevice) def __init__(self, args): self._monitor = args.collector diff --git a/Products/ZenCollector/configcache/cli/list.py b/Products/ZenCollector/configcache/cli/list.py index 65f26adc6c..04278cb560 100644 --- a/Products/ZenCollector/configcache/cli/list.py +++ b/Products/ZenCollector/configcache/cli/list.py @@ -24,91 +24,30 @@ from ..app import initialize_environment from ..app.args import get_subparser -from ..cache import DeviceQuery, ConfigStatus +from ..cache import ConfigStatus, DeviceQuery -from .args import get_common_parser, MultiChoice +from .args import get_devargs_parser, MultiChoice -class List_(object): - description = "List configurations" +class ListDevice(object): + configs = (("store.zcml", __name__),) @staticmethod def add_arguments(parser, subparsers): listp = get_subparser( subparsers, "list", - description=List_.description, - ) - list_subparsers = listp.add_subparsers(title="List Subcommands") - ListDevices.add_arguments(listp, list_subparsers) - ListOidMap.add_arguments(listp, list_subparsers) - - -class ListOidMap(object): - description = "List the oidmap configuration" - configs = (("list.zcml", __name__),) - - @staticmethod - def add_arguments(parser, subparsers): - devicep = get_subparser( - subparsers, - "oidmap", - description=ListOidMap.description, - ) - devicep.set_defaults(factory=ListOidMap) - - def __init__(self, args): - pass - - def run(self): - initialize_environment(configs=self.configs, useZope=False) - client = getRedisClient(url=getRedisUrl()) - store = createObject("oidmapcache-store", client) - status = store.get_status() - if status is None: - print("No oidmap found in the cache.") - return - - hdr_tmplt = "{0:{3}} {1:^{4}} {2:^{5}}" - row_tmplt = "{0:{3}} {1:{4}} {2:>{5}}" - - headings = ("STATUS", "LAST CHANGE", "AGE") - status_text = _format_status(status) - ts = attr.astuple(status)[-1] - ts_text = _format_date(ts) - now = time.time() - age_text = _format_timedelta(now - ts) - row = (status_text, ts_text, age_text) - - maxs, maxt, maxa = 1, 1, 1 - maxs = max(maxs, len(status_text)) - maxt = max(maxt, len(ts_text)) - maxa = max(maxa, len(age_text)) - widths = (maxs, maxt, maxa) - - print(hdr_tmplt.format(*chain(headings, widths))) - print(row_tmplt.format(*chain(row, widths))) - - -class ListDevices(object): - configs = (("list.zcml", __name__),) - - @staticmethod - def add_arguments(parser, subparsers): - devicep = get_subparser( - subparsers, - "device", description="List device configurations", - parent=get_common_parser(), + parent=get_devargs_parser(), ) - devicep.add_argument( + listp.add_argument( "-u", dest="show_uid", default=False, action="store_true", help="Display ZODB path for device", ) - devicep.add_argument( + listp.add_argument( "-f", dest="states", action=MultiChoice, @@ -117,7 +56,7 @@ def add_arguments(parser, subparsers): help="Only list configurations having these states. One or " "more states may be specified, separated by commas.", ) - devicep.set_defaults(factory=ListDevices) + listp.set_defaults(factory=ListDevice) def __init__(self, args): self._monitor = "*{}*".format(args.collector).replace("***", "*") diff --git a/Products/ZenCollector/configcache/cli/list.zcml b/Products/ZenCollector/configcache/cli/list.zcml deleted file mode 100644 index 8ec2993701..0000000000 --- a/Products/ZenCollector/configcache/cli/list.zcml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - diff --git a/Products/ZenCollector/configcache/cli/oidmap.py b/Products/ZenCollector/configcache/cli/oidmap.py new file mode 100644 index 0000000000..b178134837 --- /dev/null +++ b/Products/ZenCollector/configcache/cli/oidmap.py @@ -0,0 +1,32 @@ +############################################################################## +# +# Copyright (C) Zenoss, Inc. 2024, all rights reserved. +# +# This content is made available according to terms specified in +# License.zenoss under the directory where your Zenoss product is installed. +# +############################################################################## + +from __future__ import absolute_import, print_function + +from ..app.args import get_subparser + +from .expire import ExpireOidMap +from .show import ShowOidMap +from .stats import StatsOidMap + + +class OidMap(object): + description = "Manage the OID Map cache" + + @staticmethod + def add_arguments(parser, subparsers): + oidmapp = get_subparser( + subparsers, + "oidmap", + description=OidMap.description, + ) + oidmap_subparsers = oidmapp.add_subparsers(title="OidMap Subcommands") + ExpireOidMap.add_arguments(oidmapp, oidmap_subparsers) + ShowOidMap.add_arguments(oidmapp, oidmap_subparsers) + StatsOidMap.add_arguments(oidmapp, oidmap_subparsers) diff --git a/Products/ZenCollector/configcache/cli/remove.py b/Products/ZenCollector/configcache/cli/remove.py index 398ba59700..8ed61b0da2 100644 --- a/Products/ZenCollector/configcache/cli/remove.py +++ b/Products/ZenCollector/configcache/cli/remove.py @@ -19,29 +19,14 @@ from ..app.args import get_subparser from ..cache import DeviceQuery -from .args import get_common_parser +from .args import get_devargs_parser from ._selection import get_message, confirm -class Remove(object): - description = "Mark configurations as expired" - - @staticmethod - def add_arguments(parser, subparsers): - removep = get_subparser( - subparsers, - "remove", - description=Remove.description, - ) - remove_subparsers = removep.add_subparsers(title="Remove Subcommands") - RemoveDevices.add_arguments(removep, remove_subparsers) - RemoveOidMap.add_arguments(removep, remove_subparsers) - - class RemoveOidMap(object): description = "Remove oidmap configuration from the cache" - configs = (("remove.zcml", __name__),) + configs = (("store.zcml", __name__),) @staticmethod def add_arguments(parser, subparsers): @@ -67,20 +52,20 @@ def run(self): print("Oidmap configuration removed from the cache") -class RemoveDevices(object): +class RemoveDevice(object): description = "Delete device configurations from the cache" - configs = (("remove.zcml", __name__),) + configs = (("store.zcml", __name__),) @staticmethod def add_arguments(parser, subparsers): subp = get_subparser( subparsers, - "device", - description=RemoveDevices.description, - parent=get_common_parser(), + "remove", + description=RemoveDevice.description, + parent=get_devargs_parser(), ) - subp.set_defaults(factory=RemoveDevices) + subp.set_defaults(factory=RemoveDevice) def __init__(self, args): self._monitor = args.collector diff --git a/Products/ZenCollector/configcache/cli/remove.zcml b/Products/ZenCollector/configcache/cli/remove.zcml deleted file mode 100644 index 8ec2993701..0000000000 --- a/Products/ZenCollector/configcache/cli/remove.zcml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - diff --git a/Products/ZenCollector/configcache/cli/show.py b/Products/ZenCollector/configcache/cli/show.py index 55d9116abb..57e50c415c 100644 --- a/Products/ZenCollector/configcache/cli/show.py +++ b/Products/ZenCollector/configcache/cli/show.py @@ -25,30 +25,15 @@ from ..cache import DeviceQuery -class Show(object): - description = "Show a configuration" - - @staticmethod - def add_arguments(parser, subparsers): - showp = get_subparser( - subparsers, - "show", - description=Show.description, - ) - show_subparsers = showp.add_subparsers(title="Show Subcommands") - ShowDevice.add_arguments(showp, show_subparsers) - ShowOidMap.add_arguments(showp, show_subparsers) - - class ShowOidMap(object): description = "Show the oidmap configuration" - configs = (("show.zcml", __name__),) + configs = (("store.zcml", __name__),) @staticmethod def add_arguments(parser, subparsers): subp = get_subparser( subparsers, - "oidmap", + "show", description=ShowOidMap.description, ) termsize = get_terminal_size() @@ -90,12 +75,12 @@ def run(self): class ShowDevice(object): description = "Show a device configuration" - configs = (("show.zcml", __name__),) + configs = (("store.zcml", __name__),) @staticmethod def add_arguments(parser, subparsers): subp = get_subparser( - subparsers, "device", description=ShowDevice.description + subparsers, "show", description=ShowDevice.description ) termsize = get_terminal_size() subp.add_argument( diff --git a/Products/ZenCollector/configcache/cli/show.zcml b/Products/ZenCollector/configcache/cli/show.zcml deleted file mode 100644 index 8ec2993701..0000000000 --- a/Products/ZenCollector/configcache/cli/show.zcml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - diff --git a/Products/ZenCollector/configcache/cli/stats.py b/Products/ZenCollector/configcache/cli/stats.py index 295439b19d..74d665c97b 100644 --- a/Products/ZenCollector/configcache/cli/stats.py +++ b/Products/ZenCollector/configcache/cli/stats.py @@ -23,7 +23,7 @@ from ..app.args import get_subparser from ..cache import DeviceQuery -from .args import get_common_parser, MultiChoice +from .args import get_devargs_parser, MultiChoice from ._tables import TablesOutput, _xform from ._json import JSONOutput from ._stats import ( @@ -37,30 +37,15 @@ from ._groups import DeviceGroup, ServiceGroup, MonitorGroup, StatusGroup -class Stats(object): - description = "Show statistics about the configurations" - - @staticmethod - def add_arguments(parser, subparsers): - statsp = get_subparser( - subparsers, - "stats", - description=Stats.description, - ) - show_subparsers = statsp.add_subparsers(title="Stats Subcommands") - StatsDevices.add_arguments(statsp, show_subparsers) - StatsOidMap.add_arguments(statsp, show_subparsers) - - class StatsOidMap(object): description = "Show the statistics of the oidmap configuration" - configs = (("stats.zcml", __name__),) + configs = (("store.zcml", __name__),) @staticmethod def add_arguments(parser, subparsers): subp = get_subparser( subparsers, - "oidmap", + "stats", description=StatsOidMap.description, ) subp.set_defaults(factory=StatsOidMap) @@ -91,9 +76,9 @@ def run(self): print("Status Age: {}".format(_xform(age, "timedelta"))) -class StatsDevices(object): +class StatsDevice(object): description = "Show statistics about the device configurations" - configs = (("stats.zcml", __name__),) + configs = (("store.zcml", __name__),) _groups = ("collector", "device", "service", "status") _statistics = ("count", "avg_age", "median_age", "min_age", "max_age") @@ -102,15 +87,15 @@ class StatsDevices(object): def add_arguments(parser, subparsers): subp = get_subparser( subparsers, - "device", - StatsDevices.description, - parent=get_common_parser(), + "stats", + StatsDevice.description, + parent=get_devargs_parser(), ) subp.add_argument( "-S", dest="statistic", action=MultiChoice, - choices=StatsDevices._statistics, + choices=StatsDevice._statistics, default=argparse.SUPPRESS, help="Specify the statistics to return. One or more statistics " "may be specified (comma separated). By default, all " @@ -120,7 +105,7 @@ def add_arguments(parser, subparsers): "-G", dest="group", action=MultiChoice, - choices=StatsDevices._groups, + choices=StatsDevice._groups, default=argparse.SUPPRESS, help="Specify the statistics groupings to return. One or more " "groupings may be specified (comma separated). By default, all " @@ -133,11 +118,11 @@ def add_arguments(parser, subparsers): default="tables", help="Output statistics in the specified format", ) - subp.set_defaults(factory=StatsDevices) + subp.set_defaults(factory=StatsDevice) def __init__(self, args): stats = [] - for statId in getattr(args, "statistic", StatsDevices._statistics): + for statId in getattr(args, "statistic", StatsDevice._statistics): if statId == "count": stats.append(CountStat) elif statId == "avg_age": @@ -149,7 +134,7 @@ def __init__(self, args): elif statId == "max_age": stats.append(MaxAgeStat) self._groups = [] - for groupId in getattr(args, "group", StatsDevices._groups): + for groupId in getattr(args, "group", StatsDevice._groups): if groupId == "collector": self._groups.append(MonitorGroup(stats)) elif groupId == "device": diff --git a/Products/ZenCollector/configcache/cli/stats.zcml b/Products/ZenCollector/configcache/cli/stats.zcml deleted file mode 100644 index 8ec2993701..0000000000 --- a/Products/ZenCollector/configcache/cli/stats.zcml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - diff --git a/Products/ZenCollector/configcache/cli/expire.zcml b/Products/ZenCollector/configcache/cli/store.zcml similarity index 100% rename from Products/ZenCollector/configcache/cli/expire.zcml rename to Products/ZenCollector/configcache/cli/store.zcml diff --git a/Products/ZenCollector/configcache/configcache.py b/Products/ZenCollector/configcache/configcache.py index 8ce4764ea8..49aff85161 100644 --- a/Products/ZenCollector/configcache/configcache.py +++ b/Products/ZenCollector/configcache/configcache.py @@ -10,7 +10,7 @@ from __future__ import absolute_import, print_function from .app.args import get_arg_parser -from .cli import Expire, List_, Remove, Show, Stats +from .cli import OidMap, Device from .invalidator import Invalidator from .manager import Manager from .version import Version @@ -24,11 +24,8 @@ def main(argv=None): Version.add_arguments(parser, subparsers) Manager.add_arguments(parser, subparsers) Invalidator.add_arguments(parser, subparsers) - Expire.add_arguments(parser, subparsers) - List_.add_arguments(parser, subparsers) - Remove.add_arguments(parser, subparsers) - Show.add_arguments(parser, subparsers) - Stats.add_arguments(parser, subparsers) + OidMap.add_arguments(parser, subparsers) + Device.add_arguments(parser, subparsers) args = parser.parse_args() args.factory(args).run()