Esempio n. 1
0
    def collect(self, config):
        data = self.new_data()
        for ds in config.datasources:
            self.component = ds.component
            region = ds.params['region']
            ec2_conn = TwistedEC2Connection.adapt(boto.ec2.connect_to_region(
                region,
                aws_access_key_id=ds.ec2accesskey,
                aws_secret_access_key=ds.ec2secretkey,
            ))
            instances = yield ec2_conn.get_only_instances(ds.component)
            instance = instances.pop()
            c = yield self.unreserved_instance_count(ec2_conn, instance)

            event = None
            if c == 1:
                event = 'This instance could be reserved'
            elif c > 1:
                event = 'There are {0} instances of this type in this ' \
                    'availability zone which could be reserved'.format(c)

            if event:
                data['events'].append({
                    'summary': event,
                    'device': config.id,
                    'component': ds.component,
                    'severity': ZenEventClasses.Info,
                    'eventClass': '/AWS/Suggestion',
                })

        defer.returnValue(data)
Esempio n. 2
0
    def connect_to_region(self, ds):
        region = ds.params['region']

        if CONNECTION_TYPE.get(ds.template) == 'ec2':
            self.ec2regionconn = TwistedEC2Connection.adapt(boto.ec2.connect_to_region(
                region,
                aws_access_key_id=ds.ec2accesskey,
                aws_secret_access_key=ds.ec2secretkey,
            ))
        else:
            self.vpcregionconn = TwistedVPCConnection.adapt(boto.vpc.connect_to_region(
                region,
                aws_access_key_id=ds.ec2accesskey,
                aws_secret_access_key=ds.ec2secretkey,
            ))
Esempio n. 3
0
    def collect(self, config):
        data = self.new_data()
        for ds in config.datasources:
            self.component = ds.component
            region = ds.params['region']
            ec2regionconn = TwistedEC2Connection.adapt(boto.ec2.connect_to_region(
                region,
                aws_access_key_id=ds.ec2accesskey,
                aws_secret_access_key=ds.ec2secretkey
            ))

            try:
                zones = yield ec2regionconn.get_all_zones(ds.component)
            except Exception as ex:
                log.error("Unable to get zones for %s region, "
                          "plugin: %s, template: %s, datasource: %s, "
                          "device: %s, component: %s, %s",
                          region, ds.plugin_classname, ds.template,
                          ds.datasource, ds.device, ds.component, str(ex))
            else:
                zone = zones.pop()
                if zone.state == 'available':
                    severity = ZenEventClasses.Clear
                    data['events'].append({
                        'summary': "Monitoring Ok",
                        'component': ds.component,
                        'severity': severity,
                        'eventKey': 'aws_result',
                        'eventClass': '/Status',
                    })
                else:
                    severity = ZenEventClasses.Warning
                data['events'].append({
                    'summary': 'Zone state is {0}'.format(zone.state),
                    'component': ds.component,
                    'eventKey': 'ZoneStatus',
                    'severity': severity,
                    'eventClass': '/Status',
                })

                data['maps'].append(ObjectMap({
                    "compname": "regions/%s/zones/%s" % (
                        region, ds.component),
                    "modname": "Zone state",
                    "state": zone.state
                }))

        defer.returnValue(data)
Esempio n. 4
0
    def collect(self, config):
        data = self.new_data()
        instance_states = {}
        for ds in config.datasources:
            region_id = self.component = ds.component
            ec2regionconn = TwistedEC2Connection.adapt(boto.ec2.connect_to_region(
                region_id,
                aws_access_key_id=ds.ec2accesskey,
                aws_secret_access_key=ds.ec2secretkey,
            ))

            vpcregionconn = TwistedVPCConnection.adapt(boto.vpc.connect_to_region(
                region_id,
                aws_access_key_id=ds.ec2accesskey,
                aws_secret_access_key=ds.ec2secretkey,
            ))

            instances_d = ec2regionconn.get_only_instances(
                filters=INSTANCE_FILTERS
            )
            volumes_d = ec2regionconn.get_all_volumes()
            elastic_ips_d = ec2regionconn.get_all_addresses()
            subnets_d = vpcregionconn.get_all_subnets()
            groups_d = ec2regionconn.get_all_security_groups()

            # Retrieve this data in parallel (per region)
            deferreds = [instances_d, volumes_d, elastic_ips_d, subnets_d, groups_d]
            labels = ('Instances', 'Volumes', 'Elastic IPs', 'Subnets', 'Security Groups')
            results = yield defer.DeferredList(deferreds, consumeErrors=True)

            values = {}
            instances = volumes = []
            for i in range(len(results)):
                success, result = results[i]

                if not success:
                    log.error("Error retrieving %s for %s: %s",
                              labels[i], region_id, result.getErrorMessage())

                    # Give up on this region, since some of the data
                    # couldn't be retrieved successfully.
                    continue

                if i == 0:
                    values['instances_count'] = (len(result), 'N')
                    instances = result
                elif i == 1:
                    values['volumes_count'] = (len(result), 'N')
                    volumes = result
                elif i == 2:
                    values['elastic_ips_count'] = (len(result), 'N')
                elif i == 3:
                    values['subnets_count'] = (len(result), 'N')
                elif i == 4:
                    values['vpc_security_groups_count'] = (len(result), 'N')
                    rules_count = 0
                    for group in result:
                        rules_count = max(len(group.rules), rules_count)
                    values['vpc_security_rules_count'] = (rules_count, 'N')
                else:
                    raise ValueError("Error retrieving %s for %s: %s" %
                                     (labels[i], region_id, result.getErrorMessage()))

            data['values'][region_id] = values

            # Previously zAWSRemodelEnabled was a string, fix for this case
            if str(ds.zAWSRemodelEnabled).lower() == 'true':
                instances_relmap = yield get_instances_relmap(
                    region_id, ds, instances, [], instance_states, ec2regionconn)
                data['maps'].append(instances_relmap)
            else:
                # InstanceState moved here for optimization
                ins_res = {}
                # update instance map only when there are new instances
                if len(instances) > 0:
                    for instance in instances:
                        ins_res[prepId(instance.id)] = instance.state
                    data['maps'].append(ObjectMap({
                        "compname": "regions/%s" % region_id,
                        "modname": "Instances states",
                        "setInstancesStates": ins_res
                    }))

            # VolumeState moved here for optimization
            vol_res = {}
            # update volume map only when there are new volumes
            if len(volumes) > 0:
                for volume in volumes:
                    vol_res[prepId(volume.id)] = volume.status
                data['maps'].append(ObjectMap({
                    "compname": "regions/%s" % region_id,
                    "modname": "Volumes states",
                    "setVolumesStatuses": vol_res
                }))

        ds0 = config.datasources[0]
        if str(ds0.zAWSRemodelEnabled).lower() == 'true':
            # Run "setDiscoverGuests" every time during the monitoring
            data['maps'].append(ObjectMap({
                "modname": "Guest update",
                "setDiscoverGuests": False if ds0.getDiscoverGuests
                else True,
            }))
        defer.returnValue(data)