def get_commands(serial=None, host=None, worker_type=None, set_delivered=False): if not serial and not host: return "failed", "Must provide serial or host" if not worker_type: return "failed", "must provide worker_type" search = dict() if serial: search["serial"] = serial if host: search["host"] = host search["worker_type"] = worker_type search["delivered"] = False command_objs = db.session.query(Command).filter_by(**search).all() commands = list() for command_obj in command_objs: commands.append(get_model_as_dict(command_obj)) if set_delivered: for command_obj in command_objs: command_obj.delivered = True db.session.commit() return "success", commands
def get_service(service_id): search = {"id": service_id} service_obj = db.session.query(Service).filter_by(**search).one_or_none() if not service_obj: return None else: return get_model_as_dict(service_obj)
def get_host(host_id): search = {"id": host_id} host_obj = db.session.query(Host).filter_by(**search).one_or_none() if not host_obj: return None else: return get_model_as_dict(host_obj)
def get_all_workers(): worker_objs = db.session.query(Worker).all() workers = list() for worker_obj in worker_objs: workers.append(get_model_as_dict(worker_obj)) return workers
def get_facts(device_name): facts_obj = DeviceFacts.query.filter_by(**{ "device_name": device_name }).one_or_none() if not facts_obj: return "failed", "Could not find device facts in DB" return "success", get_model_as_dict(facts_obj)
def get_all_devices(): device_objs = db.session.query(Device).all() devices = list() for device_obj in device_objs: devices.append(get_model_as_dict(device_obj)) return devices
def get_all_devices(): device_objs = Device.query.all() devices = list() for device_obj in device_objs: devices.append(get_model_as_dict(device_obj)) return devices
def get_capture(ip, protocol, port, num_packets): # We must generate and implement specific queries based on what has been requested # Note that if we add more imports, we'll have to modify this simple method to handle all cases if ip and not protocol: # Note that if not protocol, port isn't relevant packet_objs = ( db.session.query(Capture) .filter(or_(Capture.ip_src == ip, Capture.ip_dst == ip)) .order_by(desc(Capture.timestamp)) .limit(num_packets) ) elif ip and protocol and not port: packet_objs = ( db.session.query(Capture) .filter(or_(Capture.ip_src == ip, Capture.ip_dst == ip)) .filter(func.lower(Capture.protocol) == func.lower(protocol)) .order_by(desc(Capture.timestamp)) .limit(num_packets) ) elif ip and protocol and port: packet_objs = ( db.session.query(Capture) .filter(or_(Capture.ip_src == ip, Capture.ip_dst == ip)) .filter(func.lower(Capture.protocol) == func.lower(protocol)) .filter(or_(Capture.sport == port, Capture.dport == port)) .order_by(desc(Capture.timestamp)) .limit(num_packets) ) elif not ip and protocol and not port: packet_objs = ( db.session.query(Capture) .filter(func.lower(Capture.protocol) == func.lower(protocol)) .order_by(desc(Capture.timestamp)) .limit(num_packets) ) elif not ip and protocol and port: packet_objs = ( db.session.query(Capture) .filter(func.lower(Capture.protocol) == func.lower(protocol)) .filter(or_(Capture.sport == port, Capture.dport == port)) .order_by(desc(Capture.timestamp)) .limit(num_packets) ) else: # Not sure what was requested, so just get everything packet_objs = ( db.session.query(Capture) .order_by(desc(Capture.timestamp)) .limit(num_packets) ) packets = list() for packet_obj in packet_objs: packet = get_model_as_dict(packet_obj) packets.append(packet) return packets
def get_all_hosts(): host_objs = Host.query.all() hosts = list() for host_obj in host_objs: host = get_model_as_dict(host_obj) hosts.append(host) return hosts
def get_all_services(): service_objs = db.session.query(Service).all() services = list() for service_obj in service_objs: service = get_model_as_dict(service_obj) services.append(service) return services
def get_all_hosts(): host_objs = db.session.query(Host).all() hosts = list() for host_obj in host_objs: host = get_model_as_dict(host_obj) hosts.append(host) return hosts
def get_service_ts_data(service_id, num_datapoints): service_ts_objs = (ServiceStatusTS.query.filter_by(**{ "service_id": service_id }).order_by(desc(ServiceStatusTS.timestamp)).limit(num_datapoints).all()) service_ts_data = list() for service_ts_obj in service_ts_objs: service_ts_data.append(get_model_as_dict(service_ts_obj)) return service_ts_data
def get_host_ts_data(host_id, num_datapoints): host_ts_objs = (HostStatusTS.query.filter_by(**{ "host_id": host_id }).order_by(desc(HostStatusTS.timestamp)).limit(num_datapoints).all()) host_ts_data = list() for host_ts_obj in host_ts_objs: host_ts_data.append(get_model_as_dict(host_ts_obj)) return host_ts_data
def get_all_events(num_events): event_objs = (db.session.query(Event).order_by(desc( Event.time)).limit(num_events).all()) events = list() for event_obj in event_objs: event = get_model_as_dict(event_obj) events.append(event) return events
def get_worker_status_data(worker_id, num_datapoints): worker_status_objs = (db.session.query(WorkerStatus).filter_by( **{ "worker_id": worker_id }).order_by(desc(WorkerStatus.timestamp)).limit(num_datapoints).all()) worker_status_data = list() for worker_status_obj in worker_status_objs: worker_status_data.append(get_model_as_dict(worker_status_obj)) return worker_status_data
def get_host_status_data_for_hour(host_id, hour): host_status_objs = (db.session.query(HostStatus).filter_by( **{ "host_id": host_id }).filter(HostStatus.timestamp.startswith(hour)).all()) host_status_data = list() for host_status_obj in host_status_objs: host_status_data.append(get_model_as_dict(host_status_obj)) return host_status_data
def get_service_status_data_for_hour(service_id, hour): service_status_objs = (db.session.query(ServiceStatus).filter_by( **{ "service_id": service_id }).filter(ServiceStatus.timestamp.startswith(hour)).all()) service_status_data = list() for service_status_obj in service_status_objs: service_status_data.append(get_model_as_dict(service_status_obj)) return service_status_data
def get_service_summary_data(service_id, num_datapoints): service_summary_objs = (db.session.query(ServiceStatusSummary).filter_by( **{ "service_id": service_id }).order_by(desc( ServiceStatusSummary.timestamp)).limit(num_datapoints).all()) service_summary_data = list() for service_status_obj in service_summary_objs: service_summary_data.append(get_model_as_dict(service_status_obj)) return service_summary_data
def get_host_summary_data(host_id, num_datapoints): host_summary_objs = (db.session.query(HostStatusSummary).filter_by( **{ "host_id": host_id }).order_by(desc( HostStatusSummary.timestamp)).limit(num_datapoints).all()) host_summary_data = list() for host_status_obj in host_summary_objs: host_summary_data.append(get_model_as_dict(host_status_obj)) return host_summary_data
def get_device_status_data_for_hour(device_id, hour): device_status_objs = ( db.session.query(DeviceStatus) .filter_by(**{"device_id": device_id}) .filter(DeviceStatus.timestamp.startswith(hour)) .all() ) device_status_data = list() for device_status_obj in device_status_objs: device_status_data.append(get_model_as_dict(device_status_obj)) return device_status_data
def get_device_ts_data(device_name, num_datapoints): result, info = get_device(device_name=device_name) if result != "success": return result, info device_id = info["id"] device_ts_objs = (DeviceStatusTS.query.filter_by(**{ "device_id": device_id }).order_by(desc(DeviceStatusTS.timestamp)).limit(num_datapoints).all()) device_ts_data = list() for device_ts_obj in device_ts_objs: device_ts_data.append(get_model_as_dict(device_ts_obj)) return device_ts_data
def get_port_scan_extended(host_ip, host_name, token): max_wait_time = 300 # extended port scan allowed to take 5 minutes max start_time = datetime.now() while (datetime.now() - start_time).total_seconds() < max_wait_time: search = {"host_ip": host_ip, "host_name": host_name, "token": token} portscan_obj = db.session.query(Portscan).filter_by(**search).one_or_none() if not portscan_obj: time.sleep(2) continue portscan = get_model_as_dict(portscan_obj) return "success", portscan["scan_output"] return "failed", "No scan results in time provided"
def get_device(device_id=None, device_name=None): if device_id and device_name: return "failed", "Must provide either device_id or device_name, but not both" if device_id: search = {"id": device_id} elif device_name: search = {"name": device_name} else: return "failed", "Must provide either device_id or device_name" device_obj = db.session.query(Device).filter_by(**search).one_or_none() if not device_obj: return "failed", "Could not find device in DB" return "success", get_model_as_dict(device_obj)
def get_traceroute(target, token): max_wait_time = 300 # extended port scan allowed to take 5 minutes max start_time = datetime.now() while (datetime.now() - start_time).total_seconds() < max_wait_time: # search = {"target": target, "token": token} # 'target' may have been modified search = {"token": token} traceroute_obj = db.session.query(Traceroute).filter_by(**search).one_or_none() if not traceroute_obj: time.sleep(2) continue traceroute = get_model_as_dict(traceroute_obj) return "success", traceroute["traceroute_img"] return "failed", "No traceroute results in time provided"
def get_worker(worker_id=None, serial=None, host=None, worker_type=None): search = dict() if worker_id: search["id"] = worker_id else: if not serial and not host: return "failed", "Must provide serial or host" if not worker_type: return "failed", "Must provide worker type" if serial: search["serial"] = serial if host: search["host"] = host search["worker_type"] = worker_type worker_obj = db.session.query(Worker).filter_by(**search).one_or_none() if not worker_obj: return None else: return get_model_as_dict(worker_obj)