1 |
|
%%%------------------------------------------------------------------- |
2 |
|
%%% @author Michal Piotrowski <michal.piotrowski@erlang-solutions.com> |
3 |
|
%%% @copyright (C) 2013, Erlang Solutions Ltd. |
4 |
|
%%% @doc MongooseIM hooks for general metrics |
5 |
|
%%% |
6 |
|
%%% @end |
7 |
|
%%% Created : 23 Apr 2013 by Michal Piotrowski <michal.piotrowski@erlang-solutions.com> |
8 |
|
%%%------------------------------------------------------------------- |
9 |
|
-module(mongoose_metrics_hooks). |
10 |
|
|
11 |
|
-include("jlib.hrl"). |
12 |
|
|
13 |
|
-export([get_hooks/1]). |
14 |
|
|
15 |
|
%%------------------- |
16 |
|
%% Internal exports |
17 |
|
%%------------------- |
18 |
|
-export([sm_register_connection/3, |
19 |
|
sm_remove_connection/3, |
20 |
|
auth_failed/3, |
21 |
|
user_send_packet/3, |
22 |
|
user_open_session/3, |
23 |
|
xmpp_bounce_message/3, |
24 |
|
xmpp_stanza_dropped/3, |
25 |
|
xmpp_send_element/3, |
26 |
|
register_user/3, |
27 |
|
remove_user/3, |
28 |
|
privacy_iq_get/3, |
29 |
|
privacy_iq_set/3, |
30 |
|
privacy_check_packet/3, |
31 |
|
privacy_list_push/3 |
32 |
|
]). |
33 |
|
|
34 |
|
%%------------------- |
35 |
|
%% Implementation |
36 |
|
%%------------------- |
37 |
|
|
38 |
|
%% @doc Here will be declared which hooks should be registered |
39 |
|
-spec get_hooks(_) -> [gen_hook:hook_tuple(), ...]. |
40 |
|
get_hooks(HostType) -> |
41 |
508 |
[ {sm_register_connection, HostType, fun ?MODULE:sm_register_connection/3, #{}, 50}, |
42 |
|
{sm_remove_connection, HostType, fun ?MODULE:sm_remove_connection/3, #{}, 50}, |
43 |
|
{auth_failed, HostType, fun ?MODULE:auth_failed/3, #{}, 50}, |
44 |
|
{xmpp_stanza_dropped, HostType, fun ?MODULE:xmpp_stanza_dropped/3, #{}, 50}, |
45 |
|
{xmpp_bounce_message, HostType, fun ?MODULE:xmpp_bounce_message/3, #{}, 50}, |
46 |
|
{xmpp_send_element, HostType, fun ?MODULE:xmpp_send_element/3, #{}, 50}, |
47 |
|
{register_user, HostType, fun ?MODULE:register_user/3, #{}, 50}, |
48 |
|
{remove_user, HostType, fun ?MODULE:remove_user/3, #{}, 50}, |
49 |
|
{privacy_iq_get, HostType, fun ?MODULE:privacy_iq_get/3, #{}, 1}, |
50 |
|
{privacy_iq_set, HostType, fun ?MODULE:privacy_iq_set/3, #{}, 1}, |
51 |
|
{privacy_check_packet, HostType, fun ?MODULE:privacy_check_packet/3, #{}, 55}, |
52 |
|
{privacy_list_push, HostType, fun ?MODULE:privacy_list_push/3, #{}, 1} |
53 |
|
| c2s_hooks(HostType)]. |
54 |
|
|
55 |
|
-spec c2s_hooks(mongooseim:host_type()) -> gen_hook:hook_list(mongoose_c2s_hooks:fn()). |
56 |
|
c2s_hooks(HostType) -> |
57 |
508 |
[{user_send_packet, HostType, fun ?MODULE:user_send_packet/3, #{}, 50}, |
58 |
|
{user_open_session, HostType, fun ?MODULE:user_open_session/3, #{}, 50}]. |
59 |
|
|
60 |
|
-spec sm_register_connection(Acc, Params, Extra) -> {ok, Acc} when |
61 |
|
Acc :: any(), |
62 |
|
Params :: map(), |
63 |
|
Extra :: #{host_type := mongooseim:host_type()}. |
64 |
|
sm_register_connection(Acc, _, #{host_type := HostType}) -> |
65 |
6375 |
mongoose_metrics:update(HostType, sessionSuccessfulLogins, 1), |
66 |
6375 |
mongoose_metrics:update(HostType, sessionCount, 1), |
67 |
6375 |
{ok, Acc}. |
68 |
|
|
69 |
|
-spec sm_remove_connection(Acc, Params, Extra) -> {ok, Acc} when |
70 |
|
Acc :: mongoose_acc:t(), |
71 |
|
Params :: map(), |
72 |
|
Extra :: #{host_type := mongooseim:host_type()}. |
73 |
|
sm_remove_connection(Acc, _, #{host_type := HostType}) -> |
74 |
6026 |
mongoose_metrics:update(HostType, sessionLogouts, 1), |
75 |
6026 |
mongoose_metrics:update(HostType, sessionCount, -1), |
76 |
6026 |
{ok, Acc}. |
77 |
|
|
78 |
|
-spec auth_failed(Acc, Params, Extra) -> {ok, Acc} when |
79 |
|
Acc :: any(), |
80 |
|
Params :: #{server := jid:server()}, |
81 |
|
Extra :: map(). |
82 |
|
auth_failed(Acc, #{server := Server}, _) -> |
83 |
65 |
LServer = jid:nameprep(Server), |
84 |
65 |
{ok, HostType} = mongoose_domain_api:get_host_type(LServer), |
85 |
65 |
mongoose_metrics:update(HostType, sessionAuthFails, 1), |
86 |
65 |
{ok, Acc}. |
87 |
|
|
88 |
|
-spec user_send_packet(mongoose_acc:t(), mongoose_c2s_hooks:params(), map()) -> |
89 |
|
mongoose_c2s_hooks:result(). |
90 |
|
user_send_packet(Acc, _Params, #{host_type := HostType}) -> |
91 |
21703 |
mongoose_metrics:update(HostType, xmppStanzaSent, 1), |
92 |
21703 |
mongoose_metrics:update(HostType, xmppStanzaCount, 1), |
93 |
21703 |
El = mongoose_acc:element(Acc), |
94 |
21703 |
user_send_packet_type(HostType, El), |
95 |
21703 |
{ok, Acc}. |
96 |
|
|
97 |
|
-spec user_send_packet_type(HostType :: mongooseim:host_type(), |
98 |
|
Packet :: exml:element()) -> ok | {error, not_found}. |
99 |
|
user_send_packet_type(HostType, #xmlel{name = <<"message">>}) -> |
100 |
4325 |
mongoose_metrics:update(HostType, xmppMessageSent, 1); |
101 |
|
user_send_packet_type(HostType, #xmlel{name = <<"iq">>}) -> |
102 |
10799 |
mongoose_metrics:update(HostType, xmppIqSent, 1); |
103 |
|
user_send_packet_type(HostType, #xmlel{name = <<"presence">>}) -> |
104 |
6453 |
mongoose_metrics:update(HostType, xmppPresenceSent, 1); |
105 |
|
user_send_packet_type(_, _) -> |
106 |
126 |
ok. |
107 |
|
|
108 |
|
-spec xmpp_bounce_message(Acc, Params, Extra) -> {ok, Acc} when |
109 |
|
Acc :: mongoose_acc:t(), |
110 |
|
Params :: map(), |
111 |
|
Extra :: #{host_type := mongooseim:host_type()}. |
112 |
|
xmpp_bounce_message(Acc, _, #{host_type := HostType}) -> |
113 |
33 |
mongoose_metrics:update(HostType, xmppMessageBounced, 1), |
114 |
33 |
{ok, Acc}. |
115 |
|
|
116 |
|
-spec xmpp_stanza_dropped(Acc, Params, Extra) -> {ok, Acc} when |
117 |
|
Acc :: mongoose_acc:t(), |
118 |
|
Params :: map(), |
119 |
|
Extra :: #{host_type := mongooseim:host_type()}. |
120 |
|
xmpp_stanza_dropped(Acc, _, #{host_type := HostType}) -> |
121 |
73 |
mongoose_metrics:update(HostType, xmppStanzaDropped, 1), |
122 |
73 |
{ok, Acc}. |
123 |
|
|
124 |
|
-spec xmpp_send_element(Acc, Params, Extra) -> {ok, Acc} when |
125 |
|
Acc :: mongoose_acc:t(), |
126 |
|
Params :: map(), |
127 |
|
Extra :: #{host_type := mongooseim:host_type()}. |
128 |
|
xmpp_send_element(Acc, _, #{host_type := HostType}) -> |
129 |
59472 |
case mongoose_acc:element(Acc) of |
130 |
|
#xmlel{name = Name} = El |
131 |
|
when Name == <<"iq">>; Name == <<"message">>; Name == <<"presence">> -> |
132 |
40738 |
mongoose_metrics:update(HostType, xmppStanzaCount, 1), |
133 |
40738 |
case exml_query:attr(El, <<"type">>) of |
134 |
|
<<"error">> -> |
135 |
581 |
mongoose_metrics:update(HostType, xmppErrorTotal, 1), |
136 |
581 |
xmpp_send_element_error(HostType, El); |
137 |
|
_ -> |
138 |
40157 |
mongoose_metrics:update(HostType, xmppStanzaReceived, 1), |
139 |
40157 |
xmpp_send_element_type(HostType, El) |
140 |
|
end; |
141 |
18734 |
_ -> ok |
142 |
|
end, |
143 |
59472 |
{ok, Acc}. |
144 |
|
|
145 |
|
-spec xmpp_send_element_error(HostType :: mongooseim:host_type(), |
146 |
|
Packet :: exml:element()) -> ok | {error, not_found}. |
147 |
|
xmpp_send_element_error(HostType, #xmlel{name = <<"message">>}) -> |
148 |
190 |
mongoose_metrics:update(HostType, xmppErrorMessage, 1); |
149 |
|
xmpp_send_element_error(HostType, #xmlel{name = <<"iq">>}) -> |
150 |
334 |
mongoose_metrics:update(HostType, xmppErrorIq, 1); |
151 |
|
xmpp_send_element_error(HostType, #xmlel{name = <<"presence">>}) -> |
152 |
57 |
mongoose_metrics:update(HostType, xmppErrorPresence, 1). |
153 |
|
|
154 |
|
-spec xmpp_send_element_type(HostType :: mongooseim:host_type(), |
155 |
|
Packet :: exml:element()) -> ok | {error, not_found}. |
156 |
|
xmpp_send_element_type(HostType, #xmlel{name = <<"message">>}) -> |
157 |
13889 |
mongoose_metrics:update(HostType, xmppMessageReceived, 1); |
158 |
|
xmpp_send_element_type(HostType, #xmlel{name = <<"iq">>}) -> |
159 |
17366 |
mongoose_metrics:update(HostType, xmppIqReceived, 1); |
160 |
|
xmpp_send_element_type(HostType, #xmlel{name = <<"presence">>}) -> |
161 |
8902 |
mongoose_metrics:update(HostType, xmppPresenceReceived, 1). |
162 |
|
|
163 |
|
-spec user_open_session(mongoose_acc:t(), mongoose_c2s_hooks:params(), map()) -> |
164 |
|
mongoose_c2s_hooks:result(). |
165 |
|
user_open_session(Acc, _Params, #{host_type := HostType}) -> |
166 |
6011 |
mongoose_metrics:update(HostType, xmppStanzaSent, 1), |
167 |
6011 |
mongoose_metrics:update(HostType, xmppStanzaCount, 1), |
168 |
6011 |
mongoose_metrics:update(HostType, xmppIqSent, 1), |
169 |
6011 |
{ok, Acc}. |
170 |
|
|
171 |
|
%% Register |
172 |
|
|
173 |
|
%% #rh |
174 |
|
-spec register_user(Acc, Params, Extra) -> {ok, Acc} when |
175 |
|
Acc :: any(), |
176 |
|
Params :: #{jid := jid:jid()}, |
177 |
|
Extra :: map(). |
178 |
|
register_user(Acc, #{jid := #jid{lserver = LServer}}, _) -> |
179 |
5591 |
{ok, HostType} = mongoose_domain_api:get_host_type(LServer), |
180 |
5591 |
mongoose_metrics:update(HostType, modRegisterCount, 1), |
181 |
5591 |
{ok, Acc}. |
182 |
|
|
183 |
|
-spec remove_user(Acc, Params, Extra) -> {ok, Acc} when |
184 |
|
Acc :: mongoose_acc:t(), |
185 |
|
Params :: map(), |
186 |
|
Extra :: #{host_type := mongooseim:host_type()}. |
187 |
|
remove_user(Acc, _, #{host_type := HostType}) -> |
188 |
5565 |
mongoose_metrics:update(HostType, modUnregisterCount, 1), |
189 |
5565 |
{ok, Acc}. |
190 |
|
|
191 |
|
%% Privacy |
192 |
|
|
193 |
|
-spec privacy_iq_get(Acc, Params, Extra) -> {ok, Acc} when |
194 |
|
Acc :: mongoose_acc:t(), |
195 |
|
Params :: map(), |
196 |
|
Extra :: #{host_type := mongooseim:host_type()}. |
197 |
|
privacy_iq_get(Acc, _, #{host_type := HostType}) -> |
198 |
62 |
mongoose_metrics:update(HostType, modPrivacyGets, 1), |
199 |
62 |
{ok, Acc}. |
200 |
|
|
201 |
|
-spec privacy_iq_set(Acc, Params, Extra) -> {ok, Acc} when |
202 |
|
Acc :: mongoose_acc:t(), |
203 |
|
Params :: #{iq := jlib:iq()}, |
204 |
|
Extra :: #{host_type := mongooseim:host_type()}. |
205 |
|
privacy_iq_set(Acc, #{iq := #iq{sub_el = SubEl}}, #{host_type := HostType}) -> |
206 |
139 |
#xmlel{children = Els} = SubEl, |
207 |
139 |
case xml:remove_cdata(Els) of |
208 |
|
[#xmlel{name = <<"active">>}] -> |
209 |
44 |
mongoose_metrics:update(HostType, modPrivacySetsActive, 1); |
210 |
|
[#xmlel{name = <<"default">>}] -> |
211 |
16 |
mongoose_metrics:update(HostType, modPrivacySetsDefault, 1); |
212 |
|
_ -> |
213 |
79 |
ok |
214 |
|
end, |
215 |
139 |
mongoose_metrics:update(HostType, modPrivacySets, 1), |
216 |
139 |
{ok, Acc}. |
217 |
|
|
218 |
|
-spec privacy_list_push(Acc, Params, Extra) -> {ok, Acc} when |
219 |
|
Acc :: any(), |
220 |
|
Params :: #{session_count := non_neg_integer()}, |
221 |
|
Extra :: #{host_type := mongooseim:host_type()}. |
222 |
|
privacy_list_push(Acc, #{session_count := SessionCount}, #{host_type := HostType}) -> |
223 |
78 |
mongoose_metrics:update(HostType, modPrivacyPush, SessionCount), |
224 |
78 |
{ok, Acc}. |
225 |
|
|
226 |
|
-spec privacy_check_packet(Acc, Params, Extra) -> {ok, Acc} when |
227 |
|
Acc :: mongoose_acc:t(), |
228 |
|
Params :: map(), |
229 |
|
Extra :: #{host_type := mongooseim:host_type()}. |
230 |
|
privacy_check_packet(Acc, _, #{host_type := HostType}) -> |
231 |
3455 |
mongoose_metrics:update(HostType, modPrivacyStanzaAll, 1), |
232 |
3455 |
case mongoose_acc:get(privacy, check, allow, Acc) of |
233 |
|
deny -> |
234 |
:-( |
mongoose_metrics:update(HostType, modPrivacyStanzaDenied, 1); |
235 |
|
block -> |
236 |
:-( |
mongoose_metrics:update(HostType, modPrivacyStanzaBlocked, 1); |
237 |
|
allow -> |
238 |
3455 |
ok |
239 |
|
end, |
240 |
3455 |
{ok, Acc}. |
241 |
|
|
242 |
|
%%% vim: set sts=4 ts=4 sw=4 et filetype=erlang foldmarker=%%%',%%%. foldmethod=marker: |