Created
August 19, 2014 21:29
-
-
Save harshavardhana/51fa95871330f4891d5b to your computer and use it in GitHub Desktop.
Write pidof equivalent using psutil - specifically for non Linux distributions and for GlusterFS
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/env python | |
import psutil | |
import sys | |
def pmap_find(p, name): | |
for m in p.memory_maps(grouped=True): | |
if m.path.endswith("%s.so" % name): | |
return True | |
continue | |
return False | |
def pidof(processname): | |
for p in psutil.process_iter(): | |
if p.pid == 0: | |
continue | |
if "gluster" in processname: | |
if processname == "glusterd" and pmap_find(p, "glusterd"): | |
print (p.pid) | |
if processname == "glusterfs" and pmap_find(p, "client"): | |
print (p.pid) | |
if processname == "glusterfsd" and pmap_find(p, "posix-acl"): | |
print (p.pid) | |
continue | |
if processname.strip() == p.name(): | |
print (p.pid) | |
def main(argv): | |
if len(argv) < 2: | |
sys.stderr.write("Usage: %s <processname>\n" % (argv[0],)) | |
return 1 | |
pidof(argv[1]) | |
if __name__ == "__main__": | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment