Created
May 3, 2012 20:04
-
-
Save danielrichman/2588864 to your computer and use it in GitHub Desktop.
cronjob to check for processes still using old (deleted) versions of dynamic libraries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Copyright 2012 Daniel Richman | |
import os | |
import os.path | |
import pwd | |
import traceback | |
def proc_maps(): | |
for subdir in os.listdir("/proc"): | |
try: | |
assert subdir == str(int(subdir)) | |
yield subdir, os.path.join("/proc", subdir, "maps") | |
except: | |
pass # not a pid | |
_username_cache = {} | |
def username(uid): | |
global _username_cache | |
if uid not in _username_cache: | |
_username_cache[uid] = pwd.getpwuid(uid).pw_name | |
return _username_cache[uid] | |
def proc_info(pid): | |
with open(os.path.join("/proc", pid, "status")) as f: | |
for line in f: | |
line = line.split() | |
if line[0] == "Name:": | |
name = ' '.join(line[1:]) | |
if len(line) != 2: | |
name = '(?)' + name | |
elif line[0] == "Uid:": | |
real_uid = int(line[2]) | |
user = username(real_uid) | |
return (name, user) | |
def parse_maps(map_name): | |
deleted = set() | |
with open(map_name) as f: | |
for line in f: | |
parts = line.split() | |
if parts[-1] == "(deleted)": | |
filename = ' '.join(parts[5:-1]) | |
if len(parts) != 7: | |
filename = '(?)' + name | |
for m in [".so", "/bin/", "/sbin/", "/lib"]: | |
if m in filename: | |
deleted.add(os.path.basename(filename)) | |
break | |
return list(deleted) | |
def main(): | |
header = False | |
err_pids = [] | |
for (p, m) in proc_maps(): | |
try: | |
fl = ' '.join(parse_maps(m)) | |
if fl: | |
(name, username) = proc_info(p) | |
if not header: | |
print "The following bins/libs are out of date" | |
header = True | |
print name, username, fl | |
except: | |
err_pids.append(p) | |
if err_pids: | |
print "Couldn't inspect", len(err_pids), "processes" | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment