annotate mod_stanza_counter/mod_stanza_counter.lua @ 5648:c217f4edfc4f

misc/mtail: Start of an mtail config Stashing it here in case anyone wants to continue working on it. Currently it's only counting log messages by level. Due to the permissions set by systemd on Prosody logs, mtail never managed to start correctly until permissions were manually relaxed.
author Kim Alvefur <zash@zash.se>
date Sun, 17 Sep 2023 13:36:30 +0200
parents 7dbde05b48a9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
1 -- (C) 2011, Marco Cirillo (LW.Org)
436
e4a1f0425380 mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents: 435
diff changeset
2 -- General Stanzas' Counter.
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
3
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
4 local jid_bare = require "util.jid".bare
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
5
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
6 -- Setup, Init functions.
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
7 -- initialize function counter table on the global object on start
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
8 local function init_counter()
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 534
diff changeset
9 prosody.stanza_counter = {
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
10 iq = { incoming=0, outgoing=0 },
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
11 message = { incoming=0, outgoing=0 },
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
12 presence = { incoming=0, outgoing=0 }
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
13 }
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
14 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
15
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
16 -- Setup on server start
534
3d6e0e037dab mod_stanza_counter: removed a few empty lines.
Marco Cirillo <maranda@lightwitch.org>
parents: 436
diff changeset
17 local function setup() init_counter() end
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
18
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
19 -- Basic Stanzas' Counters
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
20 local function iq_callback(check)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
21 return function(self)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
22 local origin, stanza = self.origin, self.stanza
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
23 if not prosody.stanza_counter then init_counter() end
434
ac4a6cc5776c mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents: 433
diff changeset
24 if check then
534
3d6e0e037dab mod_stanza_counter: removed a few empty lines.
Marco Cirillo <maranda@lightwitch.org>
parents: 436
diff changeset
25 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
26 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
27 prosody.stanza_counter.iq["outgoing"] = prosody.stanza_counter.iq["outgoing"] + 1
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
28 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
29 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
30 prosody.stanza_counter.iq["incoming"] = prosody.stanza_counter.iq["incoming"] + 1
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
31 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
32 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
33 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
34
434
ac4a6cc5776c mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents: 433
diff changeset
35 local function mes_callback(check)
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
36 return function(self)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
37 local origin, stanza = self.origin, self.stanza
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
38 if not prosody.stanza_counter then init_counter() end
434
ac4a6cc5776c mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents: 433
diff changeset
39 if check then
534
3d6e0e037dab mod_stanza_counter: removed a few empty lines.
Marco Cirillo <maranda@lightwitch.org>
parents: 436
diff changeset
40 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
41 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
42 prosody.stanza_counter.message["outgoing"] = prosody.stanza_counter.message["outgoing"] + 1
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
43 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
44 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
45 prosody.stanza_counter.message["incoming"] = prosody.stanza_counter.message["incoming"] + 1
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
46 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
47 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
48 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
49
434
ac4a6cc5776c mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents: 433
diff changeset
50 local function pre_callback(check)
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
51 return function(self)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
52 local origin, stanza = self.origin, self.stanza
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
53 if not prosody.stanza_counter then init_counter() end
434
ac4a6cc5776c mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents: 433
diff changeset
54 if check then
534
3d6e0e037dab mod_stanza_counter: removed a few empty lines.
Marco Cirillo <maranda@lightwitch.org>
parents: 436
diff changeset
55 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
56 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
57 prosody.stanza_counter.presence["outgoing"] = prosody.stanza_counter.presence["outgoing"] + 1
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
58 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
59 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
60 prosody.stanza_counter.presence["incoming"] = prosody.stanza_counter.presence["incoming"] + 1
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
61 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
62 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
63 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
64
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
65 -- Hook all pre-stanza events.
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
66 module:hook("pre-iq/bare", iq_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
67 module:hook("pre-iq/full", iq_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
68 module:hook("pre-iq/host", iq_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
69
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
70 module:hook("pre-message/bare", mes_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
71 module:hook("pre-message/full", mes_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
72 module:hook("pre-message/host", mes_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
73
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
74 module:hook("pre-presence/bare", pre_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
75 module:hook("pre-presence/full", pre_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
76 module:hook("pre-presence/host", pre_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
77
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
78 -- Hook all stanza events.
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
79 module:hook("iq/bare", iq_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
80 module:hook("iq/full", iq_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
81 module:hook("iq/host", iq_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
82
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
83 module:hook("message/bare", mes_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
84 module:hook("message/full", mes_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
85 module:hook("message/host", mes_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
86
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
87 module:hook("presence/bare", pre_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
88 module:hook("presence/full", pre_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
89 module:hook("presence/host", pre_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
90
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
91 -- Hook server start to initialize the counter.
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
92 module:hook("server-started", setup)