diff --git a/src/integrationPowercode.py b/src/integrationPowercode.py new file mode 100644 index 00000000..cda37022 --- /dev/null +++ b/src/integrationPowercode.py @@ -0,0 +1,127 @@ +from pythonCheck import checkPythonVersion +checkPythonVersion() +import requests +import warnings +from ispConfig import excludeSites, findIPv6usingMikrotik, bandwidthOverheadFactor, exceptionCPEs, powercode_api_key, powercode_api_url +from integrationCommon import isIpv4Permitted +import base64 +from requests.auth import HTTPBasicAuth +if findIPv6usingMikrotik == True: + from mikrotikFindIPv6 import pullMikrotikIPv6 +from integrationCommon import NetworkGraph, NetworkNode, NodeType +from urllib3.exceptions import InsecureRequestWarning + +def getCustomerInfo(): + headers= {'Content-Type': 'application/x-www-form-urlencoded'} + url = powercode_api_url + ":444/api/preseem/index.php" + data = {} + data['apiKey'] = powercode_api_key + data['action'] = 'list_customers' + + r = requests.post(url, data=data, headers=headers, verify=False, timeout=10) + return r.json() + +def getServiceInfo(customerID): + headers= {'Content-Type': 'application/x-www-form-urlencoded'} + url = powercode_api_url + ":444/api/1/index.php" + data = {} + data['apiKey'] = powercode_api_key + data['action'] = 'readCustomerService' + data['customerID'] = customerID + + r = requests.post(url, data=data, headers=headers, verify=False, timeout=10) + servicesDict = {} + for service in r.json()['services']: + if 'internetInfo' in service: + servicesDict[service['serviceID']] = {} + servicesDict[service['serviceID']]['downloadMbps'] = int(round(int(service['internetInfo']['maxIn']) / 1000)) + servicesDict[service['serviceID']]['uploadMbps'] = int(round(int(service['internetInfo']['maxOut']) / 1000)) + return servicesDict + +def createShaper(): + net = NetworkGraph() + requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning) + print("Fetching data from Powercode") + + customerInfo = getCustomerInfo() + + customerIDs = [] + for customer in customerInfo: + if customer['id'] != '1': + if customer['id'] != '': + if customer['status'] == 'Active': + customerIDint = int(customer['id']) + if customerIDint != 0: + if customerIDint != None: + if customerIDint not in customerIDs: + customerIDs.append(customerIDint) + + allServices = {} + for customerID in customerIDs: + allServices.update(getServiceInfo(customerID)) + + acceptableEquipment = ['Customer Owned Equipment', 'Router', 'Customer Owned Equipment', 'Managed Routers'] #'CPE' + + devicesByCustomerID = {} + for customer in customerInfo: + if customer['status'] == 'Active': + chosenName = '' + if customer['name'] != '': + chosenName = customer['name'] + elif customer['company_name'] != '': + chosenName = customer['company_name'] + else: + chosenName = customer['id'] + for equipment in customer['equipment']: + if equipment['type'] in acceptableEquipment: + if equipment['service_id'] in allServices: + device = {} + device['id'] = "c_" + customer['id'] + "_s_" + "_d_" + equipment['id'] + device['name'] = equipment['name'] + device['ipv4'] = equipment['ip_address'] + device['mac'] = equipment['mac_address'] + if customer['id'] not in devicesByCustomerID: + devicesByCustomerID[customer['id']] = {} + devicesByCustomerID[customer['id']]['name'] = chosenName + devicesByCustomerID[customer['id']]['downloadMbps'] = allServices[equipment['service_id']]['downloadMbps'] + devicesByCustomerID[customer['id']]['uploadMbps'] = allServices[equipment['service_id']]['uploadMbps'] + if 'devices' not in devicesByCustomerID[customer['id']]: + devicesByCustomerID[customer['id']]['devices'] = [] + devicesByCustomerID[customer['id']]['devices'].append(device) + + for customerID in devicesByCustomerID: + customer = NetworkNode( + type=NodeType.client, + id=customerID, + displayName=devicesByCustomerID[customerID]['name'], + address='', + customerName=devicesByCustomerID[customerID]['name'], + download=devicesByCustomerID[customerID]['downloadMbps'], + upload=devicesByCustomerID[customerID]['uploadMbps'], + ) + net.addRawNode(customer) + for device in devicesByCustomerID[customerID]['devices']: + newDevice = NetworkNode( + id=device['id'], + displayName=device["name"], + type=NodeType.device, + parentId=customerID, + mac=device["mac"], + ipv4=[device['ipv4']], + ipv6=[] + ) + net.addRawNode(newDevice) + net.prepareTree() + net.plotNetworkGraph(False) + if net.doesNetworkJsonExist(): + print("network.json already exists. Leaving in-place.") + else: + net.createNetworkJson() + net.createShapedDevices() + +def importFromPowercode(): + #createNetworkJSON() + createShaper() + +if __name__ == '__main__': + importFromPowercode() diff --git a/src/ispConfig.example.py b/src/ispConfig.example.py index a3c7b76a..3c2367b0 100644 --- a/src/ispConfig.example.py +++ b/src/ispConfig.example.py @@ -77,6 +77,12 @@ overwriteNetworkJSONalways = False ignoreSubnets = ['192.168.0.0/16'] allowedSubnets = ['100.64.0.0/10'] +# Powercode Integration +automaticImportPowercode = False +powercode_api_key = '' +# Everything before :444/api/ in your Powercode instance URL +powercode_api_url = '' + # Splynx Integration automaticImportSplynx = False splynx_api_key = '' diff --git a/src/scheduler.py b/src/scheduler.py index f35bfd10..eaee0b35 100644 --- a/src/scheduler.py +++ b/src/scheduler.py @@ -11,6 +11,12 @@ if automaticImportUISP: from integrationUISP import importFromUISP if automaticImportSplynx: from integrationSplynx import importFromSplynx +try: + from ispConfig import automaticImportPowercode +except: + automaticImportPowercode = False +if automaticImportPowercode: + from integrationPowercode import importFromPowercode from apscheduler.schedulers.background import BlockingScheduler from apscheduler.executors.pool import ThreadPoolExecutor @@ -27,6 +33,11 @@ def importFromCRM(): importFromSplynx() except: print("Failed to import from Splynx") + elif automaticImportPowercode: + try: + importFromPowercode() + except: + print("Failed to import from Powercode") def graphHandler(): try: