Make monitor commands executable
[integration/test.git] / tools / clustering / cluster-monitor / timed_isolation.py
1 #!/usr/bin/python
2 """
3 Controller Isolation Tool
4 Author: Phillip Shea
5 Updated: 2015-May-07
6
7 This tool isolates an indicated controller for a specified duration.
8 The tool's first integer argument corresponds to the number of a controller
9 in a json file's ordered list of controllers. This is the controller to
10 be isolated. The second argument is the duration of isolation in
11 seconds.
12
13 A file named 'cluster.json' containing a list of the IP addresses and
14 credentials of the controllers is required. It resides in the same
15 directory as monitor.py.
16
17 The file should look like this:
18
19     {
20         "cluster": {
21             "controllers": [
22                 {"ip": "172.17.10.93", "port": "8181"},
23                 {"ip": "172.17.10.93", "port": "8181"},
24                 {"ip": "172.17.10.93", "port": "8181"}
25             ],
26             "user": "username",
27             "pass": "password",
28         }
29     }
30
31 Usage:python timed_isolation.py [controller to be isolated]  [duration of isolation in seconds]
32 """
33
34 import sys
35 import time
36
37
38 def import_utility_modules():
39     global UtilLibrary, json
40     import sys
41     sys.path.append('../../../csit/libraries')
42     import UtilLibrary
43     import json
44
45
46 import_utility_modules()
47
48 try:
49     with open('cluster.json') as cluster_file:
50         data = json.load(cluster_file)
51 except:
52     print str(sys.exc_info())
53     print 'unable to open the file cluster.json'
54     exit(1)
55 try:
56     cluster_list = data["cluster"]["controllers"]
57     cluster_ips = []
58     for controller in cluster_list:
59         cluster_ips.append(controller["ip"])
60     user_name = data["cluster"]["user"]
61     user_pass = data["cluster"]["pass"]
62 except:
63     print str(sys.exc_info())
64     print 'Error reading the file cluster.json'
65     exit(1)
66 try:
67     isolate = int(sys.argv[1])
68     duration = int(sys.argv[2])
69 except:
70     print 'You must specify the number (e.g. 1, 2, 3) of the controller to isolate.'
71     exit(1)
72
73 print 'Isolating controller ' + str(isolate)
74
75 print UtilLibrary.isolate_controller(cluster_ips, user_name, user_pass, isolate)
76
77 print 'Pausing for ' + str(duration) + ' seconds...'
78 time.sleep(duration)
79
80 print UtilLibrary.flush_iptables(cluster_ips, user_name, user_pass)