-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_influx.py
138 lines (121 loc) · 3.75 KB
/
push_influx.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
import subprocess
import json
from influxdb import InfluxDBClient
import re
#Connect to InfluxDB and drop the old data.
influxdb_ip = '10.1.1.1'
username = 'agent'
password = '@g1nt'
database = 'advertisement'
start = InfluxDBClient(influxdb-ip, 8086, username,password)
start.query('drop database ' + database)
start.query('create database '+ database)
client = InfluxDBClient(influxdb-ip, 8086, username,password, database)
# Juniper MX
# List all files in the directory.
process = subprocess.Popen(['ls', 'adv_juniper'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
file_list = stdout.decode('utf-8').split("\n")
file_list.pop()
#Write data in the database, advetisement.
for file in file_list:
# Load the output of show route advertising-protocol bgp ___ table ___
with open("adv_json/"+file) as f:
data = json.load(f)
if len(list(data["route-information"][0].keys())) != 0:
# Get the advertising prefixes.
prefixes = data["route-information"][0]["route-table"][0]["rt"]
for each in prefixes:
#print(each["rt-destination"][0]["data"])
#print(each["rt-entry"][0]["as-path"][0]["data"])
prefix=each["rt-destination"][0]["data"]
aspath=each["rt-entry"][0]["as-path"][0]["data"]
#Data normalization
x=aspath.split()
if "[131618]" in x:
i = x.index("[131618]")
x.pop(i)
elif "[7483]" in x:
i = x.index("[7483]")
x.pop(i)
result = " ".join(x)
#print(result)
#Ignore /32 prefix
#Generate InfulxDB data
if re.search("/32",prefix):
continue
else:
data = [
{
"measurement" : prefix,
"fields": {
"Circuit": file,
"AS Prepend": result
}
}
]
#print(data)
#Write Data to DB.
client.write_points(data)
else:
continue
#IOS XR
# List all files in the directory.
process = subprocess.Popen(['ls', 'adv_xr'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
file_list = stdout.decode('utf-8').split("\n")
file_list.pop()
#Write data in the database, advetisement.
for file in file_list:
# Load the output of show bgp neighbor ___ advertised-routes
with open("adv_xr/"+file) as f:
x = f.readlines()
prefix = []
aspath = []
#Normalize the output and get only prefix and AS path
for each in x:
l = each.strip().split(" ")
while("" in l):
l.remove("")
if 'Aggregate' not in l:
l.insert(3,'Aggregate')
prefix.append(l[0])
aspath.append(" ".join(l[4:]))
#Ignore empty file
try:
prefix.pop(0)
prefix.pop()
prefix.pop()
aspath.pop(0)
aspath.pop()
aspath.pop()
except:
continue
#Align with the output of Juniper
for each in aspath:
i = aspath.index(each)
aspath[i] = re.sub ('(.*)(i)',r'\1 I',each)
d = {}
#Generate InfulxDB data
for x in prefix:
i = prefix.index(x)
#Ignore /32 prefix
if re.search("/32",x):
continue
else:
data = [
{
"measurement" : x,
"fields": {
"Circuit": file,
"AS Prepend": aspath[i]
}
}
]
#print(data)
#Write Data to DB.
client.write_points(data)