2017-07-26 80 views
2

我正在寻找一个Lua脚本的帮助。本质上,我希望能够在今天之前的X分钟内找到接近的日期。在下面的例子中,我用了9000分钟。匹配接近的日期在Lua中

alarm.get() 

message = "Certificate Expiry Warning - Do something" 
SUPPKEY = "Certificate Expiry" 
SUBSYS = "1.1" 
SOURCE = "SERVERNAME" 

--local pattern = "(%d-%m-%Y)" 

local t = os.date('*t'); -- get current date and time 

print(os.date("%d-%m-%Y")); --Prints todays date 

t.min = t.min - 9000; -- subtract 9000 minutes 

--print(os.date("%Y-%m-%d %H:%m:%S", os.time(t))); --Original Script 

print(os.date("%d-%m-%Y", os.time(t))); --Prints alerting date 

if string.match ~=t.min --Match string 

--if string.match(a.message, pattern) 

--then print (al.message) 

then print ("We have a match") 

--then nimbus.alarm (1, message , SUPPKEY , SUBSYS , SOURCE) --Sends alert 

else print ("Everything is fine") --Postive, no alert 

--else print (al.message) 

end 

的alarm.get抓取的文本行,看起来像这样:

域\用户名,Web服务器(Web服务器),13/01/2017年09:13,13/01/2019,COMPANY_NAME,HOSTNAME_FQDN,SITE

因此,上面显示的行作为a.message变量传递,我希望将粗体突出显示的日期与今天的日期相匹配,并将9000分钟取下。

注释掉的部分只是我测试不同的东西。

回答

2

我不知道如果我的理解这个问题很好,但是从我的角度看来你正在尝试做的两件事情:

  1. 检索当前时间减去格式DD/MM/YYYY9000分钟。
  2. 将此时间与您的程序从文件中读取的时间进行比较,并在两个日期相等时执行一些操作。

这里去我的示例代码:

-- Settings 
local ALLOWED_AGE = 9000 -- In minutes 

-- Input line (for testing only) 
local inputstr = "DOMAIN\\USERNAME,Web Server (WebServer),13/01/2017 09:13,13/01/2019,COMPANY_NAME,HOSTNAME_FQDN,SITE" 

-- Separate line into 7 variables by token "," 
local path, server, time, date, company_name, hostname, site = string.match(inputstr, "([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+)") 

-- Check, if the line is ok (not necessary, but should be here to handle possible errors) 
-- Also note, some additional checks should be here (eg. regex to match DD/MM/YYYY format) 
if date == nil then 
    print("Error reading line: "..inputstr) 
end 

-- Get current time minus 9000 minutes (in format DD/MM/YYYY) 
local target_date = os.date("%d/%m/%Y", os.time() - ALLOWED_AGE * 60) 

-- Printing what we got (for testing purposes) 
print("Target date: "..target_date..", Input date: "..date) 

-- Testing the match 
if target_date == date then 
    print("Dates are matched!") 
else 
    print("Dates are not matched!") 
end 

虽然我不知道,你是否不应该不是检查“一个日期是大/小那么其他”你的情况。

然后上面的代码应该进行修改,以这样的事:

-- Extract day, month and year from date in format DD/MM/YYYY 
local d, m, y = string.match(date, "([^/]+)/([^/]+)/([^/]+)") 
-- Note I'm adding one day, so the certificate will actually expire day after it's "valid until" date. 
local valid_until = os.time({year = y, month = m, day = d + 1}) 
local expire_time = os.time() - ALLOWED_AGE * 60 -- All certificates older than this should expire. 

-- Printing what we got (for testing purposes) 
print("Expire time: "..expire_time..", Cert valid until: "..valid_until) 

-- Is expired? 
if valid_until <= expire_time then 
    print("Oops! Certificate expired.") 
else 
    print("Certificate date is valid.") 
end 
+2

检查平等绝对是错误的做法!你想检查'date> = target_date',以防万一你在相等的时候错过了神奇的时刻... –

+0

添加了“替代”解决方案来反映@Milo Christiansen的评论。 – Electrix

+0

这一切都很棒,你完全理解我!非常感谢你们两位。我会测试这个并报告回来。 – greenage