def delete_scan_configuration(scan_configuration, standalone = False, type = 'mark_deleted'): try: db_location, err = get_db_location(standalone) if err: raise Exception(err) if type == 'expunge': cmd = ['delete from file_info where scan_configuration_id="%d"'%scan_configuration['id']] ret, err = db.execute_iud(db_location, [cmd]) if err: raise Exception(err) cmd = ['delete from file_events_history where scan_configuration_id="%d"'%scan_configuration['id']] ret, err = db.execute_iud(db_location, [cmd]) if err: raise Exception(err) cmd = ['delete from scans where scan_configuration_id="%d"'%scan_configuration['id']] ret, err = db.execute_iud(db_location, [cmd]) if err: raise Exception(err) cmd = ['delete from scan_configurations where id="%d"'%scan_configuration['id']] ret, err = db.execute_iud(db_location, [cmd]) if err: raise Exception(err) else: cmd = [ 'update scan_configurations set status_id="-1" where id="%d"'%scan_configuration['id']] ret, err = db.execute_iud(db_location, [cmd]) if err: raise Exception(err) except Exception, e: return False, 'Error removing Storage Insights configuration : %s'%str(e)
def save_email_settings(d): """Save the email server settings """ conn = None try: db_path, err = config.get_db_path() if err: raise Exception(err) d1, err = db.get_single_row(db_path, "select * from email_config") if err: raise Exception(err) if d1: # Config exists so update ret, err = db.execute_iud(db_path, [["update email_config set server=?, port=?, username=?, pswd=?, tls=? where id = ?", ( d["server"], d["port"], d["username"], d["pswd"], d["tls"], 1,)]]) if err: raise Exception(err) else: # No config exists so insert ret, err = db.execute_iud(db_path, [["insert into email_config (server, port, username, pswd, tls, id) values (?,?,?,?,?,?)", ( d["server"], d["port"], d["username"], d["pswd"], d["tls"], 1, )]]) if err: raise Exception(err) except Exception, e: return False, 'Error saving email settings : %s' % str(e)
def log_scan_start(db_location, scan_configuration, pid): scan_id = 0 try: pltfrm = None try: pltfrm = platform.uname()[0] except Exception, e: pass if pltfrm and pltfrm.lower() == 'linux': running_pid, err = get_running_process_pid() if err: raise Exception(err) if running_pid > 0: raise Exception( 'A Storage Insights scan process with process id %d is currently running. Only one scan process can run at one time. Exiting now.' % running_pid) pid = os.getpid() if pltfrm and pltfrm.lower() == 'linux': if not os.path.exists('/var/run/integralstor/applications'): os.makedirs('/var/run/integralstor/applications') with open( '/var/run/integralstor/applications/storage_insights_scan', 'w') as f: f.write('%d' % pid) query = 'select * from scans where scan_configuration_id="%d" and status_id=1' % scan_configuration[ 'id'] rows, err = db.get_multiple_rows(db_location, query) if err: raise Exception(err) if rows: #DB says there is a process running so mark it as error and then start a new one.. cmd = [ 'update scans set status_id=4, status_str="Process killed" where id = "%d"' % rows[0]['id'] ] ret, err = db.execute_iud(db_location, [cmd], get_rowid=False) if err: raise Exception(err) #!!!!!!!!!!!!CHANGE TO USE INTEGRALSTOR's CALLS initiate_time = int(time.time()) #No existing pending runs so create a new run entry cmd = [ 'insert into scans(initiate_time, scan_configuration_id, pid, status_id) values (?,?,?,?)', ( initiate_time, scan_configuration['id'], pid, 1, ) ] scan_id, err = db.execute_iud(db_location, [cmd], get_rowid=True) if err: raise Exception(err)
def delete_all_tasks(): """Delete all entries from tasks table and terminate running tasks """ try: tasks, err = get_tasks() if err: raise Exception(err) for task in tasks: if str(task['status']) == 'running': ret, err = stop_task(int(task['task_id'])) if err: # best effort pass db_path, err = config.get_db_path() if err: raise Exception(err) # Deleting entries from tasks table will also remove related # entries in subtasks table; cascading deletions. cmd_list = [['delete from tasks'], ] status, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) except Exception, e: return False, 'Error removing tasks: %s' % e
def update_auth_method(security): """Update the currently set authentication method in the db. """ try: db_path, err = config.get_db_path() if err: raise Exception(err) d, err = get_auth_settings() if err: raise Exception(err) cl = [] if not d: # Insert a default entry before they modify the settings.. cl.append(["insert into samba_global_common (id, workgroup, netbios_name, security, include_homes_section) values (?, ?, ?, ?, ?)", (1, 'workgroup', 'netbios_name', security, True,)]) else: cl.append( ["update samba_global_common set security='%s' where id=1" % security]) platform, err = config.get_platform() if err: raise Exception(err) ret, err = db.execute_iud(db_path, cl) if err: raise Exception(err) except Exception, e: return False, 'Error changing authentication method : %s' % str(e)
def record_event_notification_holding(event_id, event_type_id, event_subtype_id=-1, subsystem_type_id=-1, severity_type_id=-1): """Create an notification holding entry by looking up the triggers table for all matching entries.. """ try: ent_list, err = get_event_notification_triggers( event_type_id=event_type_id, event_subtype_id=event_subtype_id, subsystem_type_id=subsystem_type_id, severity_type_id=severity_type_id) if err: raise Exception(err) # print ent_list if ent_list: db_path, err = config.get_db_path() if err: raise Exception(err) command_list = [] for ent in ent_list: cmd = [ 'insert into event_notifications_holding(event_id, ent_id, status) values (?,?,?)', (event_id, ent['ent_id'], 1,)] command_list.append(cmd) # print command_list ret, err = db.execute_iud(db_path, command_list) if err: raise Exception(err) except Exception, e: return False, 'Error recording event notifications : %s' % str(e)
def create_event_notification_trigger(event_type_id, event_subtype_id, subsystem_type_id, notification_type_id, severity_type_id, enc_id, cron_task_id): """Create an entry in the triggers table with the supplied params. """ ent_id = None try: db_path, err = config.get_db_path() if err: raise Exception(err) ent_id, err = db.execute_iud(db_path, [[ "insert into event_notification_triggers (event_type_id, event_subtype_id, subsystem_type_id, notification_type_id, severity_type_id, enc_id, cron_task_id) values (?,?,?,?,?,?,?)", ( event_type_id, event_subtype_id, subsystem_type_id, notification_type_id, severity_type_id, enc_id, cron_task_id, ) ]], get_rowid=True) if err: raise Exception(err) except Exception, e: return None, 'Error creating event notification trigger : %s' % str(e)
def delete_all_tasks(): """Delete all entries from tasks table and terminate running tasks """ try: tasks, err = get_tasks() if err: raise Exception(err) for task in tasks: if str(task['status']) == 'running': ret, err = stop_task(int(task['task_id'])) if err: # best effort pass db_path, err = config.get_db_path() if err: raise Exception(err) # Deleting entries from tasks table will also remove related # entries in subtasks table; cascading deletions. cmd_list = [ ['delete from tasks'], ] status, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) except Exception, e: return False, 'Error removing tasks: %s' % e
def get_log_level(): d = None conn = None log_level = None try: d, err = db.get_single_row(db_path, "select * from global_params where id=1") if err: raise Exception(err) if d and "logging_level" in d: log_level = d["logging_level"] else: # Not yet set so insert the default and return it cmd_list = [] cmd = [ "insert into global_params (logging_level, id) values(?,?)", ( logging.INFO, 1, ) ] cmd_list.append(cmd) ret, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) log_level = logging.INFO except Exception, e: return None, "Error getting log level : %s" % str(e)
def create_share(name, comment, guest_ok, read_only, path, display_path, browseable, users, groups, vol, hosts_allow=None, hosts_deny=None): """Create a new share in the db.""" try: db_path, err = config.get_db_path() if err: raise Exception(err) platform, err = config.get_platform() if err: raise Exception(err) d, err = get_auth_settings() if err: raise Exception(err) if not d: raise Exception( "Authentication settings not set. Please set authentication settings before creating shares.") shl, err = get_shares_list() if err: raise Exception(err) if shl: for sh in shl: if sh["name"] == name: raise Exception("A share with that name already exists") share_id, err = db.execute_iud(db_path, [["insert into samba_shares (name, vol, path, display_path, comment, read_only, guest_ok, browseable, share_id, hosts_allow, hosts_deny) values (?,?, ?,?,?,?,?,?,NULL,?,?)", ( name, vol, path, display_path, comment, read_only, guest_ok, browseable, hosts_allow, hosts_deny,)]], True) if err: raise Exception(err) # print share_id, err except Exception, e: return False, 'Error creating CIFS share : %s' % str(e)
def log_scan_progress(db_location, params_dict): try: #print params_dict if 'status_id' not in params_dict: raise Exception('Incomplete call') required_params = ['status_str', 'scan_id'] if params_dict['status_id'] != 4: required_params.extend(['scanned_dirs_count', 'scanned_files_count', 'successful_creation_modification_transactions_count','failed_creation_modification_transactions_count', 'successful_deletion_transactions_count', 'failed_deletion_transactions_count', 'new_files_count', 'modified_files_count']) if not all(param in params_dict for param in required_params): raise Exception('Incomplete call') if params_dict['status_id'] == 4: #It is a kill so dont log the counts.. cmd = ['update scans set status_id=?, status_str=? where id = ?', (params_dict['status_id'], params_dict['status_str'], params_dict['scan_id'],)] else: cmd = ['update scans set status_id=?, status_str=?, scanned_dirs_count=?, scanned_files_count=?, successful_creation_modification_transactions_count=?, failed_creation_modification_transactions_count=?, successful_deletion_transactions_count=?, failed_deletion_transactions_count=?, new_files_count=?, modified_files_count=?, deleted_files_count=? where id = ?', (params_dict['status_id'], params_dict['status_str'], params_dict['scanned_dirs_count'], params_dict['scanned_files_count'], params_dict['successful_creation_modification_transactions_count'], params_dict['failed_creation_modification_transactions_count'], params_dict['successful_deletion_transactions_count'], params_dict['failed_deletion_transactions_count'], params_dict['new_files_count'], params_dict['modified_files_count'], params_dict['deleted_files_count'], params_dict['scan_id'],)] #print cmd ret, err = db.execute_iud(db_location, [cmd], get_rowid=False) if err: raise Exception(err) except Exception, e: print e return False, 'Error logging scan run completion information : %s'%str(e)
def update_auth_method(security): """Update the currently set authentication method in the db. """ try: db_path, err = config.get_db_path() if err: raise Exception(err) d, err = get_auth_settings() if err: raise Exception(err) cl = [] if not d: # Insert a default entry before they modify the settings.. cl.append([ "insert into samba_global_common (id, workgroup, netbios_name, security, include_homes_section) values (?, ?, ?, ?, ?)", ( 1, 'workgroup', 'netbios_name', security, True, ) ]) else: cl.append([ "update samba_global_common set security='%s' where id=1" % security ]) platform, err = config.get_platform() if err: raise Exception(err) ret, err = db.execute_iud(db_path, cl) if err: raise Exception(err) except Exception, e: return False, 'Error changing authentication method : %s' % str(e)
def create_scan_configuration(config_dict, standalone = False): try: db_location, err = get_db_location(standalone) if err: raise Exception(err) configs, err = get_scan_configurations(db_location=db_location, include_deleted=True) if err: raise Exception(err) for c in configs: if c['scan_dir'] == config_dict['scan_dir']: raise Exception('A configuration for that scan folder already exists.') if c['scan_dir'].strip().rstrip('/') in config_dict['scan_dir'].strip().rstrip('/'): raise Exception('A configuration (scan folder "%s") for a parent folder of that scan folder already exists. Please expunge it before creating.'%c['scan_dir']) if config_dict['scan_dir'].strip().rstrip('/') in c['scan_dir'].strip().rstrip('/') : raise Exception('The specified scan folder is the parent of an existing configuration (scan folder "%s"). Please expunge it before creating.'%c['scan_dir']) if 'exclude_dirs' in config_dict and config_dict['exclude_dirs']: cmd = [ 'insert into scan_configurations(scan_dir, exclude_dirs, generate_checksum, record_history, db_transaction_size, status_id) values (?,?, ?,?,?,?)', (config_dict['scan_dir'], config_dict['exclude_dirs'], config_dict['generate_checksum'], config_dict['record_history'], config_dict['db_transaction_size'], 2)] else: cmd = [ 'insert into scan_configurations(scan_dir, generate_checksum, record_history, db_transaction_size, status_id) values (?,?,?,?,?)', (config_dict['scan_dir'], config_dict['generate_checksum'], config_dict['record_history'], config_dict['db_transaction_size'], 2)] ret, err = db.execute_iud(db_location, [cmd]) if err: raise Exception(err) except Exception, e: return False, 'Error creating Storage Insights configuration : %s'%str(e)
def set_log_level(level): try: db_path, err = config.get_db_path() if err: raise Exception(err) if level not in [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL]: logger.setLevel(logging.INFO) else: d1, err = db.get_single_row( db_path, "select * from global_params") if err: raise Exception(err) cmd_list = [] if d1: cmd = [ "update global_params set logging_level=? where id = ?", (level, 1,)] else: cmd = [ "insert into global_params (logging_level, id) values(?,?)", (level, 1,)] cmd_list.append(cmd) ret, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) logger.setLevel(level) except Exception, e: return False, 'Error setting log level : %s' % str(e)
def add_to_db(dir, action, file=None): try: if file: path = '%s/%s' % (dir, file) else: path = dir path = os.path.normpath(path) tz = pytz.timezone('UTC') now_utc = datetime.datetime.now(tz) time = now_utc.strftime('%Y-%m-%d %H:%M:%S') config_dir, err = config.get_config_dir() if err: raise Exception(err) db_path = '%s/db/inotify.db' % config_dir ret, err = db.execute_iud(db_path, [ ["insert into logs (path, actions, timestamp) values (?, ?, ?)", (path, action, time,)]]) if err: print err except Exception, e: return False, 'Error inserting into database dir - %s, action - %s, file = %s : %s' % (dir, action, file, str(e))
def delete_task(task_id): """Terminate the task if it's running and delete the task entry """ try: is_stopped, err = stop_task(task_id) if err: # best effort # only remote replication tasks can be stopped(currently) pass db_path, err = config.get_db_path() if err: raise Exception(err) # Deleting entries from tasks table will also remove related # entries in subtasks table; cascading deletions. cmd_list = [ ['delete from tasks where task_id = %s' % task_id], ] status, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) except Exception, e: return False, 'Error removing task : %s' % e
def add_to_db(dir, action, file=None): try: if file: path = '%s/%s' % (dir, file) else: path = dir path = os.path.normpath(path) tz = pytz.timezone('UTC') now_utc = datetime.datetime.now(tz) time = now_utc.strftime('%Y-%m-%d %H:%M:%S') config_dir, err = config.get_config_dir() if err: raise Exception(err) db_path = '%s/db/inotify.db' % config_dir ret, err = db.execute_iud(db_path, [[ "insert into logs (path, actions, timestamp) values (?, ?, ?)", ( path, action, time, ) ]]) if err: print err except Exception, e: return False, 'Error inserting into database dir - %s, action - %s, file = %s : %s' % ( dir, action, file, str(e))
def delete_cron(cron_task_id, user='******'): """Delete a cron by the cron_task_id.""" try: db_path, err = config.get_db_path() if err: raise Exception(err) query_tasks = 'select * from tasks where cron_task_id="%d"' % int( cron_task_id) tasks, err = db.get_multiple_rows(db_path, query_tasks) if err: raise Exception(err) cmd_list = [] cmd_list.append( ['delete from cron_tasks where cron_task_id=%d' % int(cron_task_id)]) cmd_list.append( ['update tasks set status="cancelled" where cron_task_id=%d and (status is not "completed" and status is not "failed")' % int(cron_task_id)]) if tasks: for task in tasks: cmd_list.append( ['update subtasks set status="cancelled" where task_id=%d and (status is not "completed" and status is not "failed")' % task['task_id']]) ret, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) cron = crontab.CronTab(user) cron.remove_all(comment=str(cron_task_id)) cron.write() except Exception, e: return False, "Error deleting cron entry : %s" % str(e)
def create_cron_task(command, description, min="1", hour='*', day='*', dow='*', month='*', user='******', task_type_id=0): cron_task_id = None try: if not command or not description: raise Exception('Invalid parameters') db_path, err = config.get_db_path() if err: raise Exception(err) cmd = [ 'insert into cron_tasks(command,description, task_type_id) values (?,?,?)', (command, description, task_type_id)] cron_task_id, err = db.execute_iud(db_path, [cmd], get_rowid=True) if err: raise Exception(err) log_dir, err = config.get_cron_log_dir_path() if err: raise Exception(err) if not os.path.isdir(log_dir): os.mkdir(log_dir) log_file = '%s/%d.log' % (log_dir, cron_task_id) command = '%s >> %s 2>&1' % (command, log_file) cron = crontab.CronTab(user) job = cron.new(command=command, comment='%d' % cron_task_id) job.setall(min, hour, day, dow, month) if job.is_valid(): job.enable() cron.write() else: raise Exception('Cron entry not valid.') except Exception, e: return None, 'Error creating cron entry : %s' % str(e)
def delete_scan_configuration(scan_configuration, standalone=False, type='mark_deleted'): try: db_location, err = get_db_location(standalone) if err: raise Exception(err) if type == 'expunge': cmd = [ 'delete from file_info where scan_configuration_id="%d"' % scan_configuration['id'] ] ret, err = db.execute_iud(db_location, [cmd]) if err: raise Exception(err) cmd = [ 'delete from file_events_history where scan_configuration_id="%d"' % scan_configuration['id'] ] ret, err = db.execute_iud(db_location, [cmd]) if err: raise Exception(err) cmd = [ 'delete from scans where scan_configuration_id="%d"' % scan_configuration['id'] ] ret, err = db.execute_iud(db_location, [cmd]) if err: raise Exception(err) cmd = [ 'delete from scan_configurations where id="%d"' % scan_configuration['id'] ] ret, err = db.execute_iud(db_location, [cmd]) if err: raise Exception(err) else: cmd = [ 'update scan_configurations set status_id="-1" where id="%d"' % scan_configuration['id'] ] ret, err = db.execute_iud(db_location, [cmd]) if err: raise Exception(err) except Exception, e: return False, 'Error removing Storage Insights configuration : %s' % str( e)
def create_task(description, subtask_list, task_type_id=0, cron_task_id=0, node=socket.getfqdn(), initiate_time=None, attempts=3, run_as_user_name='root', retry_interval=1): """Adds a task in to tasks table which performs/runs the subtask_list according to the received arguments.""" row_id = -1 try: if not description or not subtask_list: raise Exception("Insufficient parameters") now = int(time.time()) if not initiate_time: initiate_time = now log_dir, err = config.get_tasks_log_dir_path() if err: raise Exception(err) if not os.path.isdir(log_dir): os.mkdir(log_dir) cmd = "insert into tasks (description,task_type_id, node, run_as_user_name, attempts, cron_task_id, retry_interval, create_time, initiate_time) VALUES ('%s','%d','%s','%s','%d','%d','%d', '%d', '%d');" % ( description, task_type_id, node, run_as_user_name, attempts, int(cron_task_id), retry_interval, now, initiate_time) db_path, err = config.get_db_path() if err: raise Exception(err) row_id, err = db.execute_iud(db_path, [[cmd], ], get_rowid=True) if err: raise Exception(err) task_id = None if row_id: task_id = row_id log_file = '%s/%d.log' % (log_dir, row_id) for subtask in subtask_list: for description, command in subtask.iteritems(): # command = '%s &> %s' % (command, log_file) command = '%s &> %s; rc=$?; tail -n 200 %s > %s.TMP && mv %s.TMP %s; exit $rc' % (command, log_file, log_file, log_file, log_file, log_file) cmd = "insert into subtasks (description,command,task_id) values ('%s','%s','%d');" % ( description, command, row_id) status, err = db.execute_iud( db_path, [[cmd], ], get_rowid=True) if err: raise Exception( 'Error creating scheduled command : %s' % err) else: raise Exception('Error creating scheduled task') except Exception, e: return False, ' Error adding task: %s' % e
def log_scan_progress(db_location, params_dict): try: #print params_dict if 'status_id' not in params_dict: raise Exception('Incomplete call') required_params = ['status_str', 'scan_id'] if params_dict['status_id'] != 4: required_params.extend([ 'scanned_dirs_count', 'scanned_files_count', 'successful_creation_modification_transactions_count', 'failed_creation_modification_transactions_count', 'successful_deletion_transactions_count', 'failed_deletion_transactions_count', 'new_files_count', 'modified_files_count' ]) if not all(param in params_dict for param in required_params): raise Exception('Incomplete call') if params_dict['status_id'] == 4: #It is a kill so dont log the counts.. cmd = [ 'update scans set status_id=?, status_str=? where id = ?', ( params_dict['status_id'], params_dict['status_str'], params_dict['scan_id'], ) ] else: cmd = [ 'update scans set status_id=?, status_str=?, scanned_dirs_count=?, scanned_files_count=?, successful_creation_modification_transactions_count=?, failed_creation_modification_transactions_count=?, successful_deletion_transactions_count=?, failed_deletion_transactions_count=?, new_files_count=?, modified_files_count=?, deleted_files_count=? where id = ?', ( params_dict['status_id'], params_dict['status_str'], params_dict['scanned_dirs_count'], params_dict['scanned_files_count'], params_dict[ 'successful_creation_modification_transactions_count'], params_dict[ 'failed_creation_modification_transactions_count'], params_dict['successful_deletion_transactions_count'], params_dict['failed_deletion_transactions_count'], params_dict['new_files_count'], params_dict['modified_files_count'], params_dict['deleted_files_count'], params_dict['scan_id'], ) ] #print cmd ret, err = db.execute_iud(db_location, [cmd], get_rowid=False) if err: raise Exception(err) except Exception, e: print e return False, 'Error logging scan run completion information : %s' % str( e)
def log_scan_start(db_location, scan_configuration, pid): scan_id = 0 try: pltfrm = None try: pltfrm = platform.uname()[0] except Exception, e: pass if pltfrm and pltfrm.lower() == 'linux': running_pid, err = get_running_process_pid() if err: raise Exception(err) if running_pid > 0: raise Exception('A Storage Insights scan process with process id %d is currently running. Only one scan process can run at one time. Exiting now.'%running_pid) pid = os.getpid() if pltfrm and pltfrm.lower() == 'linux': if not os.path.exists('/var/run/integralstor/applications'): os.makedirs('/var/run/integralstor/applications') with open('/var/run/integralstor/applications/storage_insights_scan', 'w') as f: f.write('%d'%pid) query = 'select * from scans where scan_configuration_id="%d" and status_id=1'%scan_configuration['id'] rows, err = db.get_multiple_rows(db_location, query) if err: raise Exception(err) if rows: #DB says there is a process running so mark it as error and then start a new one.. cmd = ['update scans set status_id=4, status_str="Process killed" where id = "%d"'%rows[0]['id']] ret, err = db.execute_iud(db_location, [cmd], get_rowid=False) if err: raise Exception(err) #!!!!!!!!!!!!CHANGE TO USE INTEGRALSTOR's CALLS initiate_time = int(time.time()) #No existing pending runs so create a new run entry cmd = ['insert into scans(initiate_time, scan_configuration_id, pid, status_id) values (?,?,?,?)', (initiate_time, scan_configuration['id'], pid, 1,)] scan_id, err = db.execute_iud(db_location, [cmd], get_rowid=True) if err: raise Exception(err)
def delete_scan(scan_id, standalone=False): try: db_location, err = get_db_location(standalone) if err: raise Exception(err) cmd = ['delete from scans where id="%d"' % scan_id] ret, err = db.execute_iud(db_location, [cmd]) if err: raise Exception(err) except Exception, e: return False, 'Error removing Storage Insights scan details : %s' % str( e)
def delete_scan(scan_id, standalone = False): try: db_location, err = get_db_location(standalone) if err: raise Exception(err) cmd = [ 'delete from scans where id="%d"'%scan_id] ret, err = db.execute_iud(db_location, [cmd]) if err: raise Exception(err) except Exception, e: return False, 'Error removing Storage Insights scan details : %s'%str(e)
def delete_auth_settings(): """ Delete all authentication settings from the db. """ try: db_path, err = config.get_db_path() if err: raise Exception(err) ret, err = db.execute_iud(db_path, [["delete from samba_global_common "], [ "delete from samba_global_ad"]]) if err: raise Exception(err) except Exception, e: return False, 'Error deleting authentication settings : %s' % str(e)
def update_cron_schedule(scan_configuration_id, cron_task_id): try: db_location, err = get_db_location(standalone=False) if err: raise Exception(err) query = 'update scan_configurations set cron_task_id="%d" where id="%d"'%(cron_task_id, scan_configuration_id) print query scan_list, err = db.execute_iud(db_location, [[query]]) if err: raise Exception(err) except Exception, e: return False, 'Error updating Storage Insights scan schedule information : %s'%str(e)
def delete_email_settings(): """Remove the email server settings """ try: db_path, err = config.get_db_path() if err: raise Exception(err) ret, err = db.execute_iud(db_path, [["delete from email_config "]]) if err: raise Exception(err) except Exception, e: return False, 'Error deleting email settings : %s' % str(e)
def remove_old_entries(older_than=43200): try: config_dir, err = config.get_config_dir() if err: raise Exception(err) query = "delete from logs where timestamp < Datetime('now', '-%d seconds');" % older_than # print query db_path = '%s/db/inotify.db' % config_dir ret, err = db.execute_iud(db_path, [[query]]) if err: print err # print ret except Exception, e: return False, 'Error purging old entries : %s' % str(e)
def delete_event_notification_trigger(ent_id): """Remove the specified triggers table entry. """ try: db_path, err = config.get_db_path() if err: raise Exception(err) ret, err = db.execute_iud(db_path, [ ["delete from event_notification_triggers where ent_id='%d'" % int(ent_id)]]) if err: raise Exception(err) except Exception, e: return False, 'Error removing event notification trigger : %s' % str(e)
def update_event_notification_trigger_cron_id(ent_id, cron_task_id): """Update the cron id for the trigger - this is because we dont know it in advance when creating the trigger. """ try: db_path, err = config.get_db_path() if err: raise Exception(err) ret, err = db.execute_iud(db_path, [ ["update event_notification_triggers set cron_task_id=? where ent_id = ?", (cron_task_id, ent_id,)]]) if err: raise Exception(err) except Exception, e: return False, "Error updating event notification trigger's cron task id : %s" % str(e)
def record_alerts(alerts_list): """Record a set of passed list containing alert dictionaries into the DB.""" try: db_path, err = config.get_db_path() if err: raise Exception(err) if alerts_list: now, err = datetime_utils.get_epoch( when='now', num_previous_days=0) if err: raise Exception(er) for alert in alerts_list: # print alert command_list = [] result, err = _get_repeat_entry(alert) # print 'repeat', result, err if err: raise Exception(err) update_alert_id = None if result: cmd = ['update alerts set repeat_count=%d, last_update_time="%d" where alert_id=%d' % ( int(result['repeat_count']) + 1, now, result['alert_id'])] update_alert_id = result['alert_id'] # print 'updating!', cmd else: if 'component' not in alert or (not alert['component']): cmd = [ 'insert into alerts(first_alert_time, last_update_time, severity_type_id, subsystem_type_id, alert_str) values (?,?,?,?,?)', (now, now, alert['severity_type_id'], alert['subsystem_type_id'], alert['alert_str'],)] else: cmd = [ 'insert into alerts(first_alert_time, last_update_time, severity_type_id, subsystem_type_id, component, alert_str) values (?,?,?,?,?,?)', (now, now, alert['severity_type_id'], alert['subsystem_type_id'], alert['component'], alert['alert_str'],)] command_list.append(cmd) # print command_list rowid, err = db.execute_iud( db_path, command_list, get_rowid=True) if err: raise Exception(err) if update_alert_id: alert_id = update_alert_id else: alert_id = rowid ret, err = event_notifications.record_event_notification_holding( event_id=alert_id, event_type_id=1, subsystem_type_id=alert['subsystem_type_id'], severity_type_id=alert['severity_type_id']) # print ret, err if err: raise Exception(err) except Exception, e: # print str(e) return False, 'Error recording alerts : %s' % str(e)
def delete_event_notification_holding(enh_id): """Remove a specific holding entry """ try: db_path, err = config.get_db_path() if err: raise Exception(err) ret, err = db.execute_iud( db_path, [["delete from event_notifications_holding where enh_id=%d" % enh_id]]) if err: raise Exception(err) except Exception, e: return False, 'Error removing event notification holding: %s' % str(e)
def delete_event_notification_configuration(enc_id): """Remove an email configuration entry that will be used for a particular trigger. """ try: db_path, err = config.get_db_path() if err: raise Exception(err) ret, err = db.execute_iud(db_path, [ ["delete from event_notification_configuration_email where enc_id='%d'" % int(enc_id)]]) if err: raise Exception(err) except Exception, e: return False, 'Error deleting event notification configuration : %s' % str(e)
def update_cron_schedule(scan_configuration_id, cron_task_id): try: db_location, err = get_db_location(standalone=False) if err: raise Exception(err) query = 'update scan_configurations set cron_task_id="%d" where id="%d"' % ( cron_task_id, scan_configuration_id) print query scan_list, err = db.execute_iud(db_location, [[query]]) if err: raise Exception(err) except Exception, e: return False, 'Error updating Storage Insights scan schedule information : %s' % str( e)
def delete_event_notification_holdings(ent_id): """Remove all the holdings entries corresponding to the specified trigger id """ try: db_path, err = config.get_db_path() if err: raise Exception(err) ret, err = db.execute_iud(db_path, [ ["delete from event_notifications_holding where ent_id='%d'" % int(ent_id)]]) if err: raise Exception(err) except Exception, e: return False, 'Error removing event notification holdings : %s' % str(e)
def create_event_notification_trigger(event_type_id, event_subtype_id, subsystem_type_id, notification_type_id, severity_type_id, enc_id, cron_task_id): """Create an entry in the triggers table with the supplied params. """ ent_id = None try: db_path, err = config.get_db_path() if err: raise Exception(err) ent_id, err = db.execute_iud(db_path, [["insert into event_notification_triggers (event_type_id, event_subtype_id, subsystem_type_id, notification_type_id, severity_type_id, enc_id, cron_task_id) values (?,?,?,?,?,?,?)", ( event_type_id, event_subtype_id, subsystem_type_id, notification_type_id, severity_type_id, enc_id, cron_task_id,)]], get_rowid=True) if err: raise Exception(err) except Exception, e: return None, 'Error creating event notification trigger : %s' % str(e)
def create_event_notification_configuration(recipient_list_str): """Create an email configuration entry that will be used for a particular trigger. """ enc_id = None try: db_path, err = config.get_db_path() if err: raise Exception(err) enc_id, err = db.execute_iud(db_path, [ ["insert into event_notification_configuration_email (recipient_list) values (?)", (recipient_list_str, )]], get_rowid=True) if err: raise Exception(err) except Exception, e: return None, 'Error creating event notification configuration: %s' % str(e)
def delete_all_alerts(): """Remove all alerts from the db """ try: db_path, err = config.get_db_path() if err: raise Exception(err) command_list = [] cmd = ['delete from alerts'] command_list.append(cmd) ret, err = db.execute_iud(db_path, command_list) if err: raise Exception(err) except Exception, e: return False, 'Error removing all alerts : %s' % str(e)
def delete_all_rsync_shares(): try: db_path, err = config.get_db_path() if err: raise Exception(err) cmd_list = [] cmd = ['delete from rsync_shares'] cmd_list.append(cmd) ret, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) conf, err = _generate_rsync_config() if err: raise Exception(err) except Exception, e: return False, 'Error deleting all rsync shares : %s' % str(e)
def purge_email_queue(older_than_days): try: cutoff_seconds, err = datetime_utils.get_epoch( when='now', num_previous_days=older_than_days) if err: raise Exception(err) db_path, err = config.get_db_path() if err: raise Exception(err) delete_command_list = [ ["delete from email_queue where create_time < %d;" % cutoff_seconds]] ret, err = db.execute_iud(db_path, delete_command_list) if err: raise Exception(err) except Exception, e: return False, 'Error purging email queue database: %s' % str(e)
def delete_all_audits(): """Remove all audit entries from the db """ try: db_path, err = config.get_db_path() if err: raise Exception(err) command_list = [] cmd = ['delete from audit'] command_list.append(cmd) ret, err = db.execute_iud(db_path, command_list) if err: raise Exception(err) except Exception, e: return False, 'Error deleting all audits : %s' % str(e)
def process_email_queue(): """Send out all the mails that are pending in the email_queue table and update the status accordingly. """ try: d, err = load_email_settings() if err: raise Exception(err) if d: eq_list, err = get_queued_emails() # print eq_list, err if err: raise Exception(err) if eq_list: db_path, err = config.get_db_path() if err: raise Exception(err) now, err = datetime_utils.get_epoch( when='now', num_previous_days=0) if err: raise Exception(er) for eq_entry in eq_list: if 'attachment_file_location' in eq_entry and eq_entry['attachment_file_location']: attachment_file_list = [ eq_entry['attachment_file_location']] else: attachment_file_list = None result, err = send_mail(d['server'], d['port'], d['username'], d['pswd'], d['tls'], eq_entry['recipients'], eq_entry['subject'], eq_entry['body'], attachment_file_list) if err: if eq_entry['attempts'] == 2: # Failed cmd = ['update email_queue set attempts=2, status=3, last_attempt_time="%s", error_message="%s" where eq_id=%d' % ( now, err, eq_entry['eq_id'])] else: # Retry once more cmd = ['update email_queue set attempts=%d, status=2, last_attempt_time="%s", error_message="%s" where eq_id=%d' % ( ((int)(eq_entry['attempts']) + 1), now, err, eq_entry['eq_id'])] else: # Success cmd = ['update email_queue set status=0, last_attempt_time="%s" where eq_id=%d' % ( now, eq_entry['eq_id'])] ret, err = db.execute_iud(db_path, [cmd]) if err: raise Exception(err) except Exception, e: return False, 'Error processing email queue: %s' % str(e)
def create_scan_configuration(config_dict, standalone=False): try: db_location, err = get_db_location(standalone) if err: raise Exception(err) configs, err = get_scan_configurations(db_location=db_location, include_deleted=True) if err: raise Exception(err) for c in configs: if c['scan_dir'] == config_dict['scan_dir']: raise Exception( 'A configuration for that scan folder already exists.') if c['scan_dir'].strip().rstrip( '/') in config_dict['scan_dir'].strip().rstrip('/'): raise Exception( 'A configuration (scan folder "%s") for a parent folder of that scan folder already exists. Please expunge it before creating.' % c['scan_dir']) if config_dict['scan_dir'].strip().rstrip( '/') in c['scan_dir'].strip().rstrip('/'): raise Exception( 'The specified scan folder is the parent of an existing configuration (scan folder "%s"). Please expunge it before creating.' % c['scan_dir']) if 'exclude_dirs' in config_dict and config_dict['exclude_dirs']: cmd = [ 'insert into scan_configurations(scan_dir, exclude_dirs, generate_checksum, record_history, db_transaction_size, status_id) values (?,?, ?,?,?,?)', (config_dict['scan_dir'], config_dict['exclude_dirs'], config_dict['generate_checksum'], config_dict['record_history'], config_dict['db_transaction_size'], 2) ] else: cmd = [ 'insert into scan_configurations(scan_dir, generate_checksum, record_history, db_transaction_size, status_id) values (?,?,?,?,?)', (config_dict['scan_dir'], config_dict['generate_checksum'], config_dict['record_history'], config_dict['db_transaction_size'], 2) ] ret, err = db.execute_iud(db_location, [cmd]) if err: raise Exception(err) except Exception, e: return False, 'Error creating Storage Insights configuration : %s' % str( e)