Giter Club home page Giter Club logo

Comments (3)

ktbyers avatar ktbyers commented on August 16, 2024

FYI, NBInventory is deprecated and you should be using NetboxInventory2 instead.

https://github.com/wvandeun/nornir_netbox

You probably would need to show more of your code and how you are trying to validate whether threading is working.

from nornir.

Oussema-99 avatar Oussema-99 commented on August 16, 2024

Thank you for the tip, I just upgraded to the recommended plugin. The issue does persist though.

This is the nr.run() task

    forti_devices = nr.filter(F(platform=FORTI_PLATFORM))  # Filter for Fortinet devices
    forti_devices.run(task=backup_with_fortiapi,
                    state_commands=state_commands.get("forti_commands"),
                    output_path=directory_path, 
                    credentials=credentials)

The task calls upon this function:

def backup_with_fortiapi(task, state_commands: dict, output_path: str, credentials: str):

    device = str(task.host)    # hostname
    if not credentials.get(device):
        logging.error(f"Couldn't find {device}'s credentials, Skipping to next device")
        return False
    
    ip = task.host.hostname
    filename = f"{output_path}{device}"
    port = "443"    # default port
    if credentials.get(device).get("port"): port = credentials.get(device).get("port")    # use different port if needed
    fortinet_device = APIDevice(host=ip, port=port, ssl_verify=False) 
    config_response = fortinet_device.api_request(path="/api/v2/monitor/system/config/backup",        # obtain config
                                        method="GET",
                                        headers={'Authorization': f'Bearer {credentials.get(device).get("token")}'},
                                        params={"scope": "global"})
    if config_response:
        write_text(data=config_response.text, file=f"{filename}_config.txt")
        logging.info(f"{ip} - Performed Configuration Backup")
    device_state = {}     
    for key, api_path in state_commands.items():       # loop through device state commands
        logging.info(f"{ip} - Executing FortiAPI for '{api_path}'...")
        response = fortinet_device.api_request(path=api_path,
                                    method="GET",
                                    headers={'Authorization': f'Bearer {credentials.get(device).get("token")}'})
        if response:
            device_state[key] = response.json().get("results")      # example: {"interfaces": {...}}    

    if device_state:
        write_to_json(data=device_state, json_file=f"{filename}_state.json")
        logging.info(f"{ip} - Performed Device State Backup") 

Upon checking the logs, I can see that the execution is being sequentially since the 2nd firewall is only being hit after the 1st one already finished execution:

10:18:09 nornir.core INFO Running task 'backup_with_fortiapi' with args {'state_commands': {'address_objects': '/api/v2/cmdb/firewall/address', 'interfaces': '/api/v2/cmdb/system/interface'}, 'output_path': 'output/06_06_2023_10:18:05/',}}
10:18:10 root WARNING 'https://96.64.3.241:443/api/v2/monitor/system/config/backup' Bad Status Code '403' 
10:18:10 root INFO 96.64.3.241 - Executing FortiAPI for '/api/v2/cmdb/firewall/address'...
10:18:10 root INFO 'https://96.64.3.241:443/api/v2/cmdb/firewall/address' - 'GET' Operation successful
10:18:10 root INFO 96.64.3.241 - Executing FortiAPI for '/api/v2/cmdb/system/interface'...
10:18:12 root INFO 'https://96.64.3.241:443/api/v2/cmdb/system/interface' - 'GET' Operation successful
10:18:12 root INFO 96.64.3.241 - Performed Device State Backup
10:18:14 root INFO 'https://51.24.47.1:443/api/v2/monitor/system/config/backup' - 'GET' Operation successful
10:18:14 root INFO 51.24.47.1 - Performed Configuration Backup
10:18:14 root INFO 51.24.47.1 - Executing FortiAPI for '/api/v2/cmdb/firewall/address'...
10:18:15 root INFO 'https://51.24.47.1:443/api/v2/cmdb/firewall/address' - 'GET' Operation successful
10:18:15 root INFO 51.24.47.1 - Executing FortiAPI for '/api/v2/cmdb/system/interface'...
10:18:15 root INFO 'https://51.24.47.1:443/api/v2/cmdb/system/interface' - 'GET' Operation successful
10:18:16 root INFO 51.24.47.1 - Performed Device State Backup

I changed the device info for security

from nornir.

Oussema-99 avatar Oussema-99 commented on August 16, 2024

Update: I tried different stuff, it seems the first API request was failing and causing delays that made it seems one device was executing before the other. It's Solved now thanks a lot and sorry for the trouble :)

from nornir.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.