jysk-oss / netbox-securecrt-inventory

NetBox SecureCRT Inventory sync
MIT License
6 stars 0 forks source link

Runtime Error: invalid memory address or nil pointer dereference #4

Closed adparis99 closed 4 weeks ago

adparis99 commented 1 month ago

First off, I think the idea for this is awesome, and was super excited to try it. Unfortunately, I got the error above/below after installing Go, getting the app running, and then hitting Sync Inventory Now. While I'm familiar with YAML and NetBox, I've got no experience with Go, so I definitely could be doing something wrong. The error:

PS C:\Users\username\Downloads\netbox-securecrt-inventory-main\netbox-securecrt-inventory-main> go run .
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x18 pc=0x65cc17]

goroutine 34 [running]:
github.com/jysk-network/netbox-securecrt-inventory/internal/inventory.(*InventorySync).writeSession(0xc000446300, {0x8bbd92, 0x6}, 0xc000163a40, {0xc00052eb10, 0xf}, {0xc00000b470?, 0x157357?}, {0x0, 0x0}, ...)
        C:/Users/username/Downloads/netbox-securecrt-inventory-main/netbox-securecrt-inventory-main/internal/inventory/inventory.go:64 +0x197
github.com/jysk-network/netbox-securecrt-inventory/internal/inventory.(*InventorySync).runSync(0xc000446300)
        C:/Users/username/Downloads/netbox-securecrt-inventory-main/netbox-securecrt-inventory-main/internal/inventory/inventory.go:136 +0x5c5
github.com/jysk-network/netbox-securecrt-inventory/internal/inventory.(*InventorySync).RunSync(0xc000446300)
        C:/Users/username/Downloads/netbox-securecrt-inventory-main/netbox-securecrt-inventory-main/internal/inventory/inventory.go:179 +0x3d
main.main.func1.1()
        C:/Users/username/Downloads/netbox-securecrt-inventory-main/netbox-securecrt-inventory-main/main.go:47 +0x25
created by main.main.func1 in goroutine 20
        C:/Users/username/Downloads/netbox-securecrt-inventory-main/netbox-securecrt-inventory-main/main.go:46 +0xc9
exit status 2
PS C:\Users\username\Downloads\netbox-securecrt-inventory-main\netbox-securecrt-inventory-main>

My YAML config:

netbox_url: https://nb-prod.domain.com
netbox_token: 12345678
root_path: Test

# Name overwrites can be used to change the name of the session
# Typical usecases is to remove domain names, extra values like .1 and so on
# name_overwrites:
  # - regex: "\\.1$"
    # value: ""

# Session Path defines where a session is saved
# There's a default template, but it's also possible to create overwrites based on the following keys:
# site_group, type, tenant_name, region_name, site_name, device_name
session_path:
    template: '{tenant_name}/{site_name}/{device_role}'
    # overwrites:
      # - key: site_group
        # value: Test Sites
        # template: _TEST/{region_name}/{site_name}
      # - key: type
        # value: virtual_machine
        # template: _Servers/{region_name}
# Enable / Disable periodic sync (note: SecureCRT needs to be restart for changes to take affect)
periodic_sync_enable: false
periodic_sync_interval: 120

# Default credentails to use, they should be defined in SecureCRT beforehand under "Preferences -> General -> Credentials"
default_credential: admin

Edit: Should've added this before, but I'm running NetBox 3.7.8, Go 1.22.4 on Windows 11, and SecureCRT 9.3.2.

patrickfnielsen commented 1 month ago

@adparis99 thanks for trying it out! I'm sorry you have issues. The issue seems to be that I'm trying to get the tenant from the site, but the site does not have a tenant assigned.

I'll push a fix today that does a few things different:

  1. Get the tenant from the device and not site
  2. Checks if it's set, or returns "No Tenant" if not

if you could please verify if that fixes the issue it would be lovely :)

patrickfnielsen commented 1 month ago

FYI: It's on main now, I'll cut a release if it works as expected for you

adparis99 commented 1 month ago

@adparis99 thanks for trying it out! I'm sorry you have issues. The issue seems to be that I'm trying to get the tenant from the site, but the site does not have a tenant assigned.

My pleasure! Thanks for bringing a great idea to life.

if you could please verify if that fixes the issue it would be lovely :)

Good news and bad news! The issue of the app crashing is fixed, and some devices are syncing! Unfortunately, not all devices are syncing, and no VMs synced either. The status in the system tray shows as "failed to create session", and there's no output in the console. Let me know if there's anything I can do on my end to help troubleshoot/debug further.

Edit: I paid a bit more attention to the system tray and noticed that the status dynamically updates throughout the sync process. I was able to see it start at Getting Devices, then Getting Virtual Machines, then finally Writing session... or something similar (it changes too quickly for me to catch it exactly) before stating failed to create session.

One other thing to note with regards to the devices that aren't syncing is that there are whole sites that don't seem to get added, despite devices in those sites being assigned the correct tenant. In one of my tenants there are twelve sites, but only six of them are populating.

patrickfnielsen commented 1 month ago

failed to create session happens when the program is unable to write session files, or create the directory structure needed. I have a feeling there might be some special characters involved in the device name or site names?

Could you maybe provide a site name, and device name under that site and I'll try replicate it? (sanitized where it makes sense of course)

adparis99 commented 4 weeks ago

Thank you! That was enough of a push to help me find the issue (one device with '?' in the name, and another with '/'). Once I updated those, the sync went through perfectly.

As an aside, I think a cool feature to add would be the ability to set the connection type based on attributes of the device/VM. An example use case would be to look create sessions with RDP for VMs, or with telnet for devices with a given custom field. I'd be more than happy to make a separate feature request for that though.

Thanks again for all the hard work!

patrickfnielsen commented 4 weeks ago

Awesome you got it working! I'll do some work on improving the handling of special characters in filenames. Please create a separate request for that and I'll look into it :)