예제 #1
0
    def init_plugin(self):
        super().init_plugin()
        self.add_view(
            "api_plugin_%s_get_layer" % self.name,
            self.api_get_layer,
            url=r"^plugin/%s/layers/(?P<layer>\S+)/$" % self.name,
            method=["GET"],
        )
        self.add_view(
            "api_plugin_%s_object_data" % self.name,
            self.api_object_data,
            url="^(?P<id>[0-9a-f]{24})/plugin/%s/object_data/$" % self.name,
            method=["GET"],
        )
        self.add_view(
            "api_plugin_%s_set_geopoint" % self.name,
            self.api_set_geopoint,
            url="^(?P<id>[0-9a-f]{24})/plugin/%s/set_geopoint/$" % self.name,
            method=["POST"],
            validate={
                "srid": StringParameter(),
                "x": FloatParameter(),
                "y": FloatParameter()
            },
        )
        self.add_view(
            "api_plugin_%s_set_layer_visibility" % self.name,
            self.api_set_layer_visibility,
            url="^plugin/%s/layer_visibility/$" % self.name,
            method=["POST"],
            validate={
                "layer": StringParameter(),
                "status": BooleanParameter()
            },
        )

        self.add_view(
            "api_plugin_%s_create" % self.name,
            self.api_create,
            url="^plugin/%s/$" % self.name,
            method=["POST"],
            validate={
                "model": DocumentParameter(ObjectModel),
                "name": UnicodeParameter(),
                "srid": StringParameter(),
                "x": FloatParameter(),
                "y": FloatParameter(),
            },
        )
예제 #2
0
    def get_validator(self, field):
        """
        Returns Parameter instance or None to clean up field
        :param field:
        :type field: Field
        :return:
        """
        from noc.core.model.fields import TagsField, TextArrayField

        if isinstance(field, BooleanField):
            return BooleanParameter()
        elif isinstance(field, IntegerField):
            return IntParameter()
        elif isinstance(field, FloatField):
            return FloatParameter()
        elif isinstance(field, DateField):
            return DateParameter()
        elif isinstance(field, DateTimeField):
            return DateTimeParameter()
        elif isinstance(field, TagsField):
            return TagsParameter(required=not field.null)
        elif isinstance(field, TextArrayField):
            return StringListParameter(required=not field.null)
        elif isinstance(field, related.ForeignKey):
            self.fk_fields[field.name] = field.remote_field.model
            return ModelParameter(field.remote_field.model, required=not field.null)
        else:
            return None
예제 #3
0
파일: conduits.py 프로젝트: fossabot/noc
 def init_plugin(self):
     super(ConduitsPlugin, self).init_plugin()
     self.add_view("api_plugin_%s_get_neighbors" % self.name,
                   self.api_get_neighbors,
                   url="^(?P<id>[0-9a-f]{24})/plugin/%s/get_neighbors/$" %
                   self.name,
                   method=["GET"])
     self.add_view(
         "api_plugin_%s_create_ducts" % self.name,
         self.api_create_ducts,
         url="^(?P<id>[0-9a-f]{24})/plugin/%s/$" % self.name,
         method=["POST"],
         validate={
             "ducts":
             DictListParameter(
                 attrs={
                     "target":
                     DocumentParameter(Object),
                     "project_distance":
                     FloatParameter(),
                     "conduits":
                     DictListParameter(
                         attrs={
                             "id": DocumentParameter(Object,
                                                     required=False),
                             "n": IntParameter(),
                             "x": IntParameter(),
                             "y": IntParameter(),
                             "status": BooleanParameter()
                         })
                 })
         })
     #
     self.conduits_model = ObjectModel.objects.filter(
         name=self.CONDUITS_MODEL).first()
예제 #4
0
def test_float_parameter():
    assert FloatParameter().clean(1.2) == 1.2
    assert FloatParameter().clean("1.2") == 1.2
    with pytest.raises(InterfaceTypeError):
        FloatParameter().clean("not a number")
    with pytest.raises(InterfaceTypeError):
        FloatParameter(min_value=10).clean(5)
    with pytest.raises(InterfaceTypeError):
        FloatParameter(max_value=7).clean(10)
    assert FloatParameter(max_value=10, default=7).clean(5) == 5
    assert FloatParameter(max_value=10, default=7).clean(None) == 7
    with pytest.raises(InterfaceTypeError):
        assert FloatParameter(max_value=10, default=15)
예제 #5
0
    def get_validator(self, field):
        """
        Returns Parameter instance or None to clean up field
        :param field:
        :type field: Field
        :return:
        """
        from noc.core.model.fields import AutoCompleteTagsField

        if isinstance(field, BooleanField):
            return BooleanParameter()
        elif isinstance(field, IntegerField):
            return IntParameter()
        elif isinstance(field, FloatField):
            return FloatParameter()
        elif isinstance(field, AutoCompleteTagsField):
            return TagsParameter(required=not field.null)
        elif isinstance(field, related.ForeignKey):
            self.fk_fields[field.name] = field.rel.to
            return ModelParameter(field.rel.to, required=not field.null)
        else:
            return None
예제 #6
0
파일: test_base.py 프로젝트: nbashev/noc
def test_float_parameter_error(raw, config):
    with pytest.raises(InterfaceTypeError):
        assert FloatParameter(**config).clean(raw)
예제 #7
0
파일: test_base.py 프로젝트: nbashev/noc
def test_float_parameter(raw, config, expected):
    assert FloatParameter(**config).clean(raw) == expected
