-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathevent-automation.py
116 lines (87 loc) · 3.56 KB
/
event-automation.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
import requests
from prefect import flow, task
from prefect.blocks.system import Secret
@task(retries=3, log_prints=True)
def get_nautobot_intf_id(device: str, interface: str) -> str | None:
"""Retrieve the Nautobot Interface ID."""
# GraphQL query to retrieve the device interfaces information
gql = """
query($device: [String]) {
devices(name: $device) {
name
interfaces {
name
id
}
}
}
"""
# Retrieve the Nautobot API token from Prefect Block Secret
secret_block = Secret.load("nautobot-token")
nautobot_token = secret_block.get()
# Get the device information from Nautobot using GraphQL
response = requests.post(
url="http://localhost:8080/api/graphql/",
headers={"Authorization": f"Token {nautobot_token}"},
json={"query": gql, "variables": {"device": device}},
)
response.raise_for_status()
# Parse the response and return the interface ID
result = response.json()["data"]["devices"][0]
for intf in result["interfaces"]:
if intf["name"] == interface:
return intf["id"]
@task(retries=3, log_prints=True)
def update_nautobot_intf_state(interface_id: str, status: str) -> bool:
"""Update the device interface status."""
# Retrieve the Nautobot API token from Prefect Block Secret
secret_block = Secret.load("nautobot-token")
nautobot_token = secret_block.get()
# Mapping Alertmanager status to Nautobot status
status = "lab-active" if status == "resolved" else "Alerted"
# Update the interface status
result = requests.patch(
url=f"http://localhost:8080/api/dcim/interfaces/{interface_id}/",
headers={"Authorization": f"Token {nautobot_token}"},
json={"status": status},
)
result.raise_for_status()
# Print the result to console
print(f"Interface ID {interface_id} status updated to {status}")
return True
@flow(log_prints=True)
def interface_flapping_processor(device: str, interface: str, status: str) -> bool:
"""Interface Flapping Event Processor."""
# Retrieve Nautobot Interface ID
intf_id = get_nautobot_intf_id(device=device, interface=interface)
if intf_id is None:
raise ValueError("Interface not found in Nautobot")
# Print the interface ID
print(f"Interface: {interface} == ID: {intf_id}")
# Update the device interface status in Nautobot
is_good = update_nautobot_intf_state(interface_id=intf_id, status=status)
return is_good
@flow(log_prints=True)
def alert_receiver(alert_group: dict):
"""Process the alert."""
# Status of the alert group
status = alert_group["status"]
# Name of the alert group
alertgroup_name = alert_group["groupLabels"]["alertname"]
# Alerts in the alert group
alerts = alert_group["alerts"]
print(f"Received alert group: {alertgroup_name} - {status}")
# Check the subject of the alert and forward to respective workflow
if alertgroup_name == "PeerInterfaceFlapping":
for alert in alerts:
# Run the interface flapping processor
result = interface_flapping_processor(
device=alert["labels"]["device"],
interface=alert["labels"]["interface"],
status=alert_group["status"],
)
# Print the result to console
print(f"Interface Flapping Processor Result: {result}")
print("Alertmanager Alert Group status processed, exiting")
if __name__ == "__main__":
_ = alert_receiver.serve(name="alert-receiver")