Пример #1
0
    def do_host_query(self, arg):
        """shost_query [text=REGEX] [ids=RANGES] [>OUTPUT_PATH]

        Query the unified map for hosts that match all of the given conditions.

        Conditions:
            text - regular expression to match host names (e.g. "node00.*").
            ids - comma-separated ranges to match IDs (e.g. "1,3-5,11").

        Optional output redirection:
            host_query command can redirect the query results into a file
            (overwrite). Simply add `>OUTPUT_PATH` at the end of the command
            (OUTPUT_PATH is the path to the file), and the query results will be
            redirected there.
        """
        global service
        (kwargs, args) = (self.kwargs, self.args)
        text = kwargs["text"]
        ids = kwargs["ids"]
        if text:
            text = re.compile(text)
        if ids:
            ids = abhttp.IDSet(ids)
        result = []
        for h in service.uhost_iter():
            if ids and h.host_id not in ids:
                continue
            if text and not text.match(h.text):
                continue
            result.append(h)
        result.sort()
        for h in result:
            print >> self.cmdout, h.host_id, h.text
Пример #2
0
 def test_tocsv(self):
     s = abhttp.IDSet()
     s.add_number(1)
     s.add_numbers(xrange(5, 11))
     s.add_numbers(xrange(21, 31))
     s.add_number(55)
     ss = s.to_csv()
     logger.warn("ss: %s", ss)
     self.assertEqual(ss, "1,5-10,21-30,55")
Пример #3
0
    def do_ptn_query(self, arg):
        """ptn_query [text=REGEX] [ids=RANGES] [format=FMT] [order=KEY_ORDER] [>OUTPUT_FILE]

        Query the unified map for patterns that match all of the given
        conditions.

        Conditions:
            text=REGEX is the regular expression to match pattern text (e.g.
            ".*error.*").

            ids=RANGES is the comma-separated ranges to match IDs (e.g.
            "1,3-5,11").

        Optional options:
            format=FMT chooses what to print. By default, the FMT is
            "%(ptn_id)s %(count)s %(first_seen)s %(last_seen)s %(text)s".

            order=KEY_ORDER orders the results according to the KEY and ORDER.
            The `ORDER` is either "asc" or "desc". The `KEY` is one of the
            following: ptn_id, count, first_seen, last_seen, eng. Examples of
            KEY_ORDER are "ptn_id_asc", "last_seen_desc", and "eng_desc".

            >OUTPUT_FILE redirects the query results into a file (overwrite).
            Simply add `>OUTPUT_PATH` at the end of the command (OUTPUT_PATH is
            the path to the file), and the query results will be redirected
            there.
        """
        global service
        (kwargs, args) = (self.kwargs, self.args)
        text = kwargs["text"]
        ids = kwargs["ids"]
        fmt = kwargs["format"]
        order = kwargs["order"]
        if text:
            text = re.compile(text)
        if ids:
            ids = abhttp.IDSet(ids)
        result = []
        for ptn in service.uptn_iter():
            if ids and ptn.ptn_id not in ids:
                continue
            if text and not text.match(ptn.text):
                continue
            DBG.ptn = ptn
            result.append(ptn)
        if order:
            _cmp = ptn_order.get_ptn_cmp(order)
            result.sort(_cmp)
        for ptn in result:
            print >> self.cmdout, ptn.format(fmt)
Пример #4
0
    def run_query_correct(self,
                          uhost_ids=None,
                          uptn_ids=None,
                          ts0=None,
                          ts1=None):
        # Test correctness of the query.
        itr = abhttp.UMsgQueryIter(self.svc,
                                   host_ids=uhost_ids,
                                   ptn_ids=uptn_ids,
                                   ts0=ts0,
                                   ts1=ts1)

        param = self.testENV.param

        ts_inc = self.ts_inc = param.TS_INC
        ts_begin = self.ts_begin = param.TS_BEGIN
        ts_len = self.ts_len = param.TS_LEN
        node_begin = self.node_begin = param.NODE_BEGIN
        node_len = self.node_len = param.NODE_LEN
        n_daemons = self.n_daemons = param.N_DAEMONS
        n_patterns = self.n_patterns = param.N_PATTERNS

        if not uhost_ids:
            uhost_ids = "%d-%d" % (node_begin, node_begin + node_len - 1)
        if not uptn_ids:
            uptn_ids = "%d-%d" % (0, n_patterns - 1)
        if ts0 == None:
            ts0 = ts_begin
        if ts1 == None:
            ts1 = ts_begin + ts_len

        # round-up for ts0
        ts0 = int((ts0 + ts_inc - 1) / ts_inc) * ts_inc
        # truncate for ts1
        ts1 = self.get_last_ts(ts1)
        citr = util.MsgGenIter(uhost_ids, uptn_ids, ts0, ts1 + 1, param=param)

        hid_set = abhttp.IDSet()
        hid_set.add_smart(uhost_ids)
        pid_set = abhttp.IDSet()
        pid_set.add_smart(uptn_ids)

        # collect messages for each timestamp
        coll = {}
        coll2 = {}
        prev_ts = ts0

        for msg2 in citr:
            msg = itr.next()
            self.assertNotEqual(msg, None)
            if msg.ts.sec != prev_ts:
                self._check_coll(coll, coll2, hid_set, pid_set)
            prev_ts = msg.ts.sec
            hid = util.host_id(msg.host)
            pid = msg.ptn_id
            coll[(hid, pid)] = msg
            hid2 = util.host_id(msg2.host)
            pid2 = msg2.ptn_id
            coll2[(hid2, pid2)] = msg2

        self.assertEqual(prev_ts, ts1)
Пример #5
0
 def test_constructor(self):
     t = abhttp.IDSet("1,5-10,21-30,55")
     s = abhttp.IDSet([1, xrange(5, 11), xrange(21, 31), 55])
     self.assertEqual(s, t)