diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2019-02-24 23:11:41 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2019-02-24 23:11:41 +0100 |
commit | c35d4b0c582180bb870cc88b113674deb0c569d2 (patch) | |
tree | 9112660a6f62c88308327c1fbdca161e4cda54a1 | |
parent | 1a0c0446cbeb93b7efd15cd1512212e8ddead0c7 (diff) | |
download | psutil-c35d4b0c582180bb870cc88b113674deb0c569d2.tar.gz |
add script to print process' AD
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | scripts/internal/procs_access_denied.py | 80 |
2 files changed, 84 insertions, 0 deletions
@@ -266,5 +266,9 @@ bench-oneshot-2: ## Same as above but using perf module (supposed to be more pr check-broken-links: ## Look for broken links in source files. git ls-files | xargs $(PYTHON) -Wa scripts/internal/check_broken_links.py +print-access-denied: +# ${MAKE} install + $(TEST_PREFIX) $(PYTHON) scripts/internal/procs_access_denied.py + help: ## Display callable targets. @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' diff --git a/scripts/internal/procs_access_denied.py b/scripts/internal/procs_access_denied.py new file mode 100644 index 00000000..9f792480 --- /dev/null +++ b/scripts/internal/procs_access_denied.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python + +# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +""" +Helper script which tries to access all info of all running processes. +It prints how many AccessDenied exceptions are raised in total and +for each Process method. +""" + +from __future__ import print_function, division +from collections import defaultdict +import sys + +import psutil + + +def term_supports_colors(file=sys.stdout): + try: + import curses + assert file.isatty() + curses.setupterm() + assert curses.tigetnum("colors") > 0 + except Exception: + return False + else: + return True + + +COLORS = term_supports_colors() + + +def hilite(s, ok=True, bold=False): + """Return an highlighted version of 'string'.""" + if not COLORS: + return s + attr = [] + if ok is None: # no color + pass + elif ok: # green + attr.append('32') + else: # red + attr.append('31') + if bold: + attr.append('1') + return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), s) + + +def main(): + tot_procs = 0 + tot_ads = 0 + signaler = object() + d = defaultdict(int) + for p in psutil.process_iter(attrs=[], ad_value=signaler): + tot_procs += 1 + for methname, value in p.info.items(): + if value is signaler: + tot_ads += 1 + d[methname] += 1 + else: + d[methname] += 0 + + for methname, ads in sorted(d.items(), key=lambda x: x[1]): + perc = (ads / tot_procs) * 100 + s = "%-20s %-3s %5.1f%% " % (methname, ads, perc) + if not ads: + s += "SUCCESS" + s = hilite(s, ok=True) + else: + s += "ACCESS DENIED" + s = hilite(s, ok=False) + print(s) + print("--------------------------") + print("total: %19s (%s total processes)" % (tot_ads, tot_procs)) + + +if __name__ == '__main__': + main() |