update+uptime: Minor tweaks, mainly to do case insensitive matches.

Signed-off-by: James Antill <james@and.org>
This commit is contained in:
James Antill
2025-11-20 14:52:48 -05:00
parent 1b9d8694ed
commit ec0e2b40cd

View File

@@ -249,6 +249,10 @@ def _verscmp_strings(xs):
yield ret.s
def _fnmatchi(path, pat):
""" Simple way to always use case insensitive filename matching. """
return fnmatch.fnmatch(path.lower(), pat.lower())
# Have nice "plain" numbers...
def _ui_int(num):
if conf_num_sep_:
@@ -428,6 +432,7 @@ def _wild_eq(s1, s2):
return True
return s1 == s2
_max_len_osnm = 0 # osname_small
_max_len_osvr = 0 # osvers ... upto the first '.'
class Host():
@@ -662,7 +667,7 @@ def filter_name_datas(datas, names):
for data in datas:
for name in names:
if fnmatch.fnmatch(data.name, name):
if _fnmatchi(data.name, name):
break
else:
continue
@@ -677,20 +682,20 @@ def filter_osname_datas(datas, names):
for data in datas:
for name in names:
if fnmatch.fnmatch(data.osinfo, name):
if _fnmatchi(data.osinfo, name):
break
if fnmatch.fnmatch(data.osinfo_small, name):
if _fnmatchi(data.osinfo_small, name):
break
if fnmatch.fnmatch(data.osname, name):
if _fnmatchi(data.osname, name):
break
if fnmatch.fnmatch(data.osname_small, name):
if _fnmatchi(data.osname_small, name):
break
if fnmatch.fnmatch(data.osvers, name):
if _fnmatchi(data.osvers, name):
break
off = data.osvers.find('.')
if off != -1:
vers = data.osvers[:off]
if fnmatch.fnmatch(vers, name):
if _fnmatchi(vers, name):
break
else:
@@ -701,21 +706,21 @@ def filter_osname_datas(datas, names):
def filter_age_min_datas(datas, age):
now = time.time()
for data in datas:
if (now - data.date_tm) < age:
if age > 0 and (now - data.date_tm) < age:
continue
yield data
# Filter datas using uptime as a minium.
def filter_uptime_min_datas(datas, uptime):
for data in datas:
if data.uptime < uptime:
if uptime > 0 and data.uptime < uptime:
continue
yield data
# Filter datas using uptime as a maximum.
def filter_uptime_max_datas(datas, uptime):
for data in datas:
if data.uptime > uptime:
if uptime > 0 and data.uptime > uptime:
continue
yield data
@@ -1391,7 +1396,7 @@ def _print_info(hosts, data):
fhosts = []
for x in data:
for host in hosts:
if not fnmatch.fnmatch(x.name, host):
if not _fnmatchi(x.name, host):
continue
fhosts.append(x)
break
@@ -1766,6 +1771,8 @@ def _cmdline_arg_ansi(oval):
raise argparse.ArgumentTypeError(f"{oval} is not valid: always/never/auto")
def _cmdline_arg_duration(oval):
if oval == "forever":
return 0
val = parse_duration(oval)
if val is None:
raise argparse.ArgumentTypeError(f"{oval} is not a duration")
@@ -1811,11 +1818,13 @@ def _cmd_help(args):
prog = os.path.basename(sys.argv[0])
if not args.hcmd:
_usage()
elif args.hcmd in ("created",):
elif args.hcmd in ("created", "installed", "reinstalled"):
print(f"""\
Usage: {prog} {args.hcmd} [duration] [host*]
See when machines were (re)installed, can be filtered for creation <= duration.
See when machine_id last changed. This happens when they are created/installed
or reinstalled. Can be filtered for time <= duration. Zero duration means
forever.
Easy way to see new machines.
@@ -1999,7 +2008,7 @@ better for this. Kind of like git blame, but instead of lines it's events.
Eg. {prog} {args.hcmd} 32h
{prog} {args.hcmd} 1d yesterday
""", end='')
elif args.hcmd in ("uptime-max",):
elif args.hcmd in ("uptime-max", "rebooted", "started"):
print(f"""\
Usage: {prog} {args.hcmd} duration [backup]
@@ -2067,7 +2076,8 @@ def _main():
# -- Start of the real commands...
cmd = subparsers.add_parser("created", help="list hosts")
als = ['installed', 'reinstalled']
cmd = subparsers.add_parser("created", aliases=als, help="list hosts")
cmd.add_argument("dur", nargs='?', default=conf_created_dur_def,
type=_cmdline_arg_duration, help="created within duration")
cmd.add_argument("hosts", nargs='*', help="wildcard hostname(s)")
@@ -2150,7 +2160,8 @@ def _main():
__defs(func=_cmd_update)
# uptime/uptime-min/uptime-max commands
cmd = subparsers.add_parser("uptime-max", help="list hosts")
als = ['rebooted','started']
cmd = subparsers.add_parser("uptime-max", aliases=als, help="list hosts")
cmd.add_argument("dur", type=_cmdline_arg_duration,
help="uptime maximum duration")
__defs(func=_cmd_uptime)