def test_protected_mount(tmpdir): """ protected_mount() """ assert protected_mount("/bin") is True assert protected_mount("/bin/") is True assert protected_mount("/mysvc") is True if Env.sysname == 'Darwin': location = "/Volumes/RAMDiskOpensvcTest" create_mount = 'diskutil erasevolume HFS+ "RAMDiskOpensvcTest" `hdiutil attach -nomount ram://524288`' delete_mount = "diskutil eject RAMDiskOpensvcTest" elif Env.sysname == 'SunOS': if os.geteuid() != 0: # Only tries mount when geteuid is 0 return location = str(tmpdir) create_mount = "sudo mount -F tmpfs swap %s" % location delete_mount = "sudo umount %s && rmdir %s" % (location, location) else: location = str(tmpdir) create_mount = "sudo mount -t tmpfs none %s" % location delete_mount = "sudo umount %s && rmdir %s" % (location, location) call(create_mount, shell=True) assert protected_mount("%s" % location) is False assert protected_mount("%s/" % location) is False assert protected_mount("%s/a" % location) is False call(delete_mount, shell=True)
def mem_u(self, d, day, start, end): cols = [ 'date', 'kbmemfree', 'kbmemused', 'pct_memused', 'kbmemsys', 'nodename' ] cmd = ['sysctl', 'hw.physmem'] (ret, out, err) = call(cmd) physmem = int(out.split(': ')[1]) / 1024 cmd = ['sysctl', 'hw.usermem'] (ret, out, err) = call(cmd) usermem = int(out.split(': ')[1]) / 1024 cmd = ['bsdsar', '-r', '-n', day] (ret, buff, err) = call(cmd) lines = [] for line in buff.split('\n'): l = line.split() if len(l) != 7: continue if l[0] == 'Time': continue free = self.kb(l[1]) used = self.kb(l[2]) + self.kb(l[3]) x = [ l[0], str(free), str(used), str(used / (used + free)), str(physmem - usermem), self.nodename ] x[0] = '%s %s' % (d, x[0]) lines.append(x) return cols, lines
def test_fsum(non_existing_file): """ fsum() """ filename = str(non_existing_file) call("echo abc > " + str(filename), shell=True) assert fsum(filename) == "0bee89b07a248e27c83fc3d5951213c1" call("rm -f " + filename, shell=True)
def test_read_cf(tmp_path): """ read_cf() """ tmp_file = os.path.join(str(tmp_path), 'foo') call("echo '[aa]\nbb = cc\n' >" + tmp_file, shell=True) config = read_cf(tmp_file) assert config.get("aa", "bb") == "cc" call("rm -f " + tmp_file, shell=True) # cf not present config = read_cf(tmp_file) assert config.sections() == []
def __init__(self, name): self.used_pct = 0 self.size = 0 self.free = 0 cmd = ['showfdmn', name] ret, out, err = call(cmd) if ret != 0: raise ExInit() d = {} """ Id Date Created LogPgs Version Domain Name 46a70bfd.000964b0 Wed Jul 25 10:38:21 2007 512 4 dom1 Vol 1K-Blks Free % Used Cmode Rblks Wblks Vol Name 2L 62914560 21056568 67% on 256 256 /dev/disk/dsk13c """ lines = out.split('\n') if len(lines) < 5: raise ExInit() header = lines[2].split() self.domid = header[0] self.name = header[-1] self.version = header[-2] self.logpgs = header[-3] self.vols = {} self.fsets = {} for line in lines[5:]: try: v = Volume(line) self += v except ExInit: pass cmd = ['showfsets', '-k', name] ret, out, err = call(cmd) if ret != 0: raise ExInit() lines = out.split('\n') n_lines = len(lines) if n_lines == 0: return h = 0 for i, line in enumerate(lines): if i != 0 and not line.startswith('\t') or i == n_lines - 1: f = Fset(lines[h:i]) self += f if not line.startswith('\t'): h = i
def stats_blockdev_day(t): d = t.strftime("%Y-%m-%d") day = t.strftime("%d") f = sarfile(day) if f is None: return [] cmd = ['sar', '-d', '-f', f] (ret, buff, err) = call(cmd, errlog=False) lines = [] last_date = '00:00:00' for line in buff.split('\n'): l = line.split() if len(l) == 8: last_date = l[0] if len(l) == 7: l = [last_date] + l if len(l) != 8: continue if l[1] == 'device': continue if l[0] == 'Average': continue """ xmlrpc: 22:05:01 DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util 00:00:00 device %busy avque r+w/s blks/s avwait avserv """ x = ['%s %s' % (d, l[0]), l[1], l[4], '0', '0', '0', l[3], l[6], l[7], l[2], Env.nodename] lines.append(x) return lines
def file_to_loop(f): """Given a file path, returns the loop device associated. For example, /path/to/file => /dev/loop0 """ if which('mdconfig') is None: return [] if not os.path.isfile(f): return [] (ret, out, err) = call(['mdconfig', '-l', '-v']) if ret != 0: return [] """ It's possible multiple loopdev are associated with the same file """ devs = [] for line in out.split('\n'): l = line.split() if len(l) < 4: continue path = ' '.join(l[3:]) if path != f: continue if not os.path.exists('/dev/' + l[0]): continue devs.append(l[0]) return devs
def swap(self, d, day, start, end): cols = [ 'date', 'kbswpfree', 'kbswpused', 'pct_swpused', 'kbswpcad', 'pct_swpcad', 'nodename' ] cmd = ['bsdsar', '-r', '-n', day] (ret, buff, err) = call(cmd, errlog=False) lines = [] if ret != 0: return cols, lines for line in buff.split('\n'): l = line.split() if len(l) != 7: continue if l[0] == 'Time': continue free = self.kb(l[6]) used = self.kb(l[5]) x = [ l[0], str(free), str(used), str(used / (free + used)), '0', '0' ] x.append(self.nodename) x[0] = '%s %s' % (d, x[0]) lines.append(x) return cols, lines
def cpu(self, d, day, start, end): cols = ['date', 'usr', 'sys', 'iowait', 'idle', 'cpu', 'nodename'] f = self.sarfile(day) lines = [] if f is None: return cols, lines cmd = ['sar', '-u', '-f', f, '-s', start, '-e', end] (ret, buff, err) = call(cmd, errlog=False) for line in buff.split('\n'): l = line.split() if len(l) != 5: continue if l[1] == '%usr': continue if l[0] == 'Average': continue l += ['all', self.nodename] l[0] = '%s %s' % (d, l[0]) lines.append(l) return cols, lines
def blockdev(self, d, day, start, end): cols = ['date', 'dev', 'pct_util', 'avgqu_sz', 'rsecps', 'await', 'svctm', 'nodename'] f = self.sarfile(day) lines = [] if f is None: return cols, lines cmd = ['sar', '-d', '-f', f, '-s', start, '-e', end] (ret, buff, err) = call(cmd, errlog=False) last_date = '00:00:00' for line in buff.split('\n'): l = line.split() if len(l) == 8: last_date = l[0] if len(l) == 7: l = [last_date] + l if len(l) != 8: continue if l[1] == 'device': continue if l[0] == 'Average': continue # 00:00:00 device %busy avque r+w/s [blks/s] avwait avserv x = ['%s %s' % (d, l[0]), l[1], l[2], l[3], l[4], l[6], l[7], self.nodename] lines.append(x) return cols, lines
def stats_cpu_day(t): d = t.strftime("%Y-%m-%d") day = t.strftime("%d") f = sarfile(day) if f is None: return [] cmd = ['sar', '-u', '-P', 'ALL', '-f', f] (ret, buff, err) = call(cmd, errlog=False) lines = [] for line in buff.split('\n'): l = line.split() if len(l) != 6: continue if l[1] == '%usr': continue if l[0] == 'Average': continue # SunOS: date %usr %sys %wio %idle # xmlrpc: date cpu usr nice sys iowait steal irq soft guest idle nodename x = ['%s %s' % (d, l[0]), 'all', '0', '0', '0', '0', '0', '0', '0', '0', '0', Env.nodename] x[1] = l[1].replace('-', 'all') x[2] = l[2] x[4] = l[3] x[5] = l[4] x[10] = l[5] lines.append(x) return lines
def do_check(self): """ # bdf -li Filesystem kbytes used avail %used iused ifree %iuse Mounted on /dev/vg00/lvol3 1048576 228160 814136 22% 2105 25607 8% / """ cmd = ['bdf', '-li'] (ret, out, err) = call(cmd) if ret != 0: return self.undef lines = out.split('\n') if len(lines) < 2: return self.undef r = [] for line in lines[1:]: l = line.split() if len(l) == 9: inst = ' '.join(l[8:]) r.append({"instance": inst, "value": l[7], "path": self.find_svc(inst), } ) elif len(l) == 8: inst = ' '.join(l[7:]) r.append({"instance": inst, "value": l[6], "path": self.find_svc(inst), } ) return r
def netdev(self, d, day, start, end): cols = [ 'date', 'dev', 'rxpckps', 'rxkBps', 'txpckps', 'txkBps', 'nodename' ] f = self.sarfile(day) lines = [] if f is None: return cols, lines cmd = ['sar', '-n', 'DEV', '-f', f, '-s', start, '-e', end] (ret, buff, err) = call(cmd, errlog=False) for line in buff.split('\n'): l = line.split() if len(l) != 6: continue if l[1] in ['IFACE', 'lo0']: continue if 'dummy' in l[1] or 'vnet' in l[1] or 'veth' in l[1] or \ 'gif' in l[1] or 'stf' in l[1]: continue if l[0] == 'Average:': continue l.append(self.nodename) l[0] = '%s %s' % (d, l[0]) lines.append(l) return cols, lines
def scan(self, lname): vid = 'unknown' pid = 'unknown' wwid = 'unknown' size = 'unknown' cmd = ['lscfg', '-vpl', lname] (ret, out, err) = call(cmd) for f in out.split('\n'): if "Manufacturer" in f: vid = f.split('.')[-1] if "Machine Type and Model" in f: pid = f.split('.')[-1] cmd = ['bootinfo', '-s', lname] out, err, ret = justcall(cmd) if ret == 0: size = int(out.strip()) else: size = 0 wwid = self.odmget(lname, 'ww_name').replace('0x', '') if wwid == 'unknown': wwid = self.get_vscsi_id(lname) self.h[lname] = dict(vid=vid, pid=pid, wwid=wwid, size=size)
def swap(self, d, day, start, end): cols = ['date', 'kbswpfree', 'nodename'] f = self.sarfile(day) lines = [] if f is None: return cols, lines cmd = ['sar', '-r', '-f', f, '-s', start, '-e', end] (ret, buff, err) = call(cmd) for line in buff.split('\n'): l = line.split() if len(l) != 3: continue if l[1] == 'freemem': continue if l[0] == 'Average': continue try: freeswap = int(l[2]) / 2 except: continue x = ['%s %s' % (d, l[0]), str(freeswap), self.nodename] lines.append(x) return cols, lines
def odmget(self, lname, attr): cmd = ['odmget', '-q', 'name='+lname+' AND attribute='+attr, 'CuAt'] (ret, out, err) = call(cmd) for f in out.split('\n'): if "value" not in f: continue return f.split(" = ")[-1].strip('"') return 'unknown'
def test_call(non_existing_file): """ call() """ ret, out, err = call(["ls", non_existing_file]) assert is_string(out) is True assert is_string(err) is True assert ret > 0
def pkgversion(package): cmd = ['pkgutil', '--pkg-info', package] (ret, out, err) = call(cmd, errlog=False, cache=True) for line in out.split('\n'): l = line.split(': ') if len(l) != 2: continue if l[0] == 'version': return l[1] return ''
def __str__(self, option=None): if option is None: cmd = [Env.syspaths.zfs, 'list', self.name] else: cmd = [Env.syspaths.zfs, 'list'] + option + [self.name] (retcode, stdout, stderr) = call(cmd, log=self.log) if retcode == 0: return stdout else: return "Failed to list info for dataset: %s" % (self.name)
def multipath_flush(dev, log=None): """ Settle udev before running a "multipath -f <dev>" to avoid the "in use" error. """ udevadm_settle() cmd = [Env.syspaths.multipath, "-f", dev] ret, out, err = call(cmd, info=True, outlog=True, log=log) if ret != 0: raise ex.Error
def test_vcall(non_existing_file): """ vcall() """ ret, out, err = vcall([]) assert ret == 0 assert out == "" assert err == "" ret, out, err = vcall(["ls2"]) assert ret == 1 ret, out, err = vcall(["ls", non_existing_file]) assert is_string(out) is True assert is_string(err) is True assert ret > 0 # redirects cmd1 = 'ls' _, _, _ = vcall([cmd1, non_existing_file], err_to_warn=True) _, _, _ = vcall([cmd1, non_existing_file], err_to_info=True) _, _, _ = call([cmd1], info=False, outlog=False, outdebug=True) cmd2 = "ls " + non_existing_file + " 2>/dev/stdout" _, _, _ = vcall(cmd2, shell=True, err_to_info=True) _, _, _ = vcall(cmd2, shell=True, err_to_warn=True) _, _, _ = vcall(cmd2, shell=True, warn_to_info=False) _, _, _ = vcall(cmd2, shell=True, outlog=False, outdebug=True) cmd3 = "ls >/dev/stderr" _, _, _ = vcall(cmd3, shell=True, warn_to_info=True) _, _, _ = vcall(cmd3, shell=True, warn_to_info=False) _, _, _ = vcall(cmd3, shell=True, errlog=False, errdebug=True) # to cache ret, out, err = vcall(cmd1, shell=True, cache=True) assert is_string(out) is True assert is_string(err) is True assert ret == 0 # from cache _, out2, _ = vcall(cmd1, shell=True, cache=True) assert out == out2 # cache discard _, _, _ = vcall("touch " + non_existing_file, shell=True, cache=True) _, _, _ = vcall("test -f " + non_existing_file, shell=True, cache=True) try: os.unlink(non_existing_file) except: pass ret, _, _ = vcall("test -f " + non_existing_file, shell=True, cache=False) assert ret == 1
def get_vscsi_id(self, lname): cmd = ['lscfg', '-l', lname] (ret, out, err) = call(cmd) if ret != 0: return 'unknown' l = out.split() if len(l) < 2: return 'unknown' d = l[1] regex = re.compile(r'-C[0-9]+-T[0-9]+') d = regex.sub('', d) return d
def listpkg(): if which('pkgutil') is None: return [] cmd = ['pkgutil', '--packages'] (ret, out, err) = call(cmd, errlog=False, cache=True) lines = [] for line in out.split('\n'): if len(line) == 0: continue x = [Env.nodename, line, pkgversion(line), ""] lines.append(x) return lines
def cpu(self, d, day, start, end): cols = ['date', 'usr', 'sys', 'nice', 'irq', 'idle', 'cpu', 'nodename'] cmd = ['bsdsar', '-u', '-n', day] (ret, buff, err) = call(cmd, errlog=False) lines = [] if ret != 0: return cols, lines for line in buff.split('\n'): l = line.split() if len(l) != 6: continue if l[0] == 'Time': continue l += ['ALL', self.nodename] l[0] = '%s %s' % (d, l[0]) lines.append(l) return cols, lines
def netdev_err(self, d, day, start, end): cols = ['date', 'rxerrps', 'txerrps', 'collps', 'dev', 'nodename'] cmd = ['bsdsar', '-I', '-n', day] (ret, buff, err) = call(cmd, errlog=False) lines = [] if ret != 0: return cols, lines for line in buff.split('\n'): l = line.split() if len(l) != 9: continue if l[0] == 'Time': continue x = [l[0], l[2], l[5], l[7], l[8], self.nodename] x[0] = '%s %s' % (d, l[0]) lines.append(x) return cols, lines
def listpkg_t(t): cmd = ['swlist', '-l', t, '-a', 'revision', '-a', 'mod_time'] (ret, out, err) = call(cmd, errlog=False, cache=True) lines = [] for line in out.split('\n'): l = line.split() if len(l) < 3: continue if line[0] == '#': continue try: l[2] = datetime.datetime.fromtimestamp(int( l[2])).strftime("%Y-%m-%d %H:%M:%S") except: l[2] = "" x = [Env.nodename, l[0], l[1], '', t, l[2]] lines.append(x) return lines
def stats_proc_day(t): d = t.strftime("%Y-%m-%d") day = t.strftime("%d") f = sarfile(day) if f is None: return [] cmd = ['sar', '-q', '-f', f] (ret, buff, err) = call(cmd) lines = [] for line in buff.split('\n'): l = line.split() if len(l) < 3: continue if ':' not in l[0]: continue """ xmlrpc: date runq_sz plist_sz ldavg_1 ldavg_5 ldavg_15 nodename """ x = ['%s %s' % (d, l[0]), l[1], '0', '0', '0', '0', Env.nodename] lines.append(x) return lines
def proc(self, d, day, start, end): cols = ['date', 'runq_sz', 'nodename'] f = self.sarfile(day) lines = [] if f is None: return cols, lines cmd = ['sar', '-q', '-f', f, '-s', start, '-e', end] (ret, buff, err) = call(cmd) for line in buff.split('\n'): l = line.split() if len(l) != 5: continue if l[1] == 'runq-sz': continue if l[0] == 'Average': continue x = ['%s %s' % (d, l[0]), l[1], self.nodename] lines.append(x) return cols, lines
def stats_block_day(t): d = t.strftime("%Y-%m-%d") day = t.strftime("%d") f = sarfile(day) if f is None: return [] cmd = ['sar', '-b', '-f', f] (ret, buff, err) = call(cmd) lines = [] for line in buff.split('\n'): l = line.split() if len(l) != 9: continue if ':' not in l[1]: continue """ xmlrpc: date tps rtps wtps rbps wbps nodename """ x = ['%s %s' % (d, l[0]), '0', '0', '0', l[1], l[4], Env.nodename] lines.append(x) return lines
def blockdev(self, d, day, start, end): cols = ['date', 'dev', 'tps', 'rsecps', 'nodename'] f = self.sarfile(day) lines = [] if f is None: return cols, lines cmd = ['sar', '-d', '-f', f, '-s', start, '-e', end] (ret, buff, err) = call(cmd, errlog=False) for line in buff.split('\n'): l = line.split() if len(l) != 4: continue if l[1] == 'device': continue if l[1] == 'Disk:': continue if l[0] == 'Average:': continue l.append(self.nodename) l[0] = '%s %s' % (d, l[0]) lines.append(l) return cols, lines