Annotate

mod_statistics/stats.lib.lua @ 6301:fa45ae704b79

mod_cloud_notify: Update Readme diff --git a/mod_cloud_notify/README.md b/mod_cloud_notify/README.md --- a/mod_cloud_notify/README.md +++ b/mod_cloud_notify/README.md @@ -1,109 +1,106 @@ ---- -labels: -- 'Stage-Beta' -summary: 'XEP-0357: Cloud push notifications' ---- +# Introduction -Introduction -============ +This module enables support for sending "push notifications" to clients +that need it, typically those running on certain mobile devices. -This module enables support for sending "push notifications" to clients that -need it, typically those running on certain mobile devices. +As well as this module, your client must support push notifications (the +apps that need it generally do, of course) and the app developer's push +gateway must be reachable from your Prosody server (this happens over a +normal XMPP server-to-server 's2s' connection). -As well as this module, your client must support push notifications (the apps -that need it generally do, of course) and the app developer's push gateway -must be reachable from your Prosody server (this happens over a normal XMPP -server-to-server 's2s' connection). - -Details -======= +# Details Some platforms, notably Apple's iOS and many versions of Android, impose -limits that prevent applications from running or accessing the network in the -background. This makes it difficult or impossible for an XMPP application to -remain reliably connected to a server to receive messages. - -In order for messaging and other apps to receive notifications, the OS vendors -run proprietary servers that their OS maintains a permanent connection to in -the background. Then they provide APIs to application developers that allow -sending notifications to specific devices via those servers. +limits that prevent applications from running or accessing the network +in the background. This makes it difficult or impossible for an XMPP +application to remain reliably connected to a server to receive +messages. -When you connect to your server with an app that requires push notifications, -it will use this module to set up a "push registration". When you receive -a message but your device is not connected to the server, this module will -generate a notification and send it to the push gateway operated by your -application's developers). Their gateway will then connect to your device's -OS vendor and ask them to forward the notification to your device. When your -device receives the notification, it will display it or wake up the app so it -can connect to XMPP and receive any pending messages. +In order for messaging and other apps to receive notifications, the OS +vendors run proprietary servers that their OS maintains a permanent +connection to in the background. Then they provide APIs to application +developers that allow sending notifications to specific devices via +those servers. -This protocol is described for developers in [XEP-0357: Push Notifications]. +When you connect to your server with an app that requires push +notifications, it will use this module to set up a "push registration". +When you receive a message but your device is not connected to the +server, this module will generate a notification and send it to the push +gateway operated by your application's developers). Their gateway will +then connect to your device's OS vendor and ask them to forward the +notification to your device. When your device receives the notification, +it will display it or wake up the app so it can connect to XMPP and +receive any pending messages. -For this module to work reliably, you must have [mod_smacks], [mod_mam] and -[mod_carbons] also enabled on your server. +This protocol is described for developers in \[XEP-0357: Push +Notifications\]. + +For this module to work reliably, you must have \[mod_smacks\], +\[mod_mam\] and \[mod_carbons\] also enabled on your server. -Some clients, notably Siskin and Snikket iOS need some additional extensions -that are not currently defined in a standard XEP. To support these clients, -see [mod_cloud_notify_extensions]. +Some clients, notably Siskin and Snikket iOS need some additional +extensions that are not currently defined in a standard XEP. To support +these clients, see \[mod_cloud_notify_extensions\]. -Configuration -============= +# Configuration - Option Default Description - ------------------------------------ ----------------- ------------------------------------------------------------------------------------------------------------------- - `push_notification_important_body` `New Message!` The body text to use when the stanza is important (see above), no message body is sent if this is empty - `push_max_errors` `16` How much persistent push errors are tolerated before notifications for the identifier in question are disabled - `push_max_devices` `5` The number of allowed devices per user (the oldest devices are automatically removed if this threshold is reached) - `push_max_hibernation_timeout` `259200` (72h) Number of seconds to extend the smacks timeout if no push was triggered yet (default: 72 hours) - `push_notification_with_body` (\*) `false` Whether or not to send the real message body to remote pubsub node. Without end-to-end encryption, enabling this may expose your message contents to your client developers and OS vendor. Not recommended. - `push_notification_with_sender` (\*) `false` Whether or not to send the real message sender to remote pubsub node. Enabling this may expose your contacts to your client developers and OS vendor. Not recommended. + Option Default Description + -------------------------------------- ---------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + `push_notification_important_body` `New Message!` The body text to use when the stanza is important (see above), no message body is sent if this is empty + `push_max_errors` `16` How much persistent push errors are tolerated before notifications for the identifier in question are disabled + `push_max_devices` `5` The number of allowed devices per user (the oldest devices are automatically removed if this threshold is reached) + `push_max_hibernation_timeout` `259200` (72h) Number of seconds to extend the smacks timeout if no push was triggered yet (default: 72 hours) + `push_notification_with_body` (\*) `false` Whether or not to send the real message body to remote pubsub node. Without end-to-end encryption, enabling this may expose your message contents to your client developers and OS vendor. Not recommended. + `push_notification_with_sender` (\*) `false` Whether or not to send the real message sender to remote pubsub node. Enabling this may expose your contacts to your client developers and OS vendor. Not recommended. -(\*) There are privacy implications for enabling these options. +(\*) There are privacy implications for enabling these options.[^1] -Internal design notes -===================== +# Internal design notes -App servers are notified about offline messages, messages stored by [mod_mam] -or messages waiting in the smacks queue. -The business rules outlined [here](//mail.jabber.org/pipermail/standards/2016-February/030925.html) are all honored[^2]. +App servers are notified about offline messages, messages stored by +\[mod_mam\] or messages waiting in the smacks queue. The business rules +outlined +[here](//mail.jabber.org/pipermail/standards/2016-February/030925.html) +are all honored[^2]. -To cooperate with [mod_smacks] this module consumes some events: -`smacks-ack-delayed`, `smacks-hibernation-start` and `smacks-hibernation-end`. -These events allow this module to send out notifications for messages received -while the session is hibernated by [mod_smacks] or even when smacks -acknowledgements for messages are delayed by a certain amount of seconds -configurable with the [mod_smacks] setting `smacks_max_ack_delay`. +To cooperate with \[mod_smacks\] this module consumes some events: +`smacks-ack-delayed`, `smacks-hibernation-start` and +`smacks-hibernation-end`. These events allow this module to send out +notifications for messages received while the session is hibernated by +\[mod_smacks\] or even when smacks acknowledgements for messages are +delayed by a certain amount of seconds configurable with the +\[mod_smacks\] setting `smacks_max_ack_delay`. -The `smacks_max_ack_delay` setting allows to send out notifications to clients -which aren't already in smacks hibernation state (because the read timeout or -connection close didn't already happen) but also aren't responding to acknowledgement -request in a timely manner. This setting thus allows conversations to be smoother -under such circumstances. +The `smacks_max_ack_delay` setting allows to send out notifications to +clients which aren't already in smacks hibernation state (because the +read timeout or connection close didn't already happen) but also aren't +responding to acknowledgement request in a timely manner. This setting +thus allows conversations to be smoother under such circumstances. -The new event `cloud-notify-ping` can be used by any module to send out a cloud -notification to either all registered endpoints for the given user or only the endpoints -given in the event data. +The new event `cloud-notify-ping` can be used by any module to send out +a cloud notification to either all registered endpoints for the given +user or only the endpoints given in the event data. -The config setting `push_notification_important_body` can be used to specify an alternative -body text to send to the remote pubsub node if the stanza is encrypted or has a body. -This way the real contents of the message aren't revealed to the push appserver but it -can still see that the push is important. -This is used by Chatsecure on iOS to send out high priority pushes in those cases for example. +The config setting `push_notification_important_body` can be used to +specify an alternative body text to send to the remote pubsub node if +the stanza is encrypted or has a body. This way the real contents of the +message aren't revealed to the push appserver but it can still see that +the push is important. This is used by Chatsecure on iOS to send out +high priority pushes in those cases for example. -Compatibility -============= - -**Note:** This module should be used with Lua 5.2 and higher. Using it with -Lua 5.1 may cause push notifications to not be sent to some clients. +# Compatibility ------- ----------------------------------------------------------------------------- - trunk Works - 0.12 Works - 0.11 Works - 0.10 Works - 0.9 Support dropped, use last supported version [675726ab06d3](//hg.prosody.im/prosody-modules/raw-file/675726ab06d3/mod_cloud_notify/mod_cloud_notify.lua) ------- ----------------------------------------------------------------------------- +**Note:** This module should be used with Lua 5.2 and higher. Using it +with Lua 5.1 may cause push notifications to not be sent to some +clients. + ------- ----------------------------------------------------------------- + trunk Works as of 25-06-13 + 13 Works + 0.12 Works + ------- ----------------------------------------------------------------- -[^1]: The service which is expected to forward notifications to something like Google Cloud Messaging or Apple Notification Service -[^2]: [business_rules.markdown](//hg.prosody.im/prosody-modules/file/tip/mod_cloud_notify/business_rules.markdown) +[^1]: The service which is expected to forward notifications to + something like Google Cloud Messaging or Apple Notification Service + +[^2]: [business_rules.md](//hg.prosody.im/prosody-modules/file/tip/mod_cloud_notify/business_rules.md)
author Menel <menel@snikket.de>
date Fri, 13 Jun 2025 10:36:52 +0200
parent 3217:063abaab666f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 = {}, {};
1080
3af947e2e6d4 mod_statistics/stats.lib.lua: Only fetch shared tables if running under Prosody
Matthew Wild <mwild1@gmail.com>
parents: 1077
diff changeset
30 local c2s_sessions, s2s_sessions;
3af947e2e6d4 mod_statistics/stats.lib.lua: Only fetch shared tables if running under Prosody
Matthew Wild <mwild1@gmail.com>
parents: 1077
diff changeset
31 if prosody and prosody.arg then
3217
063abaab666f mod_statistics: Split module:shared() into multiple calls, multiple params may be deprecated soon
Matthew Wild <mwild1@gmail.com>
parents: 2300
diff changeset
32 c2s_sessions = module:shared("/*/c2s/sessions");
063abaab666f mod_statistics: Split module:shared() into multiple calls, multiple params may be deprecated soon
Matthew Wild <mwild1@gmail.com>
parents: 2300
diff changeset
33 s2s_sessions = module:shared("/*/s2s/sessions");
1080
3af947e2e6d4 mod_statistics/stats.lib.lua: Only fetch shared tables if running under Prosody
Matthew Wild <mwild1@gmail.com>
parents: 1077
diff changeset
34 end
1072
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local stats = {
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 total_users = {
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(bare_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_c2s = {
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 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
42 };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 total_s2sin = {
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 == "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
45 };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 total_s2sout = {
1076
5616cab8b6d6 mod_statistics/stats.lib.lua: Better s2s session counting
Kim Alvefur <zash@zash.se>
parents: 1075
diff changeset
47 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
48 };
5616cab8b6d6 mod_statistics/stats.lib.lua: Better s2s session counting
Kim Alvefur <zash@zash.se>
parents: 1075
diff changeset
49 total_s2s = {
5616cab8b6d6 mod_statistics/stats.lib.lua: Better s2s session counting
Kim Alvefur <zash@zash.se>
parents: 1075
diff changeset
50 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
51 };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 total_component = {
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 get = function ()
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 local count = 0;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 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
56 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
57 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
58 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
59 count = count + 1;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
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 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 return count;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 up_since = {
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 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
68 tostring = function (up_since)
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 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
70 end;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 };
1077
b73d44afdafa mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents: 1076
diff changeset
72 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
73 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
74 tostring = human;
1072
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 time = {
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 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
78 };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 cpu = {
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 get = function ()
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 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
82 local pc = 0;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 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
84 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
85 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 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
87 return math.ceil(pc);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 end;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 tostring = "%s%%";
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92
2300
dded110af017 mod_statistics/stats: Guard usage of module:get_option() so we only call it when Prosody is running
Matthew Wild <mwild1@gmail.com>
parents: 2209
diff changeset
93 local memory_update_interval = 60;
dded110af017 mod_statistics/stats: Guard usage of module:get_option() so we only call it when Prosody is running
Matthew Wild <mwild1@gmail.com>
parents: 2209
diff changeset
94 if prosody and prosody.arg then
dded110af017 mod_statistics/stats: Guard usage of module:get_option() so we only call it when Prosody is running
Matthew Wild <mwild1@gmail.com>
parents: 2209
diff changeset
95 memory_update_interval = module:get_option_number("statistics_meminfo_interval", 60);
dded110af017 mod_statistics/stats: Guard usage of module:get_option() so we only call it when Prosody is running
Matthew Wild <mwild1@gmail.com>
parents: 2209
diff changeset
96 end
dded110af017 mod_statistics/stats: Guard usage of module:get_option() so we only call it when Prosody is running
Matthew Wild <mwild1@gmail.com>
parents: 2209
diff changeset
97
dded110af017 mod_statistics/stats: Guard usage of module:get_option() so we only call it when Prosody is running
Matthew Wild <mwild1@gmail.com>
parents: 2209
diff changeset
98
1077
b73d44afdafa mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents: 1076
diff changeset
99 if has_pposix and pposix.meminfo then
2209
2733cb8c82a9 mod_statistics: Add 'statistics_meminfo_interval' option to control the number of seconds between calls to mallinfo(). Between calls, cached results are used.
Matthew Wild <mwild1@gmail.com>
parents: 1081
diff changeset
100
2733cb8c82a9 mod_statistics: Add 'statistics_meminfo_interval' option to control the number of seconds between calls to mallinfo(). Between calls, cached results are used.
Matthew Wild <mwild1@gmail.com>
parents: 1081
diff changeset
101 local cached_meminfo, last_cache_update;
2733cb8c82a9 mod_statistics: Add 'statistics_meminfo_interval' option to control the number of seconds between calls to mallinfo(). Between calls, cached results are used.
Matthew Wild <mwild1@gmail.com>
parents: 1081
diff changeset
102 local function meminfo()
2733cb8c82a9 mod_statistics: Add 'statistics_meminfo_interval' option to control the number of seconds between calls to mallinfo(). Between calls, cached results are used.
Matthew Wild <mwild1@gmail.com>
parents: 1081
diff changeset
103 if not cached_meminfo or (os.time() - last_cache_update) > memory_update_interval then
2733cb8c82a9 mod_statistics: Add 'statistics_meminfo_interval' option to control the number of seconds between calls to mallinfo(). Between calls, cached results are used.
Matthew Wild <mwild1@gmail.com>
parents: 1081
diff changeset
104 cached_meminfo = pposix.meminfo();
2733cb8c82a9 mod_statistics: Add 'statistics_meminfo_interval' option to control the number of seconds between calls to mallinfo(). Between calls, cached results are used.
Matthew Wild <mwild1@gmail.com>
parents: 1081
diff changeset
105 last_cache_update = os.time();
2733cb8c82a9 mod_statistics: Add 'statistics_meminfo_interval' option to control the number of seconds between calls to mallinfo(). Between calls, cached results are used.
Matthew Wild <mwild1@gmail.com>
parents: 1081
diff changeset
106 end
2733cb8c82a9 mod_statistics: Add 'statistics_meminfo_interval' option to control the number of seconds between calls to mallinfo(). Between calls, cached results are used.
Matthew Wild <mwild1@gmail.com>
parents: 1081
diff changeset
107 return cached_meminfo;
2733cb8c82a9 mod_statistics: Add 'statistics_meminfo_interval' option to control the number of seconds between calls to mallinfo(). Between calls, cached results are used.
Matthew Wild <mwild1@gmail.com>
parents: 1081
diff changeset
108 end
2733cb8c82a9 mod_statistics: Add 'statistics_meminfo_interval' option to control the number of seconds between calls to mallinfo(). Between calls, cached results are used.
Matthew Wild <mwild1@gmail.com>
parents: 1081
diff changeset
109
1077
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_allocated = {
2209
2733cb8c82a9 mod_statistics: Add 'statistics_meminfo_interval' option to control the number of seconds between calls to mallinfo(). Between calls, cached results are used.
Matthew Wild <mwild1@gmail.com>
parents: 1081
diff changeset
111 get = function () return math.ceil(meminfo().allocated); end;
1077
b73d44afdafa mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents: 1076
diff changeset
112 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
113 }
b73d44afdafa mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents: 1076
diff changeset
114 stats.memory_used = {
2209
2733cb8c82a9 mod_statistics: Add 'statistics_meminfo_interval' option to control the number of seconds between calls to mallinfo(). Between calls, cached results are used.
Matthew Wild <mwild1@gmail.com>
parents: 1081
diff changeset
115 get = function () return math.ceil(meminfo().used); end;
1077
b73d44afdafa mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents: 1076
diff changeset
116 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
117 }
b73d44afdafa mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents: 1076
diff changeset
118 stats.memory_unused = {
2209
2733cb8c82a9 mod_statistics: Add 'statistics_meminfo_interval' option to control the number of seconds between calls to mallinfo(). Between calls, cached results are used.
Matthew Wild <mwild1@gmail.com>
parents: 1081
diff changeset
119 get = function () return math.ceil(meminfo().unused); end;
1077
b73d44afdafa mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents: 1076
diff changeset
120 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
121 }
b73d44afdafa mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents: 1076
diff changeset
122 stats.memory_returnable = {
2209
2733cb8c82a9 mod_statistics: Add 'statistics_meminfo_interval' option to control the number of seconds between calls to mallinfo(). Between calls, cached results are used.
Matthew Wild <mwild1@gmail.com>
parents: 1081
diff changeset
123 get = function () return math.ceil(meminfo().returnable); end;
1077
b73d44afdafa mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents: 1076
diff changeset
124 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
125 }
b73d44afdafa mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents: 1076
diff changeset
126 end
b73d44afdafa mod_statistics/stats.lib.lua: Improve memory stats (use pposix.meminfo() if available)
Kim Alvefur <zash@zash.se>
parents: 1076
diff changeset
127
1072
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 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
129 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
130 setmetatable(active_sessions, {
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 __index = function ( t, k )
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 local v = {
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 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
134 stanzas_in = {
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 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
136 };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 stanzas_out = {
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 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
139 };
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 }
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 rawset(t, k, v);
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 return v;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 });
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 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
146 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
147 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
148 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
149 if n then
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 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
151 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 return stanza;
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 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
155 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
156 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
157 if n then
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 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
159 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 return stanza;
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 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
163 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
164 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
165 return bytes;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 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
168 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
169 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
170 return bytes;
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 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
173 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
174 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
175 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
176 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
177 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 end
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 return {
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 stats = stats;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 active_sessions = active_sessions;
4dbdb1b465e8 mod_statistics: Initial version, and a rough 'prosodyctl mod_statistics top'
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 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
184 };