SNMP client

SNMP client #

Create a resident script with sleep time > 0, adjust devices table as needed.

if not devices then
  require('snmp')

  devices = {
    {
      ip = '192.168.1.1',
      mapping = {
        { oid = '1.3.6.1.2.1.1.1.0', addr = '8/0/0' },
        { oid = '1.3.6.1.4.1.14988.1.1.1.3.1.4.6', addr = '8/0/1' },
      }
    },
    {
      ip = '192.168.1.4',
      port = 1610, -- optional custom port
      version = snmp.SNMPv2, -- optional custom version
      community = 'custom', -- optional custom community
      mapping = {
        { oid = '1.3.6.1.4.1.14988.1.1.3.8.0', addr = '8/0/0' },
        { oid = '1.3.6.1.4.1.14988.1.1.3.10.0', addr = '8/0/1' },
      }
    },
  }
end

for _, dev in ipairs(devices) do
  local conn, err = snmp.open({
    version = dev.version or snmp.SNMPv1,
    community = dev.community or 'public',
    port = dev.port or 161,
    peer = dev.ip,
  })

  if conn then
    for _, map in ipairs(dev.mapping) do
      local res, err = conn:get(map.oid)
      if res and res.value ~= nil then
        grp.checkwrite(map.addr, res.value)
      else
        alert('SNMP read from %s %s failed (%s)', dev.ip, map.oid, tostring(err))
      end
    end

    conn:close()
  else
    alert('SNMP connection to %s failed (%s)', dev.ip, tostring(err))
  end
end