Mass Delete Devices from LibreNMS with Operating System: Generic Device

Recently, we embarked on expanding our network monitoring capabilities by adding a new subnets to our LibreNMS auto-discovery configuration. The goal was straightforward: to ensure comprehensive visibility across our network. However, the outcome was anything but expected. Instead of the anticipated additions, we found ourselves sifting through an influx of devices – a total that soared to 1,030, far beyond our projections.

Among these, a staggering 930 devices were identified with the operating system label “Generic Device.” This was a clear indication that while the auto-discovery process was functional, it had also swept up a significant number of devices that weren’t intended for monitoring like staff laptops.

The immediate solution might seem simple to some: revert to the previous day’s snapshot and undo the auto-discovery. However, that approach hardly offered the learning opportunity or the satisfaction of troubleshooting and resolving the issue directly. Plus, where’s the fun in taking the easy way out?

Our journey from an overwhelming 1,030 devices back to a manageable count of approximately 260 – the number we initially anticipated – was both challenging and enlightening. It underscored the importance of precise configuration and the potential pitfalls of auto-discovery when not carefully managed.

This experience led us to develop a targeted script, leveraging the LibreNMS API, designed to identify and remove devices marked with the “Generic Device” operating system. It was a practical solution to an unexpected problem, enabling us to refine our device list without compromising the integrity or the detailed monitoring of our network.

In this snapshot, it’s evident that we’re dealing with 930 devices identified by the “Generic Device” operating system.

Device search showing 930 with the operating system of Generic Device.

Below is the Python script I utilized to remove these devices, executed on a system running Ubuntu 22.04 and LibreNMS version 24.1.0-99. Let’s start by creating the script.

nano delete_generic_devices.py
#!/usr/bin/env python3

import requests

# Configuration
api_url = "http://YOUR-URL-HERE/api/v0"
api_token = "REPLACE-WITH-YOUR-TOKEN"

headers = {
    'X-Auth-Token': api_token
}

def get_generic_devices():
    """Fetch devices with Operating System set to 'Generic Device'."""
    all_devices = requests.get(f"{api_url}/devices", headers=headers).json()['devices']
    generic_devices = [device for device in all_devices if device.get('os') == 'generic']
    return generic_devices

def delete_device(device_id):
    """Delete a device by its ID and log the outcome."""
    response = requests.delete(f"{api_url}/devices/{device_id}", headers=headers)
    if response.status_code == 200 and "Removed device" in response.json().get("message", ""):
        print(f"Successfully deleted device with ID {device_id}.")
    else:
        print(f"Failed to delete device with ID {device_id}. Status: {response.status_code}, Response: {response.text}")

def main():
    generic_devices = get_generic_devices()
    for device in generic_devices:
        delete_device(device['device_id'])

if __name__ == "__main__":
    main()

Next we need to make it executable

 chmod +x delete_generic_devices.py

Now, it’s time to execute the script!

python3 delete_generic_devices.py
Running the script via terminal

Now that the script has completed its run, let’s take a look at LibreNMS. You’ll notice that there are now zero devices listed with the “Generic Device” operating system!

With the successful execution of our script and the subsequent cleanup in LibreNMS, we’ve not only streamlined our device monitoring but also reinforced the importance of precise network management. This endeavor underscores the power of automation and targeted scripting in maintaining an efficient and accurate network monitoring system. As we move forward, this experience serves as a valuable lesson in the adaptability and resourcefulness necessary in the ever-evolving field of network administration. Happy monitoring, and here’s to many more problem-solving adventures ahead!

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top