3131import requests
3232import os
3333import json
34+ import time
3435from sonmanobase import messaging
3536
3637logging .basicConfig (level = logging .INFO )
@@ -53,6 +54,7 @@ def __init__(self):
5354 # create and initialize broker connection
5455 self .manoconn = messaging .ManoBrokerRequestResponseConnection (self .name )
5556
57+ self .timeout = 0
5658 # register to Specific Manager Registry
5759 self .publish ()
5860
@@ -72,51 +74,45 @@ def on_registration_ok(self):
7274 self .manoconn .subscribe (self .on_alert_recieved ,'son.monitoring' )
7375 self .manoconn .publish (topic = 'specific.manager.registry.ssm.status' ,
7476 message = yaml .dump ({'status' :'Subscribed to son.monitoring topic, waiting for alert message' }))
77+
7578 def on_alert_recieved (self , ch , method , props , response ):
76- self .manoconn .publish (topic = 'specific.manager.registry.ssm.status' ,
77- message = yaml .dump ({'status' : 'SSM received an alert message ' }))
7879
7980 alert = json .loads (response )
80- if alert [ 'alertname' ] == 'mon_rule_vm_cpu_usage_85_perc' and alert [ 'exported_job' ] == "vnf" :
81+ if time . time () > self . timeout :
8182
82- LOG .info ('Alert message received' )
83- LOG .info ('Start reconfiguring vFW ...' )
84-
85- self .manoconn .publish (topic = 'specific.manager.registry.ssm.status' ,
86- message = yaml .dump ({'status' : 'cpu usage 85% alert message received, start reconfiguring vFW' }))
87- try :
83+ if alert ['alertname' ] == 'mon_rule_vm_cpu_usage_85_perc' and alert ['exported_job' ] == "vnf" :
8884
89- # retrieve vFW IP address
90- endpoint = os . environ [ 'HOST' ]
85+ LOG . info ( 'Alert message received' )
86+ LOG . info ( 'Start reconfiguring vFW ...' )
9187
92- # add flow entries to block ports 9999 and 5001
93- entry1 = requests .post (url = 'http://' + endpoint + ':8080/stats/flowentry/add' ,
94- data = json .dumps ({"dpid" : 1 , "cookie" : 200 , "priority" : 1000 ,
95- "match" : {"dl_type" : 0x0800 , "nw_proto" : 6 ,"tcp_dst" : 9999 }}))
96- entry2 = requests .post (url = 'http://' + endpoint + ':8080/stats/flowentry/add' ,
97- data = json .dumps ({"dpid" : 1 , "cookie" : 200 , "priority" : 1000 ,
98- "match" : {"dl_type" : 0x0800 , "nw_proto" : 17 , "udp_dst" : 5001 }}))
99- LOG .info ('vFW reconfiguration succeeded' )
100- self .manoconn .publish (topic = 'specific.manager.registry.ssm.status' ,
101- message = yaml .dump ({'status' : 'vFW reconfiguration succeeded' }))
102- except BaseException as err :
103- LOG .info ('vFW reconfiguration failed' )
10488 self .manoconn .publish (topic = 'specific.manager.registry.ssm.status' ,
105- message = yaml .dump ({'status' : 'vFW reconfiguration failed ==> "{0}"' .format (err )}))
106-
107-
108- # #check if the call was successful
109- # if (entry1.status_code == 200 and entry2.status_code == 200):
110- # LOG.info('vFW reconfiguration succeeded')
111- # self.manoconn.publish(topic='specific.manager.registry.ssm.status',
112- # message=yaml.dump({'status': 'vFW reconfiguration succeeded'}))
113- # else:
114- # LOG.info('vFW reconfiguration failed')
115- # self.manoconn.publish(topic='specific.manager.registry.ssm.status',
116- # message=yaml.dump({'status': 'vFW reconfiguration failed ==> "{0}"-"{1}"'
117- # .format(str(entry1),str(entry2))}))
118-
89+ message = yaml .dump ({'status' : 'cpu usage 85% alert message received, start reconfiguring vFW' }))
11990
91+ # retrieve vFW IP address
92+ endpoint = os .environ ['HOST' ]
93+ self .manoconn .publish (topic = 'specific.manager.registry.ssm.status' ,
94+ message = yaml .dump ({'status' : 'vFW IP address ==> "{0}"' .format (str (endpoint ))}))
95+
96+ entry1 = ''
97+ try :
98+ self .timeout = time .time () + 60 * 5
99+ entry1 = requests .post (url = 'http://' + endpoint + ':8080/stats/flowentry/add' ,
100+ data = json .dumps ({"dpid" : 1 , "cookie" : 200 , "priority" : 1000 ,
101+ "match" : {"dl_type" : 0x0800 , "nw_proto" : 17 , "udp_dst" : 5001 }}))
102+ if (entry1 .status_code == 200 ):
103+
104+ LOG .info ('vFW reconfiguration succeeded' )
105+ self .manoconn .publish (topic = 'specific.manager.registry.ssm.status' ,
106+ message = yaml .dump ({'status' : 'vFW reconfiguration succeeded' }))
107+ else :
108+ LOG .info ('vFW reconfiguration failed' )
109+ self .manoconn .publish (topic = 'specific.manager.registry.ssm.status' ,
110+ message = yaml .dump ({'status' : 'vFW reconfiguration failed ==> "{0}"' .format (str (entry1 ))}))
111+ except BaseException as err :
112+ LOG .info ('vFW reconfiguration failed' )
113+ self .manoconn .publish (topic = 'specific.manager.registry.ssm.status' ,
114+ message = yaml .dump (
115+ {'status' : 'vFW reconfiguration failed ==> "{0}"' .format (str (err ))}))
120116
121117 def publish (self ):
122118
0 commit comments