Merge pull request #7 from julthomas/dev/jth/no-des
[luasnmp.git] / trapd.lua
blob6f08b71b28b667462db9985cc5b37306f3433ce4
1 local socket = require "socket"
2 local arg = {select(1, ...)}
3 local port = tonumber(arg[1] or os.getenv("LUASNMP_TRAPDPORT")) or 6000
4 local log
5 if _VERSION > "5.1" then
6 local logger = require "logging.file"
7 log = logger(arg[2])
8 else
9 require "logging.file"
10 log = logging.file(arg[2])
11 end
12 log:setLevel(arg[3] or "INFO")
13 log:info("TRAPD capture start")
16 -- Collect input from snmptrapd and optionally write into logfile
17 -- We do not care about contents!
19 local str = ""
20 for s in io.lines() do
21 str = str .. " " .. s
22 log:debug("captured: '".. s .."'")
23 end
26 -- Send the string to luasnmp
28 sk = socket:udp()
29 local ip = socket.dns.toip("localhost")
30 if sk then
31 log:debug("sending input to "..ip..":"..port)
32 sk:sendto(str, ip, port)
33 sk:close()
34 else
35 log:error("cannot open udp socket")
36 os.exit(1)
37 end
40 -- Close the optional logfile.
42 log:info("TRAPD capture finished.")