-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
started implementation of traceroute
- Loading branch information
1 parent
3f6ff23
commit 8df666a
Showing
9 changed files
with
223 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import pika | ||
import json | ||
import yaml | ||
from quokka.controller.utils import log_console | ||
|
||
|
||
class TracerouteManager: | ||
|
||
try: | ||
with open("quokka/data/" + "traceroutemonitors.yaml", "r") as import_file: | ||
traceroute_monitors = yaml.safe_load(import_file.read()) | ||
except FileNotFoundError as e: | ||
log_console(f"Could not import traceroute monitors file: {repr(e)}") | ||
traceroute_monitors["0.0.0.0/0"] = "localhost" | ||
|
||
@staticmethod | ||
def get_channel(monitor): | ||
|
||
connection = pika.BlockingConnection(pika.ConnectionParameters(host=monitor)) | ||
channel = connection.channel() | ||
channel.queue_declare(queue="traceroute_queue", durable=True) | ||
|
||
return channel | ||
|
||
@staticmethod | ||
def find_monitor(hostname=None): | ||
|
||
if not hostname: | ||
return "localhost" | ||
|
||
if hostname in TracerouteManager.traceroute_monitors: | ||
return TracerouteManager.traceroute_monitors[hostname] | ||
|
||
else: | ||
# If we didn't find a specific monitor, default to localhost | ||
return "localhost" | ||
|
||
@staticmethod | ||
def initiate_traceroute(hostname, token): | ||
|
||
monitor = TracerouteManager.find_monitor(hostname) | ||
channel = TracerouteManager.get_channel(monitor) | ||
|
||
traceroute_info = { | ||
"hostname": hostname, | ||
"token": token, | ||
} | ||
|
||
traceroute_info_json = json.dumps(traceroute_info) | ||
channel.basic_publish( | ||
exchange="", routing_key="traceroute_queue", body=traceroute_info_json | ||
) | ||
|
||
log_console( | ||
f"Traceroute Manager: starting traceroute: hostname : {hostname}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
cfs eth distribute | ||
feature vpc | ||
feature telemetry | ||
vlan 1-9,100-105 | ||
vpc domain 10 | ||
telemetry | ||
sensor-group 1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0.0.0.0/0 : localhost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import base64 | ||
from datetime import datetime | ||
from socket import gethostname | ||
from threading import Thread | ||
|
||
from scapy.layers.inet import traceroute | ||
from util import send_traceroute | ||
|
||
|
||
class TracerouteThread(Thread): | ||
|
||
def __init__(self, quokka_ip, serial_no, traceroute_info): | ||
super().__init__() | ||
print(f"TracerouteThread: initializing thread object: traceroute_info={traceroute_info}") | ||
|
||
if "hostname" not in traceroute_info: | ||
print(f"TracerouteThread: missing information in traceroute_info: {traceroute_info}") | ||
return | ||
|
||
self.quokka_ip = quokka_ip | ||
self.serial_no = serial_no | ||
self.hostname = traceroute_info["hostname"] | ||
self.token = traceroute_info["token"] | ||
|
||
def process_traceroute(self, traceroute): | ||
|
||
print(f"TracerouteThread: generating traceroute image: {traceroute}") | ||
tmp_png = 'tmp-traceroute-graph.png' | ||
traceroute.graph(format='png', target=tmp_png) | ||
with open(tmp_png, 'rb') as png_file: | ||
traceroute_graph_bytes = base64.b64encode(png_file.read()) | ||
|
||
print(f"TracerouteThread: sending traceroute: {traceroute_graph_bytes}") | ||
status_code = send_traceroute( | ||
gethostname(), | ||
self.quokka_ip, | ||
self.serial_no, | ||
self.hostname, | ||
self.token, | ||
str(datetime.now())[:-1], | ||
traceroute_graph_bytes, | ||
) | ||
print(f"TracerouteThread: traceroute sent, result={status_code}\n") | ||
|
||
def run(self): | ||
|
||
print(f"TracerouteThread: starting traceroute: hostname = {self.hostname}") | ||
|
||
traceroute_output = traceroute(self.hostname, verbose=0) | ||
self.process_traceroute(traceroute_output[0]) | ||
|
||
print(f"\n\n-----> TracerouteThread: competed traceroute") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# ---- Worker application to traceroute packets -------------------------------- | ||
|
||
import pika | ||
import json | ||
from TracerouteThread import TracerouteThread | ||
|
||
quokka_ip = "localhost" | ||
serial_no = "111.111.111" | ||
|
||
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) | ||
channel = connection.channel() | ||
|
||
channel.queue_declare(queue='traceroute_queue', durable=True) | ||
print('\n\n [*] Traceroute Worker: waiting for messages.') | ||
|
||
|
||
def receive_traceroute_request(traceroute_channel, method, properties, body): | ||
|
||
print(f"traceroute worker: received message") | ||
traceroute_info = json.loads(body) | ||
print(f"traceroute info: {traceroute_info}") | ||
|
||
channel.basic_ack(delivery_tag=method.delivery_tag) | ||
|
||
traceroute_thread = TracerouteThread(quokka_ip, serial_no, traceroute_info) | ||
traceroute_thread.start() | ||
|
||
print('\n\n [*] Traceroute Worker: waiting for messages.') | ||
|
||
|
||
channel.basic_qos(prefetch_count=1) | ||
channel.basic_consume(on_message_callback=receive_traceroute_request, queue='traceroute_queue') | ||
|
||
try: | ||
channel.start_consuming() | ||
|
||
except KeyboardInterrupt as e: | ||
print("\nTraceroute worker shutting down") | ||
connection.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters