示例#1
0
文件: actor.py 项目: smetj/wishbone
    def generateEvent(self, data={}, destination=None):
        '''
        Generates a new event.

        This function can get overridden by
        ``wishbone.module.InputModule._generateNativeEvent``.

        The provided ``data`` will be traversed in search of valid templates
        which then will be rendered.

        Args:
            data (``data``): The payload to add to the event.
            destination (None): The destination key to write the data to

        Returns:
            wishbone.event.Event: An event containing ``data`` as a payload.

        '''
        if destination in [None, "data"]:
            event = Wishbone_Event(data)
            event.renderField(destination, self.env_template)
        else:
            event = Wishbone_Event()
            event.set(data, destination)
            event.renderField(destination, self.env_template)
        return event
示例#2
0
 def timer(self):
     while self.loop():
         if self.cron.check_trigger(time.localtime(time.time())[:5]):
             self.logging.info("Cron fired.")
             e = Event()
             e.set(self.kwargs.payload, self.kwargs.field)
             self.submit(e, self.pool.queue.outbox)
         sleep(60)
示例#3
0
 def timer(self):
     while self.loop():
         if self.cron.check_trigger(time.localtime(time.time())[:5]):
             self.logging.info("Cron fired.")
             e = Event()
             e.set(self.kwargs.payload, self.kwargs.field)
             self.submit(e, self.pool.queue.outbox)
         sleep(60)
示例#4
0
def test_module_merge():

    a = get_actor({"merge": ['@tmp.one', '@tmp.two', '@data']})
    e = Event()
    e.set(["one"], "@tmp.one")
    e.set(["two"], "@tmp.two")
    a.pool.queue.inbox.put(e)
    one = getter(a.pool.queue.outbox)
    assert one.get() == ["one", "two"]
示例#5
0
def test_module_merge():

    a = get_actor({"merge": ['@tmp.one', '@tmp.two', '@data']})
    e = Event()
    e.set(["one"], "@tmp.one")
    e.set(["two"], "@tmp.two")
    a.pool.queue.inbox.put(e)
    one = getter(a.pool.queue.outbox)
    assert one.get() == ["one", "two"]
示例#6
0
    def produce(self):

        while self.loop():
            message = self.generateMessage(self.kwargs.message)
            event = Event(message)
            for key, value in list(self.kwargs.additional_values.items()):
                event.set(value, key)
            self.submit(event, self.pool.queue.outbox)
            self.sleep()

        self.logging.info("Stopped producing events.")
示例#7
0
    def produce(self):

        while self.loop():
            message = self.generateMessage(self.kwargs.message)
            event = Event(message)
            for key, value in list(self.kwargs.additional_values.items()):
                event.set(value, key)
            self.submit(event, self.pool.queue.outbox)
            self.sleep()

        self.logging.info("Stopped producing events.")
    def processEvent(self, data, meta, queue):
        '''
        The callback executed for each Wishbone event to be created out of a
        single http request.
        '''

        e = Event(data)
        e.set(meta, 'tmp.%s' % (self.name))
        e.renderKwargs(self.kwargs_template)
        self.submit(e, queue)

        return self.getResponse(e, queue)
    def processEvent(self, data, meta, queue):
        '''
        The callback executed for each Wishbone event to be created out of a
        single http request.
        '''

        e = Event(data)
        e.set(meta, 'tmp.%s' % (self.name))
        e.renderKwargs(self.kwargs_template)
        self.submit(e, queue)

        return self.getResponse(e, queue)
