diff --git a/files/scripts/updates-uptime-cmd.py b/files/scripts/updates-uptime-cmd.py index 477bec87f6..7398e70331 100755 --- a/files/scripts/updates-uptime-cmd.py +++ b/files/scripts/updates-uptime-cmd.py @@ -97,6 +97,9 @@ conf_fast_width_history = True # at their uptime. Only for the main file. Assume they are still up etc. conf_dynamic_main_uptime = True +# How old to consider when machines were created. +conf_created_dur_def = 0 # Forever + # Hosts that we'll show info. for, by default. info/host cmds. conf_important_hosts = ["batcave*", "bastion01*", "noc*"] @@ -913,6 +916,7 @@ def _usage(short=False): """ % (prog,), end='') if short: print("""\ + created [duration] [host*]... diff/-u [backup1] [backup2] help history @@ -1444,6 +1448,67 @@ def _cmd_uptime(args): _max_update_correct('') _print_lines('', data) +def _cmd_created(args): + age = 0 + if args.dur > 0: + age = args.dur + + hosts = [] + if hasattr(args, 'hosts'): + hosts = args.hosts[:] + + data = fname1() + if age > 0: # If a host has been running longer, then ignore it + data = filter_uptime_max_datas(data, age) + if hosts: + data = filter_name_datas(data, hosts) + + hn2h = {} # hostnames to host ... for machine ids + for host in data: + hn2h[host.name] = host + + hbackups = list(reversed(backups)) + if len(hbackups) > 1 and _backup_today_identical(): + hbackups = hbackups[1:] + + rei = set() # (re)installed hosts... + dur = 0 + for backup in hbackups: + if age > 0 and age < dur: + break + dur += 60*60*24 # Broken, but mostly works. Do backups daily, or mtime. + + data = lines2datas(bfname2lines(backup)) + data = list(sorted(data)) + + # If a machine is missing from an older backup, it's marked as being + # (re)installed ... even though it might appear in an even older + # backup with the same machine_id. It is what it is. + dnames = set(x.name for x in data) + for hostname in hn2h: + if hostname not in dnames: + rei.add(hostname) + + for host in data: + if host.name not in hn2h: # Ignore + continue + if host.name in rei: # Most recent (re)install + continue + + if hn2h[host.name].machine_id != host.machine_id: + rei.add(host.name) + else: # Show the oldest date... + hn2h[host.name].date = host.date + + data = [] + for hostname in rei: + data.append(hn2h[hostname]) + data = sorted(data) + + _max_update(data) + _max_update_correct('') + _print_lines('', data) + def _diff_hosts(data1, data2, show_both=False, show_utf8=True, skip_eq=False): pdata = None while len(data1) > 0 or len(data2) > 0: @@ -1681,6 +1746,17 @@ def _cmd_help(args): prog = os.path.basename(sys.argv[0]) if not args.hcmd: _usage() + elif args.hcmd in ("created",): + print(f"""\ + Usage: {prog} {args.hcmd} [duration] [host*] + + See when machines were (re)installed, can be filtered for creation <= duration. + + Easy way to see new machines. + + Eg. {prog} {args.hcmd} 0 *-test* + {prog} {args.hcmd} 5w vmhost* bvmhost* buildhw* +""", end='') elif args.hcmd in ("diff", "diff-u"): print(f"""\ Usage: {prog} diff [backup1] [backup2] @@ -1926,6 +2002,12 @@ def _main(): # -- Start of the real commands... + cmd = subparsers.add_parser("created", 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)") + __defs(func=_cmd_created) + # diff/diff-u commands cmd = subparsers.add_parser("diff", aliases=['diff-u'], help="diff") cmd.add_argument("hists", nargs='*', type=_cmdline_arg_hist, help="history file")