예제 #8
0
class ProbeApplication(ExtDocApplication):
    """
    PMProbe application
    """
    title = "Probe"
    menu = "Setup | Probes"
    model = Probe
    query_fields = ["name"]

    REFRESH_CHUNK = config.getint("pm", "expired_refresh_chunk")
    REFRESH_TIMEOUT = config.getint("pm", "expired_refresh_timeout")

    @view(url="^(?P<name>[^/]+)/(?P<instance>\d+)/config/$",
          method=["GET"],
          validate={"last": DateTimeParameter(required=False)},
          access="config",
          api=True)
    def api_config(self, request, name, instance, last=None):
        """
        Get full probe configuration
        """
        probe = self.get_object_or_404(Probe, name=name)
        if not probe.user or request.user.id != probe.user.id:
            return self.response_forbidden()
        instance = int(instance)
        if instance >= probe.n_instances:
            return self.response_not_found("Invalid instance")
        probe_id = str(probe.id)
        now = datetime.datetime.now()
        # Refresh expired congfigs
        t0 = time.time()
        nr = 0
        dt = 0
        stopped = False
        for pc in ProbeConfig.objects.filter(probe_id=probe_id,
                                             instance_id=instance,
                                             expire__lt=now):
            pc.refresh()
            nr += 1
            if nr % self.REFRESH_CHUNK:
                # Check execution time
                dt = time.time() - t0
                if dt > self.REFRESH_TIMEOUT:
                    self.logger.info(
                        "%d configs has been refreshed in %s seconds. Giving up",
                        nr, dt)
                    stopped = True
                    break
        if nr and not stopped:
            self.logger.info("%d configs has been refreshed in %s seconds.",
                             nr, dt)
        # Get configs
        q = {"probe_id": probe_id, "instance_id": instance}
        if last:
            fmt = "%Y-%m-%dT%H:%M:%S.%f" if "." in last else "%Y-%m-%dT%H:%M:%S"
            last = datetime.datetime.strptime(last, fmt)
            q["changed"] = {"$gte": last}
        config = [{
            "uuid":
            pc["uuid"],
            "handler":
            pc["handler"],
            "interval":
            pc["interval"],
            "metrics": [{
                "metric": m["metric"],
                "metric_type": m["metric_type"],
                "thresholds": m["thresholds"],
                "convert": m["convert"],
                "scale": m["scale"],
                "collectors": {
                    "policy":
                    m["collectors"]["policy"],
                    "write_concern":
                    m["collectors"]["write_concern"],
                    "collectors": [{
                        "proto": c["proto"],
                        "address": c["address"],
                        "port": c["port"]
                    } for c in m["collectors"]["collectors"]]
                }
            } for m in pc["metrics"]],
            "config":
            pc["config"],
            "managed_object":
            pc.get("managed_object", None),
            "changed":
            pc["changed"].isoformat(),
            "expire":
            pc["expire"].isoformat()
        } for pc in ProbeConfig._get_collection().find(q)]
        if config:
            expire = min(c["expire"] for c in config)
            # Wipe out deleted configs
            deleted = [
                c["uuid"] for c in config if c["changed"] == c["expire"]
            ]
            if deleted:
                ProbeConfig.objects.filter(uuid__in=deleted).delete()
        else:
            expire = None
        return {
            "now": now.isoformat(),
            "last": last.isoformat() if last else None,
            "expire": expire,
            "config": config
        }

    @view(url="^(?P<name>[^/]+)/(?P<instance>\d+)/feed/$",
          method=["POST"],
          validate={
              "thresholds":
              DictListParameter(
                  attrs={
                      "managed_object": IntParameter(),
                      "metric": StringParameter(),
                      "metric_type": StringParameter(),
                      "ts": IntParameter(),
                      "value": FloatParameter(),
                      "old_state": StringParameter(),
                      "new_state": StringParameter()
                  })
          },
          access="config",
          api=True)
    def api_fmfeed(self, request, name, instance, thresholds):
        if thresholds:
            cnt = itertools.count()
            batch = NewEvent._get_collection().initialize_unordered_bulk_op()
            for t in thresholds:
                seq = struct.pack("!II", int(time.time()),
                                  cnt.next() & 0xFFFFFFFFL)
                batch.insert({
                    "timestamp":
                    datetime.datetime.fromtimestamp(t["ts"]),
                    "managed_object":
                    t["managed_object"],
                    "raw_vars": {
                        "source": "system",
                        "metric": t["metric"],
                        "metric_type": t["metric_type"],
                        "value": str(t["value"]),
                        "old_state": t["old_state"],
                        "new_state": t["new_state"]
                    },
                    "seq":
                    Binary(seq)
                })
            batch.execute(0)
예제 #9
0
import cachetools
# NOC modules
from error import ModelDataError
from noc.lib.utils import deep_copy
from noc.lib.escape import json_escape as q
from noc.sa.interfaces.base import (StringParameter, BooleanParameter,
                                    FloatParameter, IntParameter,
                                    StringListParameter)

id_lock = Lock()


T_MAP = {
    "str": StringParameter(),
    "int": IntParameter(),
    "float": FloatParameter(),
    "bool": BooleanParameter(),
    "strlist": StringListParameter()
}

A_TYPE = ["str", "int", "float", "bool", "objectid", "ref", "strlist"]


class ModelInterfaceAttr(EmbeddedDocument):
    meta = {
        "strict": False,
        "auto_create_index": False
    }
    name = StringField()
    type = StringField(choices=[(t, t) for t in A_TYPE])
    description = StringField()