Files
LibreQoS/UNMS_Integration.py

85 lines
3.4 KiB
Python
Raw Normal View History

2020-10-03 07:49:09 -06:00
# Copyright (C) 2020 Robert Chacón
# This file is part of LibreQoS.
#
# LibreQoS is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# LibreQoS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LibreQoS. If not, see <http://www.gnu.org/licenses/>.
#
# _ _ _ ___ ____
# | | (_) |__ _ __ ___ / _ \ ___/ ___|
# | | | | '_ \| '__/ _ \ | | |/ _ \___ \
# | |___| | |_) | | | __/ |_| | (_) |__) |
# |_____|_|_.__/|_| \___|\__\_\\___/____/
2020-10-07 04:02:44 -06:00
# v.0.7-alpha
2020-10-03 07:49:09 -06:00
#
import requests
2020-10-04 13:29:16 -06:00
from ispConfig import orgUNMSxAuthToken, unmsBaseURL, deviceModelBlacklistEnabled
2020-10-03 07:49:09 -06:00
#To omit bridged CPEs from shaping
2020-10-04 13:29:16 -06:00
if deviceModelBlacklistEnabled:
deviceModelBlacklist = ['LBE-5AC-Gen2', 'LBE-5AC-Gen2', 'LBE-5AC-LR', 'AF-LTU5', 'AFLTULR', 'AFLTUPro', 'LTU-LITE']
else:
deviceModelBlacklist = []
2020-10-03 07:49:09 -06:00
2020-10-05 00:59:09 -06:00
def pullUNMSDevices():
2020-10-03 07:49:09 -06:00
url = unmsBaseURL + "/nms/api/v2.1/sites?type=client&ucrm=true&ucrmDetails=true"
headers = {'accept':'application/json', 'x-auth-token': orgUNMSxAuthToken}
r = requests.get(url, headers=headers)
jsonData = r.json()
#print(jsonData)
2020-10-05 00:59:09 -06:00
unmsDevicesToImport = []
2020-10-03 07:49:09 -06:00
for unmsClientSite in jsonData:
2020-10-05 01:22:45 -06:00
if (unmsClientSite['identification']['status'] == 'active') and (unmsClientSite['qos']['downloadSpeed']) and (unmsClientSite['qos']['uploadSpeed']):
2020-10-03 08:46:45 -06:00
downloadSpeedMbps = int(round(unmsClientSite['qos']['downloadSpeed']/1000000))
uploadSpeedMbps = int(round(unmsClientSite['qos']['uploadSpeed']/1000000))
address = unmsClientSite['description']['address']
unmsClientSiteID = unmsClientSite['id']
2020-10-05 00:59:09 -06:00
deviceInUNMSsite = getUNMSdevicesAtClientSite(unmsClientSiteID)
for device in deviceInUNMSsite:
deviceName = device['identification']['name']
deviceMAC = device['identification']['mac']
deviceIP = device['ipAddress']
deviceModel = device['identification']['model']
deviceModelName = device['identification']['modelName']
if '/' in deviceIP:
deviceIP = deviceIP.split('/')[0]
if deviceModel not in deviceModelBlacklist:
2020-10-05 01:22:45 -06:00
print("Added " + ":\t" + deviceName)
2020-10-05 00:59:09 -06:00
thisShapedDevice = {
"identification": {
"name": deviceName,
"hostname": deviceName,
"ipAddr": deviceIP,
"mac": deviceMAC,
"model": deviceModel,
"modelName": deviceModelName,
"unmsSiteID": device['identification']['site']['id'],
2020-10-07 04:02:44 -06:00
"libreNMSSiteID": None
2020-10-05 00:59:09 -06:00
},
"qos": {
"downloadMbps": downloadSpeedMbps,
"uploadMbps": uploadSpeedMbps,
2020-10-07 04:02:44 -06:00
"accessPoint": None
2020-10-05 00:59:09 -06:00
},
}
unmsDevicesToImport.append(thisShapedDevice)
print("Imported " + address)
2020-10-05 01:22:45 -06:00
else:
print("Failed to import devices from " + unmsClientSite['description']['address'] + ". Missing QoS.")
2020-10-05 00:59:09 -06:00
return unmsDevicesToImport
2020-10-03 07:49:09 -06:00
2020-10-05 00:59:09 -06:00
def getUNMSdevicesAtClientSite(siteID):
2020-10-03 07:49:09 -06:00
url = unmsBaseURL + "/nms/api/v2.1/devices?siteId=" + siteID
headers = {'accept':'application/json', 'x-auth-token': orgUNMSxAuthToken}
r = requests.get(url, headers=headers)
2020-10-05 00:59:09 -06:00
return (r.json())