def salt_start(): lg = None try: lg, err = logger.get_script_logger('Conditional salt start completed', '/var/log/integralstor/scripts.log', level=logging.DEBUG) logger.log_or_print('Conditional salt start initiated.', lg, level='info') pog, err = grid_ops.is_part_of_grid() logger.log_or_print('Part of grid : %s' % pog, lg, level='info') if err: raise Exception(err) if not pog: logger.log_or_print('Starting salt services.', lg, level='info') out, err = command.get_command_output('service salt-minion start', False) if err: raise Exception(err) out, err = command.get_command_output('service salt-master start', False) if err: raise Exception(err) else: logger.log_or_print( 'Not starting salt services as I am part of the grid.', lg, level='info') except Exception, e: str = 'Error doing a conditional salt start : %s' % e logger.log_or_print(str, lg, level='critical') return False, str
def check_windows_processes_status(print_to_stdout=True, separator='\n'): ret_list = [] try: if print_to_stdout: ret_list.append('GRIDCell windows access processes status: ') out, err = command.get_command_output('service smb status', False) if err: raise Exception(err) ret_list.append('Windows (SMB) process status : %s' % ', '.join(out)) out, err = command.get_command_output('service winbind status', False) if err: raise Exception(err) ret_list.append('Windows (winbind) process status : %s' % ', '.join(out)) out, err = command.get_command_output('service ntpd status', False) if err: raise Exception(err) ret_list.append('Time sync (NTP) process status : %s' % ', '.join(out)) ''' Commenting out as we wont use CTDB for this release out, err = command.get_command_output('service ctdb status', False) if err: raise Exception(err) ret_list.append('Clustered windows access (CTDB) process status : %s'%', '.join(out)) ''' if print_to_stdout: print separator.join(ret_list) except Exception, e: return None, 'Error checking windows access process status: %s' % str( e)
def get_ad_users_or_groups(type): """Issue an active directory command that gets all users and groups to display in share creation/editing.""" o = None try: d, err = get_auth_settings() if err: raise Exception(err) workgroup = d['workgroup'] if type and type == "users": o, err = command.get_command_output('wbinfo -u --domain=%s' % workgroup) if err: raise Exception(err) elif type and type == "groups": o, err = command.get_command_output('wbinfo -g --domain=%s' % workgroup) if err: raise Exception(err) else: raise Exception("Unknown type specified.") # print '%s - '%type, o except Exception, e: return None, 'Error retrieving Active Directory Users/Groups : %s' % str( e)
def salt_start(): lg = None try: lg, err = logger.get_script_logger( 'Conditional salt start completed', '/var/log/integralstor/scripts.log', level=logging.DEBUG) logger.log_or_print( 'Conditional salt start initiated.', lg, level='info') pog, err = grid_ops.is_part_of_grid() logger.log_or_print('Part of grid : %s' % pog, lg, level='info') if err: raise Exception(err) if not pog: logger.log_or_print('Starting salt services.', lg, level='info') out, err = command.get_command_output( 'service salt-minion start', False) if err: raise Exception(err) out, err = command.get_command_output( 'service salt-master start', False) if err: raise Exception(err) else: logger.log_or_print( 'Not starting salt services as I am part of the grid.', lg, level='info') except Exception, e: str = 'Error doing a conditional salt start : %s' % e logger.log_or_print(str, lg, level='critical') return False, str
def view_task_details(request, task_id): return_dict = {} try: task, err = scheduler_utils.get_task(int(task_id)) if err: raise Exception(err) return_dict['task'] = task subtasks, err = scheduler_utils.get_subtasks(int(task_id)) if err: raise Exception(err) return_dict["subtasks"] = subtasks # print subtasks, err task_output = "" log_path, err = config.get_log_folder_path() if err: raise Exception(err) log_dir = '%s/task_logs' % log_path log_file_path = '%s/%d.log' % (log_dir, int(task_id)) if os.path.isfile(log_file_path): lines, err = command.get_command_output("wc -l %s" % log_file_path) no_of_lines = lines[0].split()[0] # print no_of_lines if int(no_of_lines) <= 41: # This code always updates the 0th element of the command list. # This is assuming that we will only have one long running # command. with open(log_file_path) as output: task_output = task_output + ''.join(output.readlines()) else: first, err = command.get_command_output("head -n 5 %s" % log_file_path, shell=True) if err: print err last, err = command.get_command_output("tail -n 20 %s" % log_file_path, shell=True) if err: print err # print last task_output = task_output + '\n'.join(first) task_output = task_output + "\n.... \n ....\n" task_output = task_output + '\n'.join(last) return_dict['task_output'] = task_output return django.shortcuts.render_to_response( "view_task_details.html", return_dict, context_instance=django.template.context.RequestContext(request)) except Exception, e: return_dict['base_template'] = "scheduler_base.html" return_dict["page_title"] = 'Background jobs' return_dict['tab'] = 'view_background_tasks_tab' return_dict["error"] = 'Error retriving background task details' return_dict["error_details"] = e return django.shortcuts.render_to_response( "logged_in_error.html", return_dict, context_instance=django.template.context.RequestContext(request))
def update_manifest(request): return_dict = {} try: if request.method == "GET": from integralstor_utils import manifest_status as iu mi, err = iu.generate_manifest_info() # print mi, err if err: raise Exception(err) if not mi: raise Exception('Could not load new configuration') return_dict["mi"] = mi[mi.keys()[0]] # Need the hostname here. return django.shortcuts.render_to_response( "update_manifest.html", return_dict, context_instance=django.template.context.RequestContext( request)) elif request.method == "POST": common_python_scripts_path, err = config.get_common_python_scripts_path( ) if err: raise Exception(err) ss_path, err = config.get_system_status_path() if err: raise Exception(err) #(ret,rc), err = command.execute_with_rc("python %s/generate_manifest.py %s"%(common_python_scripts_path, ss_path)) ret, err = command.get_command_output( "python %s/generate_manifest.py %s" % (common_python_scripts_path, ss_path)) # print 'mani', ret, err if err: raise Exception(err) #(ret,rc), err = command.execute_with_rc("python %s/generate_status.py %s"%(config.get_python_scripts_path(),config.get_system_status_path())) ret, err = command.get_command_output( "python %s/generate_status.py %s" % (common_python_scripts_path, ss_path)) # print 'stat', ret, err if err: raise Exception(err) return django.http.HttpResponseRedirect("/view_system_info/") except Exception, e: return_dict['base_template'] = "system_base.html" return_dict["page_title"] = 'Reload system configuration' return_dict['tab'] = 'node_info_tab' return_dict["error"] = 'Error reloading 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 update_date_time(system_date, system_time, system_timezone): return_dict = {} try: if system_time == None: pass else: cmd = 'date -s %s' % system_time output, err = command.get_command_output(cmd) if err: raise Exception(err) return_dict['time_set'] = True return_dict['time_set_to'] = system_time if system_date == None: pass else: date_component = system_date.split("/") if len(date_component) != 3: raise Exception( "The input date format is not correct (Ex: MM/DD/YYYY)") system_date = '%s-%s-%s' % (date_component[2], date_component[0], date_component[1]) output_time, err = command.get_command_output("date +%T") if err: raise Exception(err) cmd = 'date -s "%s %s"' % (system_date, output_time[0]) output, err = command.get_command_output(cmd) if err: raise Exception(err) return_dict['date_set'] = True return_dict['date_set_to'] = system_date if system_timezone == None: pass else: cmd = 'timedatectl set-timezone %s' % system_timezone output, err = command.get_command_output(cmd) if err: raise Exception(err) return_dict['timezone_set'] = True return_dict['timezone_set_to'] = system_timezone cmd = "hwclock -w" output, err = command.get_command_output(cmd) if err: raise Exception(err) except Exception, e: return None, str(e)
def update_date_time(system_date, system_time, system_timezone): return_dict = {} try: if system_time == None: pass else: cmd = 'date -s %s' % system_time output, err = command.get_command_output(cmd) if err: raise Exception(err) return_dict['time_set'] = True return_dict['time_set_to'] = system_time if system_date == None: pass else: date_component = system_date.split("/") if len(date_component) != 3: raise Exception( "The input date format is not correct (Ex: MM/DD/YYYY)") system_date = '%s-%s-%s' % (date_component[2], date_component[0], date_component[1]) output_time, err = command.get_command_output("date +%T") if err: raise Exception(err) cmd = 'date -s "%s %s"' % (system_date, output_time[0]) output, err = command.get_command_output(cmd) if err: raise Exception(err) return_dict['date_set'] = True return_dict['date_set_to'] = system_date if system_timezone == None: pass else: cmd = 'timedatectl set-timezone %s' % system_timezone output, err = command.get_command_output(cmd) if err: raise Exception(err) return_dict['timezone_set'] = True return_dict['timezone_set_to'] = system_timezone cmd = "hwclock -w" output, err = command.get_command_output(cmd) if err: raise Exception(err) except Exception, e: return None, str(e)
def create_ramdisk(size, path, pool): try: if not size: raise Exception('Size not specified') mem, err = get_mem_stats('MB') if err: raise Exception(err) if not mem: raise Exception('Error getting memory information : %s' % err) used, err = get_total_ramdisks_size() if err: raise Exception(err) if used < 0: raise Exception('Error checking RAM size') if (used + size) / mem['total'] > 0.25: raise Exception('Insufficient memory ') if not os.path.exists(path): os.makedirs(path) cmd = 'mount -t tmpfs -o size=%dm tmpfs %s' % (size, path) lines, err = command.get_command_output(cmd) if err: raise Exception('Error creating ramdisk : %s' % err) cmd = 'dd if=/dev/zero of=%s/ramfile bs=1024 count=%dK' % (path, size) lines, err = command.get_command_output(cmd) if err: raise Exception('Error initializing ramdisk : %s' % err) res, err = insert_into_ramdisks_config(pool, path, size) if err or not res: cmd = 'umount %s' % path lines, err1 = command.get_command_output(cmd) if err1: if err: err += err1 raise Exception(err) else: raise Exception(err1) else: raise Exception(err) except Exception, e: return False, 'Error creating ramdisk : %s' % str(e)
def parse_ssl_certificate(cert_location): cert_info = {} try: if not os.path.exists(cert_location): raise Exception('Could not locate certificate') lines, err = command.get_command_output( 'openssl x509 -in %s -text -noout' % cert_location) if err: raise Exception(err) look_for = [{'srch_for': 'Not Before', 'field_name': 'expiry_start'}, {'srch_for': 'Issuer', 'field_name': 'issuer'}, {'srch_for': 'Not After', 'field_name': 'expiry_end'}, {'srch_for': 'Subject', 'field_name': 'dn'}, { 'srch_for': 'Public Key Algorithm', 'field_name': 'key_algorithm'}, {'srch_for': 'Serial Number', 'field_name': 'serial_number'}, {'srch_for': 'Public-Key', 'field_name': 'key_length'}] for line in lines: for l in look_for: pattern = '[\s]*%s[\s]*:[\s]*([\S\s]*)$' % l['srch_for'] ret = re.match(pattern, line) if ret: grps = ret.groups() if grps: cert_info[l['field_name']] = grps[0] if 'key_length' in cert_info: ret = re.match('[\s]*\(([\sa-zA-Z0-9]*)\)', cert_info['key_length']) if ret and ret.groups(): cert_info['key_length'] = ret.groups()[0] except Exception, e: return None, 'Error parsing certificate : %s' % str(e)
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 get_mem_stats(units): mem = None try: if units not in ['MB', 'GB']: raise Exception('Incorrect units requested') if not units: raise Exception('Units not specified in request') if units == 'MB': flag = '-m' elif units == 'GB': flag = '-g' cmd = 'free %s' % flag lines = None lines, err = command.get_command_output(cmd) if err: raise Exception('Error getting free memory: %s' % err) if lines: for line in lines: if 'Mem:' not in line: continue components = line.split() mem = {} mem['total'] = float(components[1]) mem['used'] = float(components[2]) mem['free'] = float(components[3]) except Exception, e: return None, 'Error retriving free memory : %s' % str(e)
def get_os_partition_stats(): ret_list = [] try: lines, err = command.get_command_output( 'df -HT --exclude-type=devtmpfs --exclude-type=tmpfs --exclude-type=zfs' ) if err: raise Exception(err) #print lines, err ret_list = [] for line in lines[1:]: components = line.split() tmp_dict = {} #print components used_percentage_str = components[5] percentage_pos = used_percentage_str.find('%') used_percentage = int(used_percentage_str[:percentage_pos]) free_percentage = 100 - used_percentage tmp_dict['percentage_free_str'] = '%d%%' % free_percentage tmp_dict['percentage_free'] = free_percentage tmp_dict['fs_name'] = components[6] tmp_dict['used'] = components[3] tmp_dict['capacity'] = components[2] tmp_dict['free'] = components[4] ret_list.append(tmp_dict) except Exception, e: return None, "Error retrieving OS partition statistics : %s" % str(e)
def net_ads_join(user, pswd, password_server): try: cmd_to_run = "net ads join -U %s%%%s" % (user, pswd) # print 'Running %s'%cmd_to_run use_salt, err = config.use_salt() if err: raise Exception(err) if use_salt: import salt.client errors = [] client = salt.client.LocalClient() # print 'Running %s'%cmd_to_run #assert False r1 = client.cmd('*', 'cmd.run_all', [cmd_to_run]) # print r1 if r1: for node, ret in r1.items(): # print ret if ret["retcode"] != 0: e = "Error joining AD on node %s" % node if "stderr" in ret: e += " : %s" % ret["stderr"] errors.append(e) print errors # print r1 if errors: raise Exception(' '.join(errors)) else: lines, err = command.get_command_output(cmd_to_run) if err: raise Exception(err) except Exception, e: return False, 'Error joining AD : %s' % str(e)
def get_os_partition_stats(): ret_list = [] try: lines, err = command.get_command_output('df -HT --exclude-type=devtmpfs --exclude-type=tmpfs --exclude-type=zfs') if err: raise Exception(err) #print lines, err ret_list = [] for line in lines[1:]: components = line.split() tmp_dict = {} #print components used_percentage_str = components[5] percentage_pos = used_percentage_str.find('%') used_percentage = int(used_percentage_str[:percentage_pos]) free_percentage = 100-used_percentage tmp_dict['percentage_free_str'] = '%d%%'%free_percentage tmp_dict['percentage_free'] = free_percentage tmp_dict['fs_name'] = components[6] tmp_dict['used'] = components[3] tmp_dict['capacity'] = components[2] tmp_dict['free'] = components[4] ret_list.append(tmp_dict) except Exception, e: return None, "Error retrieving OS partition statistics : %s" % str(e)
def reload_configuration(): try: use_salt, err = config.use_salt() if err: raise Exception(err) if use_salt: import salt.client errors = '' client = salt.client.LocalClient() r1 = client.cmd('*', 'cmd.run_all', ['smbcontrol all reload-config']) if r1: for node, ret in r1.items(): # print ret if ret["retcode"] != 0: errors += "Error reloading samba on node %s " % node if errors: raise Exception(errors) else: 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) except Exception, e: return False, 'Error reloading CIFS configuration : %s' % str(e)
def kinit(user, pswd, realm): try: cmd_to_run = 'echo "%s" | kinit %s@%s' % (pswd, user, realm.upper()) # print cmd_to_run use_salt, err = config.use_salt() if err: raise Exception(err) if use_salt: import salt.client errors = [] client = salt.client.LocalClient() # print 'Running %s'%cmd_to_run #assert False r1 = client.cmd('*', 'cmd.run_all', [cmd_to_run]) if r1: for node, ret in r1.items(): # print ret if ret["retcode"] != 0: e = "Error initiating kerberos on GRIDCell %s" % node if "stderr" in ret: e += " : %s" % ret["stderr"] errors.append(e) print errors # print r1 if errors: raise Exception(' '.join(errors)) else: lines, err = command.get_command_output(cmd_to_run, shell=True) # print lines, err if err: raise Exception(err) except Exception, e: return False, 'Error initializing kerberos : %s' % str(e)
def delete_local_group(grpname): try: if not grpname: raise Exception('No username specified') d, err = get_local_group(grpname) if not d: if err: raise Exception('Error locating group : %s' % err) else: raise Exception('Error locating group') use_salt, err = config.use_salt() if err: raise Exception(err) if use_salt: import salt.client client = salt.client.LocalClient() rc = client.cmd('*', 'group.delete', [grpname]) # print rc if rc: for hostname, status in rc.items(): if not status: raise Exception("Error deleting the system group") else: raise Exception("Error deleting the system group") else: cmd_to_run = 'groupdel %s' % (grpname) lines, err = command.get_command_output(cmd_to_run) if err: raise Exception(err) except Exception, e: return False, 'Error deleting local group : %s' % str(e)
def get_certificate(cert_location): """Parse a certificate file and return a dict with required components""" cert_info = {} try: if not os.path.exists(cert_location): raise Exception('Could not locate certificate') lines, err = command.get_command_output( 'openssl x509 -in %s -text -noout' % cert_location) if err: raise Exception(err) look_for = [{'srch_for': 'Not Before', 'field_name': 'expiry_start'}, {'srch_for': 'Issuer', 'field_name': 'issuer'}, {'srch_for': 'Not After', 'field_name': 'expiry_end'}, {'srch_for': 'Subject', 'field_name': 'dn'}, { 'srch_for': 'Public Key Algorithm', 'field_name': 'key_algorithm'}, {'srch_for': 'Serial Number', 'field_name': 'serial_number'}, {'srch_for': 'Public-Key', 'field_name': 'key_length'}] for line in lines: for l in look_for: pattern = '[\s]*%s[\s]*:[\s]*([\S\s]*)$' % l['srch_for'] ret = re.match(pattern, line) if ret: grps = ret.groups() if grps: cert_info[l['field_name']] = grps[0] if 'key_length' in cert_info: ret = re.match('[\s]*\(([\sa-zA-Z0-9]*)\)', cert_info['key_length']) if ret and ret.groups(): cert_info['key_length'] = ret.groups()[0] except Exception, e: return None, 'Error parsing certificate : %s' % str(e)
def flag_node(request): try: return_dict = {} if "node" not in request.GET: raise Exception("Error flagging node. No node specified") node_name = request.GET["node"] blink_time = 255 use_salt, err = config.use_salt() if use_salt: import salt.client client = salt.client.LocalClient() ret = client.cmd(node_name, 'cmd.run', ['ipmitool chassis identify %s' % (blink_time)]) print ret if ret[node_name] == 'Chassis identify interval: %s seconds' % ( blink_time): return django.http.HttpResponse("Success") else: raise Exception("") else: out_list, err = command.get_command_output( 'service winbind status') if err: raise Exception(err) if 'Chassis identify interval: %s seconds' % ( blink_time) in out_list[0]: return django.http.HttpResponse("Success") else: raise Exception("") except Exception, e: return django.http.HttpResponse("Error")
def volume_start_stop(vol_name, operation): try: cmd = 'gluster volume %s %s' % (operation, vol_name) print cmd if operation == 'stop': (ret, rc), err = command.execute_with_conf_and_rc(cmd) if rc == 0: lines, er = command.get_output_list(ret) if er: raise Exception(er) else: err = '' tl, er = command.get_output_list(ret) if er: raise Exception(er) if tl: err = ''.join(tl) tl, er = command.get_error_list(ret) if er: raise Exception(er) if tl: err = err + ''.join(tl) raise Exception(err) else: lines, err = command.get_command_output(cmd) # print 'a', ret, err if err: raise Exception(err) if lines: print '\n'.join(lines) except Exception, e: return False, 'Error performing volume %s : %s' % (operation, e)
def check_salt_status(print_to_stdout=True, separator='\n'): ret_list = [] try: if print_to_stdout: ret_list.append( 'IntegralStor Monitoring agents and processes status') out, err = command.get_command_output('service salt-master status', False) if err: raise Exception(err) ret_list.append('Monitoring service master status : %s' % ','.join(out)) out, err = command.get_command_output('service salt-minion status', False) if err: raise Exception(err) ret_list.append('Monitoring service agent status : %s' % ','.join(out)) keys, err = grid_ops.get_all_salt_keys() if err: raise Exception(err) if keys: ret_list.append(separator + 'Admin agents that are part of the grid : ') ret_list.append(separator.join(keys['minions'])) ret_list.append( separator + 'Admin agents that are pending to be accepted into grid : ') ret_list.append(separator.join(keys['minions_pre'])) results, err = grid_ops.salt_ping_all() if err: raise Exception(err) if results: ret_list.append(separator + 'Admin agent reachability :') for minion, result in results.items(): if result: st = 'Reachable' else: st = '**Not reachable**' ret_list.append('%s : %s' % (minion, st)) if print_to_stdout: print separator.join(ret_list) except Exception, e: return None, 'Error checking admin service status : %s' % str(e)
def unmount(): try: ret, err = _header() if err: print err mount_points, err = command.get_command_output( "cat /var/log/usb-mount.track | cut -d ':' -f 1", shell=True) if err: raise Exception(err) if not mount_points: raise Exception(' Nothing to unmount!') dev_names, err = command.get_command_output( "cat /var/log/usb-mount.track | cut -d ':' -f 2", shell=True) if err: raise Exception(err) count = [] for i, mount in enumerate(mount_points, start=1): count.append(str(i)) print ' %s. %s of /dev/%s' % (i, mount, dev_names[i - 1]) print ch = '' while True: ch = raw_input( 'Provide a number to unmount its corresponding device(0 to exit): ') if ch != '0' and ch not in count: print '\t- Provide a valid number' else: break shell_scripts_path, err = config.get_shell_scripts_path() if err: raise Exception(err) if ch != '0': ret, err = command.get_command_output( "/bin/bash %s/usb-mount.sh remove %s" % (shell_scripts_path, dev_names[int(ch) - 1])) if err: raise Exception(err) for r in ret: print '\n- %s' % r except Exception, e: print str(e) raw_input('\nPress any key to continue')
def get_hardware_raid_hard_drives(controller_number, device_id): disk_list = [] try: lines, err = command.get_command_output( '/opt/dell/srvadmin/sbin/omreport storage pdisk controller=%s vdisk=%d -fmt xml' % (controller_number, device_id)) if err: raise Exception(err) if lines: root = ElementTree.fromstring(''.join(lines)) sl = root.findall('ArrayDisks/DCStorageObject') for s in sl: disk_dict = {} node = s.find('DeviceSerialNumber') if node is not None: disk_dict['serial_number'] = node.text node = s.find('DiskProductVendor') if node is not None: disk_dict['vendor'] = node.text node = s.find('ProductID') if node is not None: disk_dict['product_id'] = node.text node = s.find('ManufactureDay') if node is not None: disk_dict['manufacture_day'] = node.text node = s.find('ManufactureWeek') if node is not None: disk_dict['manufacture_week'] = node.text node = s.find('ManufactureYear') if node is not None: disk_dict['manufacture_year'] = node.text node = s.find('ObjStatus') if node is not None: try: status = int(node.text) if status == 2: disk_dict['status'] = 'Ok' elif status == 4: disk_dict['status'] = 'Critical' else: disk_dict['status'] = 'Unknown' except Exception, e: pass node = s.find('ObjState') if node is not None: try: state = int(node.text) if state == 4: disk_dict['state'] = 'Online' elif state == 8388608: disk_dict['state'] = 'Rebuilding' elif state == 1024: disk_dict['state'] = 'Removed' else: disk_dict['state'] = 'Unknown' except Exception, e: pass disk_list.append(disk_dict)
def check_integralview_processes_status(print_to_stdout=True, separator='\n'): ret_list = [] try: if print_to_stdout: ret_list.append('GRIDCell IntegralView processes status: ') out, err = command.get_command_output('service nginx status', False) if err: raise Exception(err) ret_list.append('Web server (NGINX) status : %s' % ', '.join(out)) out, err = command.get_command_output('service uwsgi status', False) if err: raise Exception(err) ret_list.append('Web application server (uwsgi) status : %s' % ', '.join(out)) if print_to_stdout: print separator.join(ret_list) except Exception, e: return None, 'Error checking IntegralView process status: %s' % str(e)
def get_hardware_raid_hard_drives(controller_number, device_id): disk_list = [] try: lines, err = command.get_command_output( '/opt/dell/srvadmin/sbin/omreport storage pdisk controller=%s vdisk=%d -fmt xml' % (controller_number, device_id)) if err: raise Exception(err) if lines: root = ElementTree.fromstring(''.join(lines)) sl = root.findall('ArrayDisks/DCStorageObject') for s in sl: disk_dict = {} node = s.find('DeviceSerialNumber') if node is not None: disk_dict['serial_number'] = node.text node = s.find('DiskProductVendor') if node is not None: disk_dict['vendor'] = node.text node = s.find('ProductID') if node is not None: disk_dict['product_id'] = node.text node = s.find('ManufactureDay') if node is not None: disk_dict['manufacture_day'] = node.text node = s.find('ManufactureWeek') if node is not None: disk_dict['manufacture_week'] = node.text node = s.find('ManufactureYear') if node is not None: disk_dict['manufacture_year'] = node.text node = s.find('ObjStatus') if node is not None: try: status = int(node.text) if status == 2: disk_dict['status'] = 'Ok' elif status == 4: disk_dict['status'] = 'Critical' else: disk_dict['status'] = 'Unknown' except Exception, e: pass node = s.find('ObjState') if node is not None: try: state = int(node.text) if state == 4: disk_dict['state'] = 'Online' elif state == 8388608: disk_dict['state'] = 'Rebuilding' elif state == 1024: disk_dict['state'] = 'Removed' else: disk_dict['state'] = 'Unknown' except Exception, e: pass disk_list.append(disk_dict)
def get_local_users(get_system_users=False): user_list = [] try: sys_ul = [] smb_ul = [] all = pwd.getpwall() # print all min_uid, err = config.get_minimum_user_uid_gid('user') if err: raise Exception(err) for user in all: if not get_system_users: if user.pw_uid < min_uid or user.pw_uid >= 65534: continue sys_ul.append(user.pw_name) d = {} d['uid'] = user.pw_uid d['gid'] = user.pw_gid d['home_dir'] = user.pw_dir d['username'] = user.pw_name d['comment'] = user.pw_gecos d['shell'] = user.pw_shell g = grp.getgrgid(d['gid']) if g: d['grpname'] = g.gr_name groups = [ g.gr_name for g in grp.getgrall() if d['username'] in g.gr_mem and g.gr_gid != d['gid'] ] gid = pwd.getpwnam(d['username']).pw_gid d['other_groups'] = groups user_list.append(d) ul, err = command.get_command_output("/usr/bin/pdbedit -d 1 -L") if err: raise Exception(err) # print '1' if ul: smb_ul = [] smb_dict = {} for u in ul: l = u.split(':') if l: if len(l) > 1: smb_dict['name'] = l[2] smb_ul.append(l[0]) if user_list: for ud in user_list: if ud['username'] in smb_ul: ud['smb_user'] = True if ud['username'] in smb_dict: ud['smb_comment'] = smb_dict[ud['username']] else: ud['smb_user'] = True ud['smb_user'] = False except Exception, e: return None, 'Error retrieving local users : %s' % str(e)
def get_services_status(): """The status of various services.""" s = {} try: platform, err = config.get_platform() if err: raise Exception(err) if platform == 'gridcell': # Commenting out ctdb for now as we wont use it for this release! #services = ['smb', 'winbind', 'ctdb', 'glusterd'] services = ['smb', 'winbind', 'glusterd'] for service_name in services: stat, err = command.get_command_output( '/sbin/service %s status' % service_name) ret = None rc = -1 tup, err = command.execute_with_rc('/sbin/service %s status' % service_name) if tup: (ret, rc) = tup if err: raise Exception(err) if rc == 0: lines, er = command.get_output_list(ret) if er: raise Exception(er) s[service_name] = [0, ','.join(lines)] else: err = '' tl, er = command.get_output_list(ret) if er: raise Exception(er) if tl: err = ','.join(tl) tl, er = command.get_error_list(ret) if er: raise Exception(er) if tl: err = err + ','.join(tl) s[service_name] = [-1, err] else: service_dict, err = services_management.get_sysd_services_status() if err: raise Exception(err) for service_name, service_info in service_dict.items(): if service_info['info']['status']['status_str'] in [ 'Failed', 'Unknown State' ]: s[service_name] = [ -1, service_info['info']['status']['output_str'] ] else: s[service_name] = [ 0, service_info['info']['status']['output_str'] ] except Exception, e: return None, 'Error retrieving services status: %s' % str(e)
def get_routes(): route_list = [] for c in command.get_command_output('route -n')[0][2:]: if re.match('0.0.0.0', c): # A simple hack to actually remove all the unnecessary white spaces # and return the atual content as a list route_list.append(" ".join(c.split()).split()) else: pass return route_list
def blink_unblink_disk(action, controller_id, channel_id, enclosure_id, target_id): try: cmd = '/opt/dell/srvadmin/sbin/omconfig storage pdisk action=%s controller=%s pdisk=%s:%s:%s' % ( action, controller_id, channel_id, enclosure_id, target_id) # print cmd lines, err = command.get_command_output(cmd) if err: raise Exception(err) except Exception, e: return False, 'Error blinking/un-blonking disk : %s' % e
def heal_info(vol_name): try: ret, err = command.get_command_output( 'gluster volume heal %s info split-brain' % vol_name) if err: raise Exception(err) if ret: print '\n'.join(ret) except Exception, e: return False, 'Error getting heal info : %s' % e
def get_hardware_raid_to_unix_mapping(): mapping_dict = {} try: lines, err = command.get_command_output( '/opt/dell/srvadmin/sbin/omreport storage vdisk -fmt xml') if err: raise Exception(err) if lines: root = ElementTree.fromstring(''.join(lines)) sl = root.findall('VirtualDisks/DCStorageObject') for s in sl: vd_dict = {} # print s node = s.find('DeviceName') if node is not None: device_name = node.text else: print 'No device name for this virtual device' continue node = s.find('Layout') if node is not None: try: layout = int(node.text) if layout == 4: vd_dict['layout'] = 'RAID-1' except Exception, e: pass node = s.find('ObjStatus') if node is not None: try: status = int(node.text) if status == 2: vd_dict['status'] = 'Ok' elif status == 3: vd_dict['status'] = 'Non-Critical' except Exception, e: pass node = s.find('ObjState') if node is not None: try: state = int(node.text) if state == 1: vd_dict['state'] = 'Ready' elif state == 32: vd_dict['state'] = 'Degraded' except Exception, e: pass node = s.find('ControllerNum') if node is not None: try: controller_number = int(node.text) vd_dict['controller_number'] = controller_number except Exception, e: pass
def get_normal_users(): """Gets all normal users, i.e. normal to the OS, and not the normal users that are specific to Integralstor. """ try: ret, err = command.get_command_output( "awk -F'[/:]' '{if ($3 >= 1000 && $3 < 65534) print $1}' /etc/passwd") if err: raise Exception(err) except Exception, e: return None, 'Could not fetch the list of normal users: %s' % str(e)
def blink_unblink_disk(action, controller_id, channel_id, enclosure_id, target_id): try: cmd = '/opt/dell/srvadmin/sbin/omconfig storage pdisk action=%s controller=%s pdisk=%s:%s:%s' % ( action, controller_id, channel_id, enclosure_id, target_id) # print cmd lines, err = command.get_command_output(cmd) if err: raise Exception(err) except Exception, e: return False, 'Error blinking/un-blonking disk : %s' % e
def get_hardware_raid_to_unix_mapping(): mapping_dict = {} try: lines, err = command.get_command_output( '/opt/dell/srvadmin/sbin/omreport storage vdisk -fmt xml') if err: raise Exception(err) if lines: root = ElementTree.fromstring(''.join(lines)) sl = root.findall('VirtualDisks/DCStorageObject') for s in sl: vd_dict = {} # print s node = s.find('DeviceName') if node is not None: device_name = node.text else: print 'No device name for this virtual device' continue node = s.find('Layout') if node is not None: try: layout = int(node.text) if layout == 4: vd_dict['layout'] = 'RAID-1' except Exception, e: pass node = s.find('ObjStatus') if node is not None: try: status = int(node.text) if status == 2: vd_dict['status'] = 'Ok' elif status == 3: vd_dict['status'] = 'Non-Critical' except Exception, e: pass node = s.find('ObjState') if node is not None: try: state = int(node.text) if state == 1: vd_dict['state'] = 'Ready' elif state == 32: vd_dict['state'] = 'Degraded' except Exception, e: pass node = s.find('ControllerNum') if node is not None: try: controller_number = int(node.text) vd_dict['controller_number'] = controller_number except Exception, e: pass
def delete_all_targets(): try: cmd = 'tgt-admin --delete ALL --force' lines, err = command.get_command_output(cmd) if err: ret_str = err ret_str += '. ' if lines: ret_str += '. '.join(lines) raise Exception(ret_str) except Exception, e: return False, 'Error deleting all targets : %s' % str(e)
def get_services_status(): """The status of various services.""" s = {} try: platform, err = config.get_platform() if err: raise Exception(err) if platform == 'gridcell': # Commenting out ctdb for now as we wont use it for this release! #services = ['smb', 'winbind', 'ctdb', 'glusterd'] services = ['smb', 'winbind', 'glusterd'] for service_name in services: stat, err = command.get_command_output( '/sbin/service %s status' % service_name) ret = None rc = -1 tup, err = command.execute_with_rc( '/sbin/service %s status' % service_name) if tup: (ret, rc) = tup if err: raise Exception(err) if rc == 0: lines, er = command.get_output_list(ret) if er: raise Exception(er) s[service_name] = [0, ','.join(lines)] else: err = '' tl, er = command.get_output_list(ret) if er: raise Exception(er) if tl: err = ','.join(tl) tl, er = command.get_error_list(ret) if er: raise Exception(er) if tl: err = err + ','.join(tl) s[service_name] = [-1, err] else: service_dict, err = services_management.get_sysd_services_status() if err: raise Exception(err) for service_name, service_info in service_dict.items(): if service_info['info']['status']['status_str'] in ['Failed', 'Unknown State']: s[service_name] = [-1, service_info['info'] ['status']['output_str']] else: s[service_name] = [ 0, service_info['info']['status']['output_str']] except Exception, e: return None, 'Error retrieving services status: %s' % str(e)
def get_normal_users(): """Gets all normal users, i.e. normal to the OS, and not the normal users that are specific to Integralstor. """ try: ret, err = command.get_command_output( "awk -F'[/:]' '{if ($3 >= 1000 && $3 < 65534) print $1}' /etc/passwd" ) if err: raise Exception(err) except Exception, e: return None, 'Could not fetch the list of normal users: %s' % str(e)
def delete_local_user(username): try: if not username: raise Exception('No username specified') d, err = get_local_user(username) if not d: if err: raise Exception('Error locating user : %s' % err) else: raise Exception('Error locating user') use_salt, err = config.use_salt() if err: raise Exception(err) if use_salt: import salt.client client = salt.client.LocalClient() rc = client.cmd('*', 'user.delete', [username]) # print rc if rc: for hostname, status in rc.items(): if not status: raise Exception("Error deleting the system user") else: raise Exception("Error deleting the system user") else: cmd_to_run = 'userdel %s' % (username) lines, err = command.get_command_output(cmd_to_run) if err: raise Exception(err) if d['smb_user']: lines, err = command.get_command_output(r'pdbedit -d 1 -x %s' % username) if err: raise Exception(err) except Exception, e: return False, 'Error deleting local user : %s' % str(e)
def display_status(): try: hostname = socket.gethostname() status, err = command.get_command_output('service salt-master status') if err: raise Exception(err) print '\n'.join(status) status, err = command.get_command_output('service salt-minion status') if err: raise Exception(err) print '\n'.join(status) status, err = command.get_command_output('service smb status') if err: raise Exception(err) print '\n'.join(status) status, err = command.get_command_output('service winbind status') if err: raise Exception(err) print '\n'.join(status) ''' #Commenting out as we wont support CTDB for this release status, err = command.get_command_output('service ctdb status') if err: raise Exception(err) print '\n'.join(status) ''' status, err = command.get_command_output('service glusterd status') if err: raise Exception(err) print '\n'.join(status) ''' #Commenting out as we wont support CTDB for this release status, err = command.get_command_output('ctdb status') if err: raise Exception(err) print '\n'.join(status) ''' except Exception, e: return False, "Error displaying GRIDCell status : %s" % e
def generate_self_signed_certificate(d): """Generate a self signed cert with the parameters specified in the passed dict.""" try: pki_dir, err = config.get_pki_dir() if err: raise Exception(err) path = '%s/%s' % (pki_dir, d['name']) if os.path.exists(path): raise Exception('A key of that name already exists') cmd = 'openssl req -new -newkey rsa:' if 'key_length' in d: key_length = int(d['key_length']) else: key_length = 1024 cmd = '%s%d' % (cmd, key_length) if 'days' in d: cmd = '%s -days %d' % (cmd, int(d['days'])) subj = '' if 'country' in d: subj = '%s/C=%s' % (subj, d['country']) if 'state' in d: subj = '%s/ST=%s' % (subj, d['state']) if 'location' in d: subj = '%s/L=%s' % (subj, d['location']) if 'o' in d: subj = '%s/O=%s' % (subj, d['o']) if 'ou' in d: subj = '%s/OU=%s' % (subj, d['ou']) if 'cn' in d: subj = '%s/CN=%s' % (subj, d['cn']) if 'email' in d: subj = '%s/emailAddress=%s' % (subj, d['email']) cmd += ' -nodes -x509 -subj %s -keyout %s/%s.cert -out %s/%s.cert' % ( subj, path, d['name'], path, d['name']) # print cmd os.mkdir(path) lines, err = command.get_command_output(cmd) if err: if os.path.exists(path): shutil.rmtree(path) raise Exception(err) except Exception, e: return False, 'Error generating self signed certificate : %s' % str(e)
def get_zfs_version(): zfs_ver = "" try: #lines, err = command.get_command_output('dmesg | grep ZFS', shell=True) lines, err = command.get_command_output( "modinfo zfs | grep -iw -e version | cut -d ':' -f 2", shell=True) if err: zfs_ver = 'Could not determine..' if lines and lines[0]: zfs_ver = 'ZFS build ' + lines[0].strip() else: zfs_ver = 'Could not determine..' except Exception, e: return None, 'Error retrieving ZFS information : %s' % str(e)
def get_idrac_addr(): url = None try: lines, err = command.get_command_output( '/opt/dell/srvadmin/sbin/omreport system summary -fmt xml') if err: raise Exception(err) if lines: root = ElementTree.fromstring(''.join(lines)) sl = root.findall('OMA/EMPObjSummary/EMPObj/Url') # print sl if sl: url = '%s/login.html' % sl[0].text except Exception, e: return None, 'Error retrieving IDRAC URL : %s' % e
def delete_local_user(userid): """Deletes a local user account in the linux system as well as in samba. userid -- The username of the account """ try: error_list = [] # First check if samba user exists. if so kick out ul, err = get_local_users() if err: raise Exception(err) found = False if ul: for ud in ul: if ud["userid"] == userid: found = True if not found: raise Exception("The user \"%s\" does not exist. " % userid) # Now check if system user exists. If so and is created by integralstor # then delete.. delete_system_user = False try: d = pwd.getpwnam(userid) name = d[4] if name.find("integralstor_user") == 0: delete_system_user = True except KeyError: pass if delete_system_user: client = salt.client.LocalClient() rc = client.cmd('*', 'user.delete', [userid]) for hostname, status in rc.items(): if not status: error_list.append( "Error deleting the userid on GRIDCell %s" % hostname) if error_list: raise Exception(','.join(error_list)) # Now delete from samba's database lines, err = command.get_command_output(r'pdbedit -d 1 -x %s' % userid) if err: raise Exception(err) except Exception, e: return False, 'Error deleting local user :%s' % str(e)
def get_serial_number(disk_name): """Given a disk by name, get its serial number.""" serial_number = None try: """ ids, err = get_all_disk_ids(disk_name) if err: raise Exception(err) id1 = None id = None if ids: for id1 in ids: if (id1.startswith('scsi')) or (id1.startswith('ata')): id = id1 break if id: parts = id.split('_') if len(parts) >= 3: serial_number = parts[2] """ #cmd = 'udevadm info -q property -n %s | grep -ie ID_SERIAL_SHORT | cut -d "=" -f2' % disk_name cmd = 'udevadm info -q property -n %s | grep -ie ID_SCSI_SERIAL | cut -d "=" -f2' % disk_name ret, err = command.get_command_output(cmd, shell=True) if err: raise Exception(err) if ret: serial_number = ret[0] if not serial_number: # Cant get it this way for some SAS drives so try smartctl cmd_disk = "/usr/sbin/smartctl -H -i /dev/%s" % disk_name dl_output = os.popen(cmd_disk).read() lines = re.split("\r?\n", dl_output) for line in lines: # In case of a SAS drive, status comes with a different string # so .. res = re.match('Serial Number:[\s]*([\S]*)', line) if not res: # For SAS drives, we get a lower case number, *&^@*&^ res = re.match('Serial number:[\s]*([\S]*)', line) if res: tup = res.groups() if tup: serial_number = tup[0] if serial_number: serial_number = serial_number.upper() except Exception, e: return None, "Error retrieving serial number : %s" % str(e)
def get_psu_status(): return_dict = {} try: lines, err = command.get_command_output( '/opt/dell/srvadmin/sbin/omreport chassis pwrsupplies -fmt xml') if err: raise Exception(err) if lines: # print lines root = ElementTree.fromstring(''.join(lines)) sl = root.findall('Chassis/Redundancy/RedunStatus') for s in sl: redundancy_str = s.text if redundancy_str == '4': return_dict['redundancy'] = True else: return_dict['redundancy'] = False psus_info = root.find('Chassis/PowerSupplyList') if psus_info is not None: if 'count' in psus_info.attrib: return_dict['psu_count'] = int(psus_info.attrib['count']) psus = root.findall('Chassis/PowerSupplyList/PowerSupply') # print psus psu_list = [] for psu in psus: switch_dict = {} switch_on = psu.find('PSSwitchOn') if switch_on is not None: if switch_on.text == 'true': switch_dict['switch_on'] = True else: switch_dict['switch_on'] = False prescence = psu.find('PSState/PSPresenceDetected') if prescence is not None: if prescence.text == 'true': switch_dict['prescence'] = True else: switch_dict['prescence'] = False failure = psu.find('PSState/PSFailureDetected') if failure is not None: if failure.text == 'true': switch_dict['failure'] = True else: switch_dict['failure'] = False psu_list.append(switch_dict) return_dict['psu_list'] = psu_list except Exception, e: return None, 'Error retrieving PSU information : %s' % e
def get_alert_logs(): alerts_odict = None try: alerts_dict = {} lines, err = command.get_command_output( '/opt/dell/srvadmin/sbin/omreport system alertlog -fmt xml') if err: raise Exception(err) if lines: root = ElementTree.fromstring(''.join(lines)) sl = root.findall('LogEntry') for s in sl: # print s alert_dict = {} timestamp = None node = s.find('TimeStamp') if node is not None: timestamp = int(node.text) alert_dict['timestamp'] = timestamp node = s.find('DateTime') if node is not None: alert_dict['date_time'] = node.text node = s.find('Description') if node is not None: alert_dict['description'] = node.text node = s.find('Category') if node is not None: alert_dict['category'] = node.text node = s.find('ID') if node is not None: id = int(node.text) alert_dict['id'] = id node = s.find('Type') if node is not None: type = int(node.text) if type == 2: alert_dict['Severity'] = 'Non-Critical' elif type == 1: alert_dict['Severity'] = 'Critical' elif type == 4: alert_dict['Severity'] = 'Ok' if not timestamp in alerts_dict: alerts_dict[timestamp] = [] alerts_dict[timestamp].append(alert_dict) alerts_odict = collections.OrderedDict( sorted(alerts_dict.items(), reverse=True)) except Exception, e: return None, 'Error retrieving alerts : %s' % e
def get_current_timezone(): return_dict = {} try: cmd = "timedatectl | grep 'Time' | grep -oE '(\S+/.+$)'" system_timezone, err = command.get_command_output(cmd, shell=True) if err: raise Exception(err) return_dict['system_timezone'] = system_timezone[0] path = os.path.realpath('/etc/localtime') components = path.split('/') if components: return_dict['timezone_str'] = '%s/%s' % ( components[-2], components[-1]) else: raise Exception('Unknown timezone') except Exception, e: return None, str(e)
def delete_ace(path, name, type, recursive=False): """Remove the ACE with the given name and type for the given path.""" try: if not path or not name or not type: raise Exception('Required information not passed') if type not in ['user', 'group']: raise Exception('Invalid type passed') if recursive: cmd = 'setfacl -R -x %s:"%s",default:%s:"%s" "%s"' % ( type, name, type, name, path) else: cmd = 'setfacl -x %s:"%s",default:%s:"%s" "%s"' % ( type, name, type, name, path) # print cmd lines, err = command.get_command_output(cmd) if err: raise Exception(err) except Exception, e: return False, 'Error removing ACL : %s' % str(e)
def get_status(): """Return the CTDB status as a dict with the node name as the key and the status as the value.""" status_dict = {} try: lines, err = command.get_command_output('ctdb -Y status') if err: raise Exception(err) i = 0 for line in lines: if i == 0: i += 1 continue # print line components = line.split(':') # print components ok = True status = '' if components: if components[3] != '0': status += '| Banned |' ok = False if components[4] != '0': status += '| Disabled |' ok = False if components[5] != '0': status += '| Unhealthy |' ok = False if components[6] != '0': status += '| Stopped |' ok = False if components[7] != '0': status += '| Inactive |' ok = False if components[8] != '0': status += '| PartiallyOnline |' ok = False if ok: status += 'OK' status_dict[components[2]] = status i += 1 except Exception, e: return None, "Error retrieving CTDB status: %s" % str(e)
def get_mdraid_info(device): """Get the devices that are part of the mdraids.""" raid_list = [] try: lines, err = command.get_command_output( 'mdadm --detail /dev/%s' % device) if not err: for line in lines: # print line if line.startswith('/dev/%s' % device): continue if not line.strip(): continue if not ':' in line: # print line comps = line.split() if len(comps) > 5: raid_list.append(comps[-1][5:]) except Exception, e: return None, "Error getting mdraid information: %s" % str(e)
def create_ace_entries(path, users, groups, recursive): """Create an ACE entry for the given path with the supplied users and groups.""" try: if recursive: cmd = r'setfacl -R -m' else: cmd = r'setfacl -m ' for user in users: cmd += 'u:"%s":---,d:u:"%s":---,' % (user, user) for group in groups: cmd += 'g:"%s":---,d:g:"%s":---,' % (group, group) if cmd[-1] == ',': cmd = cmd[:-1] cmd += ' "%s"' % path # print cmd lines, err = command.get_command_output(cmd) if err: raise Exception(err) except Exception, e: return False, 'Error adding ACL entries : %s' % str(e)
def get_all_aces(path): """Get all the access control entries for the given path.""" aces = [] try: lines, err = command.get_command_output('getfacl "%s"' % path) if err: raise Exception(err) for line in lines: if not line.strip(): # print 'blank line so skipping' continue if line.strip()[0] == '#': # print 'comment line so skipping : ',line continue # print 'normal line : ', line if line.find('::'): fields = line.strip().split(':') aces.append(fields) # print aces except Exception, e: return None, 'Error retrieving all ACLs : %s' % str(e)
def net_ads_join(user, pswd, password_server): """Join all gridcells to the AD realm user -- AD's admin username pswd -- AD's admin password password_server -- The AD server Returns True on success and False otherwise """ try: # print 'aaa' errors = [] client = salt.client.LocalClient() #cmd_to_run = "net ads join -S %s -U %s%%%s"%(password_server, user, pswd) cmd_to_run = "net ads join -U %s%%%s" % (user, pswd) print 'Running %s' % cmd_to_run output, err = command.get_command_output(cmd_to_run) print output, err if err: raise Exception(err) ''' r1 = client.cmd('gridcell-pri.integralstor.lan', 'cmd.run_all', [cmd_to_run]) #print r1 if r1: for node, ret in r1.items(): print ret if ret["retcode"] != 0: e = "Error joining AD on GRIDCell %s"%node if "stderr" in ret: e += " : %s"%ret["stderr"] errors.append(e) #print errors #print r1 if errors: raise Exception(','.join(errors)) ''' except Exception, e: return False, 'Error joining AD : %s' % str(e)
def ret_fs_usage_alert(node_name, threshold_percent=85): ret = [] try: threshold_percent = int(threshold_percent) lines, err = command.get_command_output("salt '%s' disk.percent" % node_name) if err: raise Exception(err) if lines: data = [line.strip() for line in lines[2:]] s = '' for idx, line in enumerate(data): if ':' in line: s = 'Used percentage of %s' % line if data[idx+1] and int(data[idx+1].split('%')[0]) >= threshold_percent: s = '%s %s ' % (s, data[idx+1]) ret.append(s) else: s = '' else: continue except Exception, e: return None, 'Could not determine filesystem usage: %s' % e
def get_local_users(): """Get the username, real name of all the local users in the system userid -- The username of the account pswd -- The password for the username """ user_list = None try: ul, err = command.get_command_output("/usr/bin/pdbedit -d 1 -L") if err: raise Exception(err) user_list = [] for u in ul: l = u.split(':') if l: d = {} d["userid"] = l[0] if len(l) > 1: d["name"] = l[2] user_list.append(d) except Exception, e: return None, 'Error retrieving local user list :%s' % str(e)
def sync_ctdb_files(): """ Syncs CTDB files on the localhost with the mounted admin vol. Input: ['None'] Output: Returns three variables [ret1,ret2,ret3]: ret1 -- True if sync was sucessful, else False ret2 -- True if there was a difference, False if they were already in sync ret3 -- None if there were no errors/exceptions, 'Error string' otherwise """ is_change = False try: config_dir, err = config.get_config_dir() if err: raise Exception(err) out, err = command.get_command_output( 'diff "/etc/sysconfig/ctdb" "%s/lock/ctdb"' % str(config_dir), True, True) if err: shutil.copyfile('%s/lock/ctdb' % str(config_dir), '/etc/sysconfig/ctdb') is_change = True out, err = command.get_command_output( 'diff "/etc/ctdb/nodes" "%s/lock/nodes"' % str(config_dir), True, True) if err: shutil.copyfile('%s/lock/nodes' % str(config_dir), '/etc/ctdb/nodes') is_change = True out, err = command.get_command_output( 'diff "/etc/ctdb/public_addresses" "%s/lock/public_addresses"' % str(config_dir), True, True) if err: shutil.copyfile('%s/lock/public_addresses' % str(config_dir), '/etc/ctdb/public_addresses') is_change = True lg, err = logger.get_script_logger( 'Admin volume mounter: Sync CTDB config files', '/var/log/integralstor/scripts.log', level=logging.DEBUG) if is_change == True: logger.log_or_print( 'ctdb related files were synced.', lg, level='info') logger.log_or_print( 'Restarting ctdb.', lg, level='debug') out, err = command.get_command_output( 'service ctdb restart', False, True) if not err and out: logger.log_or_print( 'Service ctdb: %s' % out, lg, level='debug') else: logger.log_or_print( 'Service ctdb error: %s' % err, lg, level='error') elif is_change == False: logger.log_or_print( 'ctdb related files are in sync.', lg, level='info') except Exception, e: return False, is_change, "Couldn't sync ctdb files: %s" % str(e)
def mount_and_configure(): lg = None try: lg, err = logger.get_script_logger( 'Admin volume mounter', '/var/log/integralstor/scripts.log', level=logging.DEBUG) logger.log_or_print( 'Admin volume mounter initiated.', lg, level='info') pog, err = grid_ops.is_part_of_grid() if err: raise Exception(err) # If the localhost is part of the gridcell, proceed if pog: logger.log_or_print('Checking glusterd service', lg, level='debug') service = 'glusterd' status, err = services_management.get_service_status([service]) if err: raise Exception(err) logger.log_or_print('Service %s status is %s' % ( service, status['status_code']), lg, level='debug') if status['status_code'] != 0: logger.log_or_print( 'Service %s not started so restarting' % service, lg, level='error') out, err = command.get_command_output( 'service %s restart' % service, False, True) if not err and out: logger.log_or_print('Service %s: %s' % (service, out), lg, level='debug') else: logger.log_or_print('Service %s error : %s' % ( service, err), lg, level='error') admin_vol_name, err = config.get_admin_vol_name() if err: raise Exception(err) # Get the config dir - the mount point. config_dir, err = config.get_config_dir() if err: raise Exception(err) ag, err = grid_ops.is_admin_gridcell() if err: raise Exception(err) admin_gridcells, err = grid_ops.get_admin_gridcells() if err: raise Exception(err) is_pooled = False peer_list, err = gluster_trusted_pools.get_peer_list() if peer_list: is_pooled = True is_mounted = False # mount only if the localhost is pooled if is_pooled: is_mounted, err = grid_ops.is_admin_vol_mounted_local() if not is_mounted: str = 'Admin volume is not mounted. Will attempt to mount now.' logger.log_or_print(str, lg, level='error') # Try to mount (ret, rc), err = command.execute_with_rc( 'mount -t glusterfs localhost:/%s %s' % (admin_vol_name, config_dir)) if err: str = 'Mount from localhost failed.' logger.log_or_print(str, lg, level='error') elif (not err) and (rc == 0): is_access, err = assert_admin_vol_mount() if err: raise Exception(err) sync, is_change, error = sync_ctdb_files() if error: # It's only a best-effort, it will try next # minute again. pass if sync == False: #raise Exception (err) pass # Restart nginx out, err = command.get_command_output( 'service nginx restart', False, True) if not err and out: logger.log_or_print( 'Service nginx: %s' % out, lg, level='debug') else: logger.log_or_print( 'Service nginx error : %s' % err, lg, level='error') # Restart uwsgi out, err = command.get_command_output( 'service uwsgi restart', False, True) if not err and out: logger.log_or_print( 'Service uwsgi: %s' % out, lg, level='debug') else: logger.log_or_print( 'Service uwsgi error : %s' % err, lg, level='error') if ag: # Restart salt-master out, err = command.get_command_output( 'service salt-master restart', False, True) if not err and out: logger.log_or_print( 'Service salt-master: %s' % out, lg, level='debug') else: logger.log_or_print( 'Service salt-master error : %s' % err, lg, level='error') # Restart salt-minion out, err = command.get_command_output( 'service salt-minion restart', False, True) if not err and out: logger.log_or_print( 'Service salt-minion: %s' % out, lg, level='debug') else: logger.log_or_print( 'Service salt-minion error : %s' % err, lg, level='error') str = 'Admin vol is mounted' logger.log_or_print(str, lg, level='info') # Admin volume is mounted, perform required checks else: sync, is_change, err = sync_ctdb_files() if err: raise Exception(err) if sync == False: raise Exception(err) logger.log_or_print('Checking services', lg, level='debug') service_list = ['nginx','ctdb','salt-minion'] if ag: service_list.append('salt-master') for service in service_list: status, err = services_management.get_service_status([ service]) if err: raise Exception(err) logger.log_or_print('Service %s status is %s' % ( service, status['status_code']), lg, level='debug') if status['status_code'] != 0: logger.log_or_print( 'Service %s is not active, restarting' % service, lg, level='error') out, err = command.get_command_output( 'service %s restart' % service, False, True) if not err and out: logger.log_or_print('Service %s: %s' % ( service, out), lg, level='debug') else: logger.log_or_print('Service %s error : %s' % ( service, err), lg, level='error') # UWSGI service config not complete so need to check # against the actual process name (ret, rc), err = command.execute_with_rc( 'pidof uwsgi', shell=True) if rc != 0: logger.log_or_print( 'Service uwsgi is not active, restarting', lg, level='error') out, err = command.get_command_output( 'service uwsgi restart', False, True) if not err and out: logger.log_or_print( 'Service uwsgi: %s' % out, lg, level='debug') else: logger.log_or_print( 'Service uwsgi error : %s' % err, lg, level='error') str = 'Admin volume is already mounted' logger.log_or_print(str, lg, level='info') except Exception, e: st = 'Error mounting admin volume : %s' % e logger.log_or_print(st, lg, level='critical') return False, st