-
Notifications
You must be signed in to change notification settings - Fork 15
/
runbook.py
68 lines (56 loc) · 1.7 KB
/
runbook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
"""
Author: Nick Russo
Purpose: Main entrypoint for narc program.
"""
import argparse
import sys
from nornir import InitNornir
from narc.tasks import run_checks
from narc.processors import ProcTerse, ProcCSV, ProcJSON
def main(args):
"""
Execution begins here.
"""
# Initialize nornir using default configuration settings
init_nornir = InitNornir()
nornir = init_nornir.with_processors([ProcTerse(), ProcCSV(), ProcJSON()])
# Execute the "run_checks" task to get started, passing in CLI args
aresult = nornir.run(task=run_checks, args=args)
# Handle failed checks by printing them out and exiting with rc=1
failed = False
for host, mresult in aresult.items():
if mresult[0].result:
print(f"{host} error: at least one check is invalid")
for chk in mresult[0].result:
name = chk.get("id", "no_id")
print(f"{host[:12]:<12} {name[:24]:<24} -> {chk['reason']}")
failed = True
if failed:
sys.exit(1)
def _process_args():
"""
Process command line arguments according to README.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"-f",
"--failonly",
help="print failures (ie, undesirable results) only",
action="store_true",
)
parser.add_argument(
"-d",
"--dryrun",
help="run offline system test (no devices needed)",
action="store_true",
)
parser.add_argument(
"-s",
"--status",
help="log timestamped status messages during runtime",
action="store_true",
)
return parser.parse_args()
if __name__ == "__main__":
main(_process_args())