Skip to content

Commit

Permalink
add script to print process' AD
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Feb 24, 2019
1 parent 1a0c044 commit c35d4b0
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
80 changes: 80 additions & 0 deletions scripts/internal/procs_access_denied.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit c35d4b0

Please sign in to comment.