comparison mod_statistics/stats.lib.lua @ 1072:4dbdb1b465e8

mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
author Matthew Wild <mwild1@gmail.com>
date Sat, 15 Jun 2013 19:08:34 +0100
parents
children 164ed759b1d2
comparison
equal deleted inserted replaced
1071:8f59b45fe6a7 1072:4dbdb1b465e8
1 local it = require "util.iterators";
2 local log = require "util.logger".init("stats");
3
4 local last_cpu_wall, last_cpu_clock;
5 local get_time = require "socket".gettime;
6
7 local active_sessions, active_jids = {}, {};
8
9 local stats = {
10 total_users = {
11 get = function () return it.count(it.keys(bare_sessions)); end
12 };
13 total_c2s = {
14 get = function () return it.count(it.keys(full_sessions)); end
15 };
16 total_s2sin = {
17 get = function () return it.count(it.keys(prosody.incoming_s2s)); end
18 };
19 total_s2sout = {
20 get = function ()
21 local count = 0;
22 for host, host_session in pairs(hosts) do
23 count = count + it.count(it.keys(host_session.s2sout));
24 end
25 return count;
26 end
27 };
28 total_component = {
29 get = function ()
30 local count = 0;
31 for host, host_session in pairs(hosts) do
32 if host_session.type == "component" then
33 local c = host_session.modules.component;
34 if c and c.connected then -- 0.9 only
35 count = count + 1;
36 end
37 end
38 end
39 return count;
40 end
41 };
42 up_since = {
43 get = function () return prosody.start_time; end;
44 tostring = function (up_since)
45 return tostring(os.time()-up_since).."s";
46 end;
47 };
48 memory_used = {
49 get = function () return collectgarbage("count")/1024; end;
50 tostring = "%0.2fMB";
51 };
52 memory_process = {
53 get = function () return pposix.meminfo().allocated/1048576; end;
54 tostring = "%0.2fMB";
55 };
56 time = {
57 tostring = function () return os.date("%T"); end;
58 };
59 cpu = {
60 get = function ()
61 local new_wall, new_clock = get_time(), os.clock();
62 local pc = 0;
63 if last_cpu_wall then
64 pc = 100/((new_wall-last_cpu_wall)/(new_clock-last_cpu_clock));
65 end
66 last_cpu_wall, last_cpu_clock = new_wall, new_clock;
67 return math.ceil(pc);
68 end;
69 tostring = "%s%%";
70 };
71 };
72
73 local add_statistics_filter; -- forward decl
74 if prosody and prosody.full_sessions then -- start_time ensures we aren't in prosodyctl
75 setmetatable(active_sessions, {
76 __index = function ( t, k )
77 local v = {
78 bytes_in = 0, bytes_out = 0;
79 stanzas_in = {
80 message = 0, presence = 0, iq = 0;
81 };
82 stanzas_out = {
83 message = 0, presence = 0, iq = 0;
84 };
85 }
86 rawset(t, k, v);
87 return v;
88 end
89 });
90 local filters = require "util.filters";
91 local function handle_stanza_in(stanza, session)
92 local s = active_sessions[session].stanzas_in;
93 local n = s[stanza.name];
94 if n then
95 s[stanza.name] = n + 1;
96 end
97 return stanza;
98 end
99 local function handle_stanza_out(stanza, session)
100 local s = active_sessions[session].stanzas_out;
101 local n = s[stanza.name];
102 if n then
103 s[stanza.name] = n + 1;
104 end
105 return stanza;
106 end
107 local function handle_bytes_in(bytes, session)
108 local s = active_sessions[session];
109 s.bytes_in = s.bytes_in + #bytes;
110 return bytes;
111 end
112 local function handle_bytes_out(bytes, session)
113 local s = active_sessions[session];
114 s.bytes_out = s.bytes_out + #bytes;
115 return bytes;
116 end
117 function add_statistics_filter(session)
118 filters.add_filter(session, "stanzas/in", handle_stanza_in);
119 filters.add_filter(session, "stanzas/out", handle_stanza_out);
120 filters.add_filter(session, "bytes/in", handle_bytes_in);
121 filters.add_filter(session, "bytes/out", handle_bytes_out);
122 end
123 end
124
125 return {
126 stats = stats;
127 active_sessions = active_sessions;
128 filter_hook = add_statistics_filter;
129 };