def sync_ntp(): return_dict = {} try: ntp_servers, err = get_ntp_servers() if err: raise Exception(err) if len(ntp_servers) < 1: raise Exception( "NTP servers are not configured, Please configure at least one server to sync") output, err = services_management.update_service_status('ntpd', 'stop') if err: raise Exception(err) for server in ntp_servers: cmd = "ntpdate -b %s" % server output, err = command.get_command_output(cmd, shell=True) if err: continue else: return_dict['ntp_sync'] = True return_dict['server_used'] = server break output, err = services_management.update_service_status( 'ntpd', 'start') if err: raise Exception(err) except Exception, e: return None, 'Sync failed'
def generate_exports(share_list): try: lines = [] lines.append('/var/log *(ro,root_squash)') if share_list: for share in share_list: line = share['path'] if 'clients' in share: client_list = [] for client in share['clients']: client_str = client['name'] if 'options' in client: client_str = '%s(%s)' % ( client_str.strip(), ','.join(client['options'])) client_list.append(client_str.strip()) line += ' ' line += ' '.join(client_list) if 'options' in share: line += '(' line += ','.join(share['options']) line += ')' lines.append(line) with open('/tmp/new_exports', 'w') as f: if lines: for line in lines: f.write('%s\n' % line) f.flush() f.close() shutil.move('/tmp/new_exports', '/etc/exports') ret, err = services_management.update_service_status('nfs', 'reload') if err: raise Exception(err) except Exception, e: return False, 'Error saving exports file : %s' % str(e)
def reload_configuration(): try: cmd_to_run = 'smbcontrol all reload-config' lines, err = command.get_command_output(cmd_to_run) if err: raise Exception(err) ret, err = services_management.update_service_status( 'winbind', 'restart') if err: raise Exception(err) ret, err = services_management.update_service_status( 'smb', 'restart') if err: raise Exception(err) except Exception, e: return False, 'Error reloading CIFS configuration : %s' % str(e)
def reload_configuration(action='restart'): try: if action not in ['restart', 'reload']: raise Exception('Error: Received invalid action') cmd_to_run = 'smbcontrol all reload-config' lines, err = command.get_command_output(cmd_to_run) if err: raise Exception(err) ret, err = services_management.update_service_status('winbind', action) if err: raise Exception(err) ret, err = services_management.update_service_status('smb', action) if err: raise Exception(err) except Exception, e: return False, 'Error reloading CIFS configuration : %s' % str(e)
def generate_exports(share_list): try: lines = [] lines.append('/var/log *(ro,root_squash)') if share_list: for share in share_list: line = share['path'] if 'clients' in share: client_list = [] for client in share['clients']: client_str = client['name'] if 'options' in client: client_str = '%s(%s)' % (client_str.strip(), ','.join( client['options'])) client_list.append(client_str.strip()) line += ' ' line += ' '.join(client_list) if 'options' in share: line += '(' line += ','.join(share['options']) line += ')' lines.append(line) with open('/tmp/new_exports', 'w') as f: if lines: for line in lines: f.write('%s\n' % line) f.flush() f.close() shutil.move('/tmp/new_exports', '/etc/exports') ret, err = services_management.update_service_status('nfs', 'reload') if err: raise Exception(err) except Exception, e: return False, 'Error saving exports file : %s' % str(e)
def delete_all_exports(): try: with open('/etc/exports', 'w') as f: pass ret, err = services_management.update_service_status('nfs', 'reload') if err: raise Exception(err) except Exception, e: return False, 'Error deleting all NFS exports : %s' % str(e)
def delete_all_exports(): try: with open('/etc/exports', 'w') as f: pass ret, err = services_management.update_service_status('nfs', 'reload') if err: raise Exception(err) except Exception, e: return False, 'Error deleting all NFS exports : %s' % str(e)
def delete_ftp_config(): try: with open('/etc/vsftpd/vsftpd.conf', 'w') as f: pass ret, err = services_management.update_service_status( 'vsftpd', 'restart') if err: raise Exception(err) except Exception, e: return False, 'Error removing FTP configuration : %s' % str(e)
def update_service_status(request): return_dict = {} try: if request.method == "GET": raise Exception("Invalid request. Please use the menus") if 'service' not in request.POST: raise Exception("Invalid request. Please use the menus") if 'action' not in request.POST or request.POST['action'] not in [ 'start', 'stop', 'restart', 'enable', 'disable' ]: raise Exception("Invalid request. Please use the menus") service = request.POST['service'] action = request.POST['action'] if 'action' == 'start' and service == 'vsftpd': # Need to make sure that all local users have their directories # created so do it.. config, err = vsftp.get_ftp_config() if err: raise Exception(err) users, err = local_users.get_local_users() if err: raise Exception(err) ret, err = create_ftp_user_dirs(config['dataset'], users) if err: raise Exception(err) audit_str = "Service status change of %s initiated to %s state." % ( service, action) audit.audit("change_service_status", audit_str, request) out, err = services_management.update_service_status(service, action) if err: raise Exception(err) if out: return django.http.HttpResponseRedirect( '/system/view_services?&service_change_status=%s' % ','.join(out)) else: return django.http.HttpResponseRedirect( '/system/view_services?service_change_status=none') except Exception, e: return_dict['base_template'] = "system_base.html" return_dict["page_title"] = 'Modify system service state' return_dict['tab'] = 'view_services_tab' return_dict["error"] = 'Error modifying system services state' return_dict["error_details"] = str(e) return django.shortcuts.render_to_response( "logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
def update_ftp_config(config): try: pki_dir, err = integralstor_config.get_pki_dir() if err: raise Exception(err) with open('/tmp/vsftpd.conf', 'w') as f: f.write( "# AutoGenerated by IntegralStor. Do not change this file manually \n" ) f.write('anonymous_enable=NO\n') f.write('local_enable=YES\n') f.write('listen=YES\n') f.write('local_umask=022\n') f.write('dirmessage_enable=YES\n') f.write('connect_from_port_20=YES\n') f.write('xferlog_enable=YES\n') f.write('xferlog_file=/var/log/xferlog\n') f.write('xferlog_std_format=YES\n') f.write('ftpd_banner=Welcome to the IntegralStor FTP service.\n') f.write('chroot_local_user=YES\n') # f.write('user_config_dir=/etc/vsftpd/users\n') f.write('local_root=/%s/$USER\n' % config['dataset']) f.write('user_sub_token=$USER\n') f.write('dirlist_enable=YES\n') f.write('download_enable=YES\n') f.write('write_enable=YES\n') f.write('pam_service_name=vsftpd\n') f.write('userlist_enable=YES\n') f.write('tcp_wrappers=YES\n') if config['ssl_enabled']: f.write('ssl_enable=yes\n') f.write('rsa_cert_file=%s/%s/%s.cert\n' % (pki_dir, config['cert_name'], config['cert_name'])) f.write('rsa_private_key_file=%s/%s/%s.cert\n' % (pki_dir, config['cert_name'], config['cert_name'])) f.write('allow_anon_ssl=NO\n') f.write('force_local_data_ssl=YES\n') f.write('force_local_logins_ssl=YES\n') f.write('ssl_tlsv1=YES\n') f.write('ssl_sslv2=NO\n') f.write('ssl_sslv3=NO\n') f.write('require_ssl_reuse=NO\n') f.write('ssl_ciphers=HIGH\n') else: f.write('ssl_enable=no\n') shutil.move('/tmp/vsftpd.conf', '/etc/vsftpd/vsftpd.conf') ret, err = services_management.update_service_status( 'vsftpd', 'restart') if err: raise Exception(err) except Exception, e: return False, 'Error updating FTP configuration files : %s' % str(e)
def update_service_status(request): return_dict = {} try: if request.method == "GET": raise Exception("Invalid request. Please use the menus") if 'service' not in request.POST: raise Exception("Invalid request. Please use the menus") if 'action' not in request.POST or request.POST['action'] not in ['start', 'stop', 'restart', 'enable', 'disable']: raise Exception("Invalid request. Please use the menus") service = request.POST['service'] action = request.POST['action'] if 'action' == 'start' and service == 'vsftpd': # Need to make sure that all local users have their directories # created so do it.. config, err = vsftp.get_ftp_config() if err: raise Exception(err) users, err = local_users.get_local_users() if err: raise Exception(err) ret, err = create_ftp_user_dirs(config['dataset'], users) if err: raise Exception(err) audit_str = "Service status change of %s initiated to %s state." % ( service, action) audit.audit("change_service_status", audit_str, request) out, err = services_management.update_service_status(service, action) if err: raise Exception(err) if out: return django.http.HttpResponseRedirect('/system/view_services?&service_change_status=%s' % ','.join(out)) else: return django.http.HttpResponseRedirect('/system/view_services?service_change_status=none') except Exception, e: return_dict['base_template'] = "system_base.html" return_dict["page_title"] = 'Modify system service state' return_dict['tab'] = 'view_services_tab' return_dict["error"] = 'Error modifying system services state' return_dict["error_details"] = str(e) return django.shortcuts.render_to_response("logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
def update_integralstor_ntp_servers(server_list): try: with open('/tmp/ntp.conf', 'w') as temp: # First create the ntp.conf file for the primary and # secondary nodes temp.write("driftfile /var/lib/ntp/drift\n") temp.write( "restrict default kod nomodify notrap nopeer noquery\n") temp.write( "restrict -6 default kod nomodify notrap nopeer noquery\n") temp.write("logfile /var/log/ntp.log\n") temp.write("\n") for server in server_list: temp.write("server %s iburst\n" % server) temp.flush() temp.close() shutil.move('/tmp/ntp.conf', '/etc/ntp.conf') ret, err = services_management.update_service_status( 'ntpd', 'restart') if err: raise Exception(err) except Exception, e: return False, 'Error updating NTP settings : %s' % str(e)
def upload_sys_info(request): return_dict = {} try: config_dir, err = config.get_config_dir() if err: raise Exception(err) if request.method == "POST": status, err = _handle_uploaded_file(request.FILES['file_field']) if err: raise Exception(err) display_name, err = config.get_config_dir() if err: raise Exception(err) zip = zipfile.ZipFile('/tmp/upload.zip', 'r') data = zip.namelist() move = zip.extractall("/tmp/upload_system_config/") logs = { 'smb_conf': '/etc/samba/smb.conf', 'ntp_conf': '/etc/ntp.conf', 'krb5_conf': '/etc/krb5.conf', 'nfs': '/etc/exports', 'ftp': '/etc/vsftpd/vsftpd.conf' } if os.path.isfile('/tmp/upload_system_config/local_groups'): groups = None with open('/tmp/upload_system_config/local_groups', 'r') as f: groups = json.load(f) if groups: for group in groups: # print 'creating group ', group ret, err = local_users.create_local_group( group['grpname']) if err: raise Exception(err) # print 'groups - ', groups if os.path.isfile('/tmp/upload_system_config/local_users'): users = None with open('/tmp/upload_system_config/local_users', 'r') as f: users = json.load(f) if users: default_group_name, err = config.get_users_default_group() if err: raise Exception(err) default_gid, err = config.get_system_uid_gid( default_group_name, 'group') if err: raise Exception(err) for user in users: # print 'creating user ', user # print 'username is ', user['comment'][18:] ret, err = local_users.create_local_user( user['username'], user['comment'][18:], 'password', default_gid) if err: raise Exception(err) if 'other_groups' in user and user['other_groups']: # print 'adding other groups' for g in user['other_groups']: ret, err = local_users.set_group_membership( g, [user['username']]) if err: raise Exception(err) # print 'users - ', users for key, value in upload_download_logs.iteritems(): if key and os.path.isfile("/tmp/upload_system_config/" + key): # print 'overwriting ', "/tmp/upload_system_config/%s"%key, # 'to ', value ret, err = _copy_file_and_overwrite( "/tmp/upload_system_config/%s" % key, value) if err: raise Exception(err) for dir in upload_download_conf_dirs: if os.path.isdir("/tmp/upload_system_config/%s" % dir): # print 'overwriting ', "/tmp/upload_system_config/%s"%dir, # ' to ', '%s/%s'%(config_dir,dir) ret, err = _copy_and_overwrite( "/tmp/upload_system_config/%s" % dir, '%s/%s' % (config_dir, dir)) if err: raise Exception(err) services_restart_list = [ 'ntpd', 'smb', 'winbind', 'tgtd', 'nfs', 'vsftpd' ] for service in services_restart_list: ret, err = services_management.update_service_status( service, 'restart') if err: raise Exception(err) audit_str = 'Upload of an external system configuration completed successfully.' audit.audit('upload_configuration', audit_str, request) return django.http.HttpResponseRedirect( "/system/view_system_info?ack=config_uploaded") else: form = common_forms.FileUploadForm() return_dict["form"] = form return django.shortcuts.render_to_response( "upload_sys_info.html", return_dict, context_instance=django.template.context.RequestContext( request)) except Exception, e: audit_str = 'Upload of an external system configuration failed : %s.' % str( e) audit.audit('upload_configuration', audit_str, request) return_dict["base_template"] = 'system_base.html' return_dict['tab'] = 'system_info_tab' return_dict["page_title"] = 'Upload system configuration' return_dict["error"] = 'Error uploading system configuration' return_dict["error_details"] = str(e) return django.shortcuts.render_to_response( "logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
def main(): lg = None try: stop_services = False scripts_log, err = config.get_scripts_log_path() if err: raise Exception(err) lg, err = logger.get_script_logger( 'OS file system check', scripts_log, level=logging.DEBUG) lck, err = lock.get_lock('check_os_filesystems') if err: raise Exception(err) if not lck: raise Exception('Could not acquire lock.') logger.log_or_print('OS filesystem check initiated.', lg, level='info') if len(sys.argv) != 3: raise Exception( 'Usage : python check_os_filesystems.py <warning_percentage> <critical_percentage>') warning_percentage = int(sys.argv[1]) critical_percentage = int(sys.argv[2]) os_disk_stats, err = disks.get_os_partition_stats() if err: raise Exception(err) alerts_list = [] for partition in os_disk_stats: fs_name = partition['fs_name'] percentage_used = 100 - partition['percentage_free'] alert = False if percentage_used > critical_percentage: severity_str = 'CRITICAL' severity_type = 3 alert = True print_percentage = critical_percentage if '/var' in fs_name: # print 'stopping services' stop_services = True logger.log_or_print( 'OS filesystem %s full. Stopping all data services.' % fs_name, lg, level='critical') elif percentage_used > warning_percentage: severity_type = 2 severity_str = 'warning' print_percentage = warning_percentage alert = True if alert: alert_str = 'Partition %s has exceeded the %s threshold capacity of %d%% and is now %d%% full.' % ( fs_name, severity_str, print_percentage, percentage_used) if severity_type == 3: alert_str += ' Stopping all data services now. Please clear up space before resuming these services.' alerts_list.append({'subsystem_type_id': 8, 'severity_type_id': severity_type, 'component': fs_name, 'alert_str': alert_str}) if alerts_list: retval, err = alerts.record_alerts(alerts_list) if err: raise Exception(err) if stop_services: services = ['smb', 'winbind', 'nfs', 'vsftpd', 'urbackup-server'] for service_name in services: services_management.update_service_status(service_name, 'stop') except Exception, e: # print str(e) lock.release_lock('check_os_filesystems') logger.log_or_print('Error checking OS filesystems : %s' % e, lg, level='critical') return -1, 'Error checking OS filesystems : %s' % e
def upload_sys_info(request): return_dict = {} try: config_dir, err = config.get_config_dir() if err: raise Exception(err) if request.method == "POST": status, err = _handle_uploaded_file(request.FILES['file_field']) if err: raise Exception(err) display_name, err = config.get_config_dir() if err: raise Exception(err) zip = zipfile.ZipFile('/tmp/upload.zip', 'r') data = zip.namelist() move = zip.extractall("/tmp/upload_system_config/") logs = {'smb_conf': '/etc/samba/smb.conf', 'ntp_conf': '/etc/ntp.conf', 'krb5_conf': '/etc/krb5.conf', 'nfs': '/etc/exports', 'ftp': '/etc/vsftpd/vsftpd.conf'} if os.path.isfile('/tmp/upload_system_config/local_groups'): groups = None with open('/tmp/upload_system_config/local_groups', 'r') as f: groups = json.load(f) if groups: for group in groups: # print 'creating group ', group ret, err = local_users.create_local_group( group['grpname']) if err: raise Exception(err) # print 'groups - ', groups if os.path.isfile('/tmp/upload_system_config/local_users'): users = None with open('/tmp/upload_system_config/local_users', 'r') as f: users = json.load(f) if users: default_group_name, err = config.get_users_default_group() if err: raise Exception(err) default_gid, err = config.get_system_uid_gid( default_group_name, 'group') if err: raise Exception(err) for user in users: # print 'creating user ', user # print 'username is ', user['comment'][18:] ret, err = local_users.create_local_user( user['username'], user['comment'][18:], 'password', default_gid) if err: raise Exception(err) if 'other_groups' in user and user['other_groups']: # print 'adding other groups' for g in user['other_groups']: ret, err = local_users.set_group_membership( g, [user['username']]) if err: raise Exception(err) # print 'users - ', users for key, value in upload_download_logs.iteritems(): if key and os.path.isfile("/tmp/upload_system_config/" + key): # print 'overwriting ', "/tmp/upload_system_config/%s"%key, # 'to ', value ret, err = _copy_file_and_overwrite( "/tmp/upload_system_config/%s" % key, value) if err: raise Exception(err) for dir in upload_download_conf_dirs: if os.path.isdir("/tmp/upload_system_config/%s" % dir): # print 'overwriting ', "/tmp/upload_system_config/%s"%dir, # ' to ', '%s/%s'%(config_dir,dir) ret, err = _copy_and_overwrite( "/tmp/upload_system_config/%s" % dir, '%s/%s' % (config_dir, dir)) if err: raise Exception(err) services_restart_list = ['ntpd', 'smb', 'winbind', 'tgtd', 'nfs', 'vsftpd'] for service in services_restart_list: ret, err = services_management.update_service_status( service, 'restart') if err: raise Exception(err) audit_str = 'Upload of an external system configuration completed successfully.' audit.audit('upload_configuration', audit_str, request) return django.http.HttpResponseRedirect("/system/view_system_info?ack=config_uploaded") else: form = common_forms.FileUploadForm() return_dict["form"] = form return django.shortcuts.render_to_response("upload_sys_info.html", return_dict, context_instance=django.template.context.RequestContext(request)) except Exception, e: audit_str = 'Upload of an external system configuration failed : %s.' % str( e) audit.audit('upload_configuration', audit_str, request) return_dict["base_template"] = 'system_base.html' return_dict['tab'] = 'system_info_tab' return_dict["page_title"] = 'Upload system configuration' return_dict["error"] = 'Error uploading system configuration' return_dict["error_details"] = str(e) return django.shortcuts.render_to_response("logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
def main(): lg = None try: stop_services = False scripts_log, err = config.get_scripts_log_path() if err: raise Exception(err) lg, err = logger.get_script_logger('OS file system check', scripts_log, level=logging.DEBUG) lck, err = lock.get_lock('check_os_filesystems') if err: raise Exception(err) if not lck: raise Exception('Could not acquire lock.') logger.log_or_print('OS filesystem check initiated.', lg, level='info') if len(sys.argv) != 3: raise Exception( 'Usage : python check_os_filesystems.py <warning_percentage> <critical_percentage>' ) warning_percentage = int(sys.argv[1]) critical_percentage = int(sys.argv[2]) os_disk_stats, err = disks.get_os_partition_stats() if err: raise Exception(err) alerts_list = [] for partition in os_disk_stats: fs_name = partition['fs_name'] percentage_used = 100 - partition['percentage_free'] alert = False if percentage_used > critical_percentage: severity_str = 'CRITICAL' severity_type = 3 alert = True print_percentage = critical_percentage if '/var' in fs_name: # print 'stopping services' stop_services = True logger.log_or_print( 'OS filesystem %s full. Stopping all data services.' % fs_name, lg, level='critical') elif percentage_used > warning_percentage: severity_type = 2 severity_str = 'warning' print_percentage = warning_percentage alert = True if alert: alert_str = 'Partition %s has exceeded the %s threshold capacity of %d%% and is now %d%% full.' % ( fs_name, severity_str, print_percentage, percentage_used) if severity_type == 3: alert_str += ' Stopping all data services now. Please clear up space before resuming these services.' alerts_list.append({ 'subsystem_type_id': 8, 'severity_type_id': severity_type, 'component': fs_name, 'alert_str': alert_str }) if alerts_list: retval, err = alerts.record_alerts(alerts_list) if err: raise Exception(err) if stop_services: services = ['smb', 'winbind', 'nfs', 'vsftpd', 'urbackup-server'] for service_name in services: services_management.update_service_status(service_name, 'stop') except Exception, e: # print str(e) lock.release_lock('check_os_filesystems') logger.log_or_print('Error checking OS filesystems : %s' % e, lg, level='critical') return -1, 'Error checking OS filesystems : %s' % e