def usage(self): """ Check usage percentage for this mountpoint. Returns dictionary with usage details. """ if self.filesystem in PSEUDO_FILESYSTEMS: return {} try: output = check_output(['df', '-k', self.mountpoint]) except CalledProcessError: raise FileSystemError('Error getting usage for {0}'.format( self.mountpoint)) header, usage = output.split('\n', 1) try: usage = ' '.join(usage.split('\n')) except ValueError: pass fs, size, used, free, percent, mp = map(lambda x: x.strip(), usage.split()) percent = percent.rstrip('%') return { 'mountpoint': self.mountpoint, 'size': long(size), 'used': long(used), 'free': long(free), 'percent': int(percent) }
def load_mountpoints(): """ Update list of FreeBSD mountpoints based on /sbin/mount output """ mountpoints = [] try: output = check_output(['/sbin/mount']) except CalledProcessError: raise FileSystemError('Error running /sbin/mount') for l in [l for l in output.split('\n') if l.strip() != '']: if l[:4] == 'map ': continue m = RE_MOUNT.match(l) if not m: continue device = m.group(1) mountpoint = m.group(2) flags = map(lambda x: x.strip(), m.group(3).split(',')) filesystem = flags[0] flags = flags[1:] entry = BSDMountPoint(device, mountpoint, filesystem) for f in flags: entry.flags.set(f, True) mountpoints.append(entry) return mountpoints
def execute(self, command): """Execute smartctl commands Generic wrapper to execute smartctl commands """ try: output = check_output(command).decode('utf-8') except CalledProcessError as e: raise SmartError('Error executing {0}: {1}'.format(' '.join(command), e)) headers = {} lines = [] for l in output.split('\n'): matched = False for name,re_header in HEADERS.items(): m = re_header.match(l) if m: headers[name] = m.groupdict() matched = True break if not matched: lines.append(l) return lines
def usage(self): """ Check usage percentage for this mountpoint. Returns dictionary with usage details. """ if self.filesystem in PSEUDO_FILESYSTEMS: return {} try: output = check_output( ('df', '-k', self.mountpoint, ) ) except CalledProcessError: raise FileSystemError('Error getting usage for {0}'.format(self.mountpoint)) (header,usage) = output.split('\n', 1) try: usage = ' '.join(usage.split('\n')) except ValueError: pass fs, size, used, free, percent, mp = [x.strip() for x in usage.split()] percent = percent.rstrip('%') return { 'mountpoint': self.mountpoint, 'size': long(size), 'used': long(used), 'free': long(free), 'percent': int(percent), }
def load_mountpoints(): """ Update list of linux mountpoints based on /bin/mount output """ mountpoints = [] try: output = check_output(['/bin/mount']) except CalledProcessError: raise FileSystemError('Error running /bin/mount') for l in [l for l in output.split('\n') if l.strip() != '']: if l[:4] == 'map ': continue m = RE_MOUNT.match(l) if not m: continue device = m.group(1) mountpoint = m.group(2) filesystem = m.group(3) flags = map(lambda x: x.strip(), m.group(4).split(',')) entry = LinuxMountPoint(device,mountpoint,filesystem) for f in flags: entry.flags.set(f,True) mountpoints.append(entry) return mountpoints
def execute(command): if not isinstance(command,list): raise SmartError('Command to execute must be a list') try: output = check_output(command) except CalledProcessError,emsg: raise SmartError('Error executing {0}: {1}'.format(' '.join(command), emsg))
def check_output(self, args): """ Wrapper for subprocess.check_output to be executed in script context """ if isinstance(args, basestring): args = [args] try: return check_output(args) except IOError, (ecode, emsg): raise ScriptError(emsg)
def check_output(self, args): """ Wrapper for subprocess.check_output to be executed in script context """ if isinstance(args, str): args = [args] try: return check_output(args) except IOError as e: raise ScriptError(e) except OSError as e: raise ScriptError(e) except CalledProcessError as e: raise ScriptError(e)
def usage(self): """ Check usage percentage for this mountpoint. Returns dictionary with usage details. """ try: output = check_output(['df', '-k', self.mountpoint]) except CalledProcessError as e: raise FileSystemError('Error checking filesystem usage: {0}'.format(e)) header, usage = output.split('\n', 1) m = re_df.match(usage) if not m: raise FileSystemError('Error matching df output line: {0}'.format(usage)) return { 'mountpoint': self.mountpoint, 'size': long(m.group(2)), 'used': long(m.group(3)), 'free': long(m.group(4)), 'percent': int(m.group(5)) }
def load_mountpoints(): """ Update mount points from /sbin/mount output """ mountpoints = [] try: output = check_output(['/sbin/mount']) except CalledProcessError as e: raise FileSystemError('Error getting mountpoints: {0}'.format(e)) for l in [l for l in output.split('\n') if l.strip() != '']: if l[:4] == 'map ': continue m = re_mountpoint.match(l) if not m: continue device = m.group(1) mountpoint = m.group(2) flags = [x.strip() for x in m.group(3).split(',')] filesystem = flags[0] flags = flags[1:] entry = OSXMountPoint(mountpoint, device, filesystem) for f in flags: if f[:11] == 'mounted by ': entry.flags.set('owner', f[11:]) else: entry.flags.set(f, True) mountpoints.append(entry) return mountpoints