def test_file_load():

    import os
    import yaml
    import shutil

    os.mkdir('/tmp/wishbone_tests')
    actor_config = ActorConfig('match', 100, 1, {}, "")
    match = Match(actor_config, location="/tmp/wishbone_tests")
    match.pool.createQueue("file")
    match.pool.queue.file.disableFallThrough()
    match.pool.queue.inbox.disableFallThrough()

    #Test 1
    rule_1 = {
        "condition": [
            {"file": "re:.*?one.*"}
        ],
        "queue": [
            {"file": {}}
        ]
    }

    with open('/tmp/wishbone_tests/one.yaml', 'w') as one:
        one.write(yaml.dump(rule_1, default_flow_style=False))
    match.start()
    sleep(1)

    e = Event("test")
    e.set({"file": "zero one two"})
    match.pool.queue.inbox.put(e)

    assert getter(match.pool.queue.file).get()["file"] == "zero one two"

    # Test 2
    rule_2 = {
        "condition": [
            {"file": "re:.*?two.*"}
        ],
        "queue": [
            {"file": {}}
        ]
    }
    with open('/tmp/wishbone_tests/two.yaml', 'w') as one:
        one.write(yaml.dump(rule_2, default_flow_style=False))
    sleep(1)

    e = Event("test")
    e.set({"file": "one two three"})
    match.pool.queue.inbox.put(e)
    assert getter(match.pool.queue.file).get()["file"] == "one two three"
    shutil.rmtree('/tmp/wishbone_tests')
示例#11
0
def test_module_humanlogformat():

    actor_config = ActorConfig('humanlogformat', 100, 1, {}, "")
    humanlogformat = HumanLogFormat(actor_config, colorize=False, ident='setup.py')
    humanlogformat.pool.queue.inbox.disableFallThrough()
    humanlogformat.pool.queue.outbox.disableFallThrough()
    humanlogformat.start()

    e = Event('test')
    e.set(Log(1367682301.430527, 6, 3342, 'Router', 'Received SIGINT. Shutting down.'))

    humanlogformat.pool.queue.inbox.put(e)
    one = getter(humanlogformat.pool.queue.outbox)
    assert one.get() == "2013-05-04T17:45:01 setup.py[3342]: informational Router: Received SIGINT. Shutting down."
def test_bigger_than():

    rule = {"bigger": {
        "condition": [
            {"bigger": ">:10"}
        ],
        "queue": [
            {"bigger": {}}
        ]
    }}

    actor = generate_actor(rule)
    e = Event("test")
    e.set({"bigger": "100"})
    actor.pool.queue.inbox.put(e)
    assert getter(actor.pool.queue.bigger).get()["bigger"] == "100"
def test_module_graphite():

    actor_config = ActorConfig('graphite', 100, 1, {}, "")
    graphite = Graphite(actor_config, template='{type}.{source}.{name} {value} {time}')
    graphite.pool.queue.inbox.disableFallThrough()
    graphite.pool.queue.outbox.disableFallThrough()
    graphite.start()

    e = Event('test')
    m = Metric(1381002603.726132, "wishbone", "hostname", "queue.outbox.in_rate", 0, "", ())
    e.set(m)

    graphite.pool.queue.inbox.put(e)
    one = getter(graphite.pool.queue.outbox)

    assert one.get() == "wishbone.hostname.queue.outbox.in_rate 0 1381002603.73"
def test_negative_list_membership():

    rule = {"list_membership": {
        "condition": [
            {"list_membership": "!in:test"}
        ],
        "queue": [
            {"list_membership": {}}
        ]
    }}

    actor = generate_actor(rule)
    one = Event("test")
    one.set({"list_membership": ["one", "three", "two"]})
    actor.pool.queue.inbox.put(one)
    assert "test" not in getter(actor.pool.queue.list_membership).get()["list_membership"]
def test_not_equal_to():

    rule = {"equal": {
        "condition": [
            {"equal": "!=:100"}
        ],
        "queue": [
            {"equal": {}}
        ]
    }}

    actor = generate_actor(rule)
    one = Event("test")
    one.set({"equal": "101"})
    actor.pool.queue.inbox.put(one)
    assert int(getter(actor.pool.queue.equal).get()["equal"]) == 101
def test_smaller_than():

    rule = {"smaller": {
        "condition": [
            {"smaller": "<:100"}
        ],
        "queue": [
            {"smaller": {}}
        ]
    }}

    actor = generate_actor(rule)
    one = Event("test")
    one.set({"smaller": "99"})
    actor.pool.queue.inbox.put(one)
    assert int(getter(actor.pool.queue.smaller).get()["smaller"]) < 100
def test_negative_regex():

    rule = {"neg_regex": {
        "condition": [
            {"neg_regex": "!re:.*?two.*"}
        ],
        "queue": [
            {"neg_regex": {}}
        ]
    }}

    actor = generate_actor(rule)
    e = Event("test")
    e.set({"neg_regex": "one twwo three"})
    actor.pool.queue.inbox.put(e)
    assert getter(actor.pool.queue.neg_regex).get()["neg_regex"] == "one twwo three"
