summaryrefslogtreecommitdiffstats
path: root/tests/utils/pidof.py
blob: 4b7071c0a484f7200a8f4dee8882f0f97051dc7b (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

from __future__ import print_function
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)