コード例 #1
0
    def onError(self, results, config):
        ds0 = config.datasources[0]
        client = txovirt.getClient(ds0.zOVirtUrl,
                                   ds0.zOVirtUser,
                                   ds0.zOVirtDomain,
                                   ds0.zOVirtPassword)

        # Try to reset the login connection on an error.
        client.login()

        errmsg = "ovirt: %s" % results.getErrorMessage()
        log.error('%s %s', config.id, errmsg)
        data = self.new_data()
        data['events'].append({
            'eventClassKey': 'oVirtEventCollectionError',
            'eventKey': eventKey(config),
            'eventClass': '/Status/',
            'summary': errmsg,
            'device': config.id,
            'severity': 4,
        })
        return data
コード例 #2
0
    def onSuccess(self, results, config):
        data = self.new_data()
        event_tree = etree.parse(StringIO(results))
        for event in event_tree.xpath('/events/*'):
            id = event.xpath('@id')[0]
            description = event.xpath('description/text()')[0]

            # Dont send alerts for logins and logouts
            if 'logged in' in description:
                continue
            if 'logged out' in description:
                continue

            code = event.xpath('code/text()')[0]
            severity = event.xpath('severity/text()')[0]
            origin = event.xpath('origin/text()')[0]
            custom_id = event.xpath('custom_id/text()')[0]
            flood_rate = event.xpath('flood_rate/text()')[0]
            try:
                correlation_id = event.xpath('correlation_id/text()')[0]
            except:
                correlation_id = ""

            rcvtime = xml.utils.iso8601.parse(event.xpath('time/text()')[0])

            #Process severity
            if not severity:
                severity = 3
            severity = SEVERITY_MAP.get(severity, 3)

            event_type = EVENT_TYPE_MAP.get(int(code), 'Unknown (%s)' % code)

            # if the component type is in the event_type use that to set the component for the event.

            component = None
            #for key in [key for key in event.keys() if key not in ['code', 'description', 'time', 'text', 'href', 'user', 'time', 'id', 'severity']]:
            for key in [e.tag for e in event.getchildren()
                        if e.tag not in ['code',
                                         'description',
                                         'time',
                                         'text',
                                         'href',
                                         'user',
                                         'time',
                                         'correlation_id',
                                         'origin',
                                         'custom_id',
                                         'flood_rate',
                                         'id',
                                         'severity']]:
                if key.lower() in event_type.lower():
                    component = event.xpath('%s/@id' % key)[0]
                    continue

            # If we dont have a component at this point try and map it out to known component types.
            # vm -> cluster -> host

            event_keys = [e.tag for e in event.getchildren()]
            if not component:
                if 'vm' in event_keys:
                    component = event.xpath('%s/@id' % 'vm')[0]
                elif 'cluster' in event_keys:
                    component = event.xpath('%s/@id' % 'cluster')[0]
                elif 'host' in event_keys:
                    component = event.xpath('%s/@id' % 'host')[0]
                elif 'data_center' in event_keys:
                    component = event.xpath('%s/@id' % 'data_center')[0]
                elif 'storage_domain' in event_keys:
                    component = event.xpath('%s/@id' % 'storage_domain')[0]
                #else:
                    # No component set this will be a device level event.
                    #print event_type
                    #print [key for key in event.keys() if key not in ['code', 'description', 'time', 'text', 'href', 'user', 'time', 'id', 'severity']]

            evt = dict(
                severity=int(severity),
                summary=str(description),
                message='%s: %s' % (str(event_type), str(description)),
                eventKey='event_%s' % str(id),
                eventClassKey='ovirt_alert',
                rcvtime=rcvtime,
                component=str(component),
                ovirt_type=str(event_type),
                correlation_id=str(correlation_id),
                device=str(config.id),
            )

            # Don't add null components
            if not component:
                del evt['component']

            # Use for generating clears later.
            #new_event_ids.add(event['id'])

            #if event['id'] in last_event_ids:
            #    continue

            data['events'].append(evt)

        ids = event_tree.xpath('*/@id')
        if ids:
            id = max(ids)
        if id:
            ds0 = config.datasources[0]
            key = "%s%s%s_%s" % (ds0.zOVirtUrl,
                                 ds0.zOVirtUser,
                                 ds0.zOVirtDomain,
                                 ds0.zOVirtPassword)
            self.last_event[key] = id
            self._save(config, self.last_event, key='events')

        data['events'].append({
            'eventClassKey': 'oVirtEventCollectionSuccess',
            'eventKey': eventKey(config),
            'summary': 'ovirt: event successful collection',
            'eventClass': '/Status/',
            'device': config.id,
            'severity': 0,
        })
        return data
