This repository has been archived by the owner on Jul 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode_Utilization_Manager.py
76 lines (62 loc) · 2.07 KB
/
Node_Utilization_Manager.py
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
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/python
#
# Node_Utilization_Manager.py
# 11/14/12
# Daemon that uses a utilization schedule to control the CPU utilization
# of servers inside of BlueCenter
#
# author: Joshua Ferguson
import csv # Read CSV files
import os # Lets us issue bash commands
import sys # Get command line arguments
from datetime import datetime # Naturally, lets us get the current time
import time
import subprocess
def start_processes(number_of_processes, command_args):
process_list = list()
for i in range(number_of_processes):
process_list.append(subprocess.Popen(command_args))
return process_list
def start_cpu_limit(util_procs, limit):
cpu_limit_procs = list()
for process in util_procs:
cpu_limit_args = ['/usr/bin/cpulimit', '-p', str(process.pid), '-l', str(limit)]
cpu_limit_procs.append(subprocess.Popen(cpu_limit_args))
return cpu_limit_procs
def kill_processes(process_list):
for process in process_list:
os.system("kill -9 " + str(process.pid))
def main():
# Need to switch from 'with' to try/except at some point
with open(sys.argv[1], 'rb') as schedule_file:
schedule_reader = csv.reader(schedule_file)
#
# Start our utilization processes and store them in util_procs
#
cwd = os.getcwd()
util_args = [(cwd+'/util')]
util_procs = start_processes(4, util_args)
#
# Start the initial cpu_limit processes at 100 limit
#
current_cpu_limit_procs = start_cpu_limit(util_procs, 100)
while True:
current_time = int(datetime.now().strftime('%s'))
current_row = schedule_reader.next()
if current_row[2] == "0":
os.system("poweroff now")
sleep_time_diff = (int(current_row[1]) - current_time)
#
# Set Utilization
#
new_cpu_limit_procs = start_cpu_limit(util_procs,int(current_row[2]))
kill_processes(current_cpu_limit_procs)
current_cpu_limit_procs = new_cpu_limit_procs
#
# Sleep until next time window
#
print "Current Time Window: " + str(current_row) + "; sleep time: " + str(sleep_time_diff)
time.sleep(sleep_time_diff)
if __name__ == "__main__":
main()