def worker_status(): worker_id = request.args.get("workerid") num_datapoints = request.args.get("datapoints") if not worker_id or not num_datapoints: return "Must provide workerid and datapoints", 400 return { "worker_data": get_worker_status_data(worker_id, num_datapoints), "worker": get_worker(worker_id), }
def initiate_capture(ip, protocol, port, count): monitor = CaptureManager.find_monitor(ip) worker = get_worker(host=monitor, worker_type=CaptureManager.worker_type) if worker is None: log_console( f"Capture Manager: could not find worker, host={monitor}, worker_type={CaptureManager.worker_type} in DB" ) return if ( protocol ): # Translate port and protocol if necessary, e.g. 'http' must become 'tcp', '80' protocol, port = CaptureManager.translate_protocol_and_port( protocol, port) capture_info = { "intuitiveNMS": get_this_ip(), "interface": interface, "ip": ip, "protocol": protocol, "port": port, "count": count, } capture_info_json = json.dumps(capture_info) if worker["connection_type"] == "rabbitmq": channel = CaptureManager.get_channel(monitor) channel.basic_publish(exchange="", routing_key="capture_queue", body=capture_info_json) log_console( f"Capture Manager: starting capture: ip:{ip} protocol:{protocol} port:{port} count:{count}" ) elif worker["connection_type"] == "http": command = dict() command["host"] = worker["host"] command["serial"] = worker["serial"] command["worker_type"] = CaptureManager.worker_type command["command"] = "start-capture" command["command_info"] = capture_info_json command["delivered"] = False set_command(command)
def worker_heartbeat(): heartbeat_info = request.get_json() if not heartbeat_info: return "Must provide heartbeat information in JSON body", 400 if "serial" not in heartbeat_info: return "Must provide 'serial' in heartbeat information", 400 worker = get_worker(serial=heartbeat_info["serial"], worker_type=heartbeat_info["worker_type"]) if worker is None: return "Unknown worker serial number in heartbeat information", 400 worker["availability"] = True worker["last_heard"] = str(datetime.now())[:-3] if "response_time" in heartbeat_info: worker["response_time"] = heartbeat_info["response_time"] if "cpu" in heartbeat_info: worker["cpu"] = heartbeat_info["cpu"] if "memory" in heartbeat_info: worker["memory"] = heartbeat_info["memory"] if "uptime" in heartbeat_info: worker["uptime"] = heartbeat_info["uptime"] record_worker_status(worker) set_worker(worker) log_console( f"Received heartbeat from {heartbeat_info['name']}, info={heartbeat_info}" ) result, commands = get_commands( serial=heartbeat_info["serial"], worker_type=heartbeat_info["worker_type"], set_delivered=True, ) if result != "success": log_console( f"Failed to retrieve commands for {heartbeat_info['serial']}") else: log_console( f"Delivered commands to {heartbeat_info['serial']}, commands={commands}" ) return {"commands": commands}, 200
def initiate_traceroute(target, token): # Target could be a URL; if so, use urlparse to extract the network location (hostname) if target.startswith("http://") or target.startswith("https://"): parsed_target = urlparse(target) target = parsed_target.netloc monitor = TracerouteManager.find_monitor(target) worker = get_worker(host=monitor, worker_type=TracerouteManager.worker_type) if worker is None: log_console( f"Traceroute Manager: could not find worker, host={monitor}, worker_type={TracerouteManager.worker_type} in DB" ) return traceroute_info = { "intuitiveNMS": get_this_ip(), "target": target, "token": token, } traceroute_info_json = json.dumps(traceroute_info) if worker["connection_type"] == "rabbitmq": channel = TracerouteManager.get_channel(monitor) channel.basic_publish( exchange="", routing_key="traceroute_queue", body=traceroute_info_json ) log_console(f"Traceroute Manager: starting traceroute: target : {target}") elif worker["connection_type"] == "http": command = dict() command["host"] = worker["host"] command["serial"] = worker["serial"] command["worker_type"] = TracerouteManager.worker_type command["command"] = "start-capture" command["command_info"] = traceroute_info_json command["delivered"] = False set_command(command)
def worker_register(): registration_info = request.get_json() if not registration_info: return "Must provide registration information in JSON body", 400 if "serial" not in registration_info: return "Must provide 'serial' in registration information", 400 if "name" not in registration_info: return "Must provide 'name' in registration information", 400 worker = get_worker(host=registration_info["name"], worker_type=registration_info["worker_type"]) if worker is None: return "Unknown worker name in registration information", 400 if registration_info["serial"] != worker["serial"]: return "Serial number in registration information does not match worker serial", 400 log_console( f"Received registration request from {registration_info['name']}, serial no: {registration_info['serial']}" ) worker["availability"] = True worker["last_heard"] = str(datetime.now())[:-3] set_worker(worker) return {}, 200