2 # Copyright 2017 xFlow Research Pvt. Ltd
3 # This file is part of MON module
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact with: wajeeha.hamid@xflowresearch.com
23 AWS-Plugin implements all the methods of MON to interact with AWS using the BOTO client
26 __author__
= "Wajeeha Hamid"
27 __date__
= "31-August-2017"
30 from connection
import Connection
31 from metric_alarms
import MetricAlarm
33 from kafka
import KafkaConsumer
34 from kafka
.errors
import KafkaError
36 exit("Kafka Error. Try activating your Kafka Services")
39 """Receives Alarm info from MetricAlarm and connects with the consumer/producer """
41 self
.conn
= Connection()
42 self
.metricAlarm
= MetricAlarm()
44 server
= {'server': 'localhost:9092', 'topic': 'alarms'}
45 #Initialize a Consumer object to consume message from the SO
46 self
._consumer
= KafkaConsumer(server
['topic'],
48 bootstrap_servers
=server
['server'])
49 #---------------------------------------------------------------------------------------------------------------------------
51 """Connecting instances with CloudWatch"""
52 self
.conn
.setEnvironment()
53 self
.cloudwatch_conn
= self
.conn
.connection_instance()
54 #---------------------------------------------------------------------------------------------------------------------------
55 def configure_alarm(self
,alarm_info
):
57 alarm_id
= self
.metricAlarm
.config_alarm(self
.cloudwatch_conn
,alarm_info
)
59 #---------------------------------------------------------------------------------------------------------------------------
60 def update_alarm_configuration(self
,test
):
61 alarm_id
= self
.metricAlarm
.update_alarm(self
.cloudwatch_conn
,test
)
63 #---------------------------------------------------------------------------------------------------------------------------
64 def delete_alarm(self
,alarm_id
):
65 return self
.metricAlarm
.delete_Alarm(self
.cloudwatch_conn
,alarm_id
)
66 #---------------------------------------------------------------------------------------------------------------------------
67 def get_alarms_list(self
,instance_id
):
68 return self
.metricAlarm
.alarms_list(self
.cloudwatch_conn
,instance_id
)
69 #---------------------------------------------------------------------------------------------------------------------------
70 def get_alarm_details(self
,alarm_id
):
71 return self
.metricAlarm
.alarm_details(self
.cloudwatch_conn
,alarm_id
)
72 #---------------------------------------------------------------------------------------------------------------------------
73 def get_metrics_data(self
,metric_name
,instance_id
,period
,metric_unit
):
74 return self
.metricAlarm
.metrics_data(self
.cloudwatch_conn
,metric_name
,instance_id
,period
,metric_unit
)
75 #---------------------------------------------------------------------------------------------------------------------------
76 def consumer(self
,alarm_info
):
78 for message
in self
._consumer
:
80 # Check the Functionlity that needs to be performed: topic = 'alarms'/'metrics'/'Access_Credentials'
81 if message
.topic
== "alarms":
82 log
.info("Action required against: %s" % (message
.topic
))
84 if message
.key
== "Configure_Alarm":
85 #alarm_info = json.loads(message.value)
86 alarm_id
= self
.configure_alarm(alarm_info
) #alarm_info = message.value
87 log
.info("New alarm created with alarmID: %s", alarm_id
)
88 #Keys other than Configure_Alarm and Notify_Alarm are already handled here which are not yet finalized
89 elif message
.key
== "Notify_Alarm":
90 alarm_details
= self
.get_alarm_details(alarm_info
['alarm_name'])#['alarm_id']
92 elif message
.key
== "Update_Alarm":
93 alarm_id
= self
.update_alarm_configuration(alarm_info
)
94 log
.info("Alarm Updated with alarmID: %s", alarm_id
)
96 elif message
.key
== "Delete_Alarm":
97 alarm_id
= self
.delete_alarm(alarm_info
['alarm_name'])
98 log
.info("Alarm Deleted with alarmID: %s", alarm_id
)
100 elif message
.key
== "Alarms_List":
101 self
.get_alarms_list(alarm_info
['resource_id'])#['alarm_names']
103 log
.debug("Unknown key, no action will be performed")
105 log
.info("Message topic not relevant to this plugin: %s",
107 except Exception as e
:
108 log
.error("Consumer exception: %s", str(e
))
109 #---------------------------------------------------------------------------------------------------------------------------
110 """For Testing Purpose: Required calls to Trigger defined functions """
116 alarm_info['resource_id'] = 'i-098da78cbd8304e17'
117 alarm_info['alarm_name'] = 'alarm-6'
118 alarm_info['alarm_metric'] = 'CPUUtilization'
119 alarm_info['alarm_severity'] = 'Critical'
120 alarm_info['instance_type'] = 'AWS/EC2'
121 alarm_info['alarm_statistics'] = 'Maximum'
122 alarm_info['alarm_comparison'] = '>='
123 alarm_info['alarm_threshold'] = 1.5
124 alarm_info['alarm_period'] = 60
125 alarm_info['alarm_evaluation_period'] = 1
126 alarm_info['alarm_unit'] = None
127 alarm_info['alarm_description'] = ''
129 #obj.configure_alarm(alarm_info)
130 #obj.update_alarm_configuration(alarm_info)
131 #obj.delete_alarm('alarm-5|i-098da78cbd8304e17')
132 #obj.get_alarms_list('i-098da78cbd8304e17')#['alarm_names']
133 #obj.get_alarm_details('alarm-5|i-098da78cbd8304e17')#['alarm_id']
134 #print obj.get_metrics_data('CPUUtilization','i-09462760703837b26','60',None) '''