forked from sthierolf/network-automation-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetbox-librenms-graph.py
More file actions
63 lines (53 loc) · 1.52 KB
/
Copy pathnetbox-librenms-graph.py
File metadata and controls
63 lines (53 loc) · 1.52 KB
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
#!/usr/bin/python3
# This file is copyright under the latest version of the EUPL.
# Please see LICENSE file for your rights under this license.
import requests
import cgi
import cgitb
import time
import urllib.parse
import sys
#
# LibreNMS HOST and API token
#
librenms_host = 'https://LIBRENMS_HOST'
token = 'LIBRENMS_API_TOKEN'
#
# get form data
#
cgitb.enable()
form = cgi.FieldStorage()
if "device" in form and "interface" in form:
timefrom = str(int(time.time()) - 28800)
device = form.getvalue('device')
interface = form.getvalue('interface')
for i in range(0,9):
device = device.replace(':' + str(i), '')
device = urllib.parse.quote(device, safe='')
interface = urllib.parse.quote(interface, safe='')
url = librenms_host + '/api/v0/devices/' + device + '/ports/' + interface + '/port_bits?width=780&height=200&from=' + timefrom
headers = {'X-Auth-Token': token}
r = requests.get(url, headers = headers)
#
# if status code is 200, then print interface graph
#
if r.status_code == requests.codes.ok:
#
# LibreNMS must return something like an ID
# If there is no ID, then just list all devices
#
print ("Content-Type: image/png")
print ()
sys.stdout.flush()
sys.stdout.buffer.write(r.content)
else:
print ('Content-Type: text/html')
print ()
print ('error')
#
# handle all other errors by printing out error
#
else:
print ('Content-Type: text/html')
print ()
print ('error')