2017-07-26 82 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