예제 #1
0
    def get_alerts_for_plugin(self, plugin=None):
        rules_list = []
        params = {'plugin': plugin.get('_id')}

        result = self.collection.find(params)

        if result.clone().count() > 0:
            for rule in result:

                rule['gauge_data'] = plugin_model.get_gauge_by_id(
                    rule['gauge'])
                rules_list.append(rule)

        return rules_list
예제 #2
0
    def get_alerts(self, type=None, server=None, limit=None):
        params = {"rule_type": type}

        if server:
            params['server'] = server['_id']

        rules_list = []
        rules = self.collection.find(params).count()

        if rules > 0:
            rules = self.collection.find(params)

            rules_list = []
            for rule in rules:
                process_id = rule.get('process', None)
                if process_id:
                    rule['process_data'] = process_model.get_by_id(process_id)

                plugin_id = rule.get('plugin', None)
                gauge_id = rule.get('gauge', None)
                if plugin_id and gauge_id:
                    rule['plugin_data'] = plugin_model.get_by_id(plugin_id)
                    rule['gauge_data'] = plugin_model.get_gauge_by_id(gauge_id)

                if server:
                    rule['server'] = server
                # Check if the rule is for specific server and get the data
                else:
                    rule_server = rule.get('server', False)
                    server_id = self.object_id(rule_server)
                    if server_id:
                        rule['server'] = server_model.get_by_id(rule_server)

                tags = rule.get('tags', False)
                if tags:
                    rule = self._get_alert_tags(rule)

                rule['notifications'] = self._get_notifications(rule)

                rule[
                    'last_trigger'] = self.alert_history_model.get_last_trigger(
                        alert_id=rule['_id'])
                rule[
                    'total_triggers'] = self.alert_history_model.count_notifications(
                        alert_id=rule['_id'])

                rules_list.append(rule)

        return rules_list
예제 #3
0
    def get_by_id(self, alert_id, recipients_dict=True):
        alert_id = self.mongo.get_object_id(alert_id)
        alert = self.collection.find_one({"_id": alert_id})
        rule_type = alert.get('rule_type')

        # Return a full dictionary with recipients instead of list
        if recipients_dict is True:
            alert['notifications'] = self._get_notifications(alert)

        process = alert.get('process')
        if process and rule_type != 'process_global':
            alert['process'] = process_model.get_by_id(process)

        plugin = alert.get('plugin', None)
        gauge = alert.get('gauge', None)
        if plugin and gauge and rule_type != 'plugin_global':
            alert['plugin'] = plugin_model.get_by_id(plugin)
            alert['gauge'] = plugin_model.get_gauge_by_id(gauge)

        return alert
예제 #4
0
파일: models.py 프로젝트: gisce/AMON
    def get_all(self, account_id=None, dashboard_id=None, public=None):

        result_list = []
        query = []
        params = {'dashboard_id': dashboard_id}
        params = self.keys_to_mongoid(data=params, keys=['dashboard_id'])

        if dashboard_id:
            query = super(DashboardMetricsModel, self).get(params=params)

        utc_now = unix_utc_now()

        for metric in query:
            mongo_id = metric.get('_id')
            server_id = metric.get('server_id')
            metric_type = metric.get('metric_type')
            unique_id = metric.get('unique_id')
            check = metric.get('check')
            order = metric.get('order', 0)

            tags = metric.get('tags', [])
            tags_list = tags_model.get_list_of_tags(tags_list=tags, to_dict=True)

            server = server_model.get_by_id(server_id)
            process = process_model.get_by_id(metric.get('process_id'))
            plugin = plugin_model.get_by_id(metric.get('plugin_id'))
            gauge = plugin_model.get_gauge_by_id(gauge_id=metric.get('gauge_id'))

            volume = volumes_model.get_by_id(metric.get('device_id'))
            interface = interfaces_model.get_by_id(metric.get('device_id'))

            healthcheck_metric = health_checks_model.get_by_id(metric.get('healthcheck_id'))

            append = False

            unit = yaxis(check)
            if metric_type == 'system_global' and check == 'memory':
                unit = "%"
            if metric_type == 'system_global' and check == 'disk':
                unit = '%'

            if public:
                url = reverse('public_dashboard_metric', kwargs={"metric_id": mongo_id})
            else:
                url = reverse('dashboard_metric', kwargs={"metric_id": mongo_id})

            result = {
                'id': mongo_id,
                'unique_id': unique_id,
                'metric_type': metric_type,
                'url': url,
                'utcnow': utc_now,
                'name': '',
                'unit': unit,
                'tags': tags_list,
                'order': order
            }

            if server:
                result.update({'server_id': server_id, 'type': 'server_metric','server_name' :server.get('name')})


                if metric_type == 'system':
                    result['name'] = "{0}".format(check)
                    if volume:
                        result['name'] = u"{0}.{1}".format(result['name'], volume['name'])

                    if interface:
                        result['name'] = u"{0}.{1}".format(result['name'], interface['name'])

                    append = True


                elif metric_type == 'process' and process:
                    process_name = process.get('name')
                    result['name'] = u"{0}.{1}".format(process_name, check)
                    result['process_id'] = process['_id']
                    append = True

                elif metric_type == 'plugin' and plugin and gauge:
                    result['name'] = u"{0}.{1}".format(plugin.get('name'), gauge.get('name'))

                    result['plugin_id'] = plugin['_id']

                    result['gauge_id'] = gauge['_id']
                    append = True

                result['name'] = u"{0}.{1}".format(server.get('name'), result['name'])


            elif healthcheck_metric:
                result['healthcheck'] = healthcheck_metric
                result['healthcheck_id'] = healthcheck_metric.get('_id')
                try:
                    del result['healthcheck']['_id']
                    del result['healthcheck']['server_id']
                    del result['healthcheck']['tags']
                    del result['healthcheck']['file_id']  # Custom scripts
                except:
                    pass

                result['type'] = 'healthcheck'
                append = True

            else:
                key = metric.get('key')
                # Overwrite keys for better visual presentation
                if check == 'network':
                    key = 'inbound' if key == 'i' else 'outbound'

                result['name'] = u"{0}.{1}".format(check, key)
                append = True


            if metric_type == 'plugin_global':
                result['name'] = u'{0}.{1}.{2}'.format(metric.get('plugin'), metric.get('gauge'), metric.get('key'))
                append = True

            result = self.mongoid_to_str(result, ['server_id', 'id', 'process_id', 'plugin_id', 'metric_id', 'gauge_id', 'healthcheck_id',])

            if append:
                result_list.append(result)


        from operator import itemgetter
        sorted_list = sorted(result_list, key=itemgetter('order')) 

        return sorted_list
