#!/usr/bin/env python3 # -*- encoding: utf-8; py-indent-offset: 4 -*- from pprint import pprint from collections import defaultdict class dictobject(defaultdict): def __getattr__(self,name): return self[name] if name in self else "" nested_dictobject = lambda: dictobject(nested_dictobject) def inv_unifi_controller(info): node = inv_tree("software.os") node["version"] = info.get("controller_version") def inv_unifi_device(info): node = inv_tree("software.configuration.snmp_info") node["name"] = info.get("name") node["contact"] = info.get("snmp_contact") node["location"] = info.get("snmp_location") node = inv_tree("software.os") node["version"] = info.get("version") node = inv_tree("harware.system") node["vendor"] = "ubiquiti" for _key in ("model","board_rev","serial","mac"): _val = info.get(_key) if _val: node[_key] = _val def inv_unifi_port(info,params,inventory_tree): _parsed = nested_dictobject() for _line in info: _parsed[_line[0]][_line[1]] = _line[2] _interfaces = [] _total_ethernet_ports = 0 _available_ethernet_ports = 0 def _saveint(num): try: return int(num) except (TypeError,ValueError): return 0 for _iface in _parsed.values(): _interfaces.append({ "index" : int(_iface.port_idx), "description" : _iface.name, "alias" : _iface.name, "speed" : _saveint(_iface.speed)*1000000, "phys_address" : "", "oper_status" : _saveint(_iface.oper_status), "admin_status" : _saveint(_iface.admin_status), "port_type" : 6, "available" : _iface.oper_status == '2' }) _total_ethernet_ports+=1 _available_ethernet_ports+=1 if _iface.oper_status == '2' else 0 node = inventory_tree.get_list("networking.interfaces:") node.extend(sorted(_interfaces, key=lambda i: i.get('index'))) node = inventory_tree.get_dict("networking.") node["available_ethernet_ports"] = _available_ethernet_ports node["total_ethernet_ports"] = _total_ethernet_ports node["total_interfaces"] = len(_parsed) inv_info["unifi_controller"] = { "inv_function" : inv_unifi_controller } inv_info["unifi_device"] = { "inv_function" : inv_unifi_device } inv_info["unifi_network_ports"] = { "inv_function" : inv_unifi_port }