summaryrefslogtreecommitdiff
path: root/clusterman/commands/node.py
blob: eb32d50d8c963023918980d8063df1b033112167 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os, json
from clusterman.config import CONF


def ls():
    nodes_path=CONF["paths"]["nodes"]
    nodes=None
    if os.path.exists(nodes_path):
        with open(nodes_path) as f:
            nodes=json.load(f)
    else:
        print("Please perform a scan before")
        exit(0)
    for node in nodes:
        print(node)


def scan(timeout):
    from_split=[int(n) for n in CONF["cluster"]["ip4_from"].split(".")]
    to_split=[int(n) for n in CONF["cluster"]["ip4_to"].split(".")]
    ignore_list=[ip.strip(" ") for ip in CONF["cluster"]["ip4_ignore"]]
    nodes=list()
    print("----- Starting node scan (timeout={}s) -----".format(timeout))
    for W in range(from_split[0],to_split[0]+1):
        for X in range(from_split[1],to_split[1]+1):
            for Y in range(from_split[2],to_split[2]+1):
                for Z in range(from_split[3],to_split[3]+1):
                    ip="{}.{}.{}.{}".format(W,X,Y,Z)
                    if ip in ignore_list:
                        print("Skipping {}".format(ip))
                        continue
                    response = os.system("ping -c 1 -W " + str(timeout)+ " " + ip + " &>/dev/null")
                    print("Contacting {}...".format(ip),end='')
                    if response==0:
                        nodes.append(ip)
                        print("=> Found!!")
                    else:
                        print("")
    with open(CONF["paths"]["nodes"], "w") as f:
        f.write(json.dumps(nodes))

def check(timeout):
    nodes_path=CONF["paths"]["nodes"]
    nodes=None
    if os.path.exists(nodes_path):
        with open(nodes_path) as f:
            nodes=json.load(f)
    else:
        print("Please perform a scan before")
        exit(0)
    if not nodes == None:
        fail=False
        for ip in nodes:
            print("Contacting {}...".format(ip),end='')
            response = os.system("ping -c 1 -W " + str(timeout)+ " " + ip + " &>/dev/null")
            if response == 0:
                print("")
            else:
                fail=True
                print("=> Not responding!!")
    
        if not fail:
            print("Success: All nodes are reachable")
        else:
            print("Error: Some of your nodes are not reachable")