예제 #5
0
    def get_all(self, account_id=None, dashboard_id=None, public=None):

        result_list = []
        query = []
        params = {'dashboard_id': dashboard_id}
        params = self.keys_to_mongoid(data=params, keys=['dashboard_id'])

        if dashboard_id:
            query = super(DashboardMetricsModel, self).get(params=params)

        utc_now = unix_utc_now()

        for metric in query:
            mongo_id = metric.get('_id')
            server_id = metric.get('server_id')
            metric_type = metric.get('metric_type')
            unique_id = metric.get('unique_id')
            check = metric.get('check')
            order = metric.get('order', 0)

            tags = metric.get('tags', [])
            tags_list = tags_model.get_list_of_tags(tags_list=tags,
                                                    to_dict=True)

            server = server_model.get_by_id(server_id)
            process = process_model.get_by_id(metric.get('process_id'))
            plugin = plugin_model.get_by_id(metric.get('plugin_id'))
            gauge = plugin_model.get_gauge_by_id(
                gauge_id=metric.get('gauge_id'))

            volume = volumes_model.get_by_id(metric.get('device_id'))
            interface = interfaces_model.get_by_id(metric.get('device_id'))

            healthcheck_metric = health_checks_model.get_by_id(
                metric.get('healthcheck_id'))

            append = False

            unit = yaxis(check)
            if metric_type == 'system_global' and check == 'memory':
                unit = "%"
            if metric_type == 'system_global' and check == 'disk':
                unit = '%'

            if public:
                url = reverse('public_dashboard_metric',
                              kwargs={"metric_id": mongo_id})
            else:
                url = reverse('dashboard_metric',
                              kwargs={"metric_id": mongo_id})

            result = {
                'id': mongo_id,
                'unique_id': unique_id,
                'metric_type': metric_type,
                'url': url,
                'utcnow': utc_now,
                'name': '',
                'unit': unit,
                'tags': tags_list,
                'order': order
            }

            if server:
                result.update({
                    'server_id': server_id,
                    'type': 'server_metric',
                    'server_name': server.get('name')
                })

                if metric_type == 'system':
                    result['name'] = "{0}".format(check)
                    if volume:
                        result['name'] = u"{0}.{1}".format(
                            result['name'], volume['name'])

                    if interface:
                        result['name'] = u"{0}.{1}".format(
                            result['name'], interface['name'])

                    append = True

                elif metric_type == 'process' and process:
                    process_name = process.get('name')
                    result['name'] = u"{0}.{1}".format(process_name, check)
                    result['process_id'] = process['_id']
                    append = True

                elif metric_type == 'plugin' and plugin and gauge:
                    result['name'] = u"{0}.{1}".format(plugin.get('name'),
                                                       gauge.get('name'))

                    result['plugin_id'] = plugin['_id']

                    result['gauge_id'] = gauge['_id']
                    append = True

                result['name'] = u"{0}.{1}".format(server.get('name'),
                                                   result['name'])

            elif healthcheck_metric:
                result['healthcheck'] = healthcheck_metric
                result['healthcheck_id'] = healthcheck_metric.get('_id')
                try:
                    del result['healthcheck']['_id']
                    del result['healthcheck']['server_id']
                    del result['healthcheck']['tags']
                    del result['healthcheck']['file_id']  # Custom scripts
                except:
                    pass

                result['type'] = 'healthcheck'
                append = True

            else:
                key = metric.get('key')
                # Overwrite keys for better visual presentation
                if check == 'network':
                    key = 'inbound' if key == 'i' else 'outbound'

                result['name'] = u"{0}.{1}".format(check, key)
                append = True

            if metric_type == 'plugin_global':
                result['name'] = u'{0}.{1}.{2}'.format(metric.get('plugin'),
                                                       metric.get('gauge'),
                                                       metric.get('key'))
                append = True

            result = self.mongoid_to_str(result, [
                'server_id',
                'id',
                'process_id',
                'plugin_id',
                'metric_id',
                'gauge_id',
                'healthcheck_id',
            ])

            if append:
                result_list.append(result)

        from operator import itemgetter
        sorted_list = sorted(result_list, key=itemgetter('order'))

        return sorted_list