plugins/mod_csi.lua
author Kim Alvefur <zash@zash.se>
Thu, 06 Apr 2023 08:01:55 +0200
changeset 13029 b7d0c1d75a37
parent 12981 74b9e05af71e
child 13030 a97f4b277221
permissions -rw-r--r--
mod_csi: Add metrics, covering changes and totals Motivation: Investigating clients that seem to forget to set CSI. Also, of course, MORE GRAPHS!
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 10433
diff changeset
     1
local st = require "prosody.util.stanza";
9076
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
local xmlns_csi = "urn:xmpp:csi:0";
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
local csi_feature = st.stanza("csi", { xmlns = xmlns_csi });
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
13029
b7d0c1d75a37 mod_csi: Add metrics, covering changes and totals
Kim Alvefur <zash@zash.se>
parents: 12981
diff changeset
     5
local sum = module:metric("gauge", "sessions_per_state", "sessions", "CSI state per session", { "csi_state" })
b7d0c1d75a37 mod_csi: Add metrics, covering changes and totals
Kim Alvefur <zash@zash.se>
parents: 12981
diff changeset
     6
local change = module:metric("counter", "changes", "events", "CSI state changes", {"csi_state"});
b7d0c1d75a37 mod_csi: Add metrics, covering changes and totals
Kim Alvefur <zash@zash.se>
parents: 12981
diff changeset
     7
b7d0c1d75a37 mod_csi: Add metrics, covering changes and totals
Kim Alvefur <zash@zash.se>
parents: 12981
diff changeset
     8
module:hook_global("stats-update", function ()
b7d0c1d75a37 mod_csi: Add metrics, covering changes and totals
Kim Alvefur <zash@zash.se>
parents: 12981
diff changeset
     9
	for _, session in pairs(prosody.full_sessions) do
b7d0c1d75a37 mod_csi: Add metrics, covering changes and totals
Kim Alvefur <zash@zash.se>
parents: 12981
diff changeset
    10
		if session.host == module.host then
b7d0c1d75a37 mod_csi: Add metrics, covering changes and totals
Kim Alvefur <zash@zash.se>
parents: 12981
diff changeset
    11
			sum:with_labels(session.state or "none"):add(1);
b7d0c1d75a37 mod_csi: Add metrics, covering changes and totals
Kim Alvefur <zash@zash.se>
parents: 12981
diff changeset
    12
		end
b7d0c1d75a37 mod_csi: Add metrics, covering changes and totals
Kim Alvefur <zash@zash.se>
parents: 12981
diff changeset
    13
	end
b7d0c1d75a37 mod_csi: Add metrics, covering changes and totals
Kim Alvefur <zash@zash.se>
parents: 12981
diff changeset
    14
end);
b7d0c1d75a37 mod_csi: Add metrics, covering changes and totals
Kim Alvefur <zash@zash.se>
parents: 12981
diff changeset
    15
10433
0b04d25c4ffb mod_csi: Cache CSI module availability to improve readabilty
Kim Alvefur <zash@zash.se>
parents: 10432
diff changeset
    16
local csi_handler_available = nil;
9076
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
module:hook("stream-features", function (event)
10433
0b04d25c4ffb mod_csi: Cache CSI module availability to improve readabilty
Kim Alvefur <zash@zash.se>
parents: 10432
diff changeset
    18
	if event.origin.username and csi_handler_available then
9076
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
		event.features:add_child(csi_feature);
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
	end
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
end);
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
function refire_event(name)
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
	return function (event)
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
		if event.origin.username then
9657
91856829f18b mod_csi: Fix copypaste mistake [luacheck]
Kim Alvefur <zash@zash.se>
parents: 9655
diff changeset
    26
			event.origin.state = event.stanza.name;
13029
b7d0c1d75a37 mod_csi: Add metrics, covering changes and totals
Kim Alvefur <zash@zash.se>
parents: 12981
diff changeset
    27
			change:with_labels(event.stanza.name):add(1);
9076
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
			module:fire_event(name, event);
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
			return true;
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
		end
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
	end;
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
end
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
module:hook("stanza/"..xmlns_csi..":active", refire_event("csi-client-active"));
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
module:hook("stanza/"..xmlns_csi..":inactive", refire_event("csi-client-inactive"));
a5daf3f6d588 mod_csi: Imported from prosody-modules 66b3085ecc49
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
10432
12a10208d86a mod_csi: Set module status based on whether a CSI handler module appears to be loaded
Kim Alvefur <zash@zash.se>
parents: 10431
diff changeset
    37
function module.load()
12a10208d86a mod_csi: Set module status based on whether a CSI handler module appears to be loaded
Kim Alvefur <zash@zash.se>
parents: 10431
diff changeset
    38
	if prosody.hosts[module.host].events._handlers["csi-client-active"] then
10433
0b04d25c4ffb mod_csi: Cache CSI module availability to improve readabilty
Kim Alvefur <zash@zash.se>
parents: 10432
diff changeset
    39
		csi_handler_available = true;
10432
12a10208d86a mod_csi: Set module status based on whether a CSI handler module appears to be loaded
Kim Alvefur <zash@zash.se>
parents: 10431
diff changeset
    40
		module:set_status("core", "CSI handler module loaded");
12a10208d86a mod_csi: Set module status based on whether a CSI handler module appears to be loaded
Kim Alvefur <zash@zash.se>
parents: 10431
diff changeset
    41
	else
10433
0b04d25c4ffb mod_csi: Cache CSI module availability to improve readabilty
Kim Alvefur <zash@zash.se>
parents: 10432
diff changeset
    42
		csi_handler_available = false;
10432
12a10208d86a mod_csi: Set module status based on whether a CSI handler module appears to be loaded
Kim Alvefur <zash@zash.se>
parents: 10431
diff changeset
    43
		module:set_status("warn", "No CSI handler module loaded");
12a10208d86a mod_csi: Set module status based on whether a CSI handler module appears to be loaded
Kim Alvefur <zash@zash.se>
parents: 10431
diff changeset
    44
	end
12a10208d86a mod_csi: Set module status based on whether a CSI handler module appears to be loaded
Kim Alvefur <zash@zash.se>
parents: 10431
diff changeset
    45
end
12a10208d86a mod_csi: Set module status based on whether a CSI handler module appears to be loaded
Kim Alvefur <zash@zash.se>
parents: 10431
diff changeset
    46
module:hook("module-loaded", module.load);
12a10208d86a mod_csi: Set module status based on whether a CSI handler module appears to be loaded
Kim Alvefur <zash@zash.se>
parents: 10431
diff changeset
    47
module:hook("module-unloaded", module.load);