8
8
from pathlib import Path
9
9
from uuid import uuid4
10
10
import re
11
+ import redis
11
12
12
13
from intelmq .lib .bot import OutputBot
13
14
from intelmq .lib .exceptions import MissingDependencyError
@@ -35,6 +36,12 @@ class MISPFeedOutputBot(OutputBot):
35
36
misp_org_name = None
36
37
misp_org_uuid = None
37
38
output_dir : str = "/opt/intelmq/var/lib/bots/mispfeed-output" # TODO: should be path
39
+ queue_db : int = 2
40
+ queue_host : str = "localhost"
41
+ queue_name : str = None
42
+ queue_password : str = None
43
+ queue_port : int = 6379
44
+ batch_size : int = 100
38
45
_is_multithreadable : bool = False
39
46
40
47
@staticmethod
@@ -45,6 +52,13 @@ def check_output_dir(dirname):
45
52
return True
46
53
47
54
def init (self ):
55
+ # Set up redis connection for length checks
56
+ if not self .queue_name :
57
+ self .queue_name = self .source_queue
58
+ self .redis = self .connect_redis ()
59
+
60
+ self .event_batch = []
61
+
48
62
if MISPEvent is None and import_fail_reason == 'syntax' :
49
63
raise MissingDependencyError ("pymisp" ,
50
64
version = '>=2.4.117.3' ,
@@ -105,21 +119,40 @@ def process(self):
105
119
106
120
event = self .receive_message ().to_dict (jsondict_as_string = True )
107
121
108
- obj = self .current_event .add_object (name = 'intelmq_event' )
109
- for object_relation , value in event .items ():
110
- try :
111
- obj .add_attribute (object_relation , value = value )
112
- except NewAttributeError :
113
- # This entry isn't listed in the harmonization file, ignoring.
114
- pass
122
+ current_queue_len = self .redis .llen (self .queue_name )
123
+ if current_queue_len % self .batch_size == 0 :
124
+ self .flush_batch ()
125
+ else :
126
+ self .event_batch .append (event )
127
+
128
+ self .acknowledge_message ()
129
+
130
+ def connect_redis (self ):
131
+ return redis .Redis (
132
+ host = self .queue_host ,
133
+ port = self .queue_port ,
134
+ db = self .queue_db ,
135
+ password = self .queue_password ,
136
+ )
137
+
138
+ def flush_batch (self ):
139
+ for event in self .event_batch :
140
+ obj = self .current_event .add_object (name = 'intelmq_event' )
141
+ for object_relation , value in event .items ():
142
+ try :
143
+ obj .add_attribute (object_relation , value = value )
144
+ except NewAttributeError :
145
+ # This entry isn't listed in the harmonization file, ignoring.
146
+ pass
115
147
116
148
feed_output = self .current_event .to_feed (with_meta = False )
117
149
118
150
with self .current_file .open ('w' ) as f :
119
151
json .dump (feed_output , f )
120
152
121
153
feed_meta_generator (self .output_dir )
122
- self .acknowledge_message ()
154
+
155
+ self .event_batch .clear ()
123
156
124
157
@staticmethod
125
158
def check (parameters ):
0 commit comments