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

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

add support for timings in lua and measure each phase in smtp, 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     local start_time = noit.timeval.now()
45     local success = true
46     if tosend then
47       write_cmd(e, tosend)
48     end
49     local actual_code, message = read_cmd(e)
50     if expected_code ~= actual_code then
51       check.status(string.format("%d/%d %s", expected_code, actual_code, message))
52       check.bad()
53       success = false
54     else
55       check.available()
56     end
57     local elapsed = noit.timeval.now() - start_time
58     local elapsed_ms = math.floor(tostring(elapsed) * 1000)
59     check.metric(phase .. "_time",  elapsed_ms)
60     return success
61   end
62 end
63
64 function initiate(module, check)
65   local e = noit.socket()
66   local rv, err = e.connect(check.target, check.config.port or 25)
67   check.unavailable()
68
69   if rv ~= 0 then
70     check.bad()
71     check.status(err or message or "no connection")
72     return
73   end
74
75   local mailfrom = string.format("MAIL FROM:<%s>", check.config.from or "")
76   local rcptto = string.format("RCPT TO:<%s>", check.config.to)
77   local action = mkaction(e, check)
78   if     action("banner", nil, 220)
79      and action("mailfrom", mailfrom, 250)
80      and action("rcptto", rcptto, 250)
81      and action("data", "DATA", 354)
82      and action("body", "Subject: Test\r\n\r\nHello.\r\n.", 250)
83      and action("quit", "QUIT", 221)
84   then
85     check.status("mail sent")
86     check.good()
87   end
88 end
89
Note: See TracBrowser for help on using the browser.