root/src/modules-lua/noit/module/smtp.lua

Revision b2cc63038d9c296673961479f37207fd9a0c213c, 1.9 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

Start... needs timings, etc. refs #56

  • Property mode set to 100644
Line 
1 -- This connects to a Varnish instance on the management port (8081)
2 -- It issues the stats comment and translates the output into metrics
3
4 module(..., package.seeall)
5
6 function onload(image)
7   return 0
8 end
9
10 function init(module)
11   return 0
12 end
13
14 function config(module, options)
15   return 0
16 end
17
18 local function read_cmd(e)
19   local final_status, out
20   final_status, out = 0, ""
21   repeat
22     local str = e.read("\r\n")
23     local status, c, message = string.match(str, "^(%d+)([-%s])(.+)$")
24     if not status then
25       return 421, "[internal error]"
26     end
27     final_status = status
28     if string.len(out) > 0 then
29       out = string.format( "%s %s", out, message)
30     else
31       out = message
32     end
33   until c ~= "-"
34   return (final_status+0), out
35 end
36
37 local function write_cmd(e, cmd)
38   e.write(cmd);
39   e.write("\r\n");
40 end
41
42 local function mkaction(e, check)
43   return function (phase, tosend, expected_code)
44     if tosend then
45       write_cmd(e, tosend)
46     end
47     local actual_code, message = read_cmd(e)
48     if expected_code ~= actual_code then
49       check.status(string.format("%d/%d %s", expected_code, actual_code, message))
50       check.bad()
51       return false
52     end
53     check.available()
54     return true
55   end
56 end
57
58 function initiate(module, check)
59   local e = noit.socket()
60   local rv, err = e.connect(check.target, check.config.port or 25)
61   check.unavailable()
62
63   if rv ~= 0 then
64     check.bad()
65     check.status(err or message or "no connection")
66     return
67   end
68
69   local mailfrom = string.format("MAIL FROM:<%s>", check.config.from or "")
70   local rcptto = string.format("RCPT TO:<%s>", check.config.to)
71   local action = mkaction(e, check)
72   if     action("banner", nil, 220)
73      and action("mail from", mailfrom, 250)
74      and action("rcpt to", rcptto, 250)
75      and action("data", "DATA", 354)
76      and action("body", "Subject: Test\r\n\r\nHello.\r\n.", 250)
77      and action("quit", "QUIT", 221)
78   then
79     check.status("mail sent")
80     check.good()
81   end
82 end
83
Note: See TracBrowser for help on using the browser.