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