def update_virus_definations(update_file_name): ret_val = None try: root, err = common.clamav_virus_definations_directory() if err: raise Exception(err) cmd = 'rm -f %s/%s' % (root, update_file_name) status, err = command.get_command_output(cmd) if err: raise Exception(err) cmd = 'rm -f /var/lib/clamav/%s' % update_file_name status, err = command.get_command_output(cmd) if err: raise Exception(err) cmd = 'mv %s/.new/%s %s' % (root, update_file_name, root) status, err = command.get_command_output(cmd) if err: raise Exception(err) cmd = 'ln -s %s/%s /var/lib/clamav/%s' % (root, update_file_name, update_file_name) status, err = command.get_command_output(cmd) if err: raise Exception(err) service = 'clamd@scan' action = 'restart' status, err = services_management.change_service_status( service, action) if err: raise Exception(err) ret_val = True except Exception, e: return None, 'Error uploading update file: %s' % str(e)
def remove_vlan(if_name): try: if '.' not in if_name: raise Exception('Invalid VLAN ID') if not os.path.exists('/etc/sysconfig/network-scripts/ifcfg-%s'%if_name): raise Exception('Specified VLAN does not exist') os.remove('/etc/sysconfig/network-scripts/ifcfg-%s'%if_name) lines, err = command.get_command_output('ip link set dev %s down'%if_name) if err: raise Exception(err) lines, err = command.get_command_output('ip link delete %s'%if_name) if err: raise Exception(err) except Exception, e: return False, 'Error removing VLAN : %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 get_all_aces(path): #Get all ACEs! 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) ''' m = re.match('[\s]*([\S]*)[\:]*([\S]+)\:([\S]*)\:([rwx-]+)', line) if m: aces.append(m.groups()) #print m.groups() ''' print aces except Exception, e: return None, 'Error retrieving all ACLs : %s' % str(e)
def net_ads_join(user, pswd, password_server): try: cmd_to_run = "net ads join -S %s -U %s%%%s"%(password_server, user, pswd) use_salt, err = common.use_salt() if err: raise Exception(err) if use_salt: 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 kinit(user, pswd, realm): try: cmd_to_run = 'echo "%s\n" | kinit %s@%s'%(pswd, user, realm) use_salt, err = common.use_salt() if err: raise Exception(err) if use_salt: 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) if err: raise Exception(err) except Exception, e: return False, 'Error initializing kerberos : %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 = common.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 update_scan_list(name, activity): if activity not in ['add', 'remove']: raise Exception('Invalid activity') ret_val = None try: root, err = common.get_clamav_config_path() if err: raise Exception(err) if not os.path.isfile('%s/scan_list.txt' % root): cmd = 'touch %s/scan_list.txt' % root status, err = command.get_command_output(cmd) if err: raise Exception(err) old_status, err = in_scan_list(name) if err: raise Exception(err) path_name, err = file_processing.normalise_path(name) if err: raise Exception(err) if activity == 'add' and not old_status: with open('%s/scan_list.txt' % root, 'a') as f: f.write('%s\n' % path_name) ret_val = True elif activity == 'remove' and old_status: with open('%s/scan_list.txt' % root, 'r') as f: lines = f.readlines() with open('%s/scan_list.txt' % root, 'w') as f: for line in lines: if line.strip('\n') != '%s' % path_name: f.write(line) ret_val = False except Exception, e: return ret_val, 'Error updating virus scan list: %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 = common.use_salt() if err: raise Exception(err) if use_salt: 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 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 = common.use_salt() if err: raise Exception(err) if use_salt: 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 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 = common.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 can_ping(hostname): connected = False try: lines, err = command.get_command_output('ping -c 1 %s'%hostname) if not err: connected = True except Exception, e: return False, "Error checking ping : %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 list_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 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 services_status(): s = {} try: platform, err = common.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 update_clamav_conf(conf_dict, service): ret_val = None try: if service not in ['clamd', 'freshclam']: raise Exception('Invalid clamav service') script_root, err = common.get_python_scripts_path() if err: raise Exception(err) root, err = common.get_clamav_config_path() if err: raise Exception(err) st = None with open('%s/config/%s.conf' % (root, service), 'w') as f: for key, value in conf_dict.items(): f.write('%s %s\n' % (key, value)) if service == 'clamd': f.write('VirusEvent %s/av_event_virus_found.py "%%v"\n' % script_root) if os.path.isfile('/etc/clamd.d/scan.conf'): os.unlink('/etc/clamd.d/scan.conf') os.symlink('%s/config/%s.conf' % (root, service), '/etc/clamd.d/scan.conf') service = 'clamd@scan' action = 'restart' status, err = services_management.change_service_status( service, action) if err: raise Exception(err) elif service == 'freshclam': f.write( 'OnOutdatedExecute %s/av_event_update_available.py "%%v"\n' % script_root) if os.path.isfile('/etc/freshclam.conf'): os.unlink('/etc/freshclam.conf') os.symlink('%s/config/%s.conf' % (root, service), '/etc/freshclam.conf') command.get_command_output('killall freshclam') cmd = 'freshclam -d' status, err = command.get_command_output(cmd) if err: raise Exception(err) except Exception, e: return None, 'Error writing %s conf file: %s' % (service, str(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_virus(virus_file): ret_val = None try: root, err = common.get_clamav_quarantine_path() if err: raise Exception(err) cmd = 'rm -rf %s/%s' % (root, virus_file) status, err = command.get_command_output(cmd) if err: raise Exception(err) ret_val = True except Exception, e: return None, 'Error deleting virus file: %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 get_local_users(get_system_users=False): user_list = [] try: sys_ul = [] smb_ul = [] all = pwd.getpwall() #print all for user in all: if not get_system_users: if user.pw_uid < 1002 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[u['username']] else: ud['smb_user'] = True ud['smb_user'] = False except Exception, e: return None, 'Error retrieving local users : %s'%str(e)
def generate_self_signed_certificate(d): try: pki_dir, err = common.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 reload_manifest(request): return_dict = {} try: if request.method == "GET": from integralstor_unicell 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("reload_manifest.html", return_dict, context_instance=django.template.context.RequestContext(request)) elif request.method == "POST": common_python_scripts_path, err = common.get_common_python_scripts_path() if err: raise Exception(err) ss_path, err = common.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"%(common.get_python_scripts_path(), common.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("/show/node_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 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 = common.use_salt() if err: raise Exception(err) if use_salt: 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 = common.use_salt() if err: raise Exception(err) if use_salt: 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_local_users(get_system_users=False): user_list = [] try: sys_ul = [] smb_ul = [] all = pwd.getpwall() #print all for user in all: if not get_system_users: if user.pw_gid < 501: 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) 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[u['username']] else: ud['smb_user'] = True ud['smb_user'] = False except Exception, e: None, 'Error retrieving local users : %s'%str(e)
def 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 set_hostname(hostname, domain_name): try: init_type, err = common.get_init_type() if err: raise Exception(err) if init_type == 'init': with open('/etc/sysconfig/network', 'w') as f: f.write('#Generated by the IntegralStor script\n') f.write('NETWORKING=yes\n') f.write('HOSTNAME=%s\n'%hostname) f.flush() f.close() ret, err = command.get_command_output('hostname %s'%hostname) if err: raise Exception(err) else: if domain_name: hn = '%s.%s'%(hostname, domain_name) else: hn = hostname ret, err = command.get_command_output('hostnamectl set-hostname %s --static'%hn) if err: raise Exception(err) ret, err = command.get_command_output ('systemctl restart systemd-hostnamed') if err: raise Exception (err) time.sleep (5) """ ret, err = command.get_command_output('hostnamectl') if err: raise Exception(err) print ret """ if os.path.isfile('/etc/salt/minion_id'): os.remove('/etc/salt/minion_id') except Exception, e: return False, 'Error setting hostname : %s'%str(e)
def set_interface_state(if_name, state): try: if (not if_name) or (not state): raise Exception('Interface name or state not specified') if state not in ['up', 'down']: raise Exception('Unsupported state in state change request') cmd = 'if%s %s'%(state, if_name) #print cmd ret, err = command.get_command_output(cmd) if err: raise Exception(err) except Exception, e: return False, 'Error setting interface state: %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)
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)
def _get_ad_users_or_groups(type): o = None try: d, err = load_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_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_serial_number(disk_name): serial_number = None try: """ ids, err = get_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 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 delete_all_virus(): ret_val = None try: virus_list, err = get_quarantine_list() if err: raise Exception(err) root, err = common.get_clamav_quarantine_path() if err: raise Exception(err) if virus_list: for f in virus_list: cmd = 'rm -rf %s/%s' % (root, f) status, err = command.get_command_output(cmd) if err: raise Exception(err) ret_value = True except Exception, e: return None, 'Error deleting virus file: %s' % str(e)
def set_local_user_group_membership(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.') glist = None if 'groups' in d: glist = d['groups'] if not glist: glist = [] glist.append(ud['grpname']) lines, err = command.get_command_output(r'usermod -G %s %s'%(','.join(glist), d['username'])) if err: raise Exception(err) except Exception, e: return False, "Error setting local user's additional groups : %s"%str(e)
def _reload_config(): try: use_salt, err = common.use_salt() if err: raise Exception(err) if use_salt: 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) except Exception, e: return False, 'Error reloading CIFS configuration : %s'%str(e)
def change_password(username, pswd): 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') lines, err = command.get_command_output(r'echo \"%s:%s\"|chpasswd'%(username, pswd)) if err: raise Exception(err) if 'smb_user' in d and d['smb_user']: #lines, err = command.get_command_output(r'smbpasswd -s %s'%(username), "%s\n%s"%(pswd, pswd)) (ret, rc), err = command.execute_with_conf_and_rc(r'smbpasswd -s %s'%(username), "%s\n%s"%(pswd, pswd)) 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) except Exception, e: return False, 'Error changing local user password : %s'%str(e)
def create_local_group(grpname, gid = None): try: #First check if groups exists. if so kick out gl, err = get_local_groups() if gl: for gd in gl: if gd["grpname"] == grpname: raise Exception("Error creating group. The group \"%s\" already exists. "%grpname) elif err: raise Exception("Error retrieving group list : %s"%err) use_salt, err = common.use_salt() if err: raise Exception(err) if use_salt: client = salt.client.LocalClient() if not gid: rc = client.cmd('*', 'group.add', [grpname]) else: rc = client.cmd('*', 'group.add', [grpname,gid]) print rc if not rc: raise Exception('Group creation failed') for hostname, status in rc.items(): if not status: raise Exception('Group creation failed') else: if gid: cmd_to_run = 'groupadd -g %s %s'%(gid, grpname) else: cmd_to_run = 'groupadd %s'%(grpname) lines, err = command.get_command_output(cmd_to_run) if err: raise Exception(err) except Exception, e: return False, 'Error creating a local group : %s'%str(e)
def create_local_user(username, name, pswd, gid = None, smb_user=True): try: #First check if user exists. if so kick out ul, err = get_local_users() if ul: for ud in ul: if ud["username"] == username: raise Exception("Error creating user. The user \"%s\" already exists. "%username) elif err: raise Exception("Error retrieving user list : %s"%err) enc_pswd = crypt.crypt(pswd, "28") use_salt, err = common.use_salt() if err: raise Exception(err) if use_salt: client = salt.client.LocalClient() if gid: rc = client.cmd('*', 'user.add', [username, None, gid]) else: rc = client.cmd('*', 'user.add', [username]) #print rc if not rc: error_list.append("Error creating the username") for hostname, status in rc.items(): if not status: error_list.append("Error creating the username on node"%hostname) rc = client.cmd('*', 'shadow.set_password', [username, enc_pswd] ) for hostname, status in rc.items(): if not status: error_list.append("Error setting the password for username on GRIDCell %s"%hostname) rc = client.cmd('*', 'user.chfullname', [username, "integralstor_user_%s"%name] ) for hostname, status in rc.items(): if not status: error_list.append("Error setting the name for username on node %s"%hostname) else: #print '1' if gid: cmd_to_run = 'useradd -g %s -p %s -c integralstor_user_%s %s'%(gid, enc_pswd, name, username) else: cmd_to_run = 'useradd -p %s -c integralstor_user_%s %s'%(enc_pswd, name, username) lines, err = command.get_command_output(cmd_to_run) #print '2' #print lines, err if err: raise Exception(err) if smb_user: #print '/usr/bin/pdbedit -d 1 -t -a -u %s -f %s'%(username, name), "%s\n%s"%(pswd, pswd) # Now all set to create samba user #print '3' #lines, err = command.get_command_output(r'/usr/bin/pdbedit -d 1 -t -a -u %s -f %s'%(username, name), "%s\n%s"%(pswd, pswd)) (ret, rc), err = command.execute_with_conf_and_rc(r'/usr/bin/pdbedit -d 1 -t -a -u %s -f %s'%(username, name), "%s\n%s"%(pswd, pswd)) 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) #print '4' #print lines, err except Exception, e: return False, 'Error creating local user : %s'%str(e)
def edit_hostname(request): return_dict = {} try: hostname = socket.gethostname() if request.method == "GET": hostname = socket.gethostname() domain_name,err = networking.get_domain_name() if err: raise Exception(err) initial = {} initial['hostname'] = hostname initial['domain_name'] = domain_name form = networking_forms.EditHostnameForm(initial=initial) return_dict['form'] = form return django.shortcuts.render_to_response("edit_hostname.html", return_dict, context_instance = django.template.context.RequestContext(request)) else: form = networking_forms.EditHostnameForm(request.POST) return_dict['form'] = form if not form.is_valid(): return django.shortcuts.render_to_response("edit_hostname.html", return_dict, context_instance = django.template.context.RequestContext(request)) cd = form.cleaned_data result_str = "" domain_name = None if 'domain_name' in cd: domain_name = cd['domain_name'] result, err = networking.set_hostname(cd['hostname'], domain_name) if not result: if err: raise Exception(err) else: raise Exception('Error setting hostname') result, err = networking.set_domain_name(domain_name) if not result: if err: raise Exception(err) else: raise Exception('Error setting domain name') python_scripts_path, err = common.get_python_scripts_path() if err: raise Exception(err) common_python_scripts_path, err = common.get_common_python_scripts_path() if err: raise Exception(err) ss_path, err = common.get_system_status_path() if err: raise Exception(err) ret, err = command.get_command_output("python %s/generate_manifest.py %s"%(common_python_scripts_path, ss_path)) if err: raise Exception(err) ret, err = command.get_command_output("python %s/generate_status.py %s"%(common_python_scripts_path, ss_path)) if err: raise Exception(err) audit_str = "Hostname set to %s."%cd['hostname'] if 'domain_name' in cd: audit_str += 'Domain name set to %s'%cd['domain_name'] ret, err = audit.audit("edit_hostname", audit_str, request.META["REMOTE_ADDR"]) if err: raise Exception(err) return django.http.HttpResponseRedirect('/view_hostname?result=saved') except Exception, e: return_dict['base_template'] = "networking_base.html" return_dict["page_title"] = 'Modify system hostname' return_dict['tab'] = 'view_hostname_tab' return_dict["error"] = 'Error modifying system hostname' 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 dashboard(request,page): return_dict = {} try: if request.method != 'GET': raise Exception('Invalid access method. Please use the menus') si, err = system_info.load_system_config() if err: raise Exception(err) if not si: raise Exception('Error loading system configuration') return_dict['system_info'] = si #By default show error page template = "logged_in_error.html" num_nodes_bad = 0 total_pool = 0 total_nodes = len(si) nodes = {} # Chart specific declarations today_day = (datetime.date.today()).strftime('%d') # will return 02, instead of 2. #today_day = datetime.datetime.today().day #if today_day < 10: # today_day = '0%d'%today_day value_list = [] time_list = [] use_salt, err = common.use_salt() if err: raise Exception(err) ks = si.keys() if not ks: raise Exception('System configuration invalid') info = ks[0] # CPU status if page == "cpu": return_dict["page_title"] = 'CPU statistics' return_dict['tab'] = 'cpu_tab' return_dict["error"] = 'Error loading CPU statistics' cpu,err = stats.get_system_stats(today_day,"cpu") if err: raise Exception(err) value_dict = {} if cpu: for key in cpu.keys(): value_list = [] time_list = [] if key == "date": pass else: if cpu[key]: for a in cpu[key]: time_list.append(a[0]) value_list.append(a[1]) value_dict[key] = value_list return_dict["data_dict"] = value_dict queue,err = stats.get_system_stats(today_day,"queue") if err: raise Exception(err) value_dict = {} if queue: for key in queue.keys(): value_list = [] time_list = [] if key == "date": pass else: for a in queue[key]: time_list.append(a[0]) value_list.append(a[1]) value_dict[key] = value_list return_dict["data_dict_queue"] = value_dict return_dict['node_name'] = info return_dict['node'] = si[info] d = {} template = "view_cpu_status.html" # Hardware elif page == "hardware": return_dict["page_title"] = 'Hardware status' return_dict['tab'] = 'hardware_tab' return_dict["error"] = 'Error loading hardware status' d = {} d['ipmi_status'] = si[info]['ipmi_status'] return_dict['hardware_status'] = d return_dict['node_name'] = info template = "view_hardware_status.html" # Memory elif page == "memory": return_dict["page_title"] = 'Memory statistics' return_dict['tab'] = 'memory_tab' return_dict["error"] = 'Error loading memory statistics' mem,err = stats.get_system_stats(today_day,"memory") if err: raise Exception(err) if mem: for a in mem["memfree"]: time_list.append(a[0]) value_list.append(a[1]) return_dict['memory_status'] = si[info]['memory'] template = "view_memory_status.html" # Network elif page == "network": return_dict["page_title"] = 'Network statistics' return_dict['tab'] = 'network_tab' return_dict["error"] = 'Error loading Network statistics' network,err = stats.get_system_stats(today_day,"network") if err: raise Exception(err) value_dict = {} if network: for key in network.keys(): value_list = [] time_list = [] if key == "date" or key == "lo": pass else: for a in network[key]["ifutil-percent"]: time_list.append(a[0]) value_list.append(a[1]) value_dict[key] = value_list return_dict["data_dict"] = value_dict print si[info]["interfaces"] return_dict["network_status"] = si[info]['interfaces'] template = "view_network_status.html" # Services elif page == "services": return_dict["page_title"] = 'System services status' return_dict['tab'] = 'services_tab' return_dict["error"] = 'Error loading system services status' return_dict['services_status'] = {} if use_salt: import salt.client client = salt.client.LocalClient() winbind = client.cmd(info,'cmd.run',['service winbind status']) smb = client.cmd(info,'cmd.run',['service smb status']) nfs = client.cmd(info,'cmd.run',['service nfs status']) iscsi = client.cmd(info,'cmd.run',['service tgtd status']) ntp = client.cmd(info,'cmd.run',['service ntpd status']) return_dict['services_status']['winbind'] = winbind[info] return_dict['services_status']['smb'] = smb[info] return_dict['services_status']['nfs'] = nfs[info] return_dict['services_status']['iscsi'] = iscsi[info] return_dict['services_status']['ntp'] = ntp[info] else: out_list, err = command.get_command_output('service winbind status', False) if err: raise Exception(err) if out_list: return_dict['services_status']['winbind'] = ' '.join(out_list) out_list, err = command.get_command_output('service smb status', False) if err: raise Exception(err) if out_list: return_dict['services_status']['smb'] = ' '.join(out_list) out_list, err = command.get_command_output('service nfs status', False) if err: raise Exception(err) if out_list: return_dict['services_status']['nfs'] = ' '.join(out_list) out_list, err = command.get_command_output('service tgtd status', False) if err: raise Exception(err) if out_list: return_dict['services_status']['iscsi'] = ' '.join(out_list) out_list, err = command.get_command_output('service ntpd status', False) if err: raise Exception(err) if out_list: return_dict['services_status']['ntp'] = ' '.join(out_list) template = "view_services_status.html" # Disks elif page == "disks": return_dict["page_title"] = 'Hard drives status' return_dict['tab'] = 'disk_tab' return_dict["error"] = 'Error loading hard drives status' sorted_disks = [] if 'disks' in si[info] and si[info]['disks']: for key,value in sorted(si[info]["disks"].iteritems(), key=lambda (k,v):v["position"]): sorted_disks.append(key) return_dict['node'] = si[info] return_dict["disk_status"] = si[info]['disks'] return_dict["disk_pos"] = sorted_disks return_dict['node_name'] = info template = "view_disks_status.html" # Pools elif page == "pools": return_dict["page_title"] = 'ZFS pools status' return_dict['tab'] = 'pools_tab' return_dict["error"] = 'Error loading ZFS pools status' pools, err = zfs.get_pools() if err: raise Exception(err) if pools: return_dict['pools'] = pools template = "view_pools_status.html" return_dict["labels"] = time_list return_dict["data"] = value_list return django.shortcuts.render_to_response(template, return_dict, context_instance=django.template.context.RequestContext(request)) except Exception, e: return_dict['base_template'] = "dashboard_base.html" 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))