Mercurial > prosody-modules
comparison mod_streamstats/mod_streamstats.lua @ 259:d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 07 Oct 2010 16:20:28 +0100 |
parents | |
children | d4f3754c4286 |
comparison
equal
deleted
inserted
replaced
258:36648205b10a | 259:d137515e0701 |
---|---|
1 local stats = prosody.stats; | |
2 | |
3 if not stats then | |
4 stats = { | |
5 stats = {}; conns = {}; | |
6 | |
7 broadcast = function (self, stat) | |
8 local value = self.stats[stat]; | |
9 for conn in pairs(self.conns) do | |
10 conn:write(stat..":"..value.."\n"); | |
11 end | |
12 end; | |
13 | |
14 adjust = function (self, stat, delta) | |
15 if delta == 0 then return; end | |
16 self.stats[stat] = (self.stats[stat] or 0) + delta; | |
17 self:broadcast(stat); | |
18 end; | |
19 | |
20 set = function (self, stat, value) | |
21 if value == self.stats[stat] then return; end | |
22 self.stats[stat] = value; | |
23 self:broadcast(stat); | |
24 end; | |
25 | |
26 add_conn = function (self, conn) | |
27 self.conns[conn] = true; | |
28 for stat, value in pairs(self.stats) do | |
29 conn:write(stat..":"..value.."\n"); | |
30 end | |
31 end; | |
32 | |
33 remove_conn = function (self, conn) | |
34 self.conns[conn] = nil; | |
35 end; | |
36 }; | |
37 prosody.stats = stats; | |
38 | |
39 local network = {}; | |
40 | |
41 function network.onconnect(conn) | |
42 stats:add_conn(conn); | |
43 end | |
44 | |
45 function network.onincoming(conn, data) | |
46 end | |
47 | |
48 function network.ondisconnect(conn, reason) | |
49 stats:remove_conn(conn); | |
50 end | |
51 | |
52 require "util.iterators"; | |
53 require "util.timer".add_task(1, function () | |
54 stats:set("s2s-in", count(keys(prosody.incoming_s2s))); | |
55 return math.random(10, 20); | |
56 end); | |
57 require "util.timer".add_task(3, function () | |
58 local s2sout_count = 0; | |
59 for _, host in pairs(prosody.hosts) do | |
60 s2sout_count = s2sout_count + count(keys(host.s2sout)); | |
61 end | |
62 stats:set("s2s-out", s2sout_count); | |
63 return math.random(10, 20); | |
64 end); | |
65 | |
66 require "net.connlisteners".register("stats", network); | |
67 require "net.connlisteners".start("stats", { port = module:get_option("stats_ports") or 5444, interface = "127.0.0.1" }); | |
68 end | |
69 | |
70 module:hook("resource-bind", function () | |
71 stats:adjust("c2s", 1); | |
72 end); | |
73 module:hook("resource-unbind", function () | |
74 stats:adjust("c2s", -1); | |
75 end); | |
76 | |
77 local c2s_count = 0; | |
78 for username, user in pairs(hosts[module.host].sessions or {}) do | |
79 for resource, session in pairs(user.sessions or {}) do | |
80 c2s_count = c2s_count + 1; | |
81 end | |
82 end | |
83 stats:adjust("c2s", c2s_count); | |
84 | |
85 module:hook("s2sin-established", function (event) | |
86 stats:adjust("s2s-in", 1); | |
87 end); | |
88 module:hook("s2sin-destroyed", function (event) | |
89 stats:adjust("s2s-in", -1); | |
90 end); | |
91 module:hook("s2sout-established", function (event) | |
92 stats:adjust("s2s-out", 1); | |
93 end); | |
94 module:hook("s2sout-destroyed", function (event) | |
95 stats:adjust("s2s-out", -1); | |
96 end); |