def __init__(self, config_path, **kwargs): kwargs['identity'] = 'networkagent' super(NetworkAgent, self).__init__(**kwargs) self.config = utils.load_config(config_path) def get_config(name): try: kwargs.pop(name) except KeyError: return self.config.get(name, '') self.agent_id = get_config('agent_id') offline_variables['node_type'] = node_type offline_variables['node_name'] = node_name offline_variables['logged_by'] = self.agent_id self.building_name = building_name self.host_ip_address = my_ip_address self.db_host = db_host self.host_name = host_name self.host_type = settings.PLATFORM['node']['type'] self.host_building_name = building_name self.curcon = db_helper.db_connection() self.my_node_id = db_helper.get_node_id() print "host_zone_id " + str(self.my_node_id) self.time_sent_notifications = {}
def __init__(self, **kwargs): super(DiscoveryAgent, self).__init__(**kwargs) # Connect to database self.curcon = db_helper.db_connection() self.agent_id = agent_id sys.path.append(PROJECT_DIR) self.device_scan_time = device_scan_time self.scan_for_devices = True self.discovery_list = list() #self.discovery_list.append('ICM100') print self.discovery_list self.new_discovery=True self.no_new_discovery_count=0 try: # Find total number of devices in the dashboard_device_info table self.curcon.execute("SELECT * FROM "+db_table_device_info) self.device_num = self.curcon.rowcount # count no. of devices discovered by Device Discovery Agent print "{} >> there are existing {} device(s) in database".format(agent_id, self.device_num) #if self.device_num != 0: # change network status of devices to OFF (OFFLINE) # rows = self.curcon.fetchall() # for row in rows: # self.curcon.execute("UPDATE "+db_table_device_info+" SET device_status=%s", ("OFF",)) # self.curcon.commit() except Exception as er: print "exception: ",er self.device_num = 0
def setup(self, sender, **kwargs): self.curcon = db_helper.db_connection() self.core.periodic(60, self.periodicProcess) self.vip.pubsub.subscribe(peer='pubsub', prefix='to/' + self.agent_id + '/update/', callback=self.appUpdate) self.updateSettings()
def __init__(self, config_path, **kwargs): super(AppLauncherAgent, self).__init__(**kwargs) self.config = utils.load_config(config_path) # self.app_number = 0 #connect to the database self.curcon = db_helper.db_connection() self.time_applauncher_start = datetime.datetime.now() self.already_started_previous_apps = False
def __init__(self, **kwargs): super(BEMOSSAgent, self).__init__(**kwargs) self.multinode_data = db_helper.get_multinode_data() self.node_name = self.multinode_data['this_node'] if not os.path.isfile(settings.MULTINODE_PARENT_IP_FILE ): # but parent file doesn't exists parent_addr = self.multinode_data['known_nodes'][0]['address'] parent_ip = self.extract_ip(parent_addr) with open(settings.MULTINODE_PARENT_IP_FILE, 'w') as f: f.write(parent_ip) self.curcon = db_helper.db_connection()
def __init__(self, *args, **kwargs): super(ThermostatControlAgent, self).__init__(*args, **kwargs) #1. initialize all agent variables self.agent_id = kwargs['name'] self.data = { 'thermostat': 'RTH8_1169269', 'sensor': '', 'sensor_weight': 0.5, 'cool_setpoint': 70, 'heat_setpoint': 80, 'mode': 'AUTO', 'deadband': 0.5 } dbcon = db_helper.db_connection() self.runPeriodically(self.periodicProcess, 60) self.subscribe(topic='update', callback=self.appUpdate) self.updateAppData(self.dbcon) self.get_nicknames(self.dbcon) self.run()
def updateParent(self, parent_node_name): parent_ip = self.extract_ip( self.nodelist_dict[parent_node_name]['address']) write_new = False if not os.path.isfile(settings.MULTINODE_PARENT_IP_FILE ): # but parent file doesn't exists write_new = True else: with open(settings.MULTINODE_PARENT_IP_FILE, 'r') as f: read_ip = f.read() if read_ip != parent_ip: write_new = True if write_new: with open(settings.MULTINODE_PARENT_IP_FILE, 'w') as f: f.write(parent_ip) if self.curcon: self.curcon.close() #close old connection self.curcon = db_helper.db_connection( ) #start new connection using new parent_ip self.vip.pubsub.publish('pubsub', 'from/multinodeagent/update_parent')
def updateParent(self, dbcon, parent_node_name): parent_ip = self.extract_ip( self.nodelist_dict[parent_node_name]['address']) write_new = False if not os.path.isfile(settings.MULTINODE_PARENT_IP_FILE ): # but parent file doesn't exists write_new = True else: with open(settings.MULTINODE_PARENT_IP_FILE, 'r') as f: read_ip = f.read() if read_ip != parent_ip: write_new = True if write_new: with open(settings.MULTINODE_PARENT_IP_FILE, 'w') as f: f.write(parent_ip) if dbcon: dbcon.close() #close old connection dbcon = db_helper.db_connection( ) #start new connection using new parent_ip self.updateMultinodeData(sender=self.name, topic='update_parent', message="")
def __init__(self, config_path, **kwargs): global agent_id super(ApprovalHelperAgent, self).__init__(**kwargs) config = utils.load_config(config_path) sys.path.append(PROJECT_DIR) db_host = settings.DATABASES['default']['HOST'] db_port = settings.DATABASES['default']['PORT'] db_database = settings.DATABASES['default']['NAME'] db_user = settings.DATABASES['default']['USER'] db_password = settings.DATABASES['default']['PASSWORD'] self.curcon = db_helper.db_connection() def get_config(name): try: kwargs.pop(name) except KeyError: return config.get(name, '') agent_id = get_config('agent_id') self.agent_id = str(agent_id)
def __init__(self, config_path, **kwargs): super(IlluminanceBasedLightingControl, self).__init__(**kwargs) self.variables = kwargs # 2. @params agent config = utils.load_config(config_path) def get_config(name): try: kwargs.pop(name) except KeyError: return config.get(name, '') self.curcon = db_helper.db_connection() self.app_id = get_config('agent_id') self.update_para('all') # monitor time should be larger or equal to device monitor time. self.monitor_time = int(settings.DEVICES['device_monitor_time']) self.calibrate_topic = 'to/' + self.app_id + '/from/ui/calibrate' self.update_target_topic = 'to/' + self.app_id + '/from/ui/update_target' self.firsttime = True
def __init__(self, config_path, **kwargs): super(SchedulerAgent, self).__init__(**kwargs) #1. initialize all agent variables self.variables = dict() config = utils.load_config(config_path) def get_config(name): try: kwargs.pop(name) except KeyError: return config.get(name, '') self.app_name = "plugload_scheduler" self.building_name = settings.PLATFORM['node']['building_name'] self.agent_id = get_config('agent_id') self.clock_time = 60 # schedule is updated every minute self.debug_agent = False self.timeStatus = kwargs self.timeStatus.clear() self.flag_time_to_change_status = False self.current_use_schedule = None self.schedule_first_run = False self.weekday_list = [ 'monday', 'tuesday', 'wednesday', 'thursday', 'friday' ] self.weekend_list = ['saturday', 'sunday'] self.old_day = self.find_day() self.active_scheduler_mode = list() self.time_next_schedule_sec = int(24 * 3600) self.current_schedule_object = None #2. connect to the database self.curcon = db_helper.db_connection() try: app_agent_id = self.agent_id self.curcon.execute( "select app_data from application_running where app_agent_id=%s", (self.agent_id, )) if self.curcon.rowcount: self.app_data = self.curcon.fetchone()[0] self.device_agent_id = self.app_data['device_agent_id'] self.topic_ui_app = 'to/' + self.agent_id + '/update/from/ui' self.topic_app_ui = 'to/ui/update/from/' + self.agent_id else: raise ValueError( "No entry in application running for this scheduler") self.curcon.execute( "SELECT schedule FROM schedule_data WHERE agent_id=%s", (self.device_agent_id, )) if self.curcon.rowcount != 0: _new_schedule_object = self.curcon.fetchone()[0] self.current_schedule_object = _new_schedule_object['plugload'] #3. get currently active schedule self.active_scheduler_mode = list() print '{} for Agent: {} >> new active schedule are as follows:'.format( self.app_name, self.device_agent_id) for each1 in self.current_schedule_object[ self.device_agent_id]: if each1 == 'active': for each2 in self.current_schedule_object[ self.device_agent_id][each1]: self.active_scheduler_mode.append(each2) for index in range(len(self.active_scheduler_mode)): print "- " + self.active_scheduler_mode[index] #4. RESTART Scheduler Agent self.set_query_mode_all_day() self.schedule_first_run = True print( "{} for Agent: {} >> DONE getting data from applications_running" .format(self.app_name, self.device_agent_id)) else: print("{} for Agent: {} >> has no previous setting before". format(self.app_name, self.device_agent_id)) except: print( "{} for Agent: {} >> error getting data from applications_running" .format(self.app_name, self.device_agent_id))
def updateParent(self, peer, sender, bus, topic, headers, message): print "Updating Connection to database" self.curcon.close() # close old connection self.curcon = db_helper.db_connection( ) # start new connection using new parent_ip
def updateParent(self, dbcon, sender, topic, message): print "Updating Connection to database" dbcon.close() # close old connection dbcon = db_helper.db_connection( ) # start new connection using new parent_ip
def check_health(self, dbcon): for node_name, node in self.multinode_status.items(): if node['health'] > 0: #initialize all online nodes to 0. If they are really online, they should change it # back to 1 or 2 (parent) within 30 seconds throught the heartbeat. node['health'] = 0 time.sleep(30) parent_node_name = None #initialize parent node online_node_exists = False for node_name, node in self.multinode_status.items(): node['last_scanned_time'] = datetime.now() if node['health'] == 0: node['health'] = -1 node['last_offline_time'] = datetime.now() self.recently_offline_node_list += [node_name] elif node['health'] == -1: #offline since long pass elif node[ 'health'] == -10: #The node was initialized to -10, and never came online. Treat it as recently going # offline for this iteration so that the agents that were supposed to be running there can be migrated node['health'] = -1 self.recently_offline_node_list += [node_name] elif node['health'] == 2: #there is some parent node present parent_node_name = node_name if node['health'] > 0: online_node_exists = True #At-least one node (itself) should be online, if not some problem assert online_node_exists, "At least one node (current node) must be online" if not parent_node_name: #parent node doesn't exist #find a suitable node to elect a parent. The node with latest update from previous parent wins. If there is #tie, then the node coming earlier in the node-list on multinode data wins online_node_last_sync = dict( ) #only the online nodes, and their last-sync-times for node_name, node in self.multinode_status.items( ): #copy only the online nodes if node['health'] > 0: online_node_last_sync[node_name] = node['last_sync_time'] latest_node = max(online_node_last_sync, key=online_node_last_sync.get) latest_sync_date = online_node_last_sync[latest_node] for node_name in self.node_name_list: if self.multinode_status[node_name][ 'health'] <= 0: #dead nodes can't be parents continue if self.multinode_status[node_name][ 'last_sync_time'] == latest_sync_date: # this is the first node with the latest update from parent #elligible parent found self.updateParent(dbcon, node_name) if node_name == self.node_name: # I am the node, so I get to become the parent self.is_parent = True print "I am the boss now, " + self.node_name break else: #I-am-not-the-first-node with latest update; somebody else is self.is_parent = False break else: #parent node exist self.updateParent(dbcon, parent_node_name) for node in self.multinode_data['known_nodes']: print node['name'] + ': ' + str( self.multinode_status[node['name']]['health']) if self.is_parent: #if this is a parent node, update the node_info table if dbcon is None: #if no database connection exists make connection dbcon = db_helper.db_connection() tbl_node_info = settings.DATABASES['default']['TABLE_node_info'] dbcon.execute('select node_id from ' + tbl_node_info) to_be_deleted_node_ids = dbcon.fetchall() for index, node in enumerate(self.multinode_data['known_nodes']): if (index, ) in to_be_deleted_node_ids: to_be_deleted_node_ids.remove( (index, )) #don't remove this current node result = dbcon.execute( 'select * from ' + tbl_node_info + ' where node_id=%s', (index, )) node_type = 'parent' if self.multinode_status[ node['name']]['health'] == 2 else "child" node_status = "ONLINE" if self.multinode_status[ node['name']]['health'] > 0 else "OFFLINE" ip_address = self.extract_ip(node['address']) last_scanned_time = self.multinode_status[ node['name']]['last_online_time'] last_offline_time = self.multinode_status[ node['name']]['last_offline_time'] last_sync_time = self.multinode_status[ node['name']]['last_sync_time'] var_list = "(node_id,node_name,node_type,node_status,ip_address,last_scanned_time,last_offline_time,last_sync_time)" value_placeholder_list = "(%s,%s,%s,%s,%s,%s,%s,%s)" actual_values_list = (index, node['name'], node_type, node_status, ip_address, last_scanned_time, last_offline_time, last_sync_time) if dbcon.rowcount == 0: dbcon.execute( "insert into " + tbl_node_info + " " + var_list + " VALUES" + value_placeholder_list, actual_values_list) else: dbcon.execute( "update " + tbl_node_info + " SET " + var_list + " = " + value_placeholder_list + " where node_id = %s", actual_values_list + (index, )) dbcon.commit() for id in to_be_deleted_node_ids: dbcon.execute( 'delete from accounts_userprofile_nodes where nodeinfo_id=%s', id) #delete entries in user-profile for the old node dbcon.commit() dbcon.execute('delete from ' + tbl_node_info + ' where node_id=%s', id) #delete the old nodes dbcon.commit() if self.recently_online_node_list: #Online nodes should be handled first because, the same node can first be #on both recently_online_node_list and recently_offline_node_list, when it goes offline shortly after #coming online self.handle_online_nodes(dbcon, self.recently_online_node_list) self.recently_online_node_list = [] # reset after handling if self.recently_offline_node_list: self.handle_offline_nodes(dbcon, self.recently_offline_node_list) self.recently_offline_node_list = [] # reset after handling
def check_health(self): for node_name, node in self.multinode_status.items(): if node['health'] > 0: #initialize all online nodes to 0. If they are really online, they should change it # back to 1 or 2 (parent) within 30 seconds throught the heartbeat. node['health'] = 0 gevent.sleep(30) parent_node_name = None #initialize parent node online_node_exists = False for node_name, node in self.multinode_status.items(): node['last_scanned_time'] = datetime.now() if node['health'] == 0: node['health'] = -1 node['last_offline_time'] = datetime.now() self.recently_offline_node_list += [node_name] elif node['health'] == -1: #offline since long pass elif node[ 'health'] == -10: #The node was initialized to -10, and never came online. Treat it as recently going # offline for this iteration so that the agents that were supposed to be running there can be migrated node['health'] = -1 self.recently_offline_node_list += [node_name] elif node['health'] == 2: #there is some parent node present parent_node_name = node_name if node['health'] > 0: online_node_exists = True #At-least one node (itself) should be online, if not some problem assert online_node_exists, "At least one node (current node) must be online" if parent_node_name: #parent node exist self.updateParent(parent_node_name) for node in self.multinode_data['known_nodes']: print node['name'] + ': ' + str( self.multinode_status[node['name']]['health']) if self.is_parent: #if this is a parent node, update the node_info table if self.curcon is None: #if no database connection exists make connection self.curcon = db_helper.db_connection() tbl_node_info = settings.DATABASES['default']['TABLE_node_info'] self.curcon.execute('select node_id from ' + tbl_node_info) to_be_deleted_node_ids = self.curcon.fetchall() for index, node in enumerate(self.multinode_data['known_nodes']): if (index, ) in to_be_deleted_node_ids: to_be_deleted_node_ids.remove( (index, )) #don't remove this current node result = self.curcon.execute( 'select * from ' + tbl_node_info + ' where node_id=%s', (index, )) node_type = 'parent' if self.multinode_status[ node['name']]['health'] == 2 else "child" node_status = "ONLINE" if self.multinode_status[ node['name']]['health'] > 0 else "OFFLINE" ip_address = self.extract_ip(node['address']) last_scanned_time = self.multinode_status[ node['name']]['last_online_time'] last_offline_time = self.multinode_status[ node['name']]['last_offline_time'] last_sync_time = self.multinode_status[ node['name']]['last_sync_time'] var_list = "(node_id,node_name,node_type,node_status,ip_address,last_scanned_time,last_offline_time,last_sync_time)" value_placeholder_list = "(%s,%s,%s,%s,%s,%s,%s,%s)" actual_values_list = (index, node['name'], node_type, node_status, ip_address, last_scanned_time, last_offline_time, last_sync_time) if self.curcon.rowcount == 0: self.curcon.execute( "insert into " + tbl_node_info + " " + var_list + " VALUES" + value_placeholder_list, actual_values_list) else: self.curcon.execute( "update " + tbl_node_info + " SET " + var_list + " = " + value_placeholder_list + " where node_id = %s", actual_values_list + (index, )) self.curcon.commit() for id in to_be_deleted_node_ids: self.curcon.execute( 'delete from accounts_userprofile_nodes where nodeinfo_id=%s', id) #delete entries in user-profile for the old node self.curcon.commit() self.curcon.execute('delete from ' + tbl_node_info + ' where node_id=%s', id) #delete the old nodes self.curcon.commit() if self.recently_online_node_list: #Online nodes should be handled first because, the same node can first be #on both recently_online_node_list and recently_offline_node_list, when it goes offline shortly after #coming online self.handle_online_nodes(self.recently_online_node_list) self.recently_online_node_list = [] # reset after handling if self.recently_offline_node_list: self.handle_offline_nodes(self.recently_offline_node_list) self.recently_offline_node_list = [] # reset after handling
def __init__(self, config_path, **kwargs): super(BasicAgent, self).__init__(**kwargs) self.variables = kwargs config = utils.load_config(config_path) self.variables = dict() def get_config(name): try: kwargs.pop(name) except KeyError: return config.get(name, '') def valid_ip(ip): parts = ip.split('.') return (len(parts) == 4 and all(part.isdigit() for part in parts) and all(0 <= int(part) <= 255 for part in parts)) #1. @params agent self.agent_id = get_config('agent_id') self.device_monitor_time = int(settings.DEVICES['device_monitor_time']) self.max_monitor_time = int(settings.DEVICES['max_monitor_time']) self.offline_variables = offline_variables self.offline_variables['logged_by'] = self.agent_id self.offline_table = offline_table self.offline_log_variables = offline_log_variables self.already_offline = False #3. DB interfaces #TODO get database parameters from settings.py, add db_table for specific table self.db_host = settings.DATABASES['default']['HOST'] self.db_port = settings.DATABASES['default']['PORT'] self.db_database = settings.DATABASES['default']['NAME'] self.db_user = settings.DATABASES['default']['USER'] self.db_password = settings.DATABASES['default']['PASSWORD'] self.db_table_active_alert = settings.DATABASES['default'][ 'TABLE_active_alert'] self.db_table_bemoss_notify = settings.DATABASES['default'][ 'TABLE_bemoss_notify'] self.db_table_alerts_notificationchanneladdress = settings.DATABASES[ 'default']['TABLE_alerts_notificationchanneladdress'] self.db_table_temp_time_counter = settings.DATABASES['default'][ 'TABLE_temp_time_counter'] self.db_table_priority = settings.DATABASES['default'][ 'TABLE_priority'] self.zone_id = settings.PLATFORM['node']['zone'] # setup connection with db -> Connect to bemossdb database self.curcon = db_helper.db_connection() # 2. @params device_info # TODO correct the launchfile in Device Discovery Agent self.curcon.execute( 'select device_type_id, device_model, address, username, password, identifiable, ' 'mac_address from device_info where agent_id=%s', (self.agent_id, )) info = self.curcon.fetchone() self.device_type_id = info[0] self.device_model = info[1] self.username = info[3] self.mac_address = info[6] if info[4] != None: self.password = decrypt_value(info[4]) else: self.password = None self.address = info[2] _address = self.address _address = _address.replace('http://', '') _address = _address.replace('https://', '') try: # validate whether or not address is an ip address socket.inet_aton(_address) if valid_ip(_address): ip_address = _address else: ip_address = None except socket.error: ip_address = None self.ip_address = ip_address if ip_address != None else None self.identifiable = info[5] self.changed_variables = None self.db_table_device = settings.DATABASES['default']['TABLE_device'] #4. @params device_api self.curcon.execute( 'select (api_name) from supported_devices where device_model=%s', (self.device_model, )) api_info = self.curcon.fetchone() self.api = api_info[0] apiLib = importlib.import_module("DeviceAPI." + self.api) #4.1 initialize device object self.Device = apiLib.API(model=self.device_model, api=self.api, address=self.address, mac_address=self.mac_address, username=self.username, password=self.password, agent_id=self.agent_id, parent=self, vip=self.vip, core=self.core) print("{0}agent is initialized for {1} using API={2} at {3}".format( self.agent_id, self.Device.get_variable('model'), self.Device.get_variable('api'), self.Device.get_variable('address'))) if 'default_monitor_interval' in self.Device.API_info()[0]: self.device_monitor_time = self.Device.API_info( )[0]['default_monitor_interval'] self.curcon.execute( "UPDATE " + self.db_table_device + " SET dashboard_view=%s WHERE agent_id=%s", ( json.dumps(self.Device.dashboard_view()), self.agent_id, )) self.curcon.commit() # Initialize ONLINE status, in case of previous offline. In consistent with self.already_offline == False. self.updateDB(self.db_table_device, 'network_status', 'agent_id', 'ONLINE', self.agent_id) # Generating log variables log_variables = {'user': '******'} for var in self.Device.ontology().values(): log_variables[var.NAME] = var.TYPE self.log_variables = log_variables # TODO: Uncomment below when the API side's dashboard_view is ready self.updateDB(self.db_table_device, 'dashboard_view', 'agent_id', json.dumps(self.Device.dashboard_view()), self.agent_id) #5. @params notification_info self.send_notification = True self.email_fromaddr = settings.NOTIFICATION['email']['fromaddr'] self.email_username = settings.NOTIFICATION['email']['username'] self.email_password = settings.NOTIFICATION['email']['password'] self.email_mailServer = settings.NOTIFICATION['email']['mailServer'] self.notify_heartbeat = settings.NOTIFICATION['heartbeat'] #6. topics define: self.update_ui_topic = 'to/' + self.agent_id + '/device_status' self.device_identify_topic = 'to/' + self.agent_id + '/identify' self.device_control_topic = 'to/' + self.agent_id + '/update'