-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmd_host_info.py
executable file
·161 lines (147 loc) · 5.06 KB
/
md_host_info.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python3
# vim: set fileencoding=utf-8 :
r"""Create markdown table of host information:
sudo salt \* saltutil.sync_grains saltenv=${USER}
sudo salt --out yaml \* grains.item saltenv=${USER} debian_version \
lsb_distrib_codename meta-data:public-ipv4 fqdn_ip4 saltversion \
uptime_days \
| bin/md_host_info.py
"""
# Standard library
# Standard Libary
import datetime
import sys
# Third-party
import yaml
def format_columns(rows, sep=None, align=None):
"""Convert a list (rows) of lists (columns) to a formatted list of lines.
When joined with newlines and printed, the output is similar to
`column -t`.
The optional align may be a list of alignment formatters.
The last (right-most) column will not have any trailing whitespace so that
it wraps as cleanly as possible.
Based on MIT licensed:
https://github.com/ClockworkNet/OpScripts/blob/master/opscripts/utils/v8.py
Based on solution provided by antak in http://stackoverflow.com/a/12065663
"""
lines = list()
if sep is None:
sep = " "
widths = [max(map(len, map(str, col))) for col in zip(*rows)]
for row in rows:
formatted = list()
last_col = len(row) - 1
for i, col in enumerate(row):
# right alighed
if align and align[i].lower() in (">", "r"):
formatted.append(str(col).rjust(widths[i]))
# center aligned
elif align and align[i].lower() in ("^", "c"):
col_formatted = str(col).center(widths[i])
if i == last_col:
col_formatted = col_formatted.rstrip()
formatted.append(col_formatted)
# left aligned
else:
if i == last_col:
formatted.append(str(col))
else:
formatted.append(str(col).ljust(widths[i]))
lines.append("| {} |".format(sep.join(formatted)))
return lines
def main():
now = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
data = yaml.safe_load(sys.stdin)
print()
align = ["l", "c", "l", "l", "l", "r"]
rows = list()
rows.append(
[
"Host (Salt Minion ID)",
"Public IP",
"OS Rel",
"OS Code",
"Salt Ver",
"Days Up",
]
)
rows.append(
[
"---------------------",
":-------:",
"------",
"-------",
"--------",
"------:",
]
)
for host in sorted(data.keys()):
grains = data[host]
uptime = int(grains["uptime_days"])
if uptime <= 30:
uptime_desc = "0 - 30"
elif 30 < uptime <= 60:
uptime_desc = "30 - 60"
elif 60 < uptime <= 90:
uptime_desc = "60 - 90"
elif 90 < uptime <= 120:
uptime_desc = "90 - 120"
elif 120 < uptime <= 180:
uptime_desc = ":warning: **120 - 180**"
elif 180 < uptime <= 270:
uptime_desc = ":warning: **180 - 270**"
elif 270 < uptime <= 360:
uptime_desc = ":warning: **270 - 360**"
else:
uptime_desc = ":warning: **360+**"
if "meta-data:public-ipv4" in grains:
aws_ip = grains["meta-data:public-ipv4"]
else:
aws_ip = False
if "fqdn_ip4" in grains:
fqdn_ips = grains["fqdn_ip4"]
else:
fqdn_ips = False
if aws_ip:
ip_f = aws_ip
elif fqdn_ips and fqdn_ips[0] and fqdn_ips[0] != "127.0.1.1":
ip_f = fqdn_ips[0]
else:
ip_f = ""
host_f = "`{}`".format(host)
os_rel = grains["debian_version"]
os_code = grains["lsb_distrib_codename"].title()
# Compensate for poor support of Debian by Salt
if not os_code and os_rel.startswith("12."):
os_code = "Bookworm"
elif not os_code and os_rel.startswith("11."):
os_code = "Bullseye"
salt_f = "{}".format(grains["saltversion"])
if grains == "Minion did not return. [Not connected]":
print(host, "| *N/A* | *Not connected*")
rows.append(
[host_f, "*N/A*", "*N/A*", "Not connected", "*N/A*" "*N/A*"]
)
else:
rows.append([host_f, ip_f, os_rel, os_code, salt_f, uptime_desc])
print("\n".join(format_columns(rows, " | ", align=align)))
print()
print("- **`{}`** total hosts".format(len(rows) - 2))
print("- All hosts are running the Debian GNU/Linux operating system (OS)")
print("- Generated {} via:".format(now))
print(" ```shell")
print(" sudo salt \\* saltutil.sync_grains saltenv=${USER}")
print(
" sudo salt --out yaml \\* grains.item saltenv=${USER}"
" debian_version \\"
)
print(
" lsb_distrib_codename meta-data:public-ipv4 fqdn_ip4"
" saltversion \\"
)
print(" uptime_days \\")
print(" | bin/md_host_info.py")
print(" ```")
print()
if __name__ == "__main__":
main()