def test_get_info_still_works_when_denied_access(self): def access_denied(): return mock.MagicMock(side_effect=util.AccessDenied) class WorkerMock(mock.MagicMock): def __getattr__(self, attr): raise util.AccessDenied() worker = WorkerMock() worker.get_memory_info = access_denied() worker.get_cpu_percent = access_denied() worker.get_cpu_times = access_denied() worker.get_nice = access_denied() worker.get_memory_percent = access_denied() worker.cmdline = [] info = get_info(worker) self.assertEquals(info["mem"], "N/A") self.assertEquals(info["cpu"], "N/A") self.assertEquals(info["ctime"], "N/A") self.assertEquals(info["pid"], "N/A") self.assertEquals(info["username"], "N/A") self.assertEquals(info["nice"], "N/A") self.assertEquals(info["create_time"], "N/A") self.assertEquals(info["age"], "N/A") worker.get_nice = mock.MagicMock(side_effect=util.NoSuchProcess(1234)) self.assertEquals(get_info(worker)["nice"], "Zombie")
def info(self): """Return process info. The info returned is a mapping with these keys: - **mem_info1**: Resident Set Size Memory in bytes (RSS) - **mem_info2**: Virtual Memory Size in bytes (VMS). - **cpu**: % of cpu usage. - **mem**: % of memory usage. - **ctime**: process CPU (user + system) time in seconds. - **pid**: process id. - **username**: user name that owns the process. - **nice**: process niceness (between -20 and 20) - **cmdline**: the command line the process was run with. """ try: info = get_info(self._worker) except NoSuchProcess: return "No such process (stopped?)" info["age"] = self.age() info["started"] = self.started info["children"] = [] info['wid'] = self.wid for child in self._worker.get_children(): info["children"].append(get_info(child)) return info
def test_get_info_still_works_when_denied_access(self): def access_denied(): return mock.MagicMock(side_effect=util.AccessDenied) class WorkerMock(mock.MagicMock): def __getattr__(self, attr): # this attr is accessed during the initialization # of the MagicMock, so we cannot raise here if attr == "_mock_methods": return None raise util.AccessDenied() worker = WorkerMock() worker.get_memory_info = access_denied() worker.get_cpu_percent = access_denied() worker.get_cpu_times = access_denied() worker.get_nice = access_denied() worker.get_memory_percent = access_denied() worker.cmdline = [] info = get_info(worker) self.assertEqual(info['mem'], 'N/A') self.assertEqual(info['cpu'], 'N/A') self.assertEqual(info['ctime'], 'N/A') self.assertEqual(info['pid'], 'N/A') self.assertEqual(info['username'], 'N/A') self.assertEqual(info['nice'], 'N/A') self.assertEqual(info['create_time'], 'N/A') self.assertEqual(info['age'], 'N/A') worker.nice = mock.MagicMock(side_effect=util.NoSuchProcess(1234)) self.assertEqual(get_info(worker)['nice'], 'Zombie')
def test_get_info_still_works_when_denied_access(self): def access_denied(): return mock.MagicMock(side_effect=util.AccessDenied) class WorkerMock(mock.MagicMock): def __getattr__(self, attr): raise util.AccessDenied() worker = WorkerMock() worker.get_memory_info = access_denied() worker.get_cpu_percent = access_denied() worker.get_cpu_times = access_denied() worker.get_nice = access_denied() worker.get_memory_percent = access_denied() worker.cmdline = [] info = get_info(worker) self.assertEqual(info['mem'], 'N/A') self.assertEqual(info['cpu'], 'N/A') self.assertEqual(info['ctime'], 'N/A') self.assertEqual(info['pid'], 'N/A') self.assertEqual(info['username'], 'N/A') self.assertEqual(info['nice'], 'N/A') self.assertEqual(info['create_time'], 'N/A') self.assertEqual(info['age'], 'N/A') worker.nice = mock.MagicMock(side_effect=util.NoSuchProcess(1234)) self.assertEqual(get_info(worker)['nice'], 'Zombie')
def info(self): """Return process info. The info returned is a mapping with these keys: - **mem_info1**: Resident Set Size Memory in bytes (RSS) - **mem_info2**: Virtual Memory Size in bytes (VMS). - **cpu**: % of cpu usage. - **mem**: % of memory usage. - **ctime**: process CPU (user + system) time in seconds. - **pid**: process id. - **username**: user name that owns the process. - **nice**: process niceness (between -20 and 20) - **cmdline**: the command line the process was run with. """ try: info = get_info(self._worker) except NoSuchProcess: return "No such process (stopped?)" info["age"] = self.age() info["started"] = self.started info["children"] = [] for child in self._worker.get_children(): info["children"].append(get_info(child)) return info
def info(self): """ return process info """ info = _INFOLINE % get_info(self._worker) lines = ["%s: %s" % (self.wid, info)] for child in self._worker.get_children(): info = _INFOLINE % get_info(child) lines.append(" %s" % info) return "\n".join(lines)
def collect_stats(self): aggregate = {} # sending by pids for pid in self.streamer.get_pids(self.name): name = None if self.name == 'circus': if pid in self.streamer.circus_pids: name = self.streamer.circus_pids[pid] try: info = util.get_info(pid) aggregate[pid] = info info['subtopic'] = pid info['name'] = name yield info except util.NoSuchProcess: # the process is gone ! pass except Exception as e: logger.exception('Failed to get info for %d. %s' % (pid, str(e))) # now sending the aggregation yield self._aggregate(aggregate)
def test_get_info(self): worker = Popen(["python -c 'import time;time.sleep(5)'"], shell=True) try: info = get_info(worker) finally: worker.terminate() self.assertTrue(isinstance(info["pid"], int)) self.assertEqual(info["nice"], 0)
def test_get_info(self): worker = Popen(["python -c 'import time;time.sleep(5)'"], shell=True) try: info = get_info(worker) finally: worker.terminate() self.assertTrue(isinstance(info['pid'], int)) self.assertEqual(info['nice'], 0)
def test_get_info(self): worker = Popen(['top'], shell=True) try: info = get_info(worker) finally: worker.terminate() self.assertTrue(isinstance(info['pid'], int)) self.assertEqual(info['nice'], 0)
def test_get_info(self): worker = Popen(["python", "-c", SLEEP % 5]) try: info = get_info(worker) finally: worker.terminate() self.assertTrue(isinstance(info['pid'], int)) if IS_WINDOWS: self.assertEqual(info['nice'], psutil.NORMAL_PRIORITY_CLASS) else: self.assertEqual(info['nice'], 0)
def collect_stats(self, watcher, pids): aggregate = {} # sending by pids for pid in pids: try: info = util.get_info(pid) aggregate[pid] = info yield (watcher, pid, info) except util.NoSuchProcess: # the process is gone ! pass except Exception, e: logger.exception('Failed to get info for %d. %s' % (pid, str(e)))
def collect_stats(self): aggregate = {} # sending by pids for pid in self.streamer.get_pids(self.name): name = None if self.name == "circus": if pid in self.streamer.circus_pids: name = self.streamer.circus_pids[pid] try: info = util.get_info(pid) aggregate[pid] = info info["subtopic"] = pid info["name"] = name yield info except util.NoSuchProcess: # the process is gone ! pass except Exception, e: logger.exception("Failed to get info for %d. %s" % (pid, str(e)))
def run(self): self.running = True while self.running: aggregate = {} # sending by pids for name, pid in self._get_pids(): try: info = util.get_info(pid, interval=0.0) aggregate[pid] = info except util.NoSuchProcess: # the process is gone ! pass except Exception: logger.exception('Failed to get info for %d' % pid) else: self.results.put((self.name, name, pid, info)) # now sending the aggregation self.results.put( (self.name, None, None, self._aggregate(aggregate))) # sleep for accuracy time.sleep(self.interval)
def run(self): self.running = True while self.running: aggregate = {} # sending by pids for name, pid in self._get_pids(): try: info = util.get_info(pid, interval=0.0) aggregate[pid] = info self.results.put((self.name, name, pid, info)) except util.NoSuchProcess: # the process is gone ! pass except Exception, e: logger.exception('Failed to get info for %d. %s' % (pid, str(e))) # now sending the aggregation self.results.put((self.name, None, None, self._aggregate(aggregate))) # sleep for accuracy time.sleep(self.interval)
def execute(self, arbiter, props): return {'info': get_info(interval=0.01)}