0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Lua on RTX1210 (4) - ハードウェア監視

Posted at

検証環境

RTX1210 BootROM Ver. 1.04
RTX1210 FlashROM Table Ver. 1.00
RTX1210 Rev.14.01.38 (Fri Jul 10 09:41:02 2020)

前提

注意事項

このスクリプトだけは Shift-JIS で保存しなければならない。
pattern = "筐体内温度%(℃%): (%d+)" が拾えない、Slackへのペイロードが空になるなどの不具合が起きる。

monitor_hardware.lua

monitor_hardware.lua
--
-- monitor_hardware.lua (Shift-JIS)
-- Hardware monitoring
--

require "strict"
local slack = require "slack"

local idle_time = 43200 -- = 60 * 60 * 12 [s]

local function log(priority, message)
  if priority == "DEBUG" then
    rt.syslog("notice", message)
  else
    rt.syslog("info", message)
    slack.post(priority, message)
  end
end

local resource_table = {
  cpu_5sec = { index = 1, pattern = "(%d+)%%%(5sec%)", name = "CPU(5sec)", unit = "%" }, -- (1)
  cpu_1min = { index = 2, pattern = "(%d+)%%%(1min%)", name = "CPU(1min)", unit = "%" },
  cpu_5min = { index = 3, pattern = "(%d+)%%%(5min%)", name = "CPU(5min)", unit = "%" },
  memory = { index = 4, pattern = "(%d+)%% used", name = "Memory", unit = "%" },
  packet_small = { index = 5, pattern = "(%d+)%%%(small%)", name = "Packet Buffer(small)", unit = "%" },
  packet_middle = { index = 6, pattern = "(%d+)%%%(middle%)", name = "Packet Buffer(middle)", unit = "%" },
  packet_large = { index = 7, pattern = "(%d+)%%%(large%)", name = "Packet Buffer(large)", unit = "%" },
  packet_huge = { index = 8, pattern = "(%d+)%%%(huge%)", name = "Packet Buffer(huge)", unit = "%" },
  temp = { index = 9, pattern = "筐体内温度%(℃%): (%d+)", name = "Temperature", unit = "C" }
}

local function rt_res_status()
  local cmd = "show environment"
  local rtn, str = rt.command(cmd)
  local rtn_table = {}
  local rtn_message = ""
  if rtn and str then
    for k, v in pairs(resource_table) do
      local value = str:match(v.pattern)
      if value then
        rtn_table[v.index] = v.name .. ": " .. value .. v.unit
      end
    end
    for i, v in ipairs(rtn_table) do
      rtn_message = rtn_message .. " " .. v
    end
    return rtn, rtn_message
  end
end

while true do
  local rtn, rtn_message = rt_res_status()
  if rtn then
    print(rtn_message) -- for DEBUG
    log("info", "(monitor_hardware.lua) " .. rtn_message)
  else
    log("info", "(monitor_hardware.lua) Failed to get environment information.")
  end
	rt.sleep(idle_time)
end

(1): pairs() が順序保証しないことに気づくまでしばらくハマった。

スケジュール

起動時に実行するよう設定。

schedule at 2 startup * lua /lua/monitor_hardware.lua

最終回予告

No-IP DDNSを定時更新してみる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?