def _adjustToTimezone(self, millis, timezone): """ Convert event timestamp to timezone from user property. """ return long(isoToTimestamp(convertTimestampToTimeZone( millis / 1000, timezone, "%Y-%m-%d %H:%M:%S" ))) * 1000
def _timeRange(self, value): try: values = [] for t in value.split('/'): values.append(int(isoToTimestamp(t)) * 1000) return values except ValueError: log.warning("Invalid timestamp: %s", value) return ()
def process(self, device, results, unused): tenants = [] for tenant in results["tenants"]: if tenant["enabled"] is not True: continue tenants.append( ObjectMap( modname="ZenPacks.zenoss.OpenStackInfrastructure.Tenant", data=dict( id="tenant-{0}".format(tenant["id"]), title=tenant["name"], # nova description=tenant["description"], # tenant description tenantId=tenant["id"], ), ) ) region_id = prepId("region-{0}".format(device.zOpenStackRegionName)) region = ObjectMap( modname="ZenPacks.zenoss.OpenStackInfrastructure.Region", data=dict(id=region_id, title=device.zOpenStackRegionName), ) flavors = [] for flavor in results["flavors"]: flavors.append( ObjectMap( modname="ZenPacks.zenoss.OpenStackInfrastructure.Flavor", data=dict( id="flavor-{0}".format(flavor["id"]), title=flavor["name"], # 256 server flavorId=flavor["id"], # performance1-1 flavorRAM=flavor["ram"] * 1024 * 1024, # 256 flavorDisk=flavor["disk"] * 1024 * 1024 * 1024, # 10 flavorVCPUs=flavor["vcpus"], ), ) ) images = [] for image in results["images"]: # If it's a snapshot, rather than a normal image, ignore it for # the time being. if "server" in image: log.debug("Ignoring image %s" % image["name"]) continue # If we can, change dates like '2014-09-30T19:45:44Z' to '2014/09/30 19:45:44.00' try: imageCreated = LocalDateTime(isoToTimestamp(image["created"].replace("Z", ""))) except Exception: log.debug("Unable to reformat imageCreated '%s'" % image["created"]) imageCreated = image["created"] try: imageUpdated = LocalDateTime(isoToTimestamp(image["updated"].replace("Z", ""))) except Exception: log.debug("Unable to reformat imageUpdated '%s'" % image["updated"]) imageUpdated = image["updated"] images.append( ObjectMap( modname="ZenPacks.zenoss.OpenStackInfrastructure.Image", data=dict( id="image-{0}".format(image["id"]), title=image["name"], # Red Hat Enterprise Linux 5.5 imageId=image["id"], # 346eeba5-a122-42f1-94e7-06cb3c53f690 imageStatus=image["status"], # ACTIVE imageCreated=imageCreated, # 2014/09/30 19:45:44.000 imageUpdated=imageUpdated, # 2014/09/30 19:45:44.000 ), ) ) servers = [] for server in results["servers"]: # Backup support is optional. Guard against it not existing. backup_schedule_enabled = None backup_schedule_daily = None backup_schedule_weekly = None try: backup_schedule_enabled = server["backup_schedule"]["enabled"] backup_schedule_daily = server["backup_schedule"]["daily"] backup_schedule_weekly = server["backup_schedule"]["weekly"] except (NotFoundError, AttributeError, KeyError): backup_schedule_enabled = False backup_schedule_daily = "DISABLED" backup_schedule_weekly = "DISABLED" # Get and classify IP addresses into public and private: (fixed/floating) public_ips = set() private_ips = set() access_ipv4 = server.get("accessIPv4") if access_ipv4: public_ips.add(access_ipv4) access_ipv6 = server.get("accessIPv6") if access_ipv6: public_ips.add(access_ipv6) address_group = server.get("addresses") if address_group: for network_name, net_addresses in address_group.items(): for address in net_addresses: if address.get("OS-EXT-IPS:type") == "fixed": private_ips.add(address["addr"]) elif address.get("OS-EXT-IPS:type") == "floating": public_ips.add(address["addr"]) else: log.info("Address type not found for %s", address["addr"]) log.info("Adding %s to private_ips", address["addr"]) private_ips.add(address["addr"]) # Flavor and Image IDs could be specified two different ways. flavor_id = None if "flavorId" in server: flavor_id = server["flavorId"] elif "flavor" in server and "id" in server["flavor"]: flavor_id = server["flavor"]["id"] image_id = None if "imageId" in server: image_id = server["imageId"] elif "image" in server and isinstance(server["image"], dict) and "id" in server["image"]: image_id = server["image"]["id"] tenant_id = server["tenant_id"] servers.append( ObjectMap( modname="ZenPacks.zenoss.OpenStackInfrastructure.Instance", data=dict( id="server-{0}".format(server["id"]), title=server["name"], # cloudserver01 resourceId=server["id"], serverId=server["id"], # 847424 serverStatus=server["status"], # ACTIVE serverBackupEnabled=backup_schedule_enabled, # False serverBackupDaily=backup_schedule_daily, # DISABLED serverBackupWeekly=backup_schedule_weekly, # DISABLED publicIps=list(public_ips), # 50.57.74.222 privateIps=list(private_ips), # 10.182.13.13 set_flavor="flavor-{0}".format(flavor_id), # flavor-performance1-1 set_image="image-{0}".format(image_id), # image-346eeba5-a122-42f1-94e7-06cb3c53f690 set_tenant="tenant-{0}".format(tenant_id), # tenant-a3a2901f2fd14f808401863e3628a858 hostId=server["hostId"], # a84303c0021aa53c7e749cbbbfac265f hostName=server["name"], # cloudserver01 ), ) ) services = [] zones = {} hostmap = {} # Find all hosts which have a nova service on them. for service in results["services"]: title = "{0}@{1} ({2})".format(service["binary"], service["host"], service["zone"]) service_id = prepId("service-{0}-{1}-{2}".format(service["binary"], service["host"], service["zone"])) host_id = prepId("host-{0}".format(service["host"])) zone_id = prepId("zone-{0}".format(service["zone"])) hostmap[host_id] = {"hostname": service["host"], "org_id": zone_id} zones.setdefault( zone_id, ObjectMap( modname="ZenPacks.zenoss.OpenStackInfrastructure.AvailabilityZone", data=dict(id=zone_id, title=service["zone"], set_parentOrg=region_id), ), ) # Currently, nova-api doesn't show in the nova service list. # Even if it does show up there in the future, I don't model # it as a NovaService, but rather as its own type of software # component. (See below) if service["binary"] == "nova-api": continue services.append( ObjectMap( modname="ZenPacks.zenoss.OpenStackInfrastructure.NovaService", data=dict( id=service_id, title=title, binary=service["binary"], enabled={"enabled": True, "disabled": False}.get(service["status"], False), operStatus={"up": "UP", "down": "DOWN"}.get(service["state"], "UNKNOWN"), set_hostedOn=host_id, set_orgComponent=zone_id, ), ) ) # Find all hosts which have a neutron agent on them. for agent in results["agents"]: host_id = prepId("host-{0}".format(agent["host"])) hostmap[host_id] = {"hostname": agent["host"], "org_id": region_id} # add any user-specified hosts which we haven't already found. if device.zOpenStackNovaApiHosts or device.zOpenStackExtraHosts: log.info("Finding additional hosts") if device.zOpenStackNovaApiHosts: log.info(" Adding zOpenStackNovaApiHosts=%s" % device.zOpenStackNovaApiHosts) if device.zOpenStackExtraHosts: log.info(" Adding zOpenStackExtraHosts=%s" % device.zOpenStackExtraHosts) for hostname in device.zOpenStackNovaApiHosts + device.zOpenStackExtraHosts: host_id = prepId("host-{0}".format(hostname)) hostmap[host_id] = {"hostname": hostname, "org_id": region_id} hosts = [] for host_id in hostmap: data = hostmap[host_id] hosts.append( ObjectMap( modname="ZenPacks.zenoss.OpenStackInfrastructure.Host", data=dict( id=host_id, title=data["hostname"], hostname=data["hostname"], set_orgComponent=data["org_id"] ), ) ) hypervisors = [] server_hypervisor_instance_name = {} for hypervisor in results["hypervisors"]: hypervisor_id = prepId("hypervisor-{0}".format(hypervisor["id"])) hypervisor_servers = [] if "servers" in hypervisor: for server in hypervisor["servers"]: server_id = "server-{0}".format(server["uuid"]) hypervisor_servers.append(server_id) server_hypervisor_instance_name[server_id] = server["name"] hypervisors.append( ObjectMap( modname="ZenPacks.zenoss.OpenStackInfrastructure.Hypervisor", data=dict( id=hypervisor_id, title="{0}.{1}".format(hypervisor["hypervisor_hostname"], hypervisor["id"]), hypervisorId=hypervisor["id"], # 1 set_instances=hypervisor_servers, set_hostByName=hypervisor["hypervisor_hostname"], ), ) ) # add hypervisor instance name to the existing server objectmaps. for om in servers: if om.id in server_hypervisor_instance_name: om.hypervisorInstanceName = server_hypervisor_instance_name[om.id] # nova-api support. # Place it on the user-specified hosts, or also find it if it's # in the nova-service list (which we ignored earlier). It should not # be, under icehouse, at least, but just in case this changes..) nova_api_hosts = set(device.zOpenStackNovaApiHosts) for service in results["services"]: if service["binary"] == "nova-api": if service["host"] not in nova_api_hosts: nova_api_hosts.add(service["host"]) # Look to see if the hostname or IP in the auth url corresponds # directly to a host we know about. If so, add it to the nova # api hosts. try: apiHostname = urlparse(results["nova_url"]).hostname apiIp = results["resolved_hostnames"].get(apiHostname, apiHostname) for host in hosts: if host.hostname == apiHostname: nova_api_hosts.add(host.hostname) else: hostIp = results["resolved_hostnames"].get(host.hostname, host.hostname) if hostIp == apiIp: nova_api_hosts.add(host.hostname) except Exception, e: log.warning("Unable to perform nova-api component discovery: %s" % e)