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 whether you accept or reject these cookies being set.

SNMP protocol in Logic Machine
#21
You can also try adding ups1:close() to the end of each resident script.
Reply
#22
Thanks for your collaboration, with the last thing that I indicated, the problem was solved and I no longer drop the connection.
Reply
#23
Hi,

I have a problem with snmp conection.

I have this script:

Code:
require "snmp"

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

assert(ups1, err)

vbind, err = ups1:get(".1.3.6.1.4.1.24681.1.2.1.0") --Uso de la CP
grp.update('38/2/1', vbind.value)
vbind, err = ups1:get(".1.3.6.1.4.1.24681.1.2.2.0") --Memoria total del sistema
grp.update('38/2/2', vbind.value)
vbind, err = ups1:get(".1.3.6.1.4.1.24681.1.2.3.0") --Memoria libre
grp.update('38/2/3', vbind.value)
vbind, err = ups1:get(".1.3.6.1.4.1.24681.1.2.4.0") --Tiempo en linea
grp.update('38/2/4', vbind.value)

ups1.close()

When I play resident script (5s) it work fine, but later, i get the following error:

Resident script:18: netsnmp: Unknown host (192.168.2.201)
stack traceback:
[C]: in function 'assert'

Can you help me?

Thanks
Reply
#24
Hi.

I have been loging data and I have created a script that restarts the snmp script when it fails, and it works again.
I have verified that the snmp script fails exactly every 2.5h

Any idea?
Reply
#25
Your script is incorrect, it must have ups1:close() instead of ups1.close()
This caused open sessions not to close properly so the script hits open socket limit after some time.
Reply
#26
(11.04.2019, 09:52)admin Wrote: Your script is incorrect, it must have ups1:close() instead of ups1.close()
This caused open sessions not to close properly so the script hits open socket limit after some time.

I had not seen it....

Thanks!!!
Reply
#27
Hi, my script still doesn´t work (only sometimes). I get the same error: 

Resident script:45: netsnmp: Unknown host ("MyIP")
stack traceback:
 [C]: in function 'assert'

Code:
require "snmp"

local IP_NAS = "MyIP"
local OID_NAS = {
 {'38/2/1',".1.3.6.1.4.1.24681.1.2.1.0"}, --Uso de la CPU
 {'38/2/2',".1.3.6.1.4.1.24681.1.2.2.0"}, --Memoria total del sistema
 {'38/2/3',".1.3.6.1.4.1.24681.1.2.3.0"}, --Memoria libre
 {'38/2/4',".1.3.6.1.4.1.24681.1.2.4.0"}, --Tiempo en linea
}

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

assert(ups1, err)

for i = 1, #OID_NAS, 1 do
    vbind, err = ups1:get(OID_NAS[i][2])
    grp.update(OID_NAS[i][1], vbind.value)  
end

ups1:close()


Any idea?

Thanks
Reply
#28
Change MyIP to real IP address of your SNMP device
Reply
#29
MyIP =192.168.2.201
Reply
#30
Post full listing of your script. The error that you have is at line 45 whereas the code you posted has only 25 lines.
Reply
#31
Sorry, here full script:

Code:
require "snmp"

--local mib,err = snmp.mib.load ('/home/ftp/snmp/....mib')
--[[
if not mib then
 log ('Fallo carga Mib: '..err)
end]]

