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 view_task_details(request, task_id): return_dict = {} try: task, err = tasks_utils.get_task(int(task_id)) if err: raise Exception(err) return_dict['task'] = task subtasks, err = tasks_utils.get_subtasks(int(task_id)) if err: raise Exception(err) return_dict["subtasks"] = subtasks # print subtasks, err task_output = "" log_dir, err = config.get_tasks_log_dir_path() if err: raise Exception(err) 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'] = "tasks_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 delete_bond(request): return_dict = {} try: req_ret, err = django_utils.get_request_parameter_values(request, [ 'name']) if err: raise Exception(err) if 'name' not in req_ret: raise Exception('Invalid request, please use the menus') name = req_ret['name'] return_dict["name"] = name if request.method == "GET": # Return the conf page return django.shortcuts.render_to_response("delete_bond_conf.html", return_dict, context_instance=django.template.context.RequestContext(request)) else: result, err = networking.delete_bond(name) if not result: if not err: raise Exception("Error removing bond") else: raise Exception(err) python_scripts_path, err = config.get_python_scripts_path() if err: raise Exception(err) python_scripts_path, err = config.get_python_scripts_path() if err: raise Exception(err) status_path, err = config.get_system_status_path() if err: raise Exception(err) ret, err = command.get_command_output( "python %s/generate_manifest.py %s" % (python_scripts_path, status_path)) if err: raise Exception(err) ret, err = command.get_command_output( "python %s/generate_status.py %s" % (python_scripts_path, status_path)) if err: raise Exception(err) audit_str = "Removed network bond %s" % (name) audit.audit("remove_bond", audit_str, request) return django.http.HttpResponseRedirect('/networking/view_interfaces?ack=removed_bond') except Exception, e: return_dict['base_template'] = "networking_base.html" return_dict["page_title"] = 'Remove a network interface bond' return_dict['tab'] = 'view_interfaces_tab' return_dict["error"] = 'Error removing a network interface bond' 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_manifest(request): return_dict = {} try: if request.method == "GET": from integralstor import manifest_status as iu mi, err = iu.generate_manifest_info(rescan_for_disks=True) # print mi, err if err: raise Exception(err) if not mi: raise Exception('Could not load new configuration') return_dict["mi"] = mi # 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": python_scripts_path, err = config.get_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"%(python_scripts_path, ss_path)) ret, err = command.get_command_output( "python %s/generate_manifest.py %s" % (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" % (python_scripts_path, ss_path)) # print 'stat', ret, err if err: raise Exception(err) audit_str = 'Reloaded system configuration after hardware scan' audit.audit('update_manifest', audit_str, request) return django.http.HttpResponseRedirect( "/system/view_system_info/") except Exception, e: return_dict['base_template'] = "system_base.html" return_dict["page_title"] = 'Reload system configuration' return_dict['tab'] = 'system_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 view_task_details(request, task_id): return_dict = {} try: task, err = tasks_utils.get_task(int(task_id)) if err: raise Exception(err) return_dict['task'] = task subtasks, err = tasks_utils.get_subtasks(int(task_id)) if err: raise Exception(err) return_dict["subtasks"] = subtasks # print subtasks, err task_output = "" log_dir, err = config.get_tasks_log_dir_path() if err: raise Exception(err) 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'] = "tasks_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 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 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 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 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_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 2> /dev/null', check_rc=False, shell=True) 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 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 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 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 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 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 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 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 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_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 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 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_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 update_manifest(request): return_dict = {} try: if request.method == "GET": from integralstor import manifest_status as iu mi, err = iu.generate_manifest_info(rescan_for_disks=True) # print mi, err if err: raise Exception(err) if not mi: raise Exception('Could not load new configuration') return_dict["mi"] = mi # 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": python_scripts_path, err = config.get_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"%(python_scripts_path, ss_path)) ret, err = command.get_command_output( "python %s/generate_manifest.py %s" % (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" % (python_scripts_path, ss_path)) # print 'stat', ret, err if err: raise Exception(err) audit_str = 'Reloaded system configuration after hardware scan' audit.audit('update_manifest', audit_str, request) return django.http.HttpResponseRedirect("/system/view_system_info/") except Exception, e: return_dict['base_template'] = "system_base.html" return_dict["page_title"] = 'Reload system configuration' return_dict['tab'] = 'system_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 kinit(user, pswd, realm): try: cmd_to_run = 'echo "%s" | kinit %s@%s' % (pswd, user, realm.upper()) # print cmd_to_run 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 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 kinit(user, pswd, realm): try: cmd_to_run = 'echo "%s" | kinit %s@%s' % (pswd, user, realm.upper()) # print cmd_to_run 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 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 configure_cluster(nodes): try: print "Adding entries in hosts file.." ret, err = networking.update_hosts_file_entry( nodes['node1']['hostname'], nodes['node1']['ip']) if err: raise Exception(err) ret, err = networking.update_hosts_file_entry( nodes['node2']['hostname'], nodes['node2']['ip']) if err: raise Exception(err) print "Please make sure that the hosts file on the other node is also updated!" print "Authenticating cluster nodes.." ret, err = command.get_command_output( "pcs cluster auth %s %s -u hacluster -p hacluster" % (nodes['node1']['hostname'], nodes['node2']['hostname'])) if err: raise Exception(err) if ret: print ret print "Setting up cluster.." ret, err = command.get_command_output( "pcs cluster setup --start --name zfs-cluster %s %s" % (nodes['node1']['hostname'], nodes['node2']['hostname'])) if err: raise Exception(err) if ret: print ret ret, err = command.get_command_output( "pcs property set no-quorum-policy=ignore") if err: raise Exception(err) if ret: print ret ret, err = command.get_command_output( "pcs resource defaults resource-stickiness=100") if err: raise Exception(err) if ret: print ret except Exception, e: print "Error configuring nodes: %s" % 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 get_hardware_specific_info(): return_dict = {} try: lines, err = command.get_command_output( 'dmidecode -s system-manufacturer') if not err and lines and lines[0].lower() != 'none': return_dict['system_manufacturer'] = lines[0] lines, err = command.get_command_output( 'dmidecode -s system-product-name') if not err and lines and lines[0].lower() != 'none': return_dict['system_product_name'] = lines[0] lines, err = command.get_command_output('dmidecode -s system-version') if not err and lines and lines[0].lower() != 'none': return_dict['system_version'] = lines[0] lines, err = command.get_command_output( 'dmidecode -s system-serial-number') if not err and lines and lines[0].lower() != 'none': return_dict['system_serial_number'] = lines[0] except Exception, e: return None, 'Error retrieving hardware specific info: %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 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 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_hardware_specific_info(): return_dict = {} try: lines, err = command.get_command_output( 'dmidecode -s system-manufacturer') if not err and lines and lines[0].lower() != 'none': return_dict['system_manufacturer'] = lines[0] lines, err = command.get_command_output( 'dmidecode -s system-product-name') if not err and lines and lines[0].lower() != 'none': return_dict['system_product_name'] = lines[0] lines, err = command.get_command_output('dmidecode -s system-version') if not err and lines and lines[0].lower() != 'none': return_dict['system_version'] = lines[0] lines, err = command.get_command_output( 'dmidecode -s system-serial-number') if not err and lines and lines[0].lower() != 'none': return_dict['system_serial_number'] = lines[0] except Exception, e: return None, 'Error retrieving hardware specific info: %s' % str(e)
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 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 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 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 _get_count(type): ret = -1 try: if type not in ['filesystem', 'volume', 'snapshot']: raise Exception('Invalid zfs type specified : %s' % type) lines, err = command.get_command_output('zfs list -H -t %s | wc -l' % type, shell=True) if err: raise Exception(err) ret = int(lines[0].strip()) except Exception, e: return None, 'Error generating ZFS count : %s' % str(e)
def test_ssh(user='******', ip=None): try: if ip and user: cmd = "ssh %s@%s 'ls'" % (user, ip) (status, ret), err = command.get_command_output(cmd) if "anaconda-ks.cfg" in status: return True, None else: return "Key setup failed", "err" else: return "Username or IP not found", "err" except Exception, e: return False, "Error testing the ssh key check : %s" % str(e)
def generate_self_signed_ssl_certificate(d): 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 generate_dmidecode_section(f): try: f.write( '--------------------- Hardware information BEGIN------------------------\n\n' ) lines, err = command.get_command_output( 'dmidecode -s system-manufacturer') f.write('System manufacturer : %s\n' % lines[0]) lines, err = command.get_command_output( 'dmidecode -s system-product-name') f.write('System product name : %s\n' % lines[0]) lines, err = command.get_command_output('dmidecode -s system-version') f.write('System version : %s\n' % lines[0]) lines, err = command.get_command_output( 'dmidecode -s system-serial-number') f.write('System serial number (service tag) : %s\n' % lines[0]) f.write('\n') f.write( '--------------------- Hardware information END ------------------------\n\n' ) except Exception, e: return False, 'Error generating hardware information section: %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_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 generate_command_based_section(f, cmd, section_name): try: lines, err = command.get_command_output(cmd) if err: raise Exception(err) f.write('--------------------- %s BEGIN ------------------------\n\n' % section_name) f.write('\n'.join(lines)) f.write('\n') f.write('--------------------- %s END ------------------------\n\n' % section_name) except Exception, e: return False, 'Error generating %s section: %s' % (section_name, str(e))
def mount_list(): 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 mounted!') dev_names, err = command.get_command_output( "cat /var/log/usb-mount.track | cut -d ':' -f 2", shell=True) if err: raise Exception(err) for i, mount in enumerate(mount_points, start=1): print ' %s. %s of /dev/%s' % (i, mount, dev_names[i - 1]) print except Exception, e: print str(e) raw_input('\nPress any key to continue')
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 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 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 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_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_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 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 set_local_user_gid(d): try: if 'username' not in d: raise Exception('Unknown user') ud, err = get_local_user(d['username']) if err: raise Exception('Error looking up user information : %s' % err) if not ud: raise Exception('Specified user information not found.') lines, err = command.get_command_output( r'usermod -g %s %s' % (d['gid'], d['username'])) if err: raise Exception(err) except Exception, e: return False, 'Error setting local user group : %s' % str(e)