Mercurial > prosody-modules
annotate mod_streamstats/mod_streamstats.lua @ 681:03ef667c96c3
mod_host_guard: removed s2smanager import.
author | Marco Cirillo <maranda@lightwitch.org> |
---|---|
date | Sat, 26 May 2012 22:48:03 +0000 |
parents | d4f3754c4286 |
children | a7f7a7e75737 |
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; |
639
d4f3754c4286
mod_streamstats: Import count and keys from util.iterators
Matthew Wild <mwild1@gmail.com>
parents:
259
diff
changeset
|
2 local iter = require "util.iterators"; |
d4f3754c4286
mod_streamstats: Import count and keys from util.iterators
Matthew Wild <mwild1@gmail.com>
parents:
259
diff
changeset
|
3 local count, keys = iter.count, iter.keys; |
259
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 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
|
6 stats = { |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 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
|
8 |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 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
|
10 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
|
11 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
|
12 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
|
13 end |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 end; |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 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
|
17 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
|
18 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
|
19 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
|
20 end; |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 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
|
23 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
|
24 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
|
25 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
|
26 end; |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 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
|
29 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
|
30 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
|
31 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
|
32 end |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 end; |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 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
|
36 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
|
37 end; |
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 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
|
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 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
|
42 |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 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
|
44 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
|
45 end |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 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
|
48 end |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 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
|
51 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
|
52 end |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 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
|
55 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
|
56 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
|
57 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
|
58 end); |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 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
|
60 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
|
61 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
|
62 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
|
63 end |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 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
|
65 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
|
66 end); |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 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
|
69 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
|
70 end |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 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
|
73 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
|
74 end); |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 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
|
76 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
|
77 end); |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 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
|
80 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
|
81 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
|
82 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
|
83 end |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 end |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 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
|
86 |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 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
|
88 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
|
89 end); |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 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
|
91 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
|
92 end); |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 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
|
94 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
|
95 end); |
d137515e0701
mod_streamstats: New module that streams live stats from Prosody over a TCP stream
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 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
|
97 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
|
98 end); |