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("mongoose.hrl"). |
12 |
|
-include("jlib.hrl"). |
13 |
|
|
14 |
|
-export([get_hooks/1]). |
15 |
|
|
16 |
|
%%------------------- |
17 |
|
%% Internal exports |
18 |
|
%%------------------- |
19 |
|
-export([sm_register_connection_hook/5, |
20 |
|
sm_remove_connection_hook/5, |
21 |
|
auth_failed/3, |
22 |
|
user_send_packet/4, |
23 |
|
user_receive_packet/5, |
24 |
|
xmpp_bounce_message/1, |
25 |
|
xmpp_stanza_dropped/4, |
26 |
|
xmpp_send_element/2, |
27 |
|
roster_get/2, |
28 |
|
roster_set/4, |
29 |
|
roster_push/4, |
30 |
|
roster_in_subscription/5, |
31 |
|
register_user/3, |
32 |
|
remove_user/3, |
33 |
|
privacy_iq_get/5, |
34 |
|
privacy_iq_set/4, |
35 |
|
privacy_check_packet/5, |
36 |
|
privacy_list_push/5 |
37 |
|
]). |
38 |
|
|
39 |
|
-ignore_xref([auth_failed/3, privacy_check_packet/5, privacy_iq_get/5, privacy_iq_set/4, |
40 |
|
privacy_list_push/5, register_user/3, remove_user/3, roster_get/2, |
41 |
|
roster_in_subscription/5, roster_push/4, roster_set/4, |
42 |
|
sm_register_connection_hook/5, sm_remove_connection_hook/5, |
43 |
|
user_receive_packet/5, user_send_packet/4, xmpp_bounce_message/1, |
44 |
|
xmpp_send_element/2, xmpp_stanza_dropped/4]). |
45 |
|
|
46 |
|
%%------------------- |
47 |
|
%% Implementation |
48 |
|
%%------------------- |
49 |
|
|
50 |
|
%% @doc Here will be declared which hooks should be registered |
51 |
|
-spec get_hooks(_) -> [ejabberd_hooks:hook(), ...]. |
52 |
|
get_hooks(HostType) -> |
53 |
421 |
[ {sm_register_connection_hook, HostType, ?MODULE, sm_register_connection_hook, 50}, |
54 |
|
{sm_remove_connection_hook, HostType, ?MODULE, sm_remove_connection_hook, 50}, |
55 |
|
{auth_failed, HostType, ?MODULE, auth_failed, 50}, |
56 |
|
{user_send_packet, HostType, ?MODULE, user_send_packet, 50}, |
57 |
|
{user_receive_packet, HostType, ?MODULE, user_receive_packet, 50}, |
58 |
|
{xmpp_stanza_dropped, HostType, ?MODULE, xmpp_stanza_dropped, 50}, |
59 |
|
{xmpp_bounce_message, HostType, ?MODULE, xmpp_bounce_message, 50}, |
60 |
|
{xmpp_send_element, HostType, ?MODULE, xmpp_send_element, 50}, |
61 |
|
{roster_get, HostType, ?MODULE, roster_get, 55}, |
62 |
|
{roster_set, HostType, ?MODULE, roster_set, 50}, |
63 |
|
{roster_push, HostType, ?MODULE, roster_push, 50}, |
64 |
|
{roster_in_subscription, HostType, ?MODULE, roster_in_subscription, 55}, |
65 |
|
{register_user, HostType, ?MODULE, register_user, 50}, |
66 |
|
{remove_user, HostType, ?MODULE, remove_user, 50}, |
67 |
|
{privacy_iq_get, HostType, ?MODULE, privacy_iq_get, 1}, |
68 |
|
{privacy_iq_set, HostType, ?MODULE, privacy_iq_set, 1}, |
69 |
|
{privacy_check_packet, HostType, ?MODULE, privacy_check_packet, 55}, |
70 |
|
{sm_broadcast, HostType, ?MODULE, privacy_list_push, 1}]. |
71 |
|
|
72 |
|
-spec sm_register_connection_hook(any(), mongooseim:host_type(), tuple(), jid:jid(), term() |
73 |
|
) -> any(). |
74 |
|
sm_register_connection_hook(Acc, HostType, _, _, _) -> |
75 |
2717 |
mongoose_metrics:update(HostType, sessionSuccessfulLogins, 1), |
76 |
2717 |
mongoose_metrics:update(HostType, sessionCount, 1), |
77 |
2717 |
Acc. |
78 |
|
|
79 |
|
-spec sm_remove_connection_hook(mongoose_acc:t(), tuple(), jid:jid(), |
80 |
|
term(), ejabberd_sm:close_reason() |
81 |
|
) -> mongoose_acc:t(). |
82 |
|
sm_remove_connection_hook(Acc, _, _, _, _Reason) -> |
83 |
2714 |
HT = mongoose_acc:host_type(Acc), |
84 |
2714 |
mongoose_metrics:update(HT, sessionLogouts, 1), |
85 |
2714 |
mongoose_metrics:update(HT, sessionCount, -1), |
86 |
2714 |
Acc. |
87 |
|
|
88 |
|
-spec auth_failed(any(), jid:user(), jid:server()) -> any(). |
89 |
|
auth_failed(Acc, _, Server) -> |
90 |
51 |
LServer = jid:nameprep(Server), |
91 |
51 |
{ok, HostType} = mongoose_domain_api:get_host_type(LServer), |
92 |
51 |
mongoose_metrics:update(HostType, sessionAuthFails, 1), |
93 |
51 |
Acc. |
94 |
|
|
95 |
|
-spec user_send_packet(mongoose_acc:t(), jid:jid(), tuple(), tuple() |
96 |
|
) -> mongoose_acc:t(). |
97 |
|
user_send_packet(Acc, _, _, Packet) -> |
98 |
6394 |
HostType = mongoose_acc:host_type(Acc), |
99 |
6394 |
mongoose_metrics:update(HostType, xmppStanzaSent, 1), |
100 |
6394 |
user_send_packet_type(HostType, Packet), |
101 |
6394 |
Acc. |
102 |
|
|
103 |
|
-spec user_send_packet_type(HostType :: mongooseim:host_type(), |
104 |
|
Packet :: exml:element()) -> ok | {error, not_found}. |
105 |
|
user_send_packet_type(HostType, #xmlel{name = <<"message">>}) -> |
106 |
1585 |
mongoose_metrics:update(HostType, xmppMessageSent, 1); |
107 |
|
user_send_packet_type(HostType, #xmlel{name = <<"iq">>}) -> |
108 |
1721 |
mongoose_metrics:update(HostType, xmppIqSent, 1); |
109 |
|
user_send_packet_type(HostType, #xmlel{name = <<"presence">>}) -> |
110 |
3088 |
mongoose_metrics:update(HostType, xmppPresenceSent, 1). |
111 |
|
|
112 |
|
-spec user_receive_packet(mongoose_acc:t(), jid:jid(), tuple(), tuple(), tuple() |
113 |
|
) -> mongoose_acc:t(). |
114 |
|
user_receive_packet(Acc, _, _, _, Packet) -> |
115 |
8528 |
HostType = mongoose_acc:host_type(Acc), |
116 |
8528 |
mongoose_metrics:update(HostType, xmppStanzaReceived, 1), |
117 |
8528 |
user_receive_packet_type(HostType, Packet), |
118 |
8528 |
Acc. |
119 |
|
|
120 |
|
-spec user_receive_packet_type(HostType :: mongooseim:host_type(), |
121 |
|
Packet :: exml:element()) -> ok | {error, not_found}. |
122 |
|
user_receive_packet_type(HostType, #xmlel{name = <<"message">>}) -> |
123 |
2235 |
mongoose_metrics:update(HostType, xmppMessageReceived, 1); |
124 |
|
user_receive_packet_type(HostType, #xmlel{name = <<"iq">>}) -> |
125 |
2379 |
mongoose_metrics:update(HostType, xmppIqReceived, 1); |
126 |
|
user_receive_packet_type(HostType, #xmlel{name = <<"presence">>}) -> |
127 |
3914 |
mongoose_metrics:update(HostType, xmppPresenceReceived, 1). |
128 |
|
|
129 |
|
-spec xmpp_bounce_message(Acc :: mongoose_acc:t()) -> mongoose_acc:t(). |
130 |
|
xmpp_bounce_message(Acc) -> |
131 |
29 |
HostType = mongoose_acc:host_type(Acc), |
132 |
29 |
mongoose_metrics:update(HostType, xmppMessageBounced, 1), |
133 |
29 |
Acc. |
134 |
|
|
135 |
|
-spec xmpp_stanza_dropped(mongoose_acc:t(), jid:jid(), jid:jid(), exml:element()) -> |
136 |
|
mongoose_acc:t(). |
137 |
|
xmpp_stanza_dropped(Acc, _, _, _) -> |
138 |
43 |
HostType = mongoose_acc:host_type(Acc), |
139 |
43 |
mongoose_metrics:update(HostType, xmppStanzaDropped, 1), |
140 |
43 |
Acc. |
141 |
|
|
142 |
|
-spec xmpp_send_element(Acc :: mongoose_acc:t(), Server :: jid:server()) -> mongoose_acc:t(). |
143 |
|
xmpp_send_element(Acc, _El) -> |
144 |
23297 |
HostType = mongoose_acc:host_type(Acc), |
145 |
23297 |
mongoose_metrics:update(HostType, xmppStanzaCount, 1), |
146 |
23297 |
case mongoose_acc:stanza_type(Acc) of |
147 |
|
<<"error">> -> |
148 |
335 |
mongoose_metrics:update(HostType, xmppErrorTotal, 1), |
149 |
335 |
case mongoose_acc:stanza_name(Acc) of |
150 |
|
<<"iq">> -> |
151 |
172 |
mongoose_metrics:update(HostType, xmppErrorIq, 1); |
152 |
|
<<"message">> -> |
153 |
141 |
mongoose_metrics:update(HostType, xmppErrorMessage, 1); |
154 |
|
<<"presence">> -> |
155 |
22 |
mongoose_metrics:update(HostType, xmppErrorPresence, 1) |
156 |
|
end; |
157 |
22962 |
_ -> ok |
158 |
|
end, |
159 |
23297 |
Acc. |
160 |
|
|
161 |
|
|
162 |
|
%% Roster |
163 |
|
|
164 |
|
-spec roster_get(mongoose_acc:t(), jid:jid()) -> mongoose_acc:t(). |
165 |
|
roster_get(Acc, _) -> |
166 |
109 |
HostType = mongoose_acc:host_type(Acc), |
167 |
109 |
mongoose_metrics:update(HostType, modRosterGets, 1), |
168 |
109 |
Acc. |
169 |
|
|
170 |
|
-spec roster_set(Acc :: any(), JID :: jid:jid(), tuple(), tuple()) -> |
171 |
|
any(). |
172 |
|
roster_set(Acc, #jid{lserver = LServer}, _, _) -> |
173 |
37 |
{ok, HostType} = mongoose_domain_api:get_host_type(LServer), |
174 |
37 |
mongoose_metrics:update(HostType, modRosterSets, 1), |
175 |
37 |
Acc. |
176 |
|
|
177 |
|
-spec roster_in_subscription(term(), jid:jid(), jid:jid(), atom(), term()) -> term(). |
178 |
|
roster_in_subscription(Acc, _, _, subscribed, _) -> |
179 |
63 |
HostType = mongoose_acc:host_type(Acc), |
180 |
63 |
mongoose_metrics:update(HostType, modPresenceSubscriptions, 1), |
181 |
63 |
Acc; |
182 |
|
roster_in_subscription(Acc, _, _, unsubscribed, _) -> |
183 |
221 |
HostType = mongoose_acc:host_type(Acc), |
184 |
221 |
mongoose_metrics:update(HostType, modPresenceUnsubscriptions, 1), |
185 |
221 |
Acc; |
186 |
|
roster_in_subscription(Acc, _, _, _, _) -> |
187 |
230 |
Acc. |
188 |
|
|
189 |
|
-spec roster_push(any(), mongooseim:host_type(), jid:jid(), mod_roster:roster()) -> any(). |
190 |
|
roster_push(HookAcc, HostType, _JID, _Item) -> |
191 |
342 |
mongoose_metrics:update(HostType, modRosterPush, 1), |
192 |
342 |
HookAcc. |
193 |
|
|
194 |
|
%% Register |
195 |
|
|
196 |
|
%% #rh |
197 |
|
-spec register_user(any(), binary(), jid:lserver()) -> any(). |
198 |
|
register_user(Acc, _, LServer) -> |
199 |
2315 |
{ok, HostType} = mongoose_domain_api:get_host_type(LServer), |
200 |
2315 |
mongoose_metrics:update(HostType, modRegisterCount, 1), |
201 |
2315 |
Acc. |
202 |
|
|
203 |
|
-spec remove_user(mongoose_acc:t(), binary(), jid:server()) -> mongoose_acc:t(). |
204 |
|
remove_user(Acc, _, _) -> |
205 |
2318 |
HostType = mongoose_acc:host_type(Acc), |
206 |
2318 |
mongoose_metrics:update(HostType, modUnregisterCount, 1), |
207 |
2318 |
Acc. |
208 |
|
|
209 |
|
%% Privacy |
210 |
|
|
211 |
|
-spec privacy_iq_get(mongoose_acc:t(), jid:jid(), jid:jid(), term(), term()) -> mongoose_acc:t(). |
212 |
|
privacy_iq_get(Acc, _, _, _, _) -> |
213 |
66 |
HostType = mongoose_acc:host_type(Acc), |
214 |
66 |
mongoose_metrics:update(HostType, modPrivacyGets, 1), |
215 |
66 |
Acc. |
216 |
|
|
217 |
|
-spec privacy_iq_set(Acc :: mongoose_acc:t(), |
218 |
|
From :: jid:jid(), |
219 |
|
_To :: jid:jid(), |
220 |
|
_IQ :: jlib:iq()) -> mongoose_acc:t(). |
221 |
|
privacy_iq_set(Acc, _, _To, #iq{sub_el = SubEl}) -> |
222 |
144 |
HostType = mongoose_acc:host_type(Acc), |
223 |
144 |
#xmlel{children = Els} = SubEl, |
224 |
144 |
case xml:remove_cdata(Els) of |
225 |
|
[#xmlel{name = <<"active">>}] -> |
226 |
44 |
mongoose_metrics:update(HostType, modPrivacySetsActive, 1); |
227 |
|
[#xmlel{name = <<"default">>}] -> |
228 |
16 |
mongoose_metrics:update(HostType, modPrivacySetsDefault, 1); |
229 |
|
_ -> |
230 |
84 |
ok |
231 |
|
end, |
232 |
144 |
mongoose_metrics:update(HostType, modPrivacySets, 1), |
233 |
144 |
Acc. |
234 |
|
|
235 |
|
-spec privacy_list_push(Acc :: mongoose_acc:t(), |
236 |
|
_From :: jid:jid(), |
237 |
|
To :: jid:jid(), |
238 |
|
Broadcast :: ejabberd_c2s:broadcast(), |
239 |
|
SessionCount :: non_neg_integer()) -> mongoose_acc:t(). |
240 |
|
privacy_list_push(Acc, _From, _To, _Broadcast, SessionCount) -> |
241 |
728 |
HostType = mongoose_acc:host_type(Acc), |
242 |
728 |
mongoose_metrics:update(HostType, modPrivacyPush, SessionCount), |
243 |
728 |
Acc. |
244 |
|
|
245 |
|
-spec privacy_check_packet(Acc :: mongoose_acc:t(), |
246 |
|
JID :: jid:jid(), |
247 |
|
term(), term(), term()) -> mongoose_acc:t(). |
248 |
|
privacy_check_packet(Acc, _, _, {_, _, _, _}, _) -> |
249 |
21289 |
HostType = mongoose_acc:host_type(Acc), |
250 |
21289 |
mongoose_metrics:update(HostType, modPrivacyStanzaAll, 1), |
251 |
21289 |
case mongoose_acc:get(privacy, check, allow, Acc) of |
252 |
|
deny -> |
253 |
:-( |
mongoose_metrics:update(HostType, modPrivacyStanzaDenied, 1); |
254 |
|
block -> |
255 |
:-( |
mongoose_metrics:update(HostType, modPrivacyStanzaBlocked, 1); |
256 |
|
allow -> |
257 |
21289 |
ok |
258 |
|
end, |
259 |
21289 |
Acc. |
260 |
|
|
261 |
|
%%% vim: set sts=4 ts=4 sw=4 et filetype=erlang foldmarker=%%%',%%%. foldmethod=marker: |