|
| 1 | +load('runzero.types', 'ImportAsset', 'NetworkInterface') |
| 2 | +load('base64', base64_encode='encode', base64_decode='decode') |
| 3 | +load('http', http_get='get', http_post='post', 'url_encode') |
| 4 | +load('json', json_encode='encode', json_decode='decode') |
| 5 | +load('net', 'ip_address') |
| 6 | +load('uuid', 'new_uuid') |
| 7 | + |
| 8 | +SWIS_BASE_URL = 'https://localhost:17774' |
| 9 | +RUNZERO_REDIRECT = 'https://console.runzero.com/' |
| 10 | + |
| 11 | +def build_assets(assets): |
| 12 | + assets_import = [] |
| 13 | + for asset in assets: |
| 14 | + asset_id = str(asset.get('NodeId', str(new_uuid))) |
| 15 | + hostname = asset.get('Fqdn', '') |
| 16 | + os = asset.get('OsVersion', '') |
| 17 | + vendor = asset.get('Vendor', '') |
| 18 | + |
| 19 | + # create the network interfaces |
| 20 | + interfaces = [] |
| 21 | + addresses = asset.get('IpAddress', []) |
| 22 | + interface = build_network_interface(ips=[addresses], mac=None) |
| 23 | + interfaces.append(interface) |
| 24 | + |
| 25 | + # Retrieve and map custom attributes |
| 26 | + cpu_util = str(asset.get('CpuPercentUtilization', '')) |
| 27 | + discovery_profile_id = str(asset.get('DiscoveryProfileId', '')) |
| 28 | + mem_util_perc = str(asset.get('PercentMemoryUsed', '')) |
| 29 | + mem_util = str(asset.get('MemoryUsed', '')) |
| 30 | + pollers = asset.get('Pollers', '') |
| 31 | + response_time = str(asset.get('ResponseTime', '')) |
| 32 | + snmp_port = str(asset.get('SnmpPort', '')) |
| 33 | + snmp_version = str(asset.get('SnmpVersion', '')) |
| 34 | + status = asset.get('Status', '') |
| 35 | + sys_object_id = asset.get('SysObjectId', '') |
| 36 | + uptime = str(asset.get('Uptime', '')) |
| 37 | + |
| 38 | + custom_attributes = { |
| 39 | + 'percentCpuUtilization': cpu_util, |
| 40 | + 'discoveryProfileId': discovery_profile_id, |
| 41 | + 'percentMemoryUtilization': mem_util_perc, |
| 42 | + 'memoryUtilized': mem_util, |
| 43 | + 'pollers': pollers, |
| 44 | + 'responseTime': response_time, |
| 45 | + 'snmp.port': snmp_port, |
| 46 | + 'snmp.version': snmp_version, |
| 47 | + 'status': status, |
| 48 | + 'sysObjectId': sys_object_id, |
| 49 | + 'uptime': uptime |
| 50 | + } |
| 51 | + |
| 52 | + # Build assets for import |
| 53 | + assets_import.append( |
| 54 | + ImportAsset( |
| 55 | + id=asset_id, |
| 56 | + hostnames=[hostname], |
| 57 | + os=os, |
| 58 | + manufacturer=vendor, |
| 59 | + networkInterfaces=interfaces, |
| 60 | + customAttributes=custom_attributes |
| 61 | + ) |
| 62 | + ) |
| 63 | + return assets_import |
| 64 | + |
| 65 | +def build_network_interface(ips, mac): |
| 66 | + ip4s = [] |
| 67 | + ip6s = [] |
| 68 | + for ip in ips[:99]: |
| 69 | + ip_addr = ip_address(ip) |
| 70 | + if ip_addr.version == 4: |
| 71 | + ip4s.append(ip_addr) |
| 72 | + elif ip_addr.version == 6: |
| 73 | + ip6s.append(ip_addr) |
| 74 | + else: |
| 75 | + continue |
| 76 | + if not mac: |
| 77 | + return NetworkInterface(ipv4Addresses=ip4s, ipv6Addresses=ip6s) |
| 78 | + else: |
| 79 | + return NetworkInterface(macAddress=mac, ipv4Addresses=ip4s, ipv6Addresses=ip6s) |
| 80 | + |
| 81 | +def get_assets(creds): |
| 82 | + |
| 83 | + url = SWIS_BASE_URL + 'SolarWinds/InformationService/v3/Json/Query?' |
| 84 | + headers = {'Accept': 'application/json', |
| 85 | + 'Authorization': 'Basic ' + creds} |
| 86 | + # Populate the SWQL query to return desired assets and attributes in the params query value e.g. |
| 87 | + # params = {'query': 'SELECT N.NodeID, N.OsVersion, N.Fqdn, N.Vendor, N.IPAddress, N.CpuPercentUtilization, N.DiscoveryProfileId, N.PercentMemoryUsed, N.MemoryUsed, N.Pollers, N.responseTime, N.snmp.port, N.snmp.version, N.status, N.sysObjectId, N.Uptime FROM Orion.Nodes'} |
| 88 | + params = {'query': ''} |
| 89 | + response = http_get(url, headers=headers, params=params) |
| 90 | + if response.status_code != 200: |
| 91 | + print('failed to retrieve assets', 'status code: ' + str(response.status_code)) |
| 92 | + data = json_decode(response.body) |
| 93 | + assets_all.extend(data) |
| 94 | + |
| 95 | + return assets_all |
| 96 | + |
| 97 | +def main(*args, **kwargs): |
| 98 | + username = kwargs['access_key'] |
| 99 | + password = kwargs['access_secret'] |
| 100 | + b64_creds = base64_encode(username + ":" + password) |
| 101 | + assets = get_assets(b64_creds) |
| 102 | + |
| 103 | + # Format asset list for import into runZero |
| 104 | + import_assets = build_assets(assets) |
| 105 | + if not import_assets: |
| 106 | + print('no assets') |
| 107 | + return None |
| 108 | + |
| 109 | + return import_assets |
0 commit comments