This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

SNMP protocol in Logic Machine
#41
Attached an example for a data type OCTET STRING. It is important to clarify that if you want to send a value, it must be passed from ascii to hexadecimal and separated by two points (Smile. For example, the asci 30 when passed to hexadecimal is 33:30. On the other hand, when writing the type of data to write, you can use the type of data or the number that represents it, according to the documentation of LUA SNMP (http://luasnmp.luaforge.net/ objects.html), whichever of the two is possible.
Code:
val=event.getvalue()

require('snmp')

conn, err = snmp.open({
  version = snmp.SNMPv1,
  community = 'public',
  peer = '192.168.0.222',
})

assert(conn, err)


if val==true then
 
vbIn = {
    {oid = "1.3.6.1.4.1.1248.4.1.1.2.1.0", type=snmp.TYPE_INTEGER, value = "1"},
    {oid = "1.3.6.1.4.1.1248.4.1.1.2.2.0", type=2, value = "33:30"}
   
}
vbOut, err, index = conn:set(vbIn)

  log(vbOut)
 
else
 
vbIn = {
{oid = "1.3.6.1.4.1.1248.4.1.1.2.1.0", type=snmp.TYPE_INTEGER, value = "0"} 
}
vbOut, err, index = conn:set(vbIn)
   
end


conn:close()
Reply
#42
Hi,

Is there any way to get an entire table by snmp?
I see the table in Mib browser and I can get single value of table but if i try to put table oid value = nil.
Reply
#43
Hi,

I´ve seen in snmp lua manual that appears different commands but I don´t see getsubtree command.
How can I implement this command to get the table?

Is there any documentation about snmp in logicmachine?
Reply
#44
Use this code to walk through SNMP:
Code:
function walk(peer, community, version, root)
  local snmp = require('snmp')
  local session, err = snmp.open({
    peer = peer,
    community = community or 'public',
    version = version or snmp.SNMPv1
  })

  if not session then
    return nil, 'unable to open session: ' .. tostring(err)
  end

  root = root or '1'

  local vb = { oid = root }
  local err
  local res = {}

  while true do
    vb, err = session:getnext(vb)

    if err then
      break
    elseif string.find(vb.oid, root) == nil or vb.type == snmp.ENDOFMIBVIEW then
      break
    else
      res[ vb.oid ] = vb.value
    end
  end

  session:close()

  if err then
    return nil, 'walk failed: ' .. tostring(err)
  else
    return res
  end
end

res, err = walk('192.168.1.1', 'public', snmp.SNMPv1, '1.0.8802.1.1.2.1.3')
log(res, err)

Arguments for walk:
1. remote device IP
2. community, defaults to 'public'
3. version, defaults to v1
4. root SNMP node, defaults to '1'

SNMP library info: http://luasnmp.luaforge.net/
Reply
#45
Success today! I made a post in this thread https://forum.logicmachine.net/showthread.php?tid=2371

Now i understand the script from this thread. My next question is how to read out the number, i've read out values from tables before, but on this one im a bit stuck.
I get this result and i want to read out the number to a grp address. i see that you use an other way to read it earlier in this thread, but from my knowledge i can do it from this scrip as well.

Code:
res, err = walk('192.168.1.252', 'public', snmp.SNMPv1, '1.3.6.1.4.1.318.1.1.26.6.3.1.6')
log(res, err)

if type(res) == 'table' then

  voltage = res.?????

log(voltage)
 
end


[Image: 4i9Se4X.png]
Reply
#46
walk is needed when you want to finds all sub-OIDs within a sub-tree.
Have a look at this example:
https://forum.logicmachine.net/showthrea...51#pid9651
You just need to provide device IP and OID to group address mapping. Script will then handle reading.
Reply
#47
Got it Blush Blush
Code:
res, err = walk('192.168.1.252', 'public', snmp.SNMPv1, '1.3.6.1.4.1.318.1.1.26.6.3.1.6')
log(res, err)

if type(res) == 'table' then

  voltage = res["1.3.6.1.4.1.318.1.1.26.6.3.1.6.1"]

log(voltage)
 
end

(21.02.2020, 08:22)admin Wrote: walk is needed when you want to finds all sub-OIDs within a sub-tree.
Have a look at this example:
https://forum.logicmachine.net/showthrea...51#pid9651
You just need to provide device IP and OID to group address mapping. Script will then handle reading.
 
Ahh okey, this is an way easier way then. Thanks
Reply
#48
Hi,

I have a problem with counter64 data type. I can log it but it hasn´t data type and I can´t write in an object.
What can I do??

   


This one works fine:

   

Thanks
Reply
#49
Use 29. 8 byte signed integer
Reply
#50
I´ve use it but update a "0" in the object.

   
Reply
#51
Change object data type to string and check what you get there
Reply
#52
I´ve tried it too but the same. I haven´t get nothing.
Reply
#53
Post full script that you are using to read values
Reply
#54
(07.03.2020, 13:20)admin Wrote: Post full script that you are using to read values
Code:
local IP_Mikrotik = "192.168.2.1"
local OID_Mikrotik = {
  {'38/5/1',".1.3.6.1.2.1.25.2.3.1.5.65536"}, --Total Memowy
  {'38/5/2',".1.3.6.1.2.1.25.2.3.1.6.65536"}, --Used-Memory
  {'38/5/3',".1.3.6.1.2.1.25.3.3.1.2.1"}, --CPU Frecuency
  {'38/5/5',".1.3.6.1.4.1.14988.1.1.3.14.0"}, --Carga CPU
  {'38/5/11',".1.3.6.1.4.1.14988.1.1.14.1.1.2.1"}, --Interface 1 Name
  {'38/5/12',".1.3.6.1.4.1.14988.1.1.14.1.1.2.2"}, --Interface 2 Name
  {'38/5/13',".1.3.6.1.4.1.14988.1.1.14.1.1.2.3"}, --Interface 3 Name
  {'38/5/14',".1.3.6.1.4.1.14988.1.1.14.1.1.2.4"}, --Interface 4 Name
  {'38/5/15',".1.3.6.1.4.1.14988.1.1.14.1.1.2.5"}, --Interface 5 Name
  {'38/5/16',".1.3.6.1.4.1.14988.1.1.14.1.1.2.6"}, --Interface 6 Name
  {'38/5/17',".1.3.6.1.4.1.14988.1.1.14.1.1.2.7"}, --Interface 7 Name
  {'38/5/21',".1.3.6.1.4.1.14988.1.1.14.1.1.11.1"}, --Interface 1 TX bytes
  {'38/5/22',".1.3.6.1.4.1.14988.1.1.14.1.1.11.2"}, --Interface 2 TX bytes
  {'38/5/23',".1.3.6.1.4.1.14988.1.1.14.1.1.11.3"}, --Interface 3 TX bytes
  {'38/5/24',".1.3.6.1.4.1.14988.1.1.14.1.1.11.4"}, --Interface 4 TX bytes
  {'38/5/25',".1.3.6.1.4.1.14988.1.1.14.1.1.11.5"}, --Interface 5 TX bytes
  {'38/5/26',".1.3.6.1.4.1.14988.1.1.14.1.1.11.6"}, --Interface 6 TX bytes
  {'38/5/27',".1.3.6.1.4.1.14988.1.1.14.1.1.11.7"}, --Interface 7 TX bytes
  {'38/5/31',".1.3.6.1.4.1.14988.1.1.14.1.1.13.1"}, --Interface 1 RX bytes
  {'38/5/32',".1.3.6.1.4.1.14988.1.1.14.1.1.13.2"}, --Interface 2 RX bytes
  {'38/5/33',".1.3.6.1.4.1.14988.1.1.14.1.1.13.3"}, --Interface 3 RX bytes
  {'38/5/34',".1.3.6.1.4.1.14988.1.1.14.1.1.13.4"}, --Interface 4 RX bytes
  {'38/5/35',".1.3.6.1.4.1.14988.1.1.14.1.1.13.5"}, --Interface 5 RX bytes
  {'38/5/36',".1.3.6.1.4.1.14988.1.1.14.1.1.13.6"}, --Interface 6 RX bytes
  {'38/5/37',".1.3.6.1.4.1.14988.1.1.14.1.1.13.7"}, --Interface 7 RX bytes
  {'38/5/0',".1.3.6.1.2.1.2.2.1.8.1"}, --Interface 1 Up/Down (Para saber si ISP esta activo
}


ups1, err = snmp.open{
  version = snmp.SNMPv1,
  community = "MonitoreoLM",
  port = 161,
  peer = IP_Mikrotik,
}

assert(ups1, err)

for _, item in ipairs(OID_Mikrotik) do
  vbind, err = ups1:get(item[2])
  if vbind then
    --log(item[1],vbind.value)
    grp.checkupdate(item[1], vbind.value) 
  else
    alert('error reading from snmp: ' .. tostring(err))
  end
end

ups1:close()

Solved!. If I convert it to string and again to number it works.

Thanks!
Reply
#55
Write does not work because SNMP lib uses a special "container" type instead of numbers to store 64-bit values. This will be changed in new versions but for now your approach with tostring/tonumber is correct.
Reply
#56
Hi,

..admin wrote: ...but for now your approach with tostring/tonumber is correct. How to do it in example above? As I see no all values should be converted tostring/tonumber, just values with digits. Can you explain a little more? I tried to test example above, and I have instead of digital values just zeros...

BR,

Alex

I added conversion for line 45:

grp.checkupdate(item[1], tonumber(tostring(vbind.value)))

but in such case I received all digital values, all strings = nil of course.. How automate this, not to write conversion separately to every line with digit values?

Alex
Reply
#57
Finally I found how do it working.

Code:
require "snmp"
local IP_Mikrotik = "192.168.10.1"
local OID_Mikrotik = {
  {'38/5/1',".1.3.6.1.2.1.25.2.3.1.5.65536"}, --Total Memowy
  {'38/5/2',".1.3.6.1.2.1.25.2.3.1.6.65536"}, --Used-Memory
  {'38/5/3',".1.3.6.1.2.1.25.3.3.1.2.1"}, --CPU Frecuency
  {'38/5/5',".1.3.6.1.4.1.14988.1.1.3.14.0"}, -- CPU Load
  {'38/5/11',".1.3.6.1.4.1.14988.1.1.14.1.1.2.1"}, --Interface 1 Name
  {'38/5/12',".1.3.6.1.4.1.14988.1.1.14.1.1.2.2"}, --Interface 2 Name
  {'38/5/13',".1.3.6.1.4.1.14988.1.1.14.1.1.2.3"}, --Interface 3 Name
  {'38/5/14',".1.3.6.1.4.1.14988.1.1.14.1.1.2.4"}, --Interface 4 Name
  {'38/5/15',".1.3.6.1.4.1.14988.1.1.14.1.1.2.5"}, --Interface 5 Name
  {'38/5/16',".1.3.6.1.4.1.14988.1.1.14.1.1.2.6"}, --Interface 6 Name
  {'38/5/17',".1.3.6.1.4.1.14988.1.1.14.1.1.2.7"}, --Interface 7 Name
  {'38/5/21',".1.3.6.1.4.1.14988.1.1.14.1.1.11.1"}, --Interface 1 TX bytes
  {'38/5/22',".1.3.6.1.4.1.14988.1.1.14.1.1.11.2"}, --Interface 2 TX bytes
  {'38/5/23',".1.3.6.1.4.1.14988.1.1.14.1.1.11.3"}, --Interface 3 TX bytes
  {'38/5/24',".1.3.6.1.4.1.14988.1.1.14.1.1.11.4"}, --Interface 4 TX bytes
  {'38/5/25',".1.3.6.1.4.1.14988.1.1.14.1.1.11.5"}, --Interface 5 TX bytes
  {'38/5/26',".1.3.6.1.4.1.14988.1.1.14.1.1.11.6"}, --Interface 6 TX bytes
  {'38/5/27',".1.3.6.1.4.1.14988.1.1.14.1.1.11.7"}, --Interface 7 TX bytes
  {'38/5/31',".1.3.6.1.4.1.14988.1.1.14.1.1.13.1"}, --Interface 1 RX bytes
  {'38/5/32',".1.3.6.1.4.1.14988.1.1.14.1.1.13.2"}, --Interface 2 RX bytes
  {'38/5/33',".1.3.6.1.4.1.14988.1.1.14.1.1.13.3"}, --Interface 3 RX bytes
  {'38/5/34',".1.3.6.1.4.1.14988.1.1.14.1.1.13.4"}, --Interface 4 RX bytes
  {'38/5/35',".1.3.6.1.4.1.14988.1.1.14.1.1.13.5"}, --Interface 5 RX bytes
  {'38/5/36',".1.3.6.1.4.1.14988.1.1.14.1.1.13.6"}, --Interface 6 RX bytes
  {'38/5/37',".1.3.6.1.4.1.14988.1.1.14.1.1.13.7"}, --Interface 7 RX bytes
  {'38/5/0',".1.3.6.1.2.1.2.2.1.8.1"}, --Interface 1 Up/Down (To find out if ISP is active)
}


ups1, err = snmp.open{
  version = snmp.SNMPv1,
  community = "public",
  port = 161,
  peer = IP_Mikrotik,
}

assert(ups1, err)

for _, item in ipairs(OID_Mikrotik) do
  vbind, err = ups1:get(item[2])
  if vbind then
if tonumber(tostring(vbind.value)) ~=nil then
      grp.checkupdate(item[1], tonumber(tostring(vbind.value))) --- Here we receive correct digital values
    --log(item[1],vbind.value)
else
    grp.checkupdate(item[1], vbind.value) --- Here we receive correct string values
  end
  else
    alert('error reading from snmp: ' .. tostring(err))
  end
end
ups1:close()
thanks to admin, he gives us solutions or ways where to possible find answers..
Reply
#58
A more correct approach of handling counter64 type values:
Code:
if vbind then
  value = vbind.value

  if type(value) == 'userdata' then
    value = tonumber(tostring(value))
  end

  if value ~= nil then
    grp.checkupdate(item[1], value)
  end
else
  alert('error reading from snmp: ' .. tostring(err))
end
Reply
#59
Hi,
how integrate to last more correct script addon, if data type = "table"
I wanted to add router uptime value, and in log I see it such way:

* arg: 1
* string: 38/5/6
* arg: 2
* table:
["minutes"]
* number: 0
["deciseconds"]
* number: 0
["days"]
* number: 0
["ticks"]
* number: 2165600
["seconds"]
* number: 56
["hours"]
* number: 6

BR,

Alex
Reply
#60
This will convert table value to a string (for example: 1d 06h 42m).
Code:
if type(value) == 'table' then
  value = string.format('%dd %02dh %02dm', value.days or 0, value.hours or 0, value.minutes or 0)
end
Reply


Forum Jump: