Exemplo n.º 1
0
    def _create_task_and_response(self,
                                  task,
                                  msg=None,
                                  detail_dict=None,
                                  tidlock=None,
                                  cache_result=None,
                                  cache_timeout=None,
                                  task_kwargs=None):
        tid, err, res = self._create_task(task,
                                          msg=msg,
                                          tidlock=tidlock,
                                          cache_result=cache_result,
                                          cache_timeout=cache_timeout,
                                          task_kwargs=task_kwargs)

        # Do not log on error
        if err:
            obj, msg = None, None
        else:
            obj = self._mon_server

        return mgmt_task_response(self.request,
                                  tid,
                                  err,
                                  res,
                                  msg=msg,
                                  obj=obj,
                                  api_view=self._apiview,
                                  detail_dict=detail_dict)
Exemplo n.º 2
0
def call_mon_history_task(request, task_function, view_fun_name, obj, dc_bound,
                          serializer, data, graph, graph_settings):
    """Function that calls task_function callback and returns output mgmt_task_response()"""
    _apiview_ = {
        'view': view_fun_name,
        'method': request.method,
        'hostname': obj.hostname,
        'graph': graph,
        'graph_params': serializer.object.copy(),
    }

    result = serializer.object.copy()
    result['desc'] = graph_settings.get('desc', '')
    result['hostname'] = obj.hostname
    result['graph'] = graph
    result['options'] = graph_settings.get('options', {})
    result['update_interval'] = graph_settings.get('update_interval', None)
    tidlock = '%s obj:%s graph:%s item_id:%s since:%d until:%d' % (
        task_function.__name__, obj.uuid, graph, serializer.item_id,
        round(serializer.object['since'],
              -2), round(serializer.object['until'], -2))

    item_id = serializer.item_id

    if item_id is None:
        items = graph_settings['items']
    else:
        item_dict = {'id': item_id}
        items = [i % item_dict for i in graph_settings['items']]

    if 'items_search_fun' in graph_settings:
        # noinspection PyCallingNonCallable
        items_search = graph_settings['items_search_fun'](graph_settings,
                                                          item_id)
    else:
        items_search = None

    history = graph_settings['history']
    # for VM the task_function is called without task group value because it's DC bound
    if dc_bound:
        tg = TG_DC_BOUND
    else:
        tg = TG_DC_UNBOUND

    ter = task_function.call(request,
                             obj.owner.id,
                             (obj.uuid, items, history, result, items_search),
                             tg=tg,
                             meta={'apiview': _apiview_},
                             tidlock=tidlock)
    # NOTE: cache_result=tidlock, cache_timeout=60)
    # Caching is disable here, because it makes no real sense.
    # The latest graphs must be fetched from zabbix and the older are requested only seldom.

    return mgmt_task_response(request,
                              *ter,
                              obj=obj,
                              api_view=_apiview_,
                              dc_bound=dc_bound,
                              data=data)
Exemplo n.º 3
0
    def put(self):
        assert self.request.dc.is_default()

        ser = UpdateSerializer(self.request, data=self.data)

        if not ser.is_valid():
            return FailureTaskResponse(self.request, ser.errors, dc_bound=False)

        version = ser.data['version']
        from core.version import __version__ as mgmt_version
        # noinspection PyUnboundLocalVariable
        if version == ('v' + mgmt_version) and not ser.data.get('force'):
            raise PreconditionRequired('System is already up-to-date')

        obj = self.request.dc
        msg = LOG_SYSTEM_UPDATE
        _apiview_ = {
            'view': 'system_update',
            'method': self.request.method,
            'version': version,
        }
        meta = {
            'apiview': _apiview_,
            'msg': LOG_SYSTEM_UPDATE,
        }
        task_kwargs = ser.data.copy()
        task_kwargs['dc_id'] = obj.id

        tid, err, res = system_update.call(self.request, None, (), kwargs=task_kwargs, meta=meta,
                                           tg=TG_DC_UNBOUND, tidlock=self.LOCK)
        if err:
            msg = obj = None  # Do not log an error here

        return mgmt_task_response(self.request, tid, err, res, msg=msg, obj=obj, api_view=_apiview_, dc_bound=False,
                                  data=self.data, detail_dict=ser.detail_dict(force_full=True))
Exemplo n.º 4
0
    def get(self):
        request = self.request
        ser = AlertSerializer(request, data=self.data)

        if not ser.is_valid():
            return FailureTaskResponse(request, ser.errors)

        dc_bound = ser.data['dc_bound']

        if dc_bound:
            tg = TG_DC_BOUND
        else:
            tg = TG_DC_UNBOUND

            if not request.dc.is_default():
                request.dc = DefaultDc()  # Warning: Changing request.dc
                logger.info(
                    '"%s %s" user="******" _changed_ dc="%s" permissions=%s',
                    request.method, request.path, request.user.username,
                    request.dc.name, request.dc_user_permissions)

                if not request.dc.settings.MON_ZABBIX_ENABLED:  # dc1_settings
                    raise Http404

        _apiview_ = {'view': 'mon_alert_list', 'method': request.method}
        _tidlock = [
            'mon_alert_list',
            'dc_id=%s' % request.dc.id,
            'vm_uuids=%s' % ','.join(ser.vms or ()),
            'node_uuids=%s' % ','.join(ser.nodes or ()),
        ]
        task_kwargs = {
            'vm_uuids': ser.vms,
            'node_uuids': ser.nodes,
        }

        for key, val in ser.data.items():
            _apiview_[key] = val
            if not (key.startswith('vm_') or key.startswith('node_')):
                task_kwargs[key] = val
                _tidlock.append('%s=%s' % (key, to_string(val)))

        tidlock = ':'.join(_tidlock)
        ter = mon_alert_list.call(request,
                                  None, (request.dc.id, ),
                                  kwargs=task_kwargs,
                                  meta={'apiview': _apiview_},
                                  tg=tg,
                                  tidlock=tidlock,
                                  cache_result=tidlock,
                                  cache_timeout=self.cache_timeout)

        return mgmt_task_response(request,
                                  *ter,
                                  obj=request.dc,
                                  api_view=_apiview_,
                                  dc_bound=dc_bound,
                                  data=self.data,
                                  detail_dict=ser.detail_dict(force_full=True))
Exemplo n.º 5
0
    def get(self):
        request, node = self.request, self.node

        if node.status not in node.STATUS_AVAILABLE_MONITORING:
            raise NodeIsNotOperational

        yyyymm, since, until, current_month = parse_yyyymm(
            self.yyyymm, node.created.replace(tzinfo=None))

        _apiview_ = {
            'view': 'mon_node_sla',
            'method': request.method,
            'hostname': node.hostname,
            'yyyymm': yyyymm
        }

        _since = int(since.strftime('%s'))
        _until = int(until.strftime('%s')) - 1
        tidlock = 'mon_node_sla node:%s yyyymm:%s' % (node.uuid, yyyymm)

        if current_month:
            cache_timeout = 300
        else:
            cache_timeout = 86400

        ter = t_mon_node_sla.call(request,
                                  node.owner.id,
                                  (node.hostname, yyyymm, _since, _until),
                                  kwargs={'node_uuid': node.uuid},
                                  meta={'apiview': _apiview_},
                                  tg=TG_DC_UNBOUND,
                                  tidlock=tidlock,
                                  cache_result=tidlock,
                                  cache_timeout=cache_timeout)

        return mgmt_task_response(request,
                                  *ter,
                                  obj=node,
                                  api_view=_apiview_,
                                  dc_bound=False,
                                  data=self.data)
Exemplo n.º 6
0
    def get(self):
        request, vm = self.request, self.vm

        if vm.status not in vm.STATUS_OPERATIONAL:
            raise VmIsNotOperational

        yyyymm, since, until, current_month = parse_yyyymm(
            self.yyyymm, vm.created.replace(tzinfo=None))

        _apiview_ = {
            'view': 'mon_vm_sla',
            'method': request.method,
            'hostname': vm.hostname,
            'yyyymm': yyyymm
        }

        _since = int(since.strftime('%s'))
        _until = int(until.strftime('%s')) - 1
        args = (vm.hostname, yyyymm, vm.node_history(_since, _until))
        tidlock = 'mon_vm_sla vm:%s yyyymm:%s' % (vm.uuid, yyyymm)

        if current_month:
            cache_timeout = 300
        else:
            cache_timeout = 86400

        ter = t_mon_vm_sla.call(request,
                                vm.owner.id,
                                args,
                                kwargs={'vm_uuid': vm.uuid},
                                meta={'apiview': _apiview_},
                                tidlock=tidlock,
                                cache_result=tidlock,
                                cache_timeout=cache_timeout)

        return mgmt_task_response(request,
                                  *ter,
                                  vm=vm,
                                  api_view=_apiview_,
                                  data=self.data)