Exemple #1
0
def device_info():

    if request.method == "GET":

        device_name = request.args.get("device")
        requested_info = request.args.get("info")
        live = request.args.get("live")

        if not device_name or not requested_info:
            return "Must provide device and info", 400
        if not live:
            get_live_info = False
        else:
            if live.lower() not in {"true", "false"}:
                return "Value of 'live', if specified, must be 'true' or 'false'"
            else:
                get_live_info = bool(live)

        status, result_info = get_device_info(
            device_name, requested_info, get_live_info
        )
        if status == "success":
            return result_info, 200
        else:
            return result_info, 406
Exemple #2
0
    def monitor(self, interval):

        while True and not self.terminate:

            devices = get_all_devices()
            print(f"Monitor: Beginning compliance monitoring for {len(devices)} devices")
            for device in devices:

                if self.terminate:
                    break

                print(f"--- monitor:compliance get environment {device['name']}")
                try:
                    result, env = get_device_info(device["name"], "environment")
                except BaseException as e:
                    print(f"!!! Exception in monitoring compliance: {repr(e)}")
                    continue

                if result != "success":
                    device["availability"] = False

                else:
                    device["os_compliance"] = check_os_compliance(device)
                    device["config_compliance"] = check_config_compliance(device)
                    device["last_compliance_check"] = str(datetime.now())[:-3]

                set_device(device)

            for _ in range(0, int(interval / 10)):
                sleep(10)
                if self.terminate:
                    break

        print("...gracefully exiting monitor:compliance")
Exemple #3
0
def check_config_compliance(device):

    standard = Compliance.query.filter_by(**{"vendor": device["vendor"], "os": device["os"]}).one_or_none()
    if standard is None:
        print(f"!!! Error retrieving compliance record for this device {device['name']}")
        return False

    standard_filename = "quokka/data/" + standard.standard_config_file

    result, config = get_device_info(device["name"], "config")
    if result != "success" or "config" not in config or "running" not in config["config"]:
        print(f"!!! Error retrieving running config for this device {device['name']}")
        return False

    config_running = config["config"]["running"]

    try:
        # standard_filename = "quokka/data/" + device["vendor"] + "." + device["os"] + "." + "standard.config"
        with open(standard_filename, "r") as config_out:
            config_standard = config_out.read()
    except (FileExistsError, FileNotFoundError) as e:
        print(f"!!! Error retrieving compliance standard file {standard_filename} for device {device['name']}")
        return False

    if config_running != config_standard:
        with open(standard_filename.replace("standard", "running" + "." + device["name"]), "w") as config_out:
            config_out.write(config_running)
        return False

    return True
Exemple #4
0
    def monitor(self, interval):

        while True and not self.terminate:

            devices = get_all_devices()
            print(f"Monitor: Beginning monitoring for {len(devices)} devices")
            for device in devices:

                try:
                    ip_address = socket.gethostbyname(device["ssh_hostname"])
                except (socket.error, socket.gaierror) as e:
                    print(
                        f"!!! Caught socket error {repr(e)}, continuing to next device"
                    )
                    ip_address = None

                if self.terminate:
                    break

                print(f"--- monitor:device get environment {device['name']}")
                time_start = time.time()
                try:
                    result, env = get_device_info(device["name"],
                                                  "environment")
                    response_time = time.time() - time_start

                except BaseException as e:
                    print(f"!!! Exception in monitoring device: {repr(e)}")
                    result = "failed"

                if result != "success":
                    device["availability"] = False
                    device["response_time"] = None
                    device["cpu"] = None
                    device["memory"] = None

                else:
                    if ip_address:
                        device["ip_address"] = ip_address

                    device["availability"] = True
                    device["response_time"] = int(response_time * 1000)
                    device["last_heard"] = str(datetime.now())[:-3]

                    device["cpu"] = calculate_cpu(env["environment"]["cpu"])
                    device["memory"] = calculate_memory(
                        env["environment"]["memory"])

                record_device_status(device)
                set_device(device)

            for _ in range(0, int(interval / 10)):
                sleep(10)
                if self.terminate:
                    break

        print("...gracefully exiting monitor:device")
Exemple #5
0
def check_os_compliance(device):

    standard = Compliance.query.filter_by(**{"vendor": device["vendor"], "os": device["os"]}).one_or_none()
    if standard is None:
        print(f"!!! Error retrieving compliance record for this device {device['name']}")
        return False

    try:
        result, facts = get_device_info(device["name"], "facts", get_live_info=True)
    except BaseException as e:
        print(f"!!! Exception getting device info in compliance monitoring for {device['name']}")
        result = "failed"

    if result == "success":
        if standard.standard_version == facts["facts"]["os_version"]:
            return True
        else:
            return False  # Just a normal incorrect version
    else:
        print(f"!!! Error retrieving version info for this device {device['name']}")
        return False
Exemple #6
0
import_compliance(filename="compliance.yaml")
import_services(filename="services.yaml")
Host.query.delete()

# Reset time-series data
from quokka.models.DeviceStatusTS import DeviceStatusTS
from quokka.models.HostStatusTS import HostStatusTS
from quokka.models.ServiceStatusTS import ServiceStatusTS
DeviceStatusTS.query.delete()
HostStatusTS.query.delete()
ServiceStatusTS.query.delete()

# Pre-populate the DB with device facts
devices = get_all_devices()
for device in devices:
    result, facts = get_device_info(device_name=device["name"],
                                    requested_info="facts")
    if result == "success":
        set_facts(device, facts)

from quokka.controller.DiscoverTask import DiscoverTask
discover_task = DiscoverTask()
discover_thread = threading.Thread(target=discover_task.discover,
                                   args=(3600, ))
discover_thread.start()

from quokka.controller.HostMonitorTask import HostMonitorTask
host_monitor_task = HostMonitorTask()
host_monitor_thread = threading.Thread(target=host_monitor_task.monitor,
                                       args=(60, ))
host_monitor_thread.start()