def refresh_alerts(request, random=None): try: from datetime import datetime cmd_list = [] #this command will insert or update the row value if the row with the user exists. cmd = ["INSERT OR REPLACE INTO admin_alerts (user, last_refresh_time) values (?,?);", (request.user.username, datetime.now())] cmd_list.append(cmd) db_path, err = common.get_db_path() if err: raise Exception(err) test, err = db.execute_iud("%s/integral_view_config.db"%db_path, cmd_list) if err: raise Exception(err) new_alerts_present, err = alerts.new_alerts() if err: raise Exception(err) if new_alerts_present: import json alerts_list, err = alerts.load_alerts() if err: raise Exception(err) if not alerts_list: raise Exception('Error loading alerts') new_alerts = json.dumps([dict(alert=pn) for pn in alerts_list]) return django.http.HttpResponse(new_alerts, mimetype='application/json') else: clss = "btn btn-default btn-sm" message = "View alerts" return django.http.HttpResponse("No New Alerts") except Exception, e: return django.http.HttpResponse("Error loading alerts : %s"%str(e))
def get_remote_replications(remote_replication_id=None): replications = [] try: db_path, err = common.get_db_path() if err: raise Exception(err) if remote_replication_id is not None: cmd = "select * from remote_replications where remote_replication_id='%s'" % remote_replication_id else: cmd = "select * from remote_replications" replications, err = db.read_multiple_rows(db_path, cmd) if err: raise Exception(err) if replications is not None: #print 'replications - ', replications for replication in replications: cron_tasks, err = scheduler_utils.get_cron_tasks( replication['cron_task_id']) #print cron_tasks if err: raise Exception(err) if not cron_tasks: raise Exception('Specified replication schedule not found') replication['schedule_description'] = cron_tasks[0][ 'schedule_description'] replication['description'] = cron_tasks[0]['description'] except Exception, e: return None, 'Error retrieving remote replications : %s' % e
def get_cron_tasks(cron_task_id=None, user='******'): cron_list = [] try: db_path, err = common.get_db_path() if err: raise Exception(err) if cron_task_id is None: query = 'select * from cron_tasks' else: query = 'select * from cron_tasks where cron_task_id=%s' % cron_task_id cron_db_entries, err = db.read_multiple_rows(db_path, query) if err: raise Exception(err) #print cron_db_entries if cron_db_entries: cron = crontab.CronTab(user) for cron_db_entry in cron_db_entries: cron_dict = {} cron_dict['description'] = cron_db_entry['description'] cron_dict['cron_task_id'] = cron_db_entry['cron_task_id'] jobs = cron.find_comment(str(cron_db_entry['cron_task_id'])) if jobs: for job in jobs: cron_dict['schedule_description'] = job.description( use_24hour_time_format=True) cron_dict['job'] = job break cron_list.append(cron_dict) except Exception, e: return None, 'Error listing all cron entries : %s' % str(e)
def remove_cron(cron_task_id, user='******'): try: db_path, err = common.get_db_path() if err: raise Exception(err) tasks, err = get_tasks_by_cron_task_id(cron_task_id) if err: raise Exception(err) cmd_list = [] cmd_list.append( ['delete from cron_tasks where cron_task_id=%d' % cron_task_id]) cmd_list.append([ 'update tasks set status="cancelled" where cron_task_id=%s and status is not "completed"' % 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"' % task['task_id'] ]) #print cmd_list 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 refresh_alerts(request, random=None): try: from datetime import datetime cmd_list = [] #this command will insert or update the row value if the row with the user exists. cmd = ["INSERT OR REPLACE INTO admin_alerts (user, last_refresh_time) values (?,?);", (request.user.username, datetime.now())] cmd_list.append(cmd) db_path, err = common.get_db_path() if err: raise Exception(err) test, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) new_alerts_present, err = alerts.new_alerts() if err: raise Exception(err) if new_alerts_present: import json alerts_list, err = alerts.load_alerts(last_n = 5) if err: raise Exception(err) if not alerts_list: raise Exception('Error loading alerts') new_alerts = json.dumps([dict(alert=pn) for pn in alerts_list]) return django.http.HttpResponse(new_alerts, content_type='application/json') else: clss = "btn btn-default btn-sm" message = "View alerts" return django.http.HttpResponse("No New Alerts") except Exception, e: return django.http.HttpResponse("Error loading alerts : %s"%str(e))
def save_auth_settings(d): try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd = [ "update samba_global_common set workgroup=?, netbios_name=?, security=?, include_homes_section=? where id = ?", ( d["workgroup"], d["netbios_name"], d["security"], True, 1, ) ] cmd_list = [] cmd_list.append(cmd) if d["security"] == "ads": d1, err = db.read_single_row(db_path, "select * from samba_global_ad") if err: raise Exception(err) if d1: cmd = [ "update samba_global_ad set realm=?, password_server=?, ad_schema_mode=?, id_map_min=?, id_map_max=?, password_server_ip=? where id = ?", ( d["realm"], d["password_server"], 'rfc2307', 16777216, 33554431, d["password_server_ip"], 1, ) ] cmd_list.append(cmd) else: cmd = [ "insert into samba_global_ad (realm, password_server, ad_schema_mode, id_map_min, id_map_max, password_server_ip, id) values(?,?,?,?,?,?,?)", ( d["realm"], d["password_server"], 'rfc2307', 16777216, 33554431, d["password_server_ip"], 1, ) ] cmd_list.append(cmd) #print cmd_list ret, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) except Exception, e: return False, 'Error saving authentication settings : %s' % str(e)
def delete_auth_settings(): try: db_path, err = common.get_db_path() if err: raise Exception(err) ret, err = db.execute_iud(db_path, [["delete from samba_auth "]]) if err: raise Exception(err) except Exception, e: return False, 'Error deleting authentication settings : %s' % str(e)
def delete_email_settings(): try: db_path, err = common.get_db_path() if err: raise Exception(err) ret, err = db.execute_iud(["delete from email_config "]) if err: raise Exception(err) except Exception, e: return False, 'Error deleting email settings : %s' % str(e)
def load_shares_list(): l = [] try: db_path, err = common.get_db_path() if err: raise Exception(err) l, err = db.read_multiple_rows(db_path, 'select * from samba_shares') if err: raise Exception(err) except Exception, e: return None, 'Error loading CIFS shares list : %s' % str(e)
def delete_remote_replication(remote_replication_id): try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd = "delete from remote_replications where remote_replication_id='%s'" % remote_replication_id rowid, err = db.execute_iud(db_path, [ [cmd], ], get_rowid=False) if err: raise Exception(err) except Exception, e: return False, 'Error deleting remote replication task : %s' % e
def delete_all_shares(): try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd_list = [] cmd_list.append(["delete from samba_shares "]) cmd_list.append(["delete from samba_valid_users "]) ret, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) except Exception, e: return False, 'Error deleting all CIFS shares : %s' % str(e)
def load_valid_users_list(share_id): l = None try: db_path, err = common.get_db_path() if err: raise Exception(err) l, err = db.read_multiple_rows( db_path, 'select * from samba_valid_users where share_id = %s' % share_id) if err: raise Exception(err) except Exception, e: return None, 'Error loading valid users list : %s' % str(e)
def save_email_settings(d): conn = None try: db_path, err = common.get_db_path() if err: raise Exception(err) d1, err = db.read_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=?, email_alerts=?, email_audit=?, email_quota=?, rcpt_list=? where id = ?", ( d["server"], d["port"], d["username"], d["pswd"], d["tls"], d["email_alerts"], d["email_audit"], d["email_quota"], d["rcpt_list"], 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, email_alerts,email_audit,email_quota, rcpt_list, id) values (?,?,?,?,?,?,?,?,?,?)", ( d["server"], d["port"], d["username"], d["pswd"], d["tls"], d["email_alerts"], d["email_audit"], d["email_quota"], d["rcpt_list"], 1, ) ]]) if err: raise Exception(err) except Exception, e: return False, 'Error saving email settings : %s' % str(e)
def update_remote_replication(remote_replication_id, new_cron_task_id): try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd = "update remote_replications set cron_task_id='%d' where remote_replication_id='%s'" % ( new_cron_task_id, remote_replication_id) rowid, err = db.execute_iud(db_path, [ [cmd], ], get_rowid=True) if err: raise Exception(err) except Exception, e: return False, 'Error updating remote replication task : %s' % e
def get_tasks_by_cron_task_id(cron_task_id): tasks = [] try: db_path, err = common.get_db_path() if err: raise Exception(err) query = 'select * from tasks where cron_task_id="%d"' % int( cron_task_id) #print query tasks, err = db.read_multiple_rows(db_path, query) if err: raise Exception(err) except Exception, e: return None, 'Error retrieving tasks by cron task id: %s' % e
def remove_task(task_id): try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd_list = [['delete from tasks where task_id = %s' % task_id], ['delete from subtasks where task_id = %s' % task_id]] #print cmd_list 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 refresh_alerts(request, random=None): from datetime import datetime cmd_list = [] #this command will insert or update the row value if the row with the user exists. cmd = ["INSERT OR REPLACE INTO admin_alerts (user, last_refresh_time) values (?,?);", (request.user.username, datetime.now())] cmd_list.append(cmd) test = db.execute_iud("%s/integral_view_config.db"%common.get_db_path(), cmd_list) if alerts.new_alerts(): import json new_alerts = json.dumps([dict(alert=pn) for pn in alerts.load_alerts()]) return django.http.HttpResponse(new_alerts, mimetype='application/json') else: clss = "btn btn-default btn-sm" message = "View alerts" return django.http.HttpResponse("No New Alerts")
def load_email_settings(): conn = None d = None try: #print '1' db_path, err = common.get_db_path() #print '2' if err: raise Exception(err) d, err = db.read_single_row(db_path, "select * from email_config where id = 1") if err: raise Exception(err) except Exception, e: return None, 'Error loading email settings : %s' % str(e)
def get_task(task_id): task = None try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd = "select * from tasks where task_id=='%d'" % int(task_id) task, err = db.read_single_row(db_path, cmd) if err: raise Exception(err) if not task: raise Exception('Selected task not found') except Exception, e: return None, 'Error retrieving task details : %s' % e
def delete_share(share_id): try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd_list = [] cmd_list.append( ["delete from samba_shares where share_id=?", (share_id, )]) cmd_list.append( ["delete from samba_valid_users where share_id=?", (share_id, )]) ret, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) except Exception, e: return False, 'Error deleting CIFS share : %s' % str(e)
def save_share(share_id, name, comment, guest_ok, read_only, path, browseable, users, groups): try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd_list = [] cmd_list.append([ "update samba_shares set comment=?, read_only=?, guest_ok=?, browseable=? where share_id=?", ( comment, read_only, guest_ok, browseable, share_id, ) ]) cmd_list.append( ["delete from samba_valid_users where share_id=?", (share_id, )]) if not guest_ok: if users: for user in users: cmd_list.append([ "insert into samba_valid_users (id, share_id, grp, name) values (NULL,?,?,?)", ( share_id, False, user, ) ]) if groups: for group in groups: cmd_list.append([ "insert into samba_valid_users (id, share_id, grp, name) values (NULL,?,?,?)", ( share_id, True, group, ) ]) ret, err = db.execute_iud(db_path, cmd_list) if err: raise Exception(err) except Exception, e: return False, 'Error saving CIFS share : %s' % str(e)
def add_cron_task(command, description, min="1", hour='*', day='*', dow='*', month='*', user='******'): cron_task_id = None try: if not command or not description: raise Exception('Invalid parameters') db_path, err = common.get_db_path() if err: raise Exception(err) #cmd = ["insert into samba_global_ad (realm, password_server, ad_schema_mode, id_map_min, id_map_max, password_server_ip, id) values(?,?,?,?,?,?,?)", (d["realm"], d["password_server"], 'rfc2307', 16777216, 33554431, d["password_server_ip"], 1,)] cmd = [ 'insert into cron_tasks(command,description) values (?,?)', (command, description) ] #print cmd cron_task_id, err = db.execute_iud(db_path, [cmd], get_rowid=True) if err: raise Exception(err) log_path, err = common.get_log_folder_path() if err: raise Exception(err) log_dir = '%s/cron_logs' % log_path 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 load_share_info(mode, index): d = None try: db_path, err = common.get_db_path() if err: raise Exception(err) query = None if mode == "by_id": query = "select * from samba_shares where share_id = %s" % index else: query = "select * from samba_shares where name = %s" % index d, err = db.read_single_row(db_path, query) if err: raise Exception(err) except Exception, e: return None, 'Error loading CIFS share information : %s' % str(e)
def change_auth_method(security): try: db_path, err = common.get_db_path() if err: raise Exception(err) cl = [] cl.append([ "update samba_global_common set security='%s' where id=1" % security ]) cl.append(["delete from samba_valid_users"]) 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 load_auth_settings(): d = None try: db_path, err = common.get_db_path() if err: raise Exception(err) d, err = db.read_single_row( db_path, "select * from samba_global_common where id=1") if err: raise Exception(err) if d and d["security"] == "ads": d1, err = db.read_single_row( db_path, "select * from samba_global_ad where id=1") if err: raise Exception(err) if d1: d.update(d1) except Exception, e: return None, 'Error loading authentication settings : %s' % str(e)
def add_remote_replication(source_dataset, destination_ip, destination_username, destination_pool, cron_task_id): remote_replication_id = None try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd = "insert into remote_replications (source_dataset,destination_ip,destination_user_name,destination_pool, cron_task_id) values ('%s','%s','%s','%s', '%d')" % ( source_dataset, destination_ip, destination_username, destination_pool, cron_task_id) remote_replication_id, err = db.execute_iud(db_path, [ [cmd], ], get_rowid=True) if err: raise Exception(err) except Exception, e: return None, 'Error adding a remote replication task : %s' % e
def get_subtasks(task_id): ''' TODO - change name to get_subtasks - change db params ''' subtasks = None try: query = "select * from subtasks where task_id = '%d'" % task_id db_path, err = common.get_db_path() if err: raise Exception(err) subtasks, err = db.read_multiple_rows(db_path, query) if err: raise Exception(err) if not subtasks: raise Exception('No subtasks found for the specified task.') except Exception, e: return None, 'Error retrieving subtasks : %s' % e
def main(): lg = None try: lg, err = logger.get_script_logger('Task processor', '/var/log/integralstor/scripts.log', level=logging.DEBUG) logger.log_or_print('Task processor execution initiated.', lg, level='info') db_path, err = common.get_db_path() if err: raise Exception(err) ret, err = scheduler_utils.process_tasks() if err: raise Exception(err) except Exception, e: str = 'Error running the task processor : %s' % e logger.log_or_print(str, lg, level='critical') return -1
def set_log_level(level): try: db_path, err = common.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.read_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 get_tasks(node=None): tasks = None try: db_path, err = common.get_db_path() if err: raise Exception(err) #start_time = int((datetime.datetime.now() - datetime.timedelta(minutes=minutes)).strftime("%s")) #end_time = int((datetime.datetime.now() + datetime.timedelta(minutes=minutes)).strftime("%s")) if not node: tasks_query = "select * from tasks order by initiate_time desc" else: tasks_query = "select * from scheduler_tasks order by initiate_time desc" #print tasks_query tasks, err = db.read_multiple_rows(db_path, tasks_query) if err: raise Exception(err) #print tasks except Exception, e: return None, 'Error retrieving tasks : %s' % e
def set_log_level(level): try: db_path, err = common.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.read_single_row("%s/integral_view_config.db"%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("%s/integral_view_config.db"%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 main(): try: lck, err = lock.get_lock('unicell_poll_for_alerts') if err: raise Exception(err) if not lck: raise Exception('Could not acquire lock. Exiting.') alert_list = [] now = int(time.time()) db_path, err = common.get_db_path() if err: raise Exception(err) tasks_query = "select * from tasks where last_run_time > '%d' and (status = 'error-retrying' or status = 'failed');" % ( now - 110) #print "\ntasks_query: ", tasks_query rows, err = db.read_multiple_rows(db_path, tasks_query) #print "\nrows: ", rows if err: raise Exception(err) if rows: for row in rows: msg = "%s: %s." % (row['status'], row['description']) alert_list.append(msg) #print "\nalert_list: ", alert_list if alert_list: alerts.raise_alert(alert_list) lock.release_lock('unicell_poll_for_alerts') except Exception, e: print "Error generating alerts : %s ! Exiting." % str(e) sys.exit(-1)
def get_remote_replications_with(source_dataset, destination_ip, destination_pool): replications = [] repls = [] try: db_path, err = common.get_db_path() if err: raise Exception(err) cmd = "select * from remote_replications where source_dataset='%s' and destination_ip='%s' and destination_pool='%s'" % ( source_dataset, destination_ip, destination_pool) replications, err = db.read_multiple_rows(db_path, cmd) if err: raise Exception(err) if replications is not None: #print 'replications - ', replications for replication in replications: repls, err = get_remote_replications( replication['remote_replication_id']) if err: raise Exception(err) except Exception, e: return None, 'Error retrieving remote replications : %s' % e
CONFIG_DIR = '%s/config'%BASE_CONF_ROOT ADMIN_VOL_NAME = "fractalio_admin_vol" if not PRODUCTION: KRB5_PATH = CONFIG_DIR SMB_CONF_PATH = CONFIG_DIR BASE_FILE_PATH = '%s/files'%BASE_CONF_ROOT BATCH_COMMANDS_DIR = '%s/batch'%BASE_CONF_ROOT AUDIT_TRAIL_DIR = '%s/audit_trail'%BASE_CONF_ROOT ALERTS_DIR = '%s/alerts'%BASE_CONF_ROOT ''' LOGIN_URL = '/login/' DB_LOCATION = common.get_db_path() STATIC_DIR_PATH = '/opt/integralstor/integralstor-unicell/integral_view/integral_view/static' TEMPLATE_DIR_PATH = "/opt/integralstor/integralstor-unicell/integral_view/integral_view/templates" TEMPLATE_DEBUG = DEBUG ADMINS = ( # ('Your Name', '*****@*****.**'), ) MANAGERS = ADMINS DATABASES = { 'default': {
# Django settings for integral_view project. import integralstor_common from integralstor_common import common DEBUG = True ALLOWED_HOSTS = ['*'] APP_DEBUG = False LOGIN_URL = '/login/' DB_LOCATION, err = common.get_db_path() STATIC_DIR_PATH = '/opt/integralstor/integralstor_unicell/integral_view/static' TEMPLATE_DIR_PATH = "/opt/integralstor/integralstor_unicell/integral_view/templates" TEMPLATE_DEBUG = DEBUG ADMINS = ( # ('Your Name', '*****@*****.**'), ) MANAGERS = ADMINS DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': '%s/integral_view_auth.db'%DB_LOCATION, # Or path to database file if using sqlite3. 'USER': '', # Not used with sqlite3. 'PASSWORD': '', # Not used with sqlite3.
import json,sqlite3 import integralstor_common from integralstor_common import command, db, common import local_users import salt.client db_path = common.get_db_path() def change_auth_method(security): cl = [] cl.append(["update samba_global_common set security='%s' where id=1"%security]) cl.append(["delete from samba_valid_users"]) db.execute_iud("%s/integral_view_config.db"%db_path, cl) def load_auth_settings(): d = None conn = None try : d = db.read_single_row("%s/integral_view_config.db"%db_path, "select * from samba_global_common where id=1") if d and d["security"] == "ads": d1 = db.read_single_row("%s/integral_view_config.db"%db_path, "select * from samba_global_ad where id=1") if d1: d.update(d1) finally: return d