local IP_NAS = "192.168.2.201"
local OID_NAS = {
 {'38/2/1',".1.3.6.1.4.1.24681.1.2.1.0"}, --Uso de la CPU
 {'38/2/2',".1.3.6.1.4.1.24681.1.2.2.0"}, --Memoria total del sistema
 {'38/2/3',".1.3.6.1.4.1.24681.1.2.3.0"}, --Memoria libre
 {'38/2/4',".1.3.6.1.4.1.24681.1.2.4.0"}, --Tiempo en linea
 {'38/2/5',".1.3.6.1.4.1.24681.1.2.11.1.3.1"}, --Temperatura HD 1
 {'38/2/6',".1.3.6.1.4.1.24681.1.2.11.1.3.2"}, --Temperatura HD 2
 {'38/2/7',".1.3.6.1.4.1.24681.1.2.11.1.3.3"}, --Temperatura HD 3
 {'38/2/8',".1.3.6.1.4.1.24681.1.2.11.1.3.4"}, --Temperatura HD 4
 {'38/2/9',".1.3.6.1.4.1.24681.1.2.11.1.4.1"}, --Estado del HD 1
 {'38/2/10',".1.3.6.1.4.1.24681.1.2.11.1.4.2"}, --Estado del HD 2
 {'38/2/11',".1.3.6.1.4.1.24681.1.2.11.1.4.3"}, --Estado del HD 3
 {'38/2/12',".1.3.6.1.4.1.24681.1.2.11.1.4.4"}, --Estado del HD 4
 {'38/2/13',".1.3.6.1.4.1.24681.1.2.11.1.6.1"}, --Capacidad Disco duro 1
 {'38/2/14',".1.3.6.1.4.1.24681.1.2.11.1.6.2"}, --Capacidad Disco duro 2
 {'38/2/15',".1.3.6.1.4.1.24681.1.2.11.1.6.3"}, --Capacidad Disco duro 3
 {'38/2/16',".1.3.6.1.4.1.24681.1.2.11.1.6.4"}, --Capacidad Disco duro 4
 {'38/2/17',".1.3.6.1.4.1.24681.1.2.11.1.7.1"}, --Smart Info HD 1
 {'38/2/18',".1.3.6.1.4.1.24681.1.2.11.1.7.2"}, --Smart Info HD 2
 {'38/2/19',".1.3.6.1.4.1.24681.1.2.11.1.7.3"}, --Smart Info HD 3
 {'38/2/20',".1.3.6.1.4.1.24681.1.2.11.1.7.4"}, --Smart Info HD 4
 {'38/2/21',".1.3.6.1.4.1.24681.1.2.13.0"}, --Hostname
 {'38/2/22',".1.3.6.1.4.1.24681.1.2.17.1.2.1"}, --Descripcion del Volumen
 {'38/2/23',".1.3.6.1.4.1.24681.1.2.17.1.4.1"}, --Capacidad total del volumen
 {'38/2/24',".1.3.6.1.4.1.24681.1.2.17.1.5.1"}, --Espacio Libre Volumen
 {'38/2/25',".1.3.6.1.4.1.24681.1.2.17.1.6"}, --Estado del volumen
}

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

assert(ups1, err)

for i = 1, #OID_NAS, 1 do
    vbind, err = ups1:get(OID_NAS[i][2])
    grp.update(OID_NAS[i][1], vbind.value)  
end

ups1:close()
Reply
#32
Hi,

Here another error that give me many times:
From 17:30 to 8:35AM last time. Now works again.

Resident script:49: attempt to index global 'vbind' (a nil value)
stack traceback:
Reply
#33
This happens because there's no error check for get() calls.

Use this code:
Code:
for _, item in ipairs(OID_NAS) do
  vbind, err = ups1:get(item[2])
  if vbind then
    grp.update(item[1], vbind.value)  
  else
    alert('error reading from snmp: ' .. tostring(err))
  end
end
Reply
#34
(18.06.2019, 08:05)admin Wrote: This happens because there's no error check for get() calls.

Use this code:
Code:
for _, item in ipairs(OID_NAS) do
 vbind, err = ups1:get(item[2])
 if vbind then
   grp.update(item[1], vbind.value)  
 else
   alert('error reading from snmp: ' .. tostring(err))
 end
end

Ok, I´ll try it.

Thanks!!
Reply
#35
Hello forum partners. Now I have two devices. The first is a projector which has an SNMP communication and I want to turn on and off, change the video input for which I have to write a value in the respective OIDs. I tested the projector both by writing and reading the SNMP variables by means of an SNMP management software and it works without any problem, but with the LM I have made a script by event to turn it on, but it does not work for me. On the other hand I have a team that uses SNMP Version 3 which replaces the community by user name, an authentication code, a privacy code and the protocol of these (MD5, SHA, DES, AES) which is not compatible with the current code, in addition to having some Tramps which I have to be aware of and I do not know how to do it, so if they have any way of doing it, it would be very helpful. I include the code I did to write about an SNMP OID, which does not work for me. Thank you and I am attentive to your comments.
Code:
val=event.getvalue()
--Abrir una sesión con snmp
require "snmp"

