0
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local ipairs = Luan.ipairs or error()
|
|
4 local try = Luan.try or error()
|
|
5 local Io = require "luan:Io.luan"
|
|
6 local uri = Io.uri or error()
|
|
7 local String = require "luan:String.luan"
|
|
8 local trim = String.trim or error()
|
|
9 local Table = require "luan:Table.luan"
|
|
10 local Mail = require "luan:mail/Mail.luan"
|
|
11 local Init = require "file:conf/Init.luan"
|
|
12 local Logging = require "luan:logging/Logging.luan"
|
|
13 local logger = Logging.logger "Utils"
|
|
14
|
|
15
|
|
16 local Utils = {}
|
|
17
|
|
18 function Utils.ssh(domain,cmd)
|
|
19 local cmd = "ssh -oConnectTimeout=10 -oServerAliveInterval=10 -oBatchMode=yes -oStrictHostKeyChecking=no -p 14299 administrator@"..domain.." '"..cmd.."'"
|
|
20 local con = uri("bash:"..cmd)
|
|
21 return con.read_text()
|
|
22 end
|
|
23
|
|
24 function Utils.ssh_is_ok(domain)
|
|
25 return try {
|
|
26 function()
|
|
27 local s = Utils.ssh(domain,"echo ssh-ok")
|
|
28 if trim(s) ~= "ssh-ok" then
|
|
29 error("check_ssh failed with: "..s)
|
|
30 end
|
|
31 return true
|
|
32 end
|
|
33 catch = function(e)
|
|
34 logger.error(e.get_message())
|
|
35 Utils.send_mail {
|
|
36 subject = domain.." ssh_is_ok failed"
|
|
37 body = ""
|
|
38 }
|
|
39 return false
|
|
40 end
|
|
41 }
|
|
42 end
|
|
43
|
|
44
|
|
45 java()
|
|
46 local MailHome = require "java:fschmidt.util.mail.MailHome"
|
|
47 local MailAddress = require "java:fschmidt.util.mail.MailAddress"
|
|
48 local PlainTextContent = require "java:fschmidt.util.mail.PlainTextContent"
|
|
49
|
|
50 function Utils.send_mail(mail)
|
|
51 mail.from = mail.from or Init.domain.."<monitor@nabble.com>"
|
|
52 mail.to = mail.to or Init.monitor_emails or error()
|
|
53
|
|
54 local jmail = MailHome.newMail()
|
|
55 jmail.setFrom( MailAddress.new(mail.from) )
|
|
56 local mail_to = {}
|
|
57 for _,to in ipairs(mail.to) do
|
|
58 mail_to[#mail_to+1] = MailAddress.new(to)
|
|
59 end
|
|
60 jmail.setTo( Table.unpack(mail_to) )
|
|
61 jmail.setSubject( mail.subject )
|
|
62 jmail.setContent( PlainTextContent.new(mail.body) )
|
|
63 MailHome.getDefaultSmtpServer().send(jmail)
|
|
64 end
|
|
65
|
|
66
|
|
67 return Utils
|