Software /
code /
prosody-modules
Annotate
mod_statistics/stats.lib.lua @ 1077:b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 15 Jun 2013 20:57:18 +0100 |
parent | 1076:5616cab8b6d6 |
child | 1080:3af947e2e6d4 |
rev | line source |
---|---|
1072
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local it = require "util.iterators"; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local log = require "util.logger".init("stats"); |
1077
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
3 local has_pposix, pposix = pcall(require, "util.pposix"); |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
4 local human; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
5 do |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
6 local tostring = tostring; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
7 local s_format = string.format; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
8 local m_floor = math.floor; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
9 local m_max = math.max; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
10 local prefixes = "kMGTPEZY"; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
11 local multiplier = 1024; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
12 |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
13 function human(num) |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
14 num = tonumber(num) or 0; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
15 local m = 0; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
16 while num >= multiplier and m < #prefixes do |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
17 num = num / multiplier; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
18 m = m + 1; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
19 end |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
20 |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
21 return s_format("%0."..m_max(0,3-#tostring(m_floor(num))).."f%sB", |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
22 num, m > 0 and (prefixes:sub(m,m) .. "i") or ""); |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
23 end |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
24 end |
1072
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 local last_cpu_wall, last_cpu_clock; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 local get_time = require "socket".gettime; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 local active_sessions, active_jids = {}, {}; |
1076
5616cab8b6d6
mod_statistics/stats.lib.lua: Better s2s session counting
Kim Alvefur <zash@zash.se>
parents:
1075
diff
changeset
|
30 local s2s_sessions = module:shared"/*/s2s/sessions"; |
5616cab8b6d6
mod_statistics/stats.lib.lua: Better s2s session counting
Kim Alvefur <zash@zash.se>
parents:
1075
diff
changeset
|
31 local c2s_sessions = module:shared"/*/c2s/sessions"; |
1072
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 local stats = { |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 total_users = { |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 get = function () return it.count(it.keys(bare_sessions)); end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 }; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 total_c2s = { |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 get = function () return it.count(it.keys(full_sessions)); end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 }; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 total_s2sin = { |
1076
5616cab8b6d6
mod_statistics/stats.lib.lua: Better s2s session counting
Kim Alvefur <zash@zash.se>
parents:
1075
diff
changeset
|
41 get = function () local i = 0; for conn,sess in next,s2s_sessions do if sess.direction == "incoming" then i = i + 1 end end return i end |
1072
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 }; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 total_s2sout = { |
1076
5616cab8b6d6
mod_statistics/stats.lib.lua: Better s2s session counting
Kim Alvefur <zash@zash.se>
parents:
1075
diff
changeset
|
44 get = function () local i = 0; for conn,sess in next,s2s_sessions do if sess.direction == "outgoing" then i = i + 1 end end return i end |
5616cab8b6d6
mod_statistics/stats.lib.lua: Better s2s session counting
Kim Alvefur <zash@zash.se>
parents:
1075
diff
changeset
|
45 }; |
5616cab8b6d6
mod_statistics/stats.lib.lua: Better s2s session counting
Kim Alvefur <zash@zash.se>
parents:
1075
diff
changeset
|
46 total_s2s = { |
5616cab8b6d6
mod_statistics/stats.lib.lua: Better s2s session counting
Kim Alvefur <zash@zash.se>
parents:
1075
diff
changeset
|
47 get = function () return it.count(it.keys(s2s_sessions)); end |
1072
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 }; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 total_component = { |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 get = function () |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 local count = 0; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 for host, host_session in pairs(hosts) do |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 if host_session.type == "component" then |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 local c = host_session.modules.component; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 if c and c.connected then -- 0.9 only |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 count = count + 1; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 return count; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 }; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 up_since = { |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 get = function () return prosody.start_time; end; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 tostring = function (up_since) |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 return tostring(os.time()-up_since).."s"; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 end; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 }; |
1077
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
69 memory_lua = { |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
70 get = function () return math.ceil(collectgarbage("count")*1024); end; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
71 tostring = human; |
1072
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 }; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 time = { |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 tostring = function () return os.date("%T"); end; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 }; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 cpu = { |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 get = function () |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 local new_wall, new_clock = get_time(), os.clock(); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 local pc = 0; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 if last_cpu_wall then |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 pc = 100/((new_wall-last_cpu_wall)/(new_clock-last_cpu_clock)); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 last_cpu_wall, last_cpu_clock = new_wall, new_clock; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 return math.ceil(pc); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 end; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 tostring = "%s%%"; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 }; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 }; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 |
1077
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
90 if has_pposix and pposix.meminfo then |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
91 stats.memory_allocated = { |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
92 get = function () return math.ceil(pposix.meminfo().allocated); end; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
93 tostring = human; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
94 } |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
95 stats.memory_used = { |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
96 get = function () return math.ceil(pposix.meminfo().used); end; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
97 tostring = human; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
98 } |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
99 stats.memory_unused = { |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
100 get = function () return math.ceil(pposix.meminfo().unused); end; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
101 tostring = human; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
102 } |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
103 stats.memory_returnable = { |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
104 get = function () return math.ceil(pposix.meminfo().returnable); end; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
105 tostring = human; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
106 } |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
107 end |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
108 |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
109 local pagesize = 4096; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
110 stats.memory_total = { |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
111 get = function () |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
112 local statm, err = io.open"/proc/self/statm"; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
113 if statm then |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
114 local total = statm:read"*n"; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
115 statm:close(); |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
116 return total * pagesize; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
117 else |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
118 module:log("debug", err); |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
119 end |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
120 end; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
121 tostring = human; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
122 }; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
123 stats.memory_rss = { |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
124 get = function () |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
125 local statm, err = io.open"/proc/self/statm"; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
126 if statm then |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
127 statm:read"*n"; -- Total size, ignore |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
128 local rss = statm:read"*n"; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
129 statm:close(); |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
130 return rss * pagesize; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
131 else |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
132 module:log("debug", err); |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
133 end |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
134 end; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
135 tostring = human; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
136 }; |
b73d44afdafa
mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents:
1076
diff
changeset
|
137 |
1072
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 local add_statistics_filter; -- forward decl |
1075
164ed759b1d2
mod_statistics/stats.lib.lua: Better check for prosody vs prosodyctl
Kim Alvefur <zash@zash.se>
parents:
1072
diff
changeset
|
139 if prosody and prosody.arg then -- ensures we aren't in prosodyctl |
1072
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 setmetatable(active_sessions, { |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 __index = function ( t, k ) |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 local v = { |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 bytes_in = 0, bytes_out = 0; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 stanzas_in = { |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 message = 0, presence = 0, iq = 0; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 }; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 stanzas_out = { |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 message = 0, presence = 0, iq = 0; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 }; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 } |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 rawset(t, k, v); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 return v; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 }); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 local filters = require "util.filters"; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 local function handle_stanza_in(stanza, session) |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 local s = active_sessions[session].stanzas_in; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 local n = s[stanza.name]; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 if n then |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 s[stanza.name] = n + 1; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 return stanza; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 local function handle_stanza_out(stanza, session) |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 local s = active_sessions[session].stanzas_out; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 local n = s[stanza.name]; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 if n then |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 s[stanza.name] = n + 1; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 return stanza; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 local function handle_bytes_in(bytes, session) |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 local s = active_sessions[session]; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 s.bytes_in = s.bytes_in + #bytes; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 return bytes; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 local function handle_bytes_out(bytes, session) |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 local s = active_sessions[session]; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 s.bytes_out = s.bytes_out + #bytes; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 return bytes; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 function add_statistics_filter(session) |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 filters.add_filter(session, "stanzas/in", handle_stanza_in); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 filters.add_filter(session, "stanzas/out", handle_stanza_out); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 filters.add_filter(session, "bytes/in", handle_bytes_in); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 filters.add_filter(session, "bytes/out", handle_bytes_out); |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 end |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 return { |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 stats = stats; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 active_sessions = active_sessions; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 filter_hook = add_statistics_filter; |
4dbdb1b465e8
mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 }; |