2018-03-07 18:07:40 +01:00
|
|
|
#!/usr/bin/env python3
|
2017-08-24 16:02:28 +02:00
|
|
|
|
2017-10-20 16:55:07 +02:00
|
|
|
import time, datetime
|
2017-08-24 16:02:28 +02:00
|
|
|
import zmq
|
2017-12-04 16:44:44 +01:00
|
|
|
import logging
|
2017-08-24 16:02:28 +02:00
|
|
|
import redis
|
2017-09-11 14:53:06 +02:00
|
|
|
import configparser
|
2017-10-25 10:41:46 +02:00
|
|
|
import argparse
|
2017-09-11 14:53:06 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import json
|
2017-08-24 16:02:28 +02:00
|
|
|
|
2018-03-31 12:36:17 +02:00
|
|
|
configfile = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config/config.cfg')
|
2017-09-11 14:53:06 +02:00
|
|
|
cfg = configparser.ConfigParser()
|
|
|
|
cfg.read(configfile)
|
2017-12-05 10:23:40 +01:00
|
|
|
logDir = cfg.get('Log', 'directory')
|
|
|
|
logfilename = cfg.get('Log', 'filename')
|
|
|
|
logPath = os.path.join(logDir, logfilename)
|
|
|
|
if not os.path.exists(logDir):
|
|
|
|
os.makedirs(logDir)
|
2017-12-05 10:32:12 +01:00
|
|
|
logging.basicConfig(filename=logPath, filemode='a', level=logging.INFO)
|
2017-12-05 09:56:32 +01:00
|
|
|
logger = logging.getLogger('zmq_subscriber')
|
2017-12-04 16:44:44 +01:00
|
|
|
|
2017-11-06 18:40:44 +01:00
|
|
|
ZMQ_URL = cfg.get('RedisGlobal', 'zmq_url')
|
2017-10-25 12:12:14 +02:00
|
|
|
CHANNEL = cfg.get('RedisLog', 'channel')
|
2017-11-30 08:17:53 +01:00
|
|
|
LISTNAME = cfg.get('RedisLIST', 'listName')
|
2017-11-03 09:32:07 +01:00
|
|
|
|
2017-11-30 08:17:53 +01:00
|
|
|
serv_list = redis.StrictRedis(
|
2017-10-27 16:36:27 +02:00
|
|
|
host=cfg.get('RedisGlobal', 'host'),
|
|
|
|
port=cfg.getint('RedisGlobal', 'port'),
|
2017-11-30 08:17:53 +01:00
|
|
|
db=cfg.getint('RedisLIST', 'db'))
|
2017-09-11 14:53:06 +02:00
|
|
|
|
|
|
|
|
2017-11-08 17:37:03 +01:00
|
|
|
###############
|
|
|
|
## MAIN LOOP ##
|
|
|
|
###############
|
|
|
|
|
2017-11-30 08:17:53 +01:00
|
|
|
def put_in_redis_list(zmq_name, content):
|
2017-11-30 16:04:03 +01:00
|
|
|
content = content.decode('utf8')
|
2017-11-30 08:17:53 +01:00
|
|
|
to_add = {'zmq_name': zmq_name, 'content': content}
|
2017-11-30 16:04:03 +01:00
|
|
|
serv_list.lpush(LISTNAME, json.dumps(to_add))
|
2017-12-04 16:44:44 +01:00
|
|
|
logger.debug('Pushed: {}'.format(json.dumps(to_add)))
|
2017-10-13 15:03:09 +02:00
|
|
|
|
2017-10-25 10:41:46 +02:00
|
|
|
def main(zmqName):
|
2017-10-25 12:12:14 +02:00
|
|
|
context = zmq.Context()
|
|
|
|
socket = context.socket(zmq.SUB)
|
|
|
|
socket.connect(ZMQ_URL)
|
|
|
|
socket.setsockopt_string(zmq.SUBSCRIBE, '')
|
|
|
|
|
2017-10-13 15:03:09 +02:00
|
|
|
while True:
|
2017-11-27 10:05:28 +01:00
|
|
|
try:
|
|
|
|
content = socket.recv()
|
|
|
|
content.replace(b'\n', b'') # remove \n...
|
2017-11-30 08:17:53 +01:00
|
|
|
put_in_redis_list(zmqName, content)
|
2017-11-27 10:05:28 +01:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
return
|
2017-10-13 15:03:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2017-10-25 10:41:46 +02:00
|
|
|
|
2017-12-04 11:14:25 +01:00
|
|
|
parser = argparse.ArgumentParser(description='A zmq subscriber. It subscribes to a ZNQ then redispatch it to the misp-dashboard')
|
2017-10-27 08:49:47 +02:00
|
|
|
parser.add_argument('-n', '--name', required=False, dest='zmqname', help='The ZMQ feed name', default="MISP Standard ZMQ")
|
2017-10-25 12:12:14 +02:00
|
|
|
parser.add_argument('-u', '--url', required=False, dest='zmqurl', help='The URL to connect to', default=ZMQ_URL)
|
2017-10-25 10:41:46 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
main(args.zmqname)
|