Skip to content

Commit 2b0693f

Browse files
add environment file to run docker images in ihr cluster
1 parent e3db6ee commit 2b0693f

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

generic/detector/anomalydetector.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import sys
23
from collections import defaultdict
34
import configparser
@@ -6,8 +7,9 @@
67
import arrow
78
import msgpack
89
import statistics
9-
from confluent_kafka import Consumer, Producer, TopicPartition
1010
import confluent_kafka
11+
from confluent_kafka import Consumer, Producer, TopicPartition
12+
from confluent_kafka.admin import AdminClient, NewTopic
1113

1214
# TODO: how to handle data holes?
1315

@@ -62,17 +64,29 @@ def __init__(self, conf_fname='anomalydetector.conf'):
6264

6365
# Initialize kafka consumer
6466
self.consumer = Consumer({
65-
'bootstrap.servers': 'kafka1:9092, kafka2:9092, kafka3:9092',
67+
'bootstrap.servers': KAFKA_HOST,
6668
'group.id': self.kafka_consumer_group,
6769
'auto.offset.reset': 'earliest',
6870
})
6971

7072
self.consumer.subscribe([self.kafka_topic_in])
7173

7274
# Initialize kafka producer
73-
self.producer = Producer({'bootstrap.servers': 'kafka1:9092,kafka2:9092,kafka3:9092',
75+
self.producer = Producer({'bootstrap.servers': KAFKA_HOST,
7476
'default.topic.config': {'compression.codec': 'snappy'}})
7577

78+
# Create kafka topic
79+
admin_client = AdminClient({'bootstrap.servers': KAFKA_HOST})
80+
81+
topic_list = [NewTopic(self.kafka_topic_out, num_partitions=10, replication_factor=1)]
82+
created_topic = admin_client.create_topics(topic_list)
83+
for topic, f in created_topic.items():
84+
try:
85+
f.result() # The result itself is None
86+
logging.warning("Topic {} created".format(topic))
87+
except Exception as e:
88+
logging.warning("Failed to create topic {}: {}".format(topic, e))
89+
7690
# Set start time
7791
self.detection_starttime = self.get_current_timestamp()
7892
logging.info('Detection starttime set to: {}'.format(self.detection_starttime))
@@ -200,6 +214,13 @@ def report_anomaly(self, timestamp, datapoint, deviation):
200214

201215

202216
if __name__ == "__main__":
217+
218+
global KAFKA_HOST
219+
if 'KAFKA_HOST' in os.environ:
220+
KAFKA_HOST = os.environ["KAFKA_HOST"]
221+
else:
222+
KAFKA_HOST = 'kafka1:9092,kafka2:9092,kafka3:9092'
223+
203224
if len(sys.argv)<2:
204225
print("usage: %s config_file" % sys.argv[0])
205226
sys.exit()

ihr.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
KAFKA_HOST=10.0.1.12:9092
2+
DB_CONNECTION_STRING="host=10.0.1.155 dbname=ihr user=romain"

0 commit comments

Comments
 (0)