コード例 #3
0
    def onSuccess(self, results, config):
        data = self.new_data()
        root = etree.Element('root')

        # Find and save the cluster tree
        for result in results[0]:
            tree = etree.parse(StringIO(result))
            root.append(tree.getroot())
            if tree.getroot().tag == 'clusters':
                cluster_tree = tree

        for result in results[0]:
            result_tree = etree.parse(StringIO(result))
            if result_tree.getroot().tag == 'storage_domains':
                count = len(result_tree.getroot().getchildren())
                addCount(root, [config.id], 'storagedomainCount', count)
            elif result_tree.getroot().tag == 'clusters':
                count = len(result_tree.getroot().getchildren())
                addCount(root, [config.id], 'clusterCount', count)
                data_centers = result_tree.xpath('//data_center/@id')
                addCount(root, data_centers, 'clusterCount')
            elif result_tree.getroot().tag == 'data_centers':
                count = len(result_tree.getroot().getchildren())
                addCount(root, [config.id], 'datacenterCount', count)
            elif result_tree.getroot().tag == 'hosts':
                count = len(result_tree.getroot().getchildren())
                addCount(root, [config.id], 'hostCount', count)
                clusters = result_tree.xpath('//cluster/@id')
                addCount(root, clusters, 'hostCount')

                for cluster in clusters:
                    datacenter = cluster_tree.xpath('//cluster[@id="%s"]/data_center/@id' % cluster)
                    addCount(root, datacenter, 'hostCount', 1)

            elif result_tree.getroot().tag == 'vms':
                count = len(result_tree.getroot().getchildren())
                addCount(root, [config.id], 'vmCount', count)
                clusters = result_tree.xpath('//cluster/@id')
                addCount(root, clusters, 'vmCount')

                hosts = result_tree.xpath('//host/@id')
                addCount(root, hosts, 'vmCount')

                for cluster in clusters:
                    datacenter = cluster_tree.xpath('//cluster[@id="%s"]/data_center/@id' % cluster)
                    addCount(root, datacenter, 'vmCount', 1)

        for result_stat in results[1]:
            root.append(etree.parse(StringIO(result_stat)).getroot())
            # This is the general format ...
            #root.xpath('//*[*/@id="368bf44e-7d29-483a-8c2e-9a79962b1e48"][name/text()="disk.read.latency"]/values/value/datum/text()')[0]

        for ds in config.datasources:
            if ds.component:
                component_id = prepId(ds.component)
            else:
                component_id = None

            for point in ds.points:
                # Handle percentage custom datapoints
                if "ovirt:" in point.xpath and point.rpn and 'xpath' in ds.params:
                    resultsDict = {}
                    try:
                        xpath = talesEvalStr(ds.params['xpath'], context=None, extra=ds.params['context'])
                        statdata = [(x.xpath('name/text()'), x.xpath('values/value/datum/text()')) for x in root.xpath(xpath) if x.tag == 'statistic']
                        for item, val in statdata:
                            resultsDict[item[0]] = val[0]
                        rpnstring = talesEvalStr(point.rpn, context=None, extra={'here': resultsDict})
                        results = rpneval(rpnstring.split(',', 1)[0], rpnstring.split(',', 1)[1])
                        data['values'][component_id][point.id] = (results, 'N')
                    except Exception:
                        pass

                # Do the rest using xpath
                elif 'xpath' in ds.params:
                    try:
                        # Some points may not exist in the xml, skip those...
                        xpath = talesEvalStr(ds.params['xpath'], context=None, extra=ds.params['context'])

                        results = root.xpath(xpath+point.xpath)
                        if 'Count' in point.xpath and not results:
                            results = ['0']
                        results = results[0]

                        # If rpn is defined, lets calculate the new results.
                        if point.rpn:
                            results = rpneval(
                                results, talesEvalStr(point.rpn, context=None, extra=ds.params['context']))
                        data['values'][component_id][point.id] = (results, 'N')
                    except Exception:
                        pass
        data['events'].append({
            'eventClassKey': 'oVirtCollectionSuccess',
            'eventKey': eventKey(config),
            'summary': 'ovirt: successful collection',
            'eventClass': '/Status/Perf/',
            'device': config.id,
            'severity': 0,
        })
        return data