Get mac vendor result as dci

Started by Egert143, January 18, 2021, 01:48:44 PM

Previous topic - Next topic

Egert143

Hello

What is the best way to get "https://api.macvendors.com/%{node_mac}" result as DCI value and apply that via template ?

Egert

Alex Kirhenshtein

On one of the nodes with agent (let's call it "node1") add external parameter:

ExternalParameter=ResolveMAC(*):curl -s "https://api.macvendors.com/$1"

Then add DCI in the template:
Parameter=ResolveMAC(%{node_mac})
Source Node=node1

But I'd use mapping tables instead of calling remote service.

Egert143

#2
Thanks for the example. Why would mapping tables be better choice ?

Also seems like no easy way to get node mac address like %{node_primary_ip} ?

Egert

Filipp Sudanov

Mapping tables might better just for the reason of not disclosing the addresses to 3-rd party and for reliability.

You can add this script to script library and then use macro %[name_of_script] to get mac address of that node's interface that has primary IP address.
mac = NULL;
for (i:$node->interfaces)
{
  if ($node->ipAddr == i->ipAddr) mac = i->macAddr;
}
return mac;

Alex Kirhenshtein

#4
You can download latest mapping here: http://standards-oui.ieee.org/oui.txt

Then parse it and save in the mapping table (in this example - using nxshell):

#!/usr/bin/env nxshell

TABLE_NAME='MAC Address'

# find mapping table, create if missing
table_id = -1
for t in session.listMappingTables():
    if t.name == TABLE_NAME:
        table_id = t.id
        break
if table_id == -1:
    table_id = session.createMappingTable(TABLE_NAME, 'Lookup table for MAC -> Vendor', 0)

target_table = session.getMappingTable(table_id)

with open('oui.txt', 'r') as f:
    target_table.data.clear()
    for line in f.readlines():
        if "(hex)" in line:
            (mac, _, name) = line.strip().split('\t')
            mac = mac.split(' ')[0].replace('-', ':')
            target_table.data.add(MappingTableEntry(mac, name, None))

session.updateMappingTable(target_table)


Then you can lookup with map() function (in this example - find interface which have primary IP address):


for (i : $node->interfaces) {
if (i->ipAddr == $node->ipAddr) {
return map("MAC Address", (substr(i->macAddr, 0, 8)));
}
}


Result: Apple, Inc.


Egert143

Many thanks for greate examples, got it working and went with mapping table version. :)

Egert