This repository has been archived by the owner on May 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fog_agent.py
187 lines (145 loc) · 5.8 KB
/
fog_agent.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
import logging, argparse, netifaces, sys
import re
import subprocess
from threading import Thread
import pingparsing
from properties.tc_config import *
from properties.firewall import *
from properties.test_property import *
from fog_api import create_app
from queue import Queue
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
# List of all known Properties
_known_properties=[
TestProperty("TestProperty")
]
class FogAgent(object):
ping_running = False
def __init__(self, hostname, iface, port, properties):
self.hostname = hostname
self.api_iface = iface
self.api_port = port
self.known_properties = properties
self.rtt_dict = {}
def property_running(self, property):
return property.status()
def running_properties(self):
running = []
for property in self.known_properties:
if self.property_running(property):
running.append(property)
return running
def start_property(self, property, parameter):
if property.start(parameter):
logging.info("Property %s started " % (property.name))
return True
else:
logging.error("Property %s not started" % (property.name))
return False
def stop_property(self, property):
if not self.property_running(property):
logging.info("Property %s is not running" % (property.name))
return True
property.stop()
if not self.property_running(property):
logging.info("Stopped property %s" % (property.name) )
return True
else:
logging.error("Couldn't stop property %s - still running" % (property.name))
return False
def update_property(self, property, parameter):
# if not self.property_running(property):
# logging.info("Property %s is not running" % (property.name))
# return False
if property.update(parameter):
logging.info("Updated property %s" % (property.name) )
return True
else:
logging.error("Couldn't update property %s - still running" % (property.name))
return False
def get_property(self, name):
for prop in self.known_properties:
if prop.name == name:
return prop
return None
def ping_hosts(self, num_of_threads, host_list, seconds):
hosts = Queue()
results = Queue()
self.ping_running = True
for i in range (num_of_threads):
worker = Thread(target=self.ping_thread, args=(i, hosts, seconds, results))
worker.setDaemon(True)
worker.start()
for host in host_list:
hosts.put(host)
hosts.join()
while not results.empty():
self.update_rtt_dict(results.get())
logging.info("Ping ended")
self.ping_running = False
def ping_thread(self, thread, hosts, seconds, results):
while True:
host = hosts.get()
logging.info(host)
hostname = host['hostname']
ip = host['ip']
logging.info("Thread %s: Starting Ping to %s at %s for %s seconds" % (thread, hostname, ip, seconds))
cmd = ['ping', '-w', seconds, ip]
output = subprocess.Popen(cmd, stdout=subprocess.PIPE)
parser = pingparsing.PingParsing()
stats = parser.parse(output.stdout.read())
tmp = {
hostname: stats.as_dict(),
}
results.put(tmp)
hosts.task_done()
def update_rtt_dict(self, host):
hostname = list(host)[0]
logging.info("Adding RTT statistics for host %s" % (hostname))
self.rtt_dict.update(host)
def get_rtt_host(self, host):
return self.rtt_dict.get(host)
def get_all_rtts(self):
return self.rtt_dict
def get_ip(iface):
try:
if netifaces.ifaddresses(iface):
ip = netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr']
logging.info("Using %s as API IP" % (ip))
return ip
except ValueError:
logging.error("Interface %s not known" % (iface))
def fog_agent_main(args):
parser = argparse.ArgumentParser()
parser.add_argument("-hostname", dest='hostname', required=True, help="Hostname of the host")
parser.add_argument("-iface", dest='iface', required=True, help="API Interface")
parser.add_argument("-port", dest='port', required=True, help="API Port")
# get arguments
args = parser.parse_args()
# check if API interfaces exists and get IP
api_ip = get_ip(args.iface)
properties = _known_properties
if api_ip is not None:
logging.info("Starting agent on Host %s at %s:%s" % (args.hostname, api_ip, args.port))
agent = FogAgent(args.hostname, api_ip, args.port, properties)
logging.info("Started")
# initialize tc-config and firewall property
ifaces = netifaces.interfaces()
iface_candidates = []
for face in ifaces:
if (face != args.iface) and (face != 'lo') and (''.join([i for i in face if not i.isdigit()])==''.join([i for i in args.iface if not i.isdigit()])):
iface_candidates.append(face)
if len(iface_candidates)==1:
tc_iface = iface_candidates[0]
else:
tc_iface = args.iface
logging.info(f"Initializing TC on the {tc_iface} network interface.")
properties.append(Tc_config('TC', tc_iface))
properties.append(Firewall('Firewall', tc_iface))
logging.info("Known properties: %s" % [p.name for p in properties])
# start api
api = create_app(agent)
api.run(host=api_ip, port=args.port, threaded=True)
if __name__ == "__main__":
fog_agent_main(sys.argv[1:])