def process_iter(): """Return a generator yielding a Process instance for all running processes. Every new Process instance is only created once and then cached into an internal table which is updated every time this is used. Cached Process instances are checked for identity so that you're safe in case a PID has been reused by another process, in which case the cached instance is updated. yuetian: 1. the origion use a check_running function to check whether PID has been reused by another process in which case yield a new Process instance hint:i did not use check_running function because the container of pid is set 2. the origion use a sorted list(_pmap.items()) + list(dict.fromkeys(new_pids).items() to get pid and proc to make res.proc is only a instance of a pid Process hint(bugs):i did not use fromkeys(new_pids) because i did not get the meanning of using proc """ decorators.needlinux(True) pid_set = set(pids()) for pid in pid_set: try: check_process = Process(pid) res = check_process.get_process_name() except exceptions.NoSuchProcess: pass else: yield check_process
def get_disk_info(): """ :return: get disk info of the system """ decorators.needlinux(True) info = os.popen("df -lh") allDiskInfo = [] for line in enumerate(info.readlines()): if line[0] != 0: blockInfo = [] for block in line[1].split(" "): if len(block) != 0: blockInfo.append(block) allDiskInfo.append({ "FileSystem": blockInfo[0], "Size": blockInfo[1], "Used": blockInfo[2], "Available": blockInfo[3], "Percentage": blockInfo[4], }) else: continue try: return allDiskInfo except: raise RuntimeError("couldn't find disk")
def get_disk_usage_all(raw=False): """ :param raw: if raw is True, will use Byte as the measure. Automatically use MB/GB otherwise. :return: return a dict of disk usage """ decorators.needlinux(True) byte2gb = 1024 * 1024 * 1024 byte2mb = 1024 * 1024 st = os.statvfs("/") free = st.f_bavail * st.f_frsize total = st.f_blocks * st.f_frsize unit = "Byte" if not raw: if total > byte2gb: free, total = \ free / byte2gb, total / byte2gb unit = "GB" elif total > byte2mb: free, total = \ free / byte2mb, total / byte2mb unit = "MB" return { "totalSpace": total, "usedSpace": total - free, "freeSpace": free, "unit":unit }
def get_kernel_version(): """ get linux kernel verions, e.g.('2', '6', '32'): """ decorators.needlinux(True) versions = os.uname()[2] # version = versions[0: versions.find('_')] return tuple([info for info in versions.split('.')])
def mutt_sendmail(m_to, subject, body, attach, content_is_html=False): """ Notice: this function is not recommended to use. Use SMTPServer/Mail instead. :param exec_cwd: exec working directory. Plz use :param m_to: recipt list, separated by , :param subject: subject :param body: email content :param attach: email attachment :param content_is_html: is htm mode opened :return: return True on success, False otherwise """ from tlib.platform import shell decorators.needlinux(mutt_sendmail) shellobj = shell.ShellExec() temp_cwd = os.getcwd() str_att = '' cmdstr = '' if attach == '': if content_is_html is True: cmdstr = 'echo "%s"|/usr/bin/mutt -e "my_hdr Content-Type:'\ 'text/html" -s "%s" %s' \ % (body, subject, m_to) else: cmdstr = 'echo "%s"|/usr/bin/mutt -s "%s" %s' % ( body, subject, m_to ) else: attlist = attach.strip().split(',') attlen = len(attlist) for i in range(0, attlen): str_att += '-a ' + attlist[i] if(i != attlen - 1): str_att += ' ' if content_is_html is True: cmdstr = 'echo %s|/usr/bin/mutt -e "my_hdr Content-Type:'\ 'text/html" -s "%s" %s %s' % (body, subject, str_att, m_to) else: cmdstr = 'echo %s|/usr/bin/mutt -s "%s" %s %s' % ( body, subject, str_att, m_to ) ret_dic = shellobj.run(cmdstr, 60) os.chdir(temp_cwd) if ret_dic['returncode'] == 0: return True else: logger.warn(ret_dic['stderr']) return False
def __init__(self, pid): decorators.needlinux(True) self._pid = pid self._process_name = None self._create_time = None if not os.path.lexists("/proc/%s/exe" % self._pid): raise exceptions.NoSuchProcess(self._pid, self._process_name) else: self._create_time = self.create_time()
def get_net_recv_speed(str_interface, intvl_in_sec): """ get average network recv-speed during a time period (intvl_in_sec) """ decorators.needlinux(True) unittest.TestCase.assertGreater(intvl_in_sec, 0) tx_bytes0 = get_net_through(str_interface)[1] time.sleep(intvl_in_sec) tx_bytes1 = get_net_through(str_interface)[1] return (tx_bytes1 - tx_bytes0) / intvl_in_sec
def boot_time(): """Return the system boot time expressed in seconds since the epoch. """ decorators.needlinux(True) with open('/proc/stat', 'rb') as f: for line in f: if line.startswith(b'btime'): ret = float(line.strip().split()[1]) return ret raise RuntimeError("line 'btime' not found in /proc/stat")
def get_boottime_since_epoch(): """ :return: return boot time (seconds) since epoch """ decorators.needlinux(True) fp = open('/proc/stat', 'r') try: for line in fp: if line.startswith('btime'): return float(line.strip().split()[1]) raise RuntimeError("line 'btime' not found") finally: fp.close()
def get_swapinfo(): """ get swamp info of the system """ decorators.needlinux(True) fp = open('/proc/swaps', 'r') reg = '([\\/A-Za-z0-9]+)[\\s]+([a-z]+)[\\s]+([0-9]+)'\ '[\\s]+([0-9]+)[\\s]+([\\-0-9]+).*' regobj = re.compile(reg) total = used = 0 try: for line in fp: if regobj.search(line) is not None: sp = line.split() total += int(sp[2]) used += int(sp[3]) finally: fp.close() if total == 0: total = used = 0 msg = 'Failed to get total from /proc/swaps or '\ 'the system does not have swap mounted.' \ ' Total and used were set to 0' warnings.warn(msg, RuntimeWarning) sin = sout = None fp = open('/proc/vmstat', 'r') try: for line in fp: # values are expressed in 4 kilo bytes, we want bytes instead if line.startswith('pswpin'): sin = int(line.split(' ')[1]) * 4 * 1024 elif line.startswith('pswpout'): sout = int(line.split(' ')[1]) * 4 * 1024 if sin is not None and sout is not None: break else: # we might get here when dealing with exotic Linux flavors, see: # http://code.google.com/p/psutil/issues/detail?id=313 msg = "'sin' and 'sout' swap memory stats couldn't " \ "be determined and were set to 0" warnings.warn(msg, RuntimeWarning) sin = sout = 0 finally: fp.close() free = total - used return SWAPINFO(total, free, used, sin, sout)
def get_meminfo(): """ get system memory info """ decorators.needlinux(True) total = free = buffers = cached = active = inactive = None fp = open('/proc/meminfo', 'r') try: for line in fp: if line.startswith('MemTotal'): total = int(line.split()[1]) * 1024 elif line.startswith('MemFree'): free = int(line.split()[1]) * 1024 elif line.startswith('Buffers'): buffers = int(line.split()[1]) * 1024 elif line.startswith('Cached:'): cached = int(line.split()[1]) * 1024 elif line.startswith('Active:'): active = int(line.split()[1]) * 1024 elif line.startswith('Inactive:'): inactive = int(line.split()[1]) * 1024 if cached is not None \ and active is not None \ and inactive is not None: break else: # we might get here when dealing with exotic Linux flavors, see: # http://code.google.com/p/psutil/issues/detail?id=313 msg = "'cached', 'active' and 'inactive' memory stats couldn't " \ "be determined and were set to 0" warnings.warn(msg, RuntimeWarning) total = free = buffers = cached = active = inactive = 0 finally: fp.close() avail = free + buffers + cached used = total - free percent = int((total - avail) * 100 / total) return MemInfo( total, avail, percent, used, free, active, inactive, buffers, cached )
def get_net_transmit_speed(str_interface, intvl_in_sec=1): """ get network interface write/read speed E.g. :: import tlib print tlib.linux.get_net_transmit_speed('eth1', 5) """ decorators.needlinux(True) unittest.TestCase.assertGreater(intvl_in_sec, 0) rx_bytes0 = get_net_through(str_interface)[0] time.sleep(intvl_in_sec) rx_bytes1 = get_net_through(str_interface)[0] return (rx_bytes1 - rx_bytes0) / intvl_in_sec
def net_io_counters(): """ get network statistics with a list of namedtuple (bytes_sent, bytes_recv, packets_sent, packets_recv, errin, errout, dropin, dropout) example :: { 'lo': ( 235805206817, 235805206817, 315060887, 315060887, 0, 0, 0, 0 ), 'eth1': ( 18508976300272, 8079464483699, 32776530804, 32719666038, 0, 0, 708015, 0 ), 'eth0': ( 0, 0, 0, 0, 0, 0, 0, 0 ) } """ decorators.needlinux(True) fhandle = open("/proc/net/dev", "r") try: lines = fhandle.readlines() finally: fhandle.close() retdict = {} for line in lines[2:]: colon = line.rfind(':') assert colon > 0, repr(line) name = line[:colon].strip() fields = line[colon + 1:].strip().split() bytes_recv = int(fields[0]) packets_recv = int(fields[1]) errin = int(fields[2]) dropin = int(fields[3]) bytes_sent = int(fields[8]) packets_sent = int(fields[9]) errout = int(fields[10]) dropout = int(fields[11]) retdict[name] = (bytes_sent, bytes_recv, packets_sent, packets_recv, errin, errout, dropin, dropout) return retdict
def get_net_through(str_interface): """ get network interface statistics by a interface (eth0, e,g,) """ decorators.needlinux(True) rx_bytes = tx_bytes = -1 fp = open('/proc/net/dev', 'r') try: for line in fp: if str_interface in line: data = line.split('%s:' % str_interface)[1].split() rx_bytes, tx_bytes = (data[0], data[8]) finally: fp.close() if rx_bytes < 0 or tx_bytes < 0: msg = 'Failed to parse /proc/net/dev' warnings.warn(msg, RuntimeWarning) unittest.TestCase.assertGreaterEqual(rx_bytes, 0) unittest.TestCase.assertGreaterEqual(tx_bytes, 0) return int(rx_bytes), int(tx_bytes)
def get_cpu_core_usage(coreindex, intvl_in_sec=1): """ :param index: cpu core index """ decorators.needlinux(True) unittest.TestCase.assertGreater(intvl_in_sec, 0) ret = [] ret = [0 for _ in _CPU_COLUMNS] cpu_info0 = _get_cput_by_stat(coreindex) time.sleep(intvl_in_sec) cpu_info1 = _get_cput_by_stat(coreindex) total = float(0.0) for i in range(0, len(cpu_info1)): minus = float(cpu_info1[i]) - float(cpu_info0[i]) total = total + minus ret[i] = minus for i in range(0, len(ret)): ret[i] = ret[i] * 100 / total return CPUInfo(*ret)
def get_cpu_usage(intvl_in_sec=1): """ get cpu usage statistics during a time period (intvl_in_sec), return a namedtuple CPUInfo """ decorators.needlinux(True) unittest.TestCase.assertGreater(intvl_in_sec, 0) ret = [] for i in range(0, len(_CPU_COLUMNS)): ret.append(0) cpu_info0 = _get_cput_by_stat() time.sleep(intvl_in_sec) cpu_info1 = _get_cput_by_stat() total = float(0.0) for i in range(0, len(cpu_info1)): minus = float(cpu_info1[i]) - float(cpu_info0[i]) total = total + minus ret[i] = minus for i in range(0, len(ret)): ret[i] = ret[i] * 100 / total return CPUInfo(*ret)
def get_cpu_nums(): """ get cpu nums """ decorators.needlinux(True) try: return os.sysconf("SC_NPROCESSORS_ONLN") except ValueError: # as a second fallback we try to parse /proc/cpuinfo num = 0 fp = open('/proc/cpuinfo', 'r') try: lines = fp.readlines() finally: fp.close() for line in lines: if line.lower().startswith('processor'): num += 1 # unknown format (e.g. amrel/sparc architectures), see: # http://code.google.com/p/psutil/issues/detail?id=200 # try to parse /proc/stat as a last resort if num == 0: fp = open('/proc/stat', 'r') try: lines = fp.readlines() finally: fp.close() search = re.compile(r'cpu\d') for line in lines: line = line.split(' ')[0] if search.match(line): num += 1 if num == 0: raise RuntimeError("couldn't determine platform's NUM_CPUS") return num
def pids(): """Returns a list of PIDs currently running on the system.""" decorators.needlinux(True) return [int(x) for x in os.listdir(b'/proc') if x.isdigit()]