#!/opt/lnxall_app/bin/lua local cjson = require("cjson") local socket = require("socket") local function check_template_files(config_path) local base_dir = "/app/template/" local file = io.open(config_path, "r") if not file then return false, "-打开文件失败:" .. config_path end local content = file:read("*all") file:close() local config = cjson.decode(content) local missing_files = {} if config.dev_class then for _, class in ipairs(config.dev_class) do if class.devs then for _, dev in ipairs(class.devs) do local tpl_name = dev.template if tpl_name and tpl_name ~= "" then local full_path = base_dir .. tpl_name local f = io.open(full_path, "r") if not f then table.insert(missing_files, full_path) else f:close() end end end end end end if #missing_files > 0 then ext_info = "-缺少以下模板文件:" for _, path in ipairs(missing_files) do ext_info = ext_info .. "\n--" .. path end return false, ext_info else return true end return false end local function check_port(ip, port) local sock = socket.tcp() sock:settimeout(1) local result, err = sock:connect(ip, port) sock:close() if result then return true else return false end end local function check_lc_ip(cfg) local file = io.open(cfg, "r") if not file then return false, "-打开文件失败:" .. cfg end local content = file:read("*all") file:close() local data = cjson.decode(content) local no_list = {} if data.devices then for _, device in ipairs(data.devices) do if device.type == "ems" then if not check_port(device.ipaddr, 4840) then return false, "-LC_NO:" .. device.no .. ",ip:" .. device.ipaddr .. "端口4840未开启" end end end end return true end local function check_lc_template_files(cfg) local file = io.open(cfg, "r") if not file then return false, "-打开文件失败:" .. cfg end local content = file:read("*all") file:close() local data = cjson.decode(content) local no_list = {} if data.devices then for _, device in ipairs(data.devices) do if device.type ~= "ems" then local ret, ext_info = check_template_files("/app/config/lc_devs/" .. device.no .. "/device_config.json") if not ret then return false, ext_info end end end end return true end local function check_emms2_ip() local cfg = "/app/config/system_cfg.json" local file = io.open(cfg, "r") if not file then return false, "-打开文件失败:" .. cfg end local content = file:read("*all") file:close() local sys_cfg = cjson.decode(content) if sys_cfg.en_mqtt_emms2 ~= 1 then return true end -- local cfg = "/app/config/emms2_mqtt.json" local file = io.open(cfg, "r") if not file then return false, "-打开文件失败:" .. cfg end local content = file:read("*all") file:close() local sys_cfg = cjson.decode(content) local ret, ext_info = check_port(sys_cfg.host, sys_cfg.port) if not ret then return false, "-EMMS2_MQTT:" .. sys_cfg.host .. ":" .. sys_cfg.port .. "未开启" else return true end end local function check_stactrl_ip() local cfg = "/app/config/system_cfg.json" local file = io.open(cfg, "r") if not file then return false, "-打开文件失败:" .. cfg end local content = file:read("*all") file:close() local sys_cfg = cjson.decode(content) if sys_cfg.en_mqtt_stactrl ~= 1 then return true end -- local cfg = "/app/config/stactrl_mqtt.json" local file = io.open(cfg, "r") if not file then return false, "-打开文件失败:" .. cfg end local content = file:read("*all") file:close() local sys_cfg = cjson.decode(content) local ret, ext_info = check_port(sys_cfg.host, sys_cfg.port) if not ret then return false, "-STACTRL_MQTT:" .. sys_cfg.host .. ":" .. sys_cfg.port .. "未开启" else return true end end local function check_directory(path) local result = os.execute("test -d " .. path) if result == true or result == 0 then return true else return false end end local function check_json_file(file_path) local file = io.open(file_path, "r") if not file then return false, "-打开文件失败:" .. file_path end local content = file:read("*all") file:close() local status, result = pcall(function() return cjson.decode(content) end) if not status then return false, "不是一个合法的json文件: " .. file_path end return true end local check_functions = { {name = "检查80端口", func = check_port, param = {"localhost", 80}}, {name = "检查4840端口", func = check_port, param = {"localhost", 4840}}, {name = "检查数据库目录", func = check_directory, param = {"/app/database"}}, {name = "检查从机端口", func = check_lc_ip, param = {"/app/config/ems_lc_list.json"}}, {name = "检查EMS层设备模板配置", func = check_template_files, param = {"/app/config/device_config.json"}}, {name = "检查LC层设备模板配置", func = check_lc_template_files, param = {"/app/config/ems_lc_list.json"}}, {name = "检查云平台MQTT端口", func = check_emms2_ip, param = {"no_use"}}, {name = "检查站控MQTT端口", func = check_stactrl_ip, param = {"no_use"}}, {name = "检查告警模板", func = check_json_file, param = {"/app/alarminfo/alarm_def.json"}}, } function run_self_check() print("业务应用开始自检...") for i, check_item in ipairs(check_functions) do local result, ext_info = check_item.func(unpack(check_item.param)) if not result then print(check_item.name .. ":失败") if ext_info then print(ext_info) end else print(check_item.name .. ":成功") end end end local exit_code = run_self_check() os.exit(exit_code)