-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraveyard2rst.py
executable file
·116 lines (84 loc) · 2.7 KB
/
graveyard2rst.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
#!/usr/bin/python3
# -*- coding: utf-8; -*-
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2018-2020, Sven Eckelmann <[email protected]>
import datetime
import os
import os.path
import pickle
import pprint
import sys
def load_state(path):
try:
return pickle.load(open(path, 'rb'))
except FileNotFoundError:
return dict()
def usage():
prog = 'graveyard2rst.py'
if len(sys.argv) > 0:
prog = sys.argv[0]
print('%s DBPATH OUT_RST' % (prog))
def graveyard2zones(graveyard):
zones = {}
old_node_timelimit = datetime.datetime.utcnow() - datetime.timedelta(14)
for node_id in graveyard:
node = graveyard[node_id]
node['id'] = node_id
if old_node_timelimit < node['lastseen']:
continue
year = node['lastseen'].year
if year not in zones:
zones[year] = {}
month = node['lastseen'].month
if month not in zones[year]:
zones[year][month] = []
zones[year][month].append(node)
for year in zones:
for month in zones[year]:
days = zones[year][month]
days.sort(reverse=True,
key=lambda node: (node['lastseen'].date(),
node['firstseen'].date()))
return zones
def write_zones(f, zones):
years = list(zones.keys())
years.sort(reverse=True)
f.write("=========\n")
f.write("Graveyard\n")
f.write("=========\n")
for year in years:
f.write("\n%u\n" % year)
f.write("%s\n" % ("=" * len(str(year))))
months = list(zones[year].keys())
months.sort(reverse=True)
for month in months:
title = "%u-%02u" % (year, month)
f.write("\n%s\n" % (title))
f.write("%s\n\n" % ("-" * len(title)))
for node in zones[year][month]:
f.write("* %s\n" % node['hostname'])
f.write(" - id: %s\n" % node['id'])
born = node['firstseen'].strftime('%Y-%m-%d')
died = node['lastseen'].strftime('%Y-%m-%d')
f.write(" - lifespan: %s - %s\n" % (born, died))
f.write("\n")
def main():
if len(sys.argv) != 3:
usage()
sys.exit(1)
dbpath = sys.argv[1]
feed_out = sys.argv[2]
graveyard_path = os.path.join(dbpath, "graveyard.pickle")
feed_outtmp = feed_out + '.tmp'
# load
graveyard = load_state(graveyard_path)
# data crunching
zones = graveyard2zones(graveyard)
# save
with open(feed_outtmp, 'w') as f:
write_zones(f, zones)
f.flush()
os.fsync(f.fileno())
os.rename(feed_outtmp, feed_out)
if __name__ == "__main__":
main()