Ejemplo n.º 1
0
 def setUpClass(cls):
     log.info("------- setUpClass -------")
     shutil.rmtree(STORE_PATH, ignore_errors=True)
     make_store()
     bs = bq.Bstore()
     bs.open(STORE_PATH)
     # Create TAG
     self = cls
     bs.attr_new("TAG")
     pitr = bq.Bptn_iter(bs)
     self.odd_group = odd_group = set()
     self.even_group = even_group = set()
     self.three_group = three_group = set()
     # Add some tags `odd` or `even`
     for ptn in pitr:
         ptn_id = ptn.ptn_id()
         if ptn_id % 2:
             bs.ptn_attr_value_add(ptn_id, "TAG", "odd")
             odd_group.add(ptn_id)
         else:
             bs.ptn_attr_value_add(ptn_id, "TAG", "even")
             even_group.add(ptn_id)
         # to demonstrate that we can have multiple TAG values
         if ptn_id % 3 == 0:
             bs.ptn_attr_value_add(ptn_id, "TAG", "three")
             three_group.add(ptn_id)
     # NOTE Uncomment to crash the process :D
     bs.close()
     log.info("------- setUpClass COMPLETED -------")
Ejemplo n.º 2
0
 def _test_query_tag(self, tag, result):
     bs = bq.Bstore()
     bs.open(STORE_PATH)
     lst = []
     log.info("These are `%s` patterns" % tag)
     aitr = bq.Bptn_attr_iter(bs)
     aitr.set_filter(attr_type="TAG", attr_value=tag)
     for (ptn_id, attr_type, attr_value) in aitr:
         log.info(ptn_id)
         self.assertIn(ptn_id, result)
         lst.append(ptn_id)
     self.assertEqual(len(result), len(lst))
     self.assertEqual(result, set(lst))
     del aitr
Ejemplo n.º 3
0
def GetBstore(name):
    for store in settings.SYSLOG_CFG['stores']:
        if name == store['name']:
            path = str(store['path'])
            bs = Bq.Bstore()
            bs.open(path)
            return bs
    return None
Ejemplo n.º 4
0
 def test_ptn_tags(self):
     # get tags by pattern
     bs = bq.Bstore()
     bs.open(STORE_PATH)
     log.info("pattern tags ...")
     pitr = bq.Bptn_iter(bs)
     for ptn in pitr:
         ptn_id = ptn.ptn_id()
         aitr = bq.Bptn_attr_iter(bs)
         aitr.set_filter(ptn_id=ptn_id, attr_type="TAG")
         tags = [aval for (ptnid, atype, aval) in aitr]
         log.info("%d: %s" % (ptn_id, str(tags)))
         tagset = set(tags)
         self.assertEqual(len(tags), len(tagset))
         if ptn_id % 2:
             self.assertIn("odd", tagset)
             tagset.remove("odd")
         else:
             self.assertIn("even", tagset)
             tagset.remove("even")
         if ptn_id % 3 == 0:
             self.assertIn("three", tagset)
             tagset.remove("three")
         self.assertEqual(len(tagset), 0)
Ejemplo n.º 5
0
def BqMessageQuery(bs, start, end, compId, ptnId=0):
    ''' Queries for messages with Bq api
        Takes start/end time arguments'''
    if not bs:
        return {"messages": [], "iTotalRecords": 0, "iTotalDisplayRecords": 0}
    try:
        messages = {}
        msg_list = []
        for ptnid in ptnId:
            mi = Bq.Bmsg_iter(bs)
            mi.set_filter(comp_id=compId,
                          ptn_id=ptnid,
                          tv_begin=(start, 0),
                          tv_end=(end, 0))
            for m in mi:
                if end > 0 and m.tv_sec() > end:
                    break
                msg_obj = {}
                tkn_list = []
                for tkn in m:
                    tkn_obj = {}
                    tkn_str = tkn.tkn_str()
                    tkn_obj['tkn_text'] = tkn.tkn_str()
                    tkn_list.append(tkn_obj)
                msg_obj['ptn_id'] = m.ptn_id()
                msg_obj['comp_id'] = m.comp_id()
                msg_obj['timestamp'] = m.tv_sec()
                msg_obj['tkn_list'] = tkn_list
                msg_list.append(msg_obj)
        if mi:
            del mi
        bs.close()
        messages['messages'] = msg_list
        return messages
    except Exception as e:
        e_type, e_obj, e_tb = sys.exc_info()
        log.write('BqMessageQuery: Line' + repr(e_tb.tb_lineno) + ' ' +
                  repr(e))
        if bs:
            bs.close()
        return {'BqMessageQuery Error': str(e)}
Ejemplo n.º 6
0
 def __init__(self, bs):
     self.mitr = Bq.Bmsg_iter(bs)
Ejemplo n.º 7
0
    while True:
        try:
            m = next(itr)
        except StopIteration, e:
            break
        m = m.strip()
        ts1 = m.split(' ')[0]
        if ts0 != ts1:
            listB.append(m)
            break
        listA.append(m)


benv = testenv.get_benv()

bs = Bq.Bstore()
bs.open(benv['BSTORE'])

itr0 = iter(Messages(bs))
proc = sp.Popen("./gen-log.pl", stdout=sp.PIPE, stderr=sp.PIPE, shell=True)
itr1 = iter(proc.stdout)

list00 = []
list01 = []
list10 = []
list11 = []

msg0 = next(itr0)
msg1 = next(itr1)

list01.append(msg0.strip())
Ejemplo n.º 8
0
#!/usr/bin/python

import os
import sys
import testenv
import re
import subprocess as sp

from baler import Bq

benv = testenv.get_benv()

bs = Bq.Bstore()
bs.open(benv['BSTORE'])
pitr = Bq.Bptn_iter(bs)
ptns = set(str(p) for p in pitr)

# gen-ptns.pl generates with \u2022 wildcard.
proc = sp.Popen("./gen-ptns.pl", stdout=sp.PIPE, shell=True)
r = re.compile(u"\u2022")
for p in proc.stdout:
    p = p.strip().decode('utf-8')
    p = r.sub("<dec>", p)
    p = "<host> " + str(p)
    ptns.remove(p)

assert (len(ptns) == 0)