示例#18
0
def test_smaller_than():

    rule = {
        "smaller": {
            "condition": [{
                "smaller": "<:100"
            }],
            "queue": [{
                "smaller": {}
            }]
        }
    }

    actor = generate_actor(rule)
    one = Event("test")
    one.set({"smaller": "99"})
    actor.pool.queue.inbox.put(one)
    assert int(getter(actor.pool.queue.smaller).get()["smaller"]) < 100
示例#19
0
def test_bigger_than():

    rule = {
        "bigger": {
            "condition": [{
                "bigger": ">:10"
            }],
            "queue": [{
                "bigger": {}
            }]
        }
    }

    actor = generate_actor(rule)
    e = Event("test")
    e.set({"bigger": "100"})
    actor.pool.queue.inbox.put(e)
    assert getter(actor.pool.queue.bigger).get()["bigger"] == "100"
示例#20
0
文件: fresh.py 项目: smetj/wishbone
    def countDown(self):

        while self.loop():
            if self._counter > 0:
                self._counter -= 1
                sleep(1)
            else:
                self.logging.info("Timeout of %s seconds expired.  Generated timeout event." % (self.kwargs.timeout))
                self._incoming = False
                while self.loop() and not self._incoming:
                    e = Event()
                    e.set(self.kwargs.timeout_payload)
                    self.submit(e, self.pool.queue.timeout)
                    self._sleeper(self.kwargs.repeat_interval)
                self.logging.info("Incoming data resumed. Sending recovery event.")
                e = Event()
                e.set(self.kwargs.recovery_payload)
                self.submit(e, self.pool.queue.timeout)
示例#21
0
def test_not_equal_to():

    rule = {
        "equal": {
            "condition": [{
                "equal": "!=:100"
            }],
            "queue": [{
                "equal": {}
            }]
        }
    }

    actor = generate_actor(rule)
    one = Event("test")
    one.set({"equal": "101"})
    actor.pool.queue.inbox.put(one)
    assert int(getter(actor.pool.queue.equal).get()["equal"]) == 101
示例#22
0
def test_negative_regex():

    rule = {
        "neg_regex": {
            "condition": [{
                "neg_regex": "!re:.*?two.*"
            }],
            "queue": [{
                "neg_regex": {}
            }]
        }
    }

    actor = generate_actor(rule)
    e = Event("test")
    e.set({"neg_regex": "one twwo three"})
    actor.pool.queue.inbox.put(e)
    assert getter(
        actor.pool.queue.neg_regex).get()["neg_regex"] == "one twwo three"
示例#23
0
def test_negative_list_membership():

    rule = {
        "list_membership": {
            "condition": [{
                "list_membership": "!in:test"
            }],
            "queue": [{
                "list_membership": {}
            }]
        }
    }

    actor = generate_actor(rule)
    one = Event("test")
    one.set({"list_membership": ["one", "three", "two"]})
    actor.pool.queue.inbox.put(one)
    assert "test" not in getter(
        actor.pool.queue.list_membership).get()["list_membership"]
def test_smaller_than_equal_to():

    rule = {"smaller_equal": {
        "condition": [
            {"smaller_equal": "<=:100"}
        ],
        "queue": [
            {"smaller_equal": {}}
        ]
    }}

    actor = generate_actor(rule)
    one = Event("test")
    one.set({"smaller_equal": "100"})
    two = Event("test")
    two.set({"smaller_equal": "99"})
    actor.pool.queue.inbox.put(one)
    actor.pool.queue.inbox.put(two)
    assert int(getter(actor.pool.queue.smaller_equal).get()["smaller_equal"]) == 100
    assert int(getter(actor.pool.queue.smaller_equal).get()["smaller_equal"]) < 100
 def scheduler(self, url):
     while self.loop():
         try:
             response = requests.get(
                 url,
                 auth=(self.kwargs.username, self.kwargs.password),
                 allow_redirects=self.kwargs.allow_redirects,
                 timeout=self.kwargs.timeout
             )
         except requests.exceptions.Timeout:
             self.logging.warning("Timeout occurred fetching resource.")
         except Exception as err:
             self.logging.warning("Problem requesting resource.  Reason: %s" % (err))
             sleep(1)
         else:
             event = Event(response.text)
             event.set(response.status_code, "@tmp.%s.status_code" % (self.name))
             event.set(response.url, "@tmp.%s.url" % (self.name))
             self.submit(event, self.pool.queue.outbox)
             sleep(self.kwargs.interval)
