2015-10-22 14:00:17 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% File : mod_metrics.erl
|
|
|
|
%%% Author : Christophe Romain <christophe.romain@process-one.net>
|
|
|
|
%%% Purpose : Simple metrics handler for runtime statistics
|
|
|
|
%%% Created : 22 Oct 2015 by Christophe Romain <christophe.romain@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2017-01-02 21:41:53 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 ProcessOne
|
2015-10-22 14:00:17 +02:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_metrics).
|
|
|
|
|
|
|
|
-behaviour(ejabberd_config).
|
|
|
|
-author('christophe.romain@process-one.net').
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-include("logger.hrl").
|
2016-08-09 09:56:32 +02:00
|
|
|
-include("xmpp.hrl").
|
2015-10-22 14:00:17 +02:00
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
-export([start/2, stop/1, send_metrics/4, opt_type/1, mod_opt_type/1,
|
|
|
|
depends/2]).
|
2015-10-22 14:00:17 +02:00
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
-export([offline_message_hook/2,
|
2015-10-22 14:00:17 +02:00
|
|
|
sm_register_connection_hook/3, sm_remove_connection_hook/3,
|
2017-01-09 15:02:17 +01:00
|
|
|
user_send_packet/1, user_receive_packet/1,
|
2017-02-16 09:00:26 +01:00
|
|
|
s2s_send_packet/1, s2s_receive_packet/1,
|
2015-10-22 14:00:17 +02:00
|
|
|
remove_user/2, register_user/2]).
|
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
start(Host, _Opts) ->
|
2016-08-09 09:56:32 +02:00
|
|
|
ejabberd_hooks:add(offline_message_hook, Host, ?MODULE, offline_message_hook, 20),
|
|
|
|
ejabberd_hooks:add(sm_register_connection_hook, Host, ?MODULE, sm_register_connection_hook, 20),
|
|
|
|
ejabberd_hooks:add(sm_remove_connection_hook, Host, ?MODULE, sm_remove_connection_hook, 20),
|
|
|
|
ejabberd_hooks:add(user_send_packet, Host, ?MODULE, user_send_packet, 20),
|
|
|
|
ejabberd_hooks:add(user_receive_packet, Host, ?MODULE, user_receive_packet, 20),
|
|
|
|
ejabberd_hooks:add(s2s_send_packet, Host, ?MODULE, s2s_send_packet, 20),
|
|
|
|
ejabberd_hooks:add(s2s_receive_packet, Host, ?MODULE, s2s_receive_packet, 20),
|
|
|
|
ejabberd_hooks:add(remove_user, Host, ?MODULE, remove_user, 20),
|
|
|
|
ejabberd_hooks:add(register_user, Host, ?MODULE, register_user, 20).
|
2015-10-22 14:00:17 +02:00
|
|
|
|
|
|
|
stop(Host) ->
|
2016-08-09 09:56:32 +02:00
|
|
|
ejabberd_hooks:delete(offline_message_hook, Host, ?MODULE, offline_message_hook, 20),
|
|
|
|
ejabberd_hooks:delete(sm_register_connection_hook, Host, ?MODULE, sm_register_connection_hook, 20),
|
|
|
|
ejabberd_hooks:delete(sm_remove_connection_hook, Host, ?MODULE, sm_remove_connection_hook, 20),
|
|
|
|
ejabberd_hooks:delete(user_send_packet, Host, ?MODULE, user_send_packet, 20),
|
|
|
|
ejabberd_hooks:delete(user_receive_packet, Host, ?MODULE, user_receive_packet, 20),
|
|
|
|
ejabberd_hooks:delete(s2s_send_packet, Host, ?MODULE, s2s_send_packet, 20),
|
|
|
|
ejabberd_hooks:delete(s2s_receive_packet, Host, ?MODULE, s2s_receive_packet, 20),
|
|
|
|
ejabberd_hooks:delete(remove_user, Host, ?MODULE, remove_user, 20),
|
|
|
|
ejabberd_hooks:delete(register_user, Host, ?MODULE, register_user, 20).
|
2015-10-22 14:00:17 +02:00
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2015-10-22 14:00:17 +02:00
|
|
|
%%====================================================================
|
|
|
|
%% Hooks handlers
|
|
|
|
%%====================================================================
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec offline_message_hook(any(), message()) -> any().
|
|
|
|
offline_message_hook(Acc, #message{to = #jid{lserver = LServer}}) ->
|
2017-01-21 11:47:47 +01:00
|
|
|
push(LServer, offline_message),
|
|
|
|
Acc.
|
2015-10-22 14:00:17 +02:00
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec sm_register_connection_hook(ejabberd_sm:sid(), jid(), ejabberd_sm:info()) -> any().
|
2015-10-22 14:00:17 +02:00
|
|
|
sm_register_connection_hook(_SID, #jid{lserver=LServer}, _Info) ->
|
|
|
|
push(LServer, sm_register_connection).
|
2016-08-09 09:56:32 +02:00
|
|
|
|
|
|
|
-spec sm_remove_connection_hook(ejabberd_sm:sid(), jid(), ejabberd_sm:info()) -> any().
|
2015-10-22 14:00:17 +02:00
|
|
|
sm_remove_connection_hook(_SID, #jid{lserver=LServer}, _Info) ->
|
|
|
|
push(LServer, sm_remove_connection).
|
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec user_send_packet({stanza(), ejabberd_c2s:state()}) -> {stanza(), ejabberd_c2s:state()}.
|
|
|
|
user_send_packet({Packet, #{jid := #jid{lserver = LServer}} = C2SState}) ->
|
2015-10-22 14:00:17 +02:00
|
|
|
push(LServer, user_send_packet),
|
2017-01-09 15:02:17 +01:00
|
|
|
{Packet, C2SState}.
|
2016-08-12 12:17:42 +02:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec user_receive_packet({stanza(), ejabberd_c2s:state()}) -> {stanza(), ejabberd_c2s:state()}.
|
|
|
|
user_receive_packet({Packet, #{jid := #jid{lserver = LServer}} = C2SState}) ->
|
2015-10-22 14:00:17 +02:00
|
|
|
push(LServer, user_receive_packet),
|
2017-01-09 15:02:17 +01:00
|
|
|
{Packet, C2SState}.
|
2015-10-22 14:00:17 +02:00
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec s2s_send_packet(stanza()) -> any().
|
|
|
|
s2s_send_packet(Packet) ->
|
|
|
|
#jid{lserver = LServer} = xmpp:get_from(Packet),
|
2015-10-22 14:00:17 +02:00
|
|
|
push(LServer, s2s_send_packet).
|
2016-08-09 09:56:32 +02:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec s2s_receive_packet({stanza(), ejabberd_s2s_in:state()}) ->
|
|
|
|
{stanza(), ejabberd_s2s_in:state()}.
|
|
|
|
s2s_receive_packet({Packet, S2SState}) ->
|
|
|
|
To = xmpp:get_to(Packet),
|
|
|
|
LServer = ejabberd_router:host_of_route(To#jid.lserver),
|
|
|
|
push(LServer, s2s_receive_packet),
|
|
|
|
{Packet, S2SState}.
|
2015-10-22 14:00:17 +02:00
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec remove_user(binary(), binary()) -> any().
|
2015-10-22 14:00:17 +02:00
|
|
|
remove_user(_User, Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
push(jid:nameprep(Server), remove_user).
|
2016-08-09 09:56:32 +02:00
|
|
|
|
|
|
|
-spec register_user(binary(), binary()) -> any().
|
2015-10-22 14:00:17 +02:00
|
|
|
register_user(_User, Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
push(jid:nameprep(Server), register_user).
|
2015-10-22 14:00:17 +02:00
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% metrics push handler
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
push(Host, Probe) ->
|
|
|
|
spawn(?MODULE, send_metrics, [Host, Probe, {127,0,0,1}, 11111]).
|
|
|
|
|
|
|
|
send_metrics(Host, Probe, Peer, Port) ->
|
|
|
|
% our default metrics handler is https://github.com/processone/grapherl
|
|
|
|
% grapherl metrics are named first with service domain, then nodename
|
|
|
|
% and name of the data itself, followed by type timestamp and value
|
|
|
|
% example => process-one.net/xmpp-1.user_receive_packet:c/1441784958:1
|
|
|
|
[_, NodeId] = str:tokens(jlib:atom_to_binary(node()), <<"@">>),
|
|
|
|
[Node | _] = str:tokens(NodeId, <<".">>),
|
|
|
|
BaseId = <<Host/binary, "/", Node/binary, ".">>,
|
|
|
|
DateTime = erlang:universaltime(),
|
|
|
|
UnixTime = calendar:datetime_to_gregorian_seconds(DateTime) - 62167219200,
|
|
|
|
TS = integer_to_binary(UnixTime),
|
|
|
|
case gen_udp:open(0) of
|
|
|
|
{ok, Socket} ->
|
|
|
|
case Probe of
|
|
|
|
{Key, Val} ->
|
|
|
|
BVal = integer_to_binary(Val),
|
|
|
|
Data = <<BaseId/binary, (jlib:atom_to_binary(Key))/binary,
|
|
|
|
":g/", TS/binary, ":", BVal/binary>>,
|
|
|
|
gen_udp:send(Socket, Peer, Port, Data);
|
|
|
|
Key ->
|
|
|
|
Data = <<BaseId/binary, (jlib:atom_to_binary(Key))/binary,
|
|
|
|
":c/", TS/binary, ":1">>,
|
|
|
|
gen_udp:send(Socket, Peer, Port, Data)
|
|
|
|
end,
|
|
|
|
gen_udp:close(Socket);
|
|
|
|
Error ->
|
|
|
|
?WARNING_MSG("can not open udp socket to grapherl: ~p", [Error])
|
|
|
|
end.
|
2016-03-04 11:45:18 +01:00
|
|
|
|
|
|
|
opt_type(_) ->
|
|
|
|
[].
|
2016-05-02 11:18:18 +02:00
|
|
|
|
|
|
|
mod_opt_type(_) ->
|
|
|
|
[].
|