Exemple #1
0
def get(client, dataset, pathElts, resType="gd"):
    ''' Returns a query on a path element
    resType can be gd - generator dict
                   gl - generator list
                   nd  - dict
                   nl  - list'''
    if "d" in resType.lower():
        result = {}
    else:
        result = []
    query = [create_query([(pathElts, [])], dataset)]
    for batch in client.get(query):
        for notif in batch["notifications"]:
            if debug:
                pretty_print(notif["updates"])
            if "d" in resType.lower():
                result.update(notif["updates"])
            elif "l" in resType.lower():
                result.append(notif["updates"])
            else:
                return (f"Unknown resType: {resType}")
    if "g" in resType.lower():
        yield result
    elif "n" in resType.lower():
        return result
    else:
        return (f"Unknown resType: {resType}")
def main(apiserverAddr, token=None, certs=None, key=None, ca=None):
    pathElts = ["DatasetInfo", "Devices"]
    query = [create_query([(pathElts, [])], "analytics")]

    with GRPCClient(apiserverAddr, token=token, key=key, ca=ca,
                    certs=certs) as client:
        for batch in client.get(query):
            for notif in batch["notifications"]:
                pretty_print(notif["updates"])
    return 0
Exemple #3
0
def get(client, dataset, pathElts):
    ''' Returns a query on a path element'''
    result = {}
    query = [create_query([(pathElts, [])], dataset)]

    for batch in client.get(query):
        for notif in batch["notifications"]:
            if debug:
                pretty_print(notif["updates"])
            result.update(notif["updates"])
    return result
def main(apiserverAddr, dId, token=None, certs=None, ca=None, key=None):
    pathElts = [
        "Sysdb", "interface", "status", "eth", "phy", "slice", "1",
        "intfStatus",
        Wildcard()
    ]
    query = [create_query([(pathElts, ["active"])], dId)]

    with GRPCClient(apiserverAddr, token=token, key=key, ca=ca,
                    certs=certs) as client:
        for batch in client.get(query):
            for notif in batch["notifications"]:
                pretty_print(notif["updates"])
    return 0
Exemple #5
0
def query_vlan_members(client: GRPCClient, device_id, vlan_id):
    pathElts = [
        "Sysdb",
        "bridging",
        "config",
        "vlanConfig",
        {
            "value": vlan_id
        },
        "intf",
    ]
    query = [create_query([(pathElts, [])], device_id)]

    for intfs in grpc_query(client, query):
        yield from intfs.keys()
Exemple #6
0
def query_devices(client):
    pathElts = [
        "DatasetInfo",
        "Devices",
    ]
    query = [create_query([(pathElts, [])], "analytics")]

    # yield device serials (device_id) if they are active
    for devices in grpc_query(client, query):
        for device, data in devices.items():
            if data["status"] == "active":
                yield {
                    "hostname": data["hostname"],
                    "serial": device,
                }
Exemple #7
0
def query_ip_config(client, device_id, vlan_id):
    pathElts = [
        "Sysdb",
        "ip",
        "config",
        "ipIntfConfig",
        f"Vlan{vlan_id}",
    ]
    query = [create_query([(pathElts, [])], device_id)]

    for svi in grpc_query(client, query):
        ip_addr = svi["addrWithMask"]
        ip_virtual_addr = svi["virtualAddrWithMask"]
        for addr in ip_addr, ip_virtual_addr:
            if addr == "0.0.0.0/0":
                yield None
            else:
                yield addr
Exemple #8
0
def query_vlan_config(client, device_id):
    pathElts = [
        "Sysdb",
        "bridging",
        "config",
        "vlanConfig",
        Wildcard(),
    ]
    query = [create_query([(pathElts, [])], device_id)]

    # yield dictionaries containing vlan ID and name
    for vlan in grpc_query(client, query):
        if vlan["internal"]:  # ignore internal vlans
            continue
        yield {
            "id": vlan["vlanId"]["value"],
            "name": vlan["configuredName"],
        }
def main(apiserverAddr, dId, intfId, token=None, cert=None, key=None, ca=None):
    pathElts = [
        "Devices",
        dId,
        "versioned-data",
        "interfaces",
        "data",
        intfId,
        "rates",
    ]
    query = [
        create_query([(pathElts, ["outOctets"])], "analytics")
    ]

    with GRPCClient(apiserverAddr, token=token, certs=cert, key=key,
                    ca=ca) as client:
        for batch in client.subscribe(query):
            for notif in batch["notifications"]:
                pretty_print(notif["updates"])
    return 0
Exemple #10
0
def main(apiserverAddr,
         token=None,
         cert=None,
         key=None,
         ca=None,
         days=0,
         hours=1,
         minutes=0):
    startDtime = datetime.datetime.now() - datetime.timedelta(
        days=days, hours=hours, minutes=minutes)
    start = Timestamp()
    start.FromDatetime(startDtime)  # type: ignore
    pathElts = ["events", "activeEvents"]
    query = [create_query([(pathElts, [])], "analytics")]

    with GRPCClient(apiserverAddr, certs=cert, key=key, token=token,
                    ca=ca) as client:
        for batch in client.get(query, start=start):
            for notif in batch["notifications"]:
                pretty_print(notif["updates"])
    return 0
def getEventsCfg(client):
    ''' Gets the configuration data of each event turbine'''
    event_names = getEventTurbineNames(client)

    dataset = "analytics"
    event_config = {}

    for event in event_names:

        # Initialize the event dictionary where the default and custom rules
        # along with the path elements for each type will be stored
        event_dict = {}
        event_dict['default'] = {"path_elements": [], "updates": {}}
        pathElts = ["Turbines", "config", event, Wildcard()]
        query = [create_query([(pathElts, [])], dataset)]
        for batch in client.get(query):
            for notif in batch["notifications"]:

                # Only build a dictionary for custom rules if the custom key exists
                if "custom" in notif['path_elements']:
                    if "custom" in event_dict.keys():
                        event_dict['custom']["updates"].update(
                            notif['updates'])
                        event_dict['custom']["path_elements"] = notif[
                            'path_elements']
                    else:
                        event_dict['custom'] = {
                            "path_elements": [],
                            "updates": {}
                        }
                        event_dict['custom']["updates"].update(
                            notif['updates'])
                        event_dict['custom']["path_elements"] = notif[
                            'path_elements']
                if "default" in notif['path_elements']:
                    event_dict['default']["updates"].update(notif['updates'])
                    event_dict['default']["path_elements"] = notif[
                        'path_elements']
        event_config[event] = event_dict
    return unfreeze(event_config)