示例#26
0
文件: fresh.py 项目: zarath/wishbone
    def countDown(self):

        while self.loop():
            if self._counter > 0:
                self._counter -= 1
                sleep(1)
            else:
                self.logging.info(
                    "Timeout of %s seconds expired.  Generated timeout event."
                    % (self.kwargs.timeout))
                self._incoming = False
                while self.loop() and not self._incoming:
                    e = Event()
                    e.set(self.kwargs.timeout_payload)
                    self.submit(e, self.pool.queue.timeout)
                    self._sleeper(self.kwargs.repeat_interval)
                self.logging.info(
                    "Incoming data resumed. Sending recovery event.")
                e = Event()
                e.set(self.kwargs.recovery_payload)
                self.submit(e, self.pool.queue.timeout)
示例#27
0
def test_file_load():

    import os
    import yaml
    import shutil

    os.mkdir('/tmp/wishbone_tests')
    actor_config = ActorConfig('match', 100, 1, {}, "")
    match = Match(actor_config, location="/tmp/wishbone_tests")
    match.pool.createQueue("file")
    match.pool.queue.file.disableFallThrough()
    match.pool.queue.inbox.disableFallThrough()

    #Test 1
    rule_1 = {"condition": [{"file": "re:.*?one.*"}], "queue": [{"file": {}}]}

    with open('/tmp/wishbone_tests/one.yaml', 'w') as one:
        one.write(yaml.dump(rule_1, default_flow_style=False))
    match.start()
    sleep(1)

    e = Event("test")
    e.set({"file": "zero one two"})
    match.pool.queue.inbox.put(e)

    assert getter(match.pool.queue.file).get()["file"] == "zero one two"

    # Test 2
    rule_2 = {"condition": [{"file": "re:.*?two.*"}], "queue": [{"file": {}}]}
    with open('/tmp/wishbone_tests/two.yaml', 'w') as one:
        one.write(yaml.dump(rule_2, default_flow_style=False))
    sleep(1)

    e = Event("test")
    e.set({"file": "one two three"})
    match.pool.queue.inbox.put(e)
    assert getter(match.pool.queue.file).get()["file"] == "one two three"
    shutil.rmtree('/tmp/wishbone_tests')
示例#28
0
def test_smaller_than_equal_to():

    rule = {
        "smaller_equal": {
            "condition": [{
                "smaller_equal": "<=:100"
            }],
            "queue": [{
                "smaller_equal": {}
            }]
        }
    }

    actor = generate_actor(rule)
    one = Event("test")
    one.set({"smaller_equal": "100"})
    two = Event("test")
    two.set({"smaller_equal": "99"})
    actor.pool.queue.inbox.put(one)
    actor.pool.queue.inbox.put(two)
    assert int(getter(
        actor.pool.queue.smaller_equal).get()["smaller_equal"]) == 100
    assert int(getter(
        actor.pool.queue.smaller_equal).get()["smaller_equal"]) < 100
    def handleMessage(self, message):

        e = Event("\n".join(message.arguments))
        e.set(message.type, '@tmp.%s.type' % (self.name))
        e.set(message.source, '@tmp.%s.source' % (self.name))

        if message.type == "pubmsg":
            e.set(message.target, '@tmp.%s.channel' % (self.name))
            self.submit(e, getattr(self.pool.queue, message.target.strip('#').lower()))
        elif message.type == "privmsg":
            self.submit(e, getattr(self.pool.queue, "priv__%s" % (self.kwargs.nickname)))

        self.submit(e.clone(), self.pool.queue.outbox)
示例#30
0
def test_event_set():

    e = Event({"one": 1, "two": {"three": 3}})
    e.set({"four": 4}, "data.two.three")
    assert e.dump()["data"]["two"]["three"]["four"] == 4