def post(self): conn = DatabaseUtil(os.environ.get("DB_CONN"), os.environ.get("DB_USER"), os.environ.get("DB_PASSWORD"), os.environ.get("DB_NAME")) args = self.api_utils.parameters(self.main_model(), blacklist="append", selected_oid="append", ip_list="append") list_of_ids = {} try: blacklist = list(eval(items) for items in list(args['blacklist'])) selected_oid = list(filter(None, args['selected_oid'])) ip_list = list(eval(items) for items in list(args['ip_list'])) del args['ip_list'] del args['blacklist'] del args['selected_oid'] SnmpPollerSchema().load(args) if (selected_oid and ip_list) and self.api_utils.ip_validator( ip_list) and self.api_utils.ip_validator( blacklist) and self.api_utils.oid_validator( conn, selected_oid): poller_data = self.main_schema().load(args) poller_result = self.db_utils.insert_data(self.module_name, self.main_model, poller_data, commit=True) list_of_ids['snmp_poller'] = poller_result if blacklist is not None: for blacklist_element in blacklist: blacklist_obj = {} blacklist_obj['ip_address'] = blacklist_element[ 'ip_address'] blacklist_obj['snmp_poller_id'] = poller_result blacklist_obj[ 'system_description'] = blacklist_element[ 'system_description'] blacklist_obj['system_name'] = blacklist_element[ 'system_name'] blacklist_obj['brand'] = blacklist_element['brand'] blacklist_data = BlacklistSchema().load(blacklist_obj) blacklist_result = self.db_utils.insert_data( self.module_name, Blacklist, blacklist_data, commit=True) list_of_ids['blacklist'] = blacklist_result if selected_oid is not None: for selected_oid_element in selected_oid: selected_oid_obj = {} selected_oid_obj['oid_key'] = selected_oid_element selected_oid_obj['snmp_poller_id'] = poller_result selected_oid_data = SelectedOidSchema().load( selected_oid_obj) selected_oid_result = self.db_utils.insert_data( self.module_name, SelectedOid, selected_oid_data, commit=True) list_of_ids['selected_oid'] = selected_oid_result oid_raw = conn.select_query('Select oid_key from oid_list') oid_main = [oid['oid_key'] for oid in oid_raw] oid_main.extend([ 'ip_address', 'status', 'brand', 'system_description', 'system_name' ]) SchemaBuilder(args['table_name'], fields=oid_main).create_table(default_date=True) SchemaBuilder().create_data_retention(args['table_name']) conn = DatabaseUtil(os.environ.get("DB_CONN"), os.environ.get("DB_USER"), os.environ.get("DB_PASSWORD"), os.environ.get("DB_NAME")) query_string = 'INSERT INTO {0} (ip_address,system_description,brand,system_name) values ({1})'.format( args['table_name'], '%(ip_address)s,%(system_description)s,%(brand)s ,%(system_name)s' ) conn.insert_many_query(query_string, ip_list) args['snmp_poller_id'] = poller_result logger.log("Inserted data to SNMP Poller. Status CREATED 201") return { 'message': 'Successfully added.', 'snmp_poller_payload': args }, 201 else: return { 'message': 'Payload verification failed.', 'snmp_poller_payload': args }, 422 except ValidationError as value_error: logger.log("Validation encountered on SNMP poller table: %s" % (str(value_error)), log_type='ERROR') self.db_utils.data_rollback(list_of_ids) return { 'lists': value_error.messages, 'type': 'ValidationError', 'message': 'Validation errors in your request' }, 422 except Exception as err: logger.log("Error encountered on SNMP poller : %s" % str(err), log_type='ERROR') self.db_utils.data_rollback(list_of_ids) return eval(str(err)), 422
def put(self, id=None): args = self.api_utils.parameters(self.main_model(), blacklist="append", selected_oid="append", ip_list="append") poller_id = id conn = DatabaseUtil(os.environ.get("DB_CONN"), os.environ.get("DB_USER"), os.environ.get("DB_PASSWORD"), os.environ.get("DB_NAME")) poll_data = conn.select_query( 'Select status from snmp_poller where id = {0}'.format(poller_id)) if not poll_data: return { 'message': "ID doesn't exist", "type": "ReferenceError" }, 422 poll_status = poll_data[0]['status'] list_of_ids = {} if poll_status != 1: try: blacklist = list( eval(items) for items in list(args['blacklist'])) selected_oid = list(filter(None, args['selected_oid'])) ip_list = list(eval(items) for items in list(args['ip_list'])) del args['ip_list'] del args['blacklist'] del args['selected_oid'] SnmpPollerSchema().load(args) if (selected_oid and ip_list) and self.api_utils.ip_validator( ip_list) and self.api_utils.ip_validator( blacklist) and self.api_utils.oid_validator( conn, selected_oid): if blacklist is not None: self.db_utils.delete_data_using_id( Blacklist, {'snmp_poller_id': poller_id}) for blacklist_element in blacklist: blacklist_obj = {} blacklist_obj['ip_address'] = blacklist_element[ 'ip_address'] blacklist_obj[ 'system_description'] = blacklist_element[ 'system_description'] blacklist_obj['system_name'] = blacklist_element[ 'system_name'] blacklist_obj['brand'] = blacklist_element['brand'] blacklist_obj['snmp_poller_id'] = poller_id blacklist_data = BlacklistSchema().load( blacklist_obj) blacklist_result = self.db_utils.insert_data( self.module_name, Blacklist, blacklist_data, commit=True) list_of_ids['blacklist'] = blacklist_result if selected_oid is not None: self.db_utils.delete_data_using_id( SelectedOid, {'snmp_poller_id': poller_id}) for selected_oid_element in selected_oid: selected_oid_obj = {} selected_oid_obj['oid_key'] = selected_oid_element selected_oid_obj['snmp_poller_id'] = poller_id selected_oid_data = SelectedOidSchema().load( selected_oid_obj) selected_oid_result = self.db_utils.insert_data( self.module_name, SelectedOid, selected_oid_data, commit=True) list_of_ids['selected_oid'] = selected_oid_result if ip_list is not None: table_name = conn.select_query( 'Select table_name from snmp_poller where id = %s ' % (poller_id))[0] new_table_name = table_name["table_name"] conn.truncate_table('Truncate table %s ' % (new_table_name)) query_string = 'INSERT INTO {0} (ip_address,system_description,brand,system_name) values ({1})'.format( new_table_name, '%(ip_address)s,%(system_description)s,%(brand)s ,%(system_name)s' ) conn.insert_many_query(query_string, ip_list) del args['table_name'] args['status'] = 0 args['pid'] = 0 self.db_utils.update_data(self.main_model, {'id': poller_id}, args, commit=True) args['snmp_poller_id'] = poller_id logger.log("Update SNMP Poller. Status Succesful 200") return { 'message': 'Update Successful.', 'snmp_poller_payload': args }, 200 else: return { 'message': 'Payload verification failed.', 'snmp_poller_payload': args }, 422 except ValidationError as value_error: logger.log("Validation encountered on SNMP poller table: %s" % (str(value_error)), log_type='ERROR') self.db_utils.data_rollback(list_of_ids) return { 'lists': value_error.messages, 'type': 'ValidationError', 'message': 'Validation errors in your request' }, 422 except Exception as err: logger.log("Error encountered on SNMP poller : %s" % str(err), log_type='ERROR') self.db_utils.data_rollback(list_of_ids) return eval(str(err)), 422 else: return {'message': "Update unsuccessfull. Poller is running."}, 422