Esempio n. 1
0
    def handle_request_and_fail(self, data):
        """Execute the livestatus request.

        This function creates a LiveStatusRequest method, calls the parser,
        handles the execution of the request and formatting of the result.

        """
        request = LiveStatusRequest(data, self.datamgr, self.query_cache,
                                    self.db, self.pnp_path, self.return_queue,
                                    self.counters)
        request.parse_input(data)
        if sorted([q.my_type
                   for q in request.queries]) == ['command', 'query', 'wait']:
            # The Multisite way
            for query in [
                    q for q in request.queries if q.my_type == 'command'
            ]:
                result = query.launch_query()
                response = query.response
                response.format_live_data(result, query.columns, query.aliases)
                output, keepalive = response.respond()
            output = [q for q in request.queries if q.my_type == 'wait'
                      ] + [q for q in request.queries if q.my_type == 'query']
        elif sorted([q.my_type for q in request.queries]) == ['query', 'wait']:
            # The Thruk way
            output = [q for q in request.queries if q.my_type == 'wait'
                      ] + [q for q in request.queries if q.my_type == 'query']
            keepalive = True
        elif sorted([q.my_type
                     for q in request.queries]) == ['command', 'query']:
            for query in [
                    q for q in request.queries if q.my_type == 'command'
            ]:
                result = query.launch_query()
                response = query.response
                response.format_live_data(result, query.columns, query.aliases)
                output, keepalive = response.respond()
            for query in [q for q in request.queries if q.my_type == 'query']:
                # This was a simple query, respond immediately
                result = query.launch_query()
                # Now bring the retrieved information to a form which can be sent back to the client
                response = query.response
                response.format_live_data(result, query.columns, query.aliases)
                output, keepalive = response.respond()

        elif sorted([q.my_type for q in request.queries]) == ['query']:
            for query in [q for q in request.queries if q.my_type == 'query']:
                # This was a simple query, respond immediately
                result = query.launch_query()
                # Now bring the retrieved information to a form which can be sent back to the client
                response = query.response
                response.format_live_data(result, query.columns, query.aliases)
                output, keepalive = response.respond()
        elif sorted([q.my_type for q in request.queries]) == ['command']:
            for query in [
                    q for q in request.queries if q.my_type == 'command'
            ]:
                result = query.launch_query()
                response = query.response
                response.format_live_data(result, query.columns, query.aliases)
                output, keepalive = response.respond()
        elif [q.my_type for q in request.queries
              if q.my_type != 'command'] == []:
            # Only external commands. Thruk uses it when it sends multiple
            # objects into a downtime.
            for query in [
                    q for q in request.queries if q.my_type == 'command'
            ]:
                result = query.launch_query()
                response = query.response
                response.format_live_data(result, query.columns, query.aliases)
                output, keepalive = response.respond()
        else:
            # We currently do not handle this kind of composed request
            output = ""
            logger.error(
                "[Livestatus] We currently do not handle this kind of composed request"
            )
            print sorted([q.my_type for q in request.queries])

        logger.debug("[Livestatus] Request duration %.4fs" %
                     (time.time() - request.tic))
        return output, keepalive