Mercurial > prosody-modules
comparison mod_streamstats/mod_streamstats.lua @ 741:a7f7a7e75737
mod_streamstats: Switch to portmanager and other 0.9 goodness
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 11 Jul 2012 19:07:15 +0200 |
parents | d4f3754c4286 |
children |
comparison
equal
deleted
inserted
replaced
740:1861d6ba6ff6 | 741:a7f7a7e75737 |
---|---|
1 local stats = prosody.stats; | 1 module:set_global(); |
2 local stats = module:shared"stats"; | |
2 local iter = require "util.iterators"; | 3 local iter = require "util.iterators"; |
3 local count, keys = iter.count, iter.keys; | 4 local count, keys = iter.count, iter.keys; |
4 | 5 |
5 if not stats then | 6 stats.stats = stats.stats or {}; |
6 stats = { | 7 stats.conns = stats.conns or {}; |
7 stats = {}; conns = {}; | 8 |
8 | 9 setmetatable(stats, { |
10 __index = { | |
11 | |
9 broadcast = function (self, stat) | 12 broadcast = function (self, stat) |
10 local value = self.stats[stat]; | 13 local value = self.stats[stat]; |
11 for conn in pairs(self.conns) do | 14 for conn in pairs(self.conns) do |
12 conn:write(stat..":"..value.."\n"); | 15 conn:write(stat..":"..value.."\n"); |
13 end | 16 end |
22 set = function (self, stat, value) | 25 set = function (self, stat, value) |
23 if value == self.stats[stat] then return; end | 26 if value == self.stats[stat] then return; end |
24 self.stats[stat] = value; | 27 self.stats[stat] = value; |
25 self:broadcast(stat); | 28 self:broadcast(stat); |
26 end; | 29 end; |
27 | 30 |
28 add_conn = function (self, conn) | 31 add_conn = function (self, conn) |
29 self.conns[conn] = true; | 32 self.conns[conn] = true; |
30 for stat, value in pairs(self.stats) do | 33 for stat, value in pairs(self.stats) do |
31 conn:write(stat..":"..value.."\n"); | 34 conn:write(stat..":"..value.."\n"); |
32 end | 35 end |
33 end; | 36 end; |
34 | 37 |
35 remove_conn = function (self, conn) | 38 remove_conn = function (self, conn) |
36 self.conns[conn] = nil; | 39 self.conns[conn] = nil; |
37 end; | 40 end; |
38 }; | 41 }; |
39 prosody.stats = stats; | 42 }); |
40 | 43 |
41 local network = {}; | 44 local network = {}; |
42 | 45 |
43 function network.onconnect(conn) | 46 function network.onconnect(conn) |
44 stats:add_conn(conn); | 47 stats:add_conn(conn); |
45 end | |
46 | |
47 function network.onincoming(conn, data) | |
48 end | |
49 | |
50 function network.ondisconnect(conn, reason) | |
51 stats:remove_conn(conn); | |
52 end | |
53 | |
54 require "util.iterators"; | |
55 require "util.timer".add_task(1, function () | |
56 stats:set("s2s-in", count(keys(prosody.incoming_s2s))); | |
57 return math.random(10, 20); | |
58 end); | |
59 require "util.timer".add_task(3, function () | |
60 local s2sout_count = 0; | |
61 for _, host in pairs(prosody.hosts) do | |
62 s2sout_count = s2sout_count + count(keys(host.s2sout)); | |
63 end | |
64 stats:set("s2s-out", s2sout_count); | |
65 return math.random(10, 20); | |
66 end); | |
67 | |
68 require "net.connlisteners".register("stats", network); | |
69 require "net.connlisteners".start("stats", { port = module:get_option("stats_ports") or 5444, interface = "127.0.0.1" }); | |
70 end | 48 end |
71 | 49 |
72 module:hook("resource-bind", function () | 50 function network.onincoming(conn, data) |
73 stats:adjust("c2s", 1); | 51 end |
52 | |
53 function network.ondisconnect(conn, reason) | |
54 stats:remove_conn(conn); | |
55 end | |
56 | |
57 module:add_timer(1, function () | |
58 stats:set("s2s-in", count(keys(prosody.incoming_s2s))); | |
59 return math.random(10, 20); | |
74 end); | 60 end); |
75 module:hook("resource-unbind", function () | 61 module:add_timer(3, function () |
76 stats:adjust("c2s", -1); | 62 local s2sout_count = 0; |
63 for _, host in pairs(prosody.hosts) do | |
64 s2sout_count = s2sout_count + count(keys(host.s2sout)); | |
65 end | |
66 stats:set("s2s-out", s2sout_count); | |
67 return math.random(10, 20); | |
77 end); | 68 end); |
78 | 69 |
79 local c2s_count = 0; | 70 |
80 for username, user in pairs(hosts[module.host].sessions or {}) do | 71 function module.add_host(module) |
81 for resource, session in pairs(user.sessions or {}) do | 72 module:hook("resource-bind", function () |
82 c2s_count = c2s_count + 1; | 73 stats:adjust("c2s", 1); |
74 end); | |
75 module:hook("resource-unbind", function () | |
76 stats:adjust("c2s", -1); | |
77 end); | |
78 | |
79 local c2s_count = 0; | |
80 for username, user in pairs(hosts[module.host].sessions or {}) do | |
81 for resource, session in pairs(user.sessions or {}) do | |
82 c2s_count = c2s_count + 1; | |
83 end | |
83 end | 84 end |
85 stats:set("c2s", c2s_count); | |
86 | |
87 module:hook("s2sin-established", function (event) | |
88 stats:adjust("s2s-in", 1); | |
89 end); | |
90 module:hook("s2sin-destroyed", function (event) | |
91 stats:adjust("s2s-in", -1); | |
92 end); | |
93 module:hook("s2sout-established", function (event) | |
94 stats:adjust("s2s-out", 1); | |
95 end); | |
96 module:hook("s2sout-destroyed", function (event) | |
97 stats:adjust("s2s-out", -1); | |
98 end); | |
84 end | 99 end |
85 stats:adjust("c2s", c2s_count); | |
86 | 100 |
87 module:hook("s2sin-established", function (event) | 101 module:provides("net", { |
88 stats:adjust("s2s-in", 1); | 102 default_port = 5444; |
89 end); | 103 listener = network; |
90 module:hook("s2sin-destroyed", function (event) | 104 }); |
91 stats:adjust("s2s-in", -1); | |
92 end); | |
93 module:hook("s2sout-established", function (event) | |
94 stats:adjust("s2s-out", 1); | |
95 end); | |
96 module:hook("s2sout-destroyed", function (event) | |
97 stats:adjust("s2s-out", -1); | |
98 end); |