1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
import sys |
5
|
|
|
import os |
6
|
|
|
|
7
|
|
|
from kytos.core.db import Mongo |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
def unset_links(mongo: Mongo) -> None: |
11
|
|
|
"""Unset links""" |
12
|
|
|
print( |
13
|
|
|
"Trying to $unset links 'active' and metadata[last_status_is_active|last_status_change|notified_up_at]..." |
14
|
|
|
) |
15
|
|
|
db = mongo.client[mongo.db_name] |
16
|
|
|
res = db.links.update_many( |
17
|
|
|
{}, |
18
|
|
|
{ |
19
|
|
|
"$unset": { |
20
|
|
|
"active": 1, |
21
|
|
|
"metadata.last_status_is_active": 1, |
22
|
|
|
"metadata.last_status_change": 1, |
23
|
|
|
"metadata.notified_up_at": 1, |
24
|
|
|
} |
25
|
|
|
}, |
26
|
|
|
) |
27
|
|
|
print(f"Modified {res.modified_count} links objects") |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def aggregate_unset_links(mongo: Mongo) -> None: |
31
|
|
|
"""Aggregate unset links.""" |
32
|
|
|
db = mongo.client[mongo.db_name] |
33
|
|
|
res = db.links.aggregate( |
34
|
|
|
[ |
35
|
|
|
{ |
36
|
|
|
"$unset": [ |
37
|
|
|
"active", |
38
|
|
|
"metadata.last_status_is_active", |
39
|
|
|
"metadata.last_status_change", |
40
|
|
|
"metadata.notified_up_at", |
41
|
|
|
] |
42
|
|
|
} |
43
|
|
|
] |
44
|
|
|
) |
45
|
|
|
print( |
46
|
|
|
"Aggregating links $unset active and metadata[last_status_is_active|last_status_change|notified_up_at]" |
47
|
|
|
) |
48
|
|
|
for doc in res: |
49
|
|
|
print(doc) |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
def unset_switches_and_intfs(mongo: Mongo) -> None: |
53
|
|
|
"""Unset switches and interfaces""" |
54
|
|
|
print("Trying to $unset switches and interfaces 'active'") |
55
|
|
|
db = mongo.client[mongo.db_name] |
56
|
|
|
res = db.switches.update_many( |
57
|
|
|
{}, |
58
|
|
|
{ |
59
|
|
|
"$unset": { |
60
|
|
|
"active": 1, |
61
|
|
|
"interfaces.$[].active": 1, |
62
|
|
|
} |
63
|
|
|
}, |
64
|
|
|
) |
65
|
|
|
print(f"Modified {res.modified_count} switches objects") |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
def aggregate_unset_switches_and_intfs(mongo: Mongo) -> None: |
69
|
|
|
"""Aggregate unset switches and interfaces.""" |
70
|
|
|
db = mongo.client[mongo.db_name] |
71
|
|
|
res = db.switches.aggregate( |
72
|
|
|
[ |
73
|
|
|
{ |
74
|
|
|
"$unset": [ |
75
|
|
|
"active", |
76
|
|
|
"interfaces.active", |
77
|
|
|
] |
78
|
|
|
} |
79
|
|
|
] |
80
|
|
|
) |
81
|
|
|
print( |
82
|
|
|
"Aggregating switches and interfaces $unset active" |
83
|
|
|
) |
84
|
|
|
for doc in res: |
85
|
|
|
print(doc) |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
if __name__ == "__main__": |
89
|
|
|
mongo = Mongo() |
90
|
|
|
cmds = { |
91
|
|
|
"aggregate_unset_links": aggregate_unset_links, |
92
|
|
|
"unset_links": unset_links, |
93
|
|
|
"aggregate_unset_switches_and_intfs": aggregate_unset_switches_and_intfs, |
94
|
|
|
"unset_switches_and_intfs": unset_switches_and_intfs, |
95
|
|
|
} |
96
|
|
|
try: |
97
|
|
|
cmd = os.environ["CMD"] |
98
|
|
|
cmds[cmd](mongo) |
99
|
|
|
except KeyError: |
100
|
|
|
print( |
101
|
|
|
f"Please set the 'CMD' env var. \nIt has to be one of these: {list(cmds.keys())}" |
102
|
|
|
) |
103
|
|
|
sys.exit(1) |
104
|
|
|
|