ups1, err = snmp.open{
  version = snmp.SNMPv1,
  community = "public",
  port = 161,
  peer = "192.168.0.243",
}
assert(ups1, err)

if value==true then

  vbind, err = ups1:set("1.3.6.1.4.1.1248.4.1.1.2.1.0", 1) 
 
else
vbind, err = ups1:set("1.3.6.1.4.1.1248.4.1.1.2.1.0", 0) 
 
   
end

ups1:close()
Reply
#36
See library documentation on how to correctly call set function: http://luasnmp.luaforge.net/snmp.html#snmp_set

I haven't tried using SNMPv3, but you can try this code:
Code:
ups1, err = snmp.open({
  port = 161,
  peer = "192.168.0.243",
  version = snmp.SNMPv3,
  user = "username",
  password = "123456",
})

SNMP traps are not supported at this moment.
Reply
#37
Hi Admin, thanks for replying. With the help of the library manual I understand the script a bit more, in addition to the version 3 SNMP, and then I will try to try the version with what the manual says. The problem I have is to write to an OID, I have already tried many things but it still does not work, I have even commented, tried several parts of the code but it still does not work. I put it together to see if I'm wrong about something. Thank you and I am attentive to your comments
Code:
val=event.getvalue()
--Abrir una sesión con snmp
require "snmp"
--value=1
ups1, err = snmp.open{
  version = snmp.SNMPv1,
  community = "public",
  port = 161,
  peer = "192.168.0.248",
}
assert(ups1, err)

vbOut, err = ups1:set("1.3.6.1.4.1.1248.4.1.1.2.1.0", 0)

--vlist = {
--{oid = "1.3.6.1.4.1.1248.4.1.1.2.1.0", 0}
--}

--vbind = {
--{oid = "1.3.6.1.4.1.1248.4.1.1.2.1.0", type = TYPE_INTEGER32, value = "0"}
--}


--vbOut, err = ups1:set(vbIn)

--vbOut, err = ups1:set(vbind)
log(vbind)
log(err)
log(index)
--assert(vbOut, err)


--if value==true then

  --vbind, err = ups1:set("1.3.6.1.4.1.1248.4.1.1.2.1.0", 1) 
 
--else
--vbind, err = ups1:set("1.3.6.1.4.1.1248.4.1.1.2.1.0", 0) 
 
   
--end

ups1:close()
Reply
#38
Here's an example script that sets identity on Mikrotik router via SNMP. You can modify it for your device. Resulting status or error will be visible in Alerts tab.

Code:
require('snmp')

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

if conn then
  res, err = conn:set({
    {
      oid = '1.3.6.1.2.1.1.5.0',
      value = 'New_Identity',
    }
  })

  if err then
    alert('snmp set failed: ' .. tostring(err))
  else
    alert('snmp set ok')
  end

  conn:close()
else
  alert('snmp connection failed: ' .. tostring(err))
end
Reply
#39
Hi Admin, thanks for replying. Use the code you told me, but it's not working. I have attached the code adapted to my OID and my IP together with an error log. I have made several modifications but none of them work for me. Thank you and I am attentive to your comments.

Code:
val=event.getvalue()
--Abrir una sesión con snmp
require('snmp')

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

if conn then
  res, err = conn:set({
    {
      oid = '1.3.6.1.4.1.1248.4.1.1.2.1.0',
        value = '0',
    }
  })

  if err then
    alert('snmp set failed: ' .. tostring(err))
    log (err)
    log ('snmp set failed')
  else
    alert('snmp set ok')
    log ('snmp set ok')
  end

  conn:close()
else
  alert('snmp connection failed: ' .. tostring(err))
  log ('snmp connection failed')
end

Event for ON/OFF_PROYECTOR (0/1/0) 12.07.2019 09:47:23
* string: snmp: bad type (2) prim=0 in index 1


Event for ON/OFF_PROYECTOR (0/1/0) 12.07.2019 09:47:23
* string: snmp set failed


I have the library
libnetsnmp 5.4.4-1
luasnmp 1.0.6-2-2
Reply
#40
Hi, I share with you the script by event that works to turn an EPSON projector on and off using snmp.

Code:
val=event.getvalue()

require('snmp')

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

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"} 
}
vbOut, err, index = conn:set(vbIn)

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


Forum Jump: