Skip to content

Added AsusWRT device import plugin - #958

Merged
jokob-sk merged 2 commits into
netalertx:mainfrom
labmonkey:asuswrt-import
Jan 16, 2025
Merged

Added AsusWRT device import plugin#958
jokob-sk merged 2 commits into
netalertx:mainfrom
labmonkey:asuswrt-import

Conversation

@labmonkey

Copy link
Copy Markdown
Contributor

You can now import your devices from AsusWRT routers. Check Readme for more info.

Fixes #624

@jokob-sk
jokob-sk merged commit b4fc05d into netalertx:main Jan 16, 2025
@jokob-sk

Copy link
Copy Markdown
Collaborator

Hey @labmonkey,

Thanks a lot for the contribution 🙏

@labmonkey

labmonkey commented Jan 16, 2025

Copy link
Copy Markdown
Contributor Author

Thanks a lot. BTW regarding my comment In case an existing imported device is renamed in Asus Router it will not be renamed in NetAlertX. In this case it has to be done manually or the device should be removed and it will appear on the next scan. Is there any way around this? I've noticed that even If I repeat the following command on existing device:

        plugin_objects.add_object(
            primaryId=entry_mac,
            secondaryId=handleEmpty(device.connection.ip_address),
            watched1=handleEmpty(device.description.name),
            watched2=handleEmpty(device.description.vendor),
            extra=pluginName,
            foreignKey=entry_mac,
        )

It will correctly update the device online/offline status and IP address but If the watched1 or watched2 change it will not update those. I would assume that it compares the devices by primaryId that is the MAC address but the rest seems to be ignored?

@jokob-sk

jokob-sk commented Jan 16, 2025 via email

Copy link
Copy Markdown
Collaborator

@labmonkey

Copy link
Copy Markdown
Contributor Author

Hi. I see your point and I agree that there should be one source of truth.

Let me describe a little issue I am facing. So far I was naming all my devices in my Router. Just for easier administration. I thought that I could just keep them in sync with NAX and have one less thing to think about. The initial import works flawlessly as designed and has all the up to date information. I made two tests where things become a little bit different:

  • I've initially imported a device but did not set a name and then did a second import with the name. In the first step the device was just shown with empty name field as expected and in the second step that name was replaced with a real name. This means that in case the device has no name it can be overridden.
  • In the second scenario I've imported device with a name and then did second import with different name. As I mentioned in this case it just kept the initial name and did not allow it to be overridden.

As you can see the behaviour is different although I can totally understand that it is better to allow renaming unnamed devices rather than have no name at all.

The reason I was testing that is because in a real world scenario as soon as a new device joins my network the Router will give it an initial name most likely based on its hostname that sometimes is descriptive enough and sometimes just a not human friendly device model. This means that each new device will be imported into NAX with some name before I even get a chance to give it a normal name in my Router (assuming my cronjob is triggered every 5 minutes). Not judging anything here, just wanted to give you the idea of what can happen.

So just to wrap it up I think that the current behaviour is not that big of an issue, at least to me as I don't have that many devices but I had an idea that can work around this problem. I've noticed that I can also work with the DB in my plugin so it is possible to do things such as:

    db = DB()
    db.open()
    db.sql.execute("UPDATE Devices SET devName = 'test' WHERE devMac = '12:34:56...'")

So each time a device is imported I could check if the name changed an just trigger a proper SQL. Since by design NAX is source of truth I think there could be a flag in my plugin settings where an user could decide if he wants to allow this to happen. I just don't know how much this would be convenient alternative or going against the rules, what do you think?

@jokob-sk

Copy link
Copy Markdown
Collaborator

Thanks a lot for the in-depth answer.

I agree, that the 5 min interval is too short to get any edits done before the device is imported.

In some other plugins, I believe in the Mikrotik and Omada ones, what the developers have allowed is updating the device name in NAX and then syncing the new name into the router. Not sure if Asus has a write API for this purpose though. I also understand that this would shift your workflow a bit, so not sure if that's something you would entertain.

Regarding DB access, that's true. It's as simple as to run a query, but it's going against some design decisions and should be used in edge cases, as the system monitors the CurrentScan table for certain changes and triggers e.g. notifications based on those. I'd rather then implement a setting that would allow the user to specify which columns are always updated when synced from a plugin. This then could be used across any plugin in the app, giving more flexibility across the system, not just on this one.

Or it could be a simple setting (UPDATE_NAME) which would default to false and if set the system would update teh names from scans as well.

Anyway - let me know what are your thoughts. you can have a look at the code here as well: https://github.com/jokob-sk/NetAlertX/blob/a16c2dfed647f9e457ed189dd4cd0ec84e392204/server/device.py#L314

@labmonkey

Copy link
Copy Markdown
Contributor Author

Thanks for explaining this. I'm not sure if Asus has a write API but I also cannot find anything like this in the library I am using. Anyway I think it would be more than I am currently motivated to work on :-) With the DB as I expected running SQL directly is risky, but that was just an idea do discuss.
I like the idea with giving a list of columns that plugin can update. I suppose a field like this could be as simple as adding its definition to plugins config.json same as it is done with others.
Anyway to me what was most important was to import devices from the Router as it is 100% correct list of devices I was interested to see in NAX and now I am fully able to use this tool the way I needed. Maybe some day you can consider expanding the plugins capabilities but I don't want to push it as it is really not a necessity for me right now. Thanks for merging the plugin.

@jokob-sk

Copy link
Copy Markdown
Collaborator

thanks @labmonkey - I will consider these improvements in the future :)

@jokob-sk

Copy link
Copy Markdown
Collaborator

It took a while , but should be in the next release - the ability for a plugin to overwrite fields.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Help needed] Import from Asus routers

2 participants