def main(): output = [] c = unifiapi.controller(endpoint='https://rocketfire.tech-cloud01.lan.sdok.no:8443', username='******', password='******', verify=False) s = c.sites['default']() pp = pprint.PrettyPrinter(indent=4) for ap in s.devices(): fields = {} fields.update(ap['system-stats']) fields.update(ap['sys_stats']) fields.update({'state': ap['state']}) fields.update({'num_sta': ap['num_sta']}) #last_seen last_seen = int(time.time()) - int(ap['last_seen']) fields.update({'last_seen': last_seen}) fields_cleaned = {} for key,field in fields.items(): try: fields_cleaned.update({key:float(str(field).strip('"'))}) except Exception as e: fields_cleaned.update({key:field}) output.append( { "measurement": "unifi_ap_stat", "tags": { "name": ap['name'], "model": ap['model'], 'mac': ap['mac'], 'ip': ap['ip'] }, "fields": fields_cleaned } ) client = InfluxDBClient('pluto.tech-cloud01.lan.sdok.no', 8086, 'root', 'root', 'gondul') client.write_points(output) print(output)
# EDIT profile = 'default' influx_host = os.getenv('INFLUX_HOST', 'localhost') influx_port = os.getenv('INFLUX_PORT', '8086') influx_database = os.getenv('INFLUX_DATABASE', 'unifi') controller_uri = os.getenv('CONTROLLER_URI', 'https://unifi:8443') controller_username = os.getenv('CONTROLLER_USERNAME', 'admin') controller_password = os.getenv('CONTROLLER_PASSWORD', 'password') controller_verify = bool(strtobool(os.getenv('CONTROLLER_VERIFY', 'false'))) controller_site = os.getenv('CONTROLLER_SITE', 'default') default_tags = os.getenv('INFLUX_DEFAULTTAGS') client = InfluxDBClient(influx_host, influx_port) c = controller(endpoint=controller_uri, username=controller_username, password=controller_password, verify=controller_verify) s = c.sites[controller_site]() pp = pprint.PrettyPrinter(indent=4) try: default_tags = { 'hostname': s.sysinfo()[0]['hostname'], 'site': controller_site, 'desc': c.sites[controller_site]['desc'] } except: print("Could not generate site default tags") client.create_database(influx_database) client.switch_database(influx_database)
from unifiapi import controller from time import sleep from requests import post # create a file secrets.py and define # url = "slack comptible endpoint url" from secrets import url c = controller() s = c.sites['default']() site_name = c.sites['default']['desc'] hlth = None alerts = None def find_name(alert): for key in alert: if 'name' in key: return alert[key] def alert_to_attachment(alerts, previous=None): res = [] already_seen = [] if previous: already_seen = set((x['_id'] for x in previous)) for alert in alerts: if alert['_id'] not in already_seen: # New alert name = find_name(alert)
continue candidate = line.split()[0] if candidate: yield candidate def new_firewall_group(name, list_members, group_type='address-group'): return { 'name': name, 'group_type': group_type, 'group_members': list(list_members) } print("Logging into controller") c = unifiapi.controller() s = c.sites['default']() print("Getting firewall listing") fwg = s.firewallgroups() for list_name, url in sync_list.items(): print(f'Syncing {list_name}') list_ips = sorted(set((download_ips(url)))) try: curfw = fwg[ list_name] # this will raise KeyError and fall back to adding the firewall curips = sorted(set(curfw['group_members'])) if curips == list_ips: print( "Found IDENTICAL existing list {} with {} members - download list has {} members" .format(list_name, len(curips), len(list_ips)))
#!/usr/bin/env python from unifiapi import controller from time import sleep my_profile = 'default' my_site = 'default' print("Connecting to controller") c = controller(my_profile) print("Connection to site {}".format(my_site)) s = c.sites[my_site]() print("Fetching site settings") settings = s.settings() print("Disabling DPI") settings['dpi']['enabled'] = False settings['dpi'].update() print("Sleeping 5 seconds") sleep(5) print("Re-enabling DPI") settings['dpi']['enabled'] = True settings['dpi'].update()
import unifiapi from dotenv import load_dotenv import os from pprint import pprint from tabulate import tabulate load_dotenv() UNIFI_SITE_ID = os.getenv('UNIFI_SITE_ID') UNIFI_ENDPOINT = os.getenv('UNIFI_ENDPOINT') UNIFI_USERNAME = os.getenv('UNIFI_USERNAME') UNIFI_PASSWORD = os.getenv('UNIFI_PASSWORD') controller = unifiapi.controller(endpoint=UNIFI_ENDPOINT, username=UNIFI_USERNAME, password=UNIFI_PASSWORD, verify=False) site = controller.sites[UNIFI_SITE_ID]() clients = site.active_clients() wireless_clients = list(filter(lambda x: x['is_wired'] == False, clients)) to_tabulate = [] for client in wireless_clients: if 'name' in client.keys(): _name = client['name'] elif 'hostname' in client.keys(): _name = client['hostname'] else:
from time import sleep from datetime import datetime from pprint import pprint from time import time # EDIT profile = 'byip' site_name = 'default' default_tags = None client = InfluxDBClient('localhost', 8086) # END EDIT c = controller(profile=profile) s = c.sites[site_name]() try: default_tags = { 'hostname': s.sysinfo()[0]['hostname'], 'site': site_name, 'desc': c.sites[site_name]['desc'] } except: print("Could not generate site default tags") client.create_database('unifi') client.switch_database('unifi') current_data = {}