summaryrefslogtreecommitdiffstats
path: root/tests/utils/pidof.py
blob: 01f2f0ad5bd66138a003da846139d7de4906737c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/python2

import sys

try:
    import psutil
except ImportError:
    print("Please install psutil --> pip install psutil")
    sys.exit(1)

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
    try:
        pidof(argv[1])
    except Exception as err:
        print err
        sys.stderr.write("Please be root - %s\n" % err);
        sys.exit(1)

if __name__ == "__main__":
    main(sys.argv)