snmptrap を IRC に出力(設定して起動)

ビルド(http://nsrzakki.hateblo.jp/entry/2012/05/05/000718)のつづきです。
Luaで一通り追加したモジュールをrequireしてエラーにならなければたぶん大丈夫。
ここで扱う設定とかは全部テスト的にってことで、/var/tmp/に置きます。
真面目に使ってる場所では、適当にパス変えて使ってます。
というか、今ならfluentdとか他に良いものが沢山あるので使えるものが使ったほうがいい。ircプラグインもあるし。
後、IRCサーバは別にある前提です。

●設定
・/var/tmp/snmptrapd

traphandle default /usr/local/lua-5.1.4/bin/lua /var/tmp/queue.lua -c /var/tmp/trap2irc.conf

・/var/tmp/trap2irc.conf

---------------------- config section -----------------------
logparameter = {
  filename = "/var/tmp/loabot.log" ,  -- ログですqueue用とirc出力用共通
  filemaxsize = 102400 ,  -- ログファイルサイズ
  maxindex = 10 -- ログファイル世代数
}

botparameter = {
  botnickname = "luabot" ,  -- ircニックネーム
  joinchannel = "#snmptrap" ,  -- メッセージを出すチャネル名
  channelpass = "snmptrap" ,  -- チャネルパスワードがあれば設定する
  sleeptime = 1.0 ,  -- bot内のループ内スリープ時間
  getmsgcount = 5  -- 1ループでktserverから取り出し→IRC出力するメッセージの最大数(IRC出力負荷調整用)
}

ircserverinfo = {
  host = "localhost",  -- ircサーバ
  port = 16667,  -- ircサーバのLISTENポート
  timeout = 60,  -- 何かのタイムアウトw
  password = nil ,  -- サーバパスワード
  secure = false  -- 暗号化通信有無(まともに使ってないので、多分設定してもまともに動作しない気がする)
}

kyotoserverinfo = {
  server = "localhost",  -- ktserver
  ktport = nil ,  -- ktserverのポート番号、memcached側を使うので実際には未使用
  memport = 11211 ,  -- memcachedプラグイン側のLISTENポート
  queuekeyword = "#snmptrap"  -- ktserver内のkey名(queue用とirc用で共通にする必要があります)
}

●スクリプト類
・/var/tmp/queue.lua

---------------------- load section -----------------------
require "Memcached"
require "logging.rolling_file"
require "alt_getopt"

---------------------- function section ----------------------
function usage()
  print (string.format("%s", "Usage : " .. arg[0] .. " -c file"))
end

function queue(queuemsg)
  local memcache = Memcached.Connect(kyotoserverinfo.server,kyotoserverinfo.memport)

  logger:debug(string.format('%s',queuemsg))
  local key = kyotoserverinfo.queuekeyword
  if memcache:set(key,queuemsg) then
    logger:debug(string.format('%s',"Set "..key..","..queuemsg))
  else
    logger:error(string.format('%s',"Set Error "..key..","..queuemsg))
  end
end

---------------------- init section -----------------------
local optarg
local optind
local long_opts = {
   conf = "c",
   help = "h"
}
opts,optind,optarg = alt_getopt.get_ordered_opts (arg, "c:h", long_opts)
for i,v in ipairs (opts) do
  if v == "c" then
    configfile = optarg[i]
  elseif v == "h" then
    usage()
    os.exit(0)
  end
end
if configfile == nil then
  usage()
  os.exit(0)
end

----------------------- run --------------------
dofile(configfile)

logger = logging.rolling_file(logparameter.filename,logparameter.filemaxsize,logparameter.maxindex)
logger:setLevel(logging.INFO)

queuemsg = "====\n"..os.date().."\n"..io.read("*a")
queue(queuemsg)

os.exit(0)

・/var/tmp/trap2irc.lua

---------------------- load section -----------------------
require "irc"
require "Memcached"
require "logging.rolling_file"
require "alt_getopt"

---------------------- function section ----------------------
function usage()
  print (string.format("%s", "Usage : " .. arg[0] .. " -c file"))
end

function botrun()
  -- Init Ktserver
  local memcache = Memcached.Connect(kyotoserverinfo.server,kyotoserverinfo.memport)

  -- set bot parameter
  local sleep = require "socket".sleep
  local s = irc.new{nick = botparameter.botnickname}

  -- ircserver connect
  s:connect(ircserverinfo)
  s:join(botparameter.joinchannel,botparameter.channelpass)

  -- recv msg logging callback
  s:hook("OnChat", function(user, channel, message)
    logger:info(string.format('%s',channel.." "..user.nick.." "..message))
  end)

  -- bot main loop
  while true do
    s:think()
    sleep(botparameter.sleeptime)

    for var = 1, botparameter.getmsgcount, 1 do
      local key = kyotoserverinfo.queuekeyword
      local workval = memcache:get(key)
      if workval ~= nil then
        s:sendChat(botparameter.joinchannel,string.format('%s',workval))
        memcache:delete(key)
        logger:debug(string.format('%s',"Get "..key..","..workval))
      else
        logger:debug(string.format('%s',"Get Error "..key))
      end
    end

  end
end

---------------------- init section -----------------------
local optarg
local optind
local long_opts = {
   conf = "c",
   help = "h"
}
opts,optind,optarg = alt_getopt.get_ordered_opts (arg, "c:h", long_opts)
for i,v in ipairs (opts) do
  if v == "c" then
    configfile = optarg[i]
  elseif v == "h" then
    usage()
    os.exit(0)
  end
end
if configfile == nil then
  usage()
  os.exit(0)
end

----------------------- run --------------------
dofile(configfile)

logger = logging.rolling_file(logparameter.filename,logparameter.filemaxsize,logparameter.maxindex)
logger:setLevel(logging.INFO)

botrun()

os.exit(0)


●起動
・ktserver

/usr/local/kyototycoon-0.9.53/bin/ktserver \
-port 11212 \
-th 2 \
-log /var/tmp/ktserver.log -ls \
-dmn -pid /var/tmp/ktserver.pid \
-ulog /var/tmp -sid 1 \
-plsv /usr/local/kyototycoon-0.9.53/libexec/ktplugservmemc.so \
-plex port=11211#tout=10#thnum=16#opts=fq#qtout=1 \
"/var/tmp/ircout.kct#ktopts=p"

※ulog,sidはレプリケーション等の考慮をしないなら、不要かと思います。

・bot

/usr/local/lua-5.1.4/bin/lua trap2irc.lua -c trap2irc.conf &

※実運用ではデーモン化させるのですが、適当にnohupして起動させる程度でもなんとかなります。

・snmptrapd

/usr/sfw/sbin/snmptrapd -n -OQ -c /var/tmp/snmptrapd.conf -o /var/tmp/snmptrapd.log -u /var/tmp/snmptrapd.pid

※/usr/sfwは、Solarisパターンの起動という感じで。

●テスト

snmptrap -v 2c -c public localhost '' .1.3.6.1.4.1.8072.99999 .1.3.6.1.4.1.8072.99999.1 s "Test Message"

※snmptrapd.confでそもそも制限を入れていないため、v2だろうがv1だろうがコミュニティ名設定も関係ないです。
 真面目に設定する場合は辺も考慮すれば良いかなと。