~cypheon/ecertmon

ref: b4a31be79e652a155dbeaef5b952988919742a15 ecertmon/src/metrics_handler.erl -rw-r--r-- 941 bytes
b4a31be7 — Johann Rudloff Use timeout for TLS connection 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
-module(metrics_handler).
-behavior(cowboy_handler).

-export([init/2]).

format_metric({Hostname, Port}) ->
  Target = {Hostname, Port},
  {ok, ScannerPid} = certmon_sup:get_scanner_pid(Target),
  Valid = try gen_server:call(ScannerPid, {get_status}) of
            {valid, Epoch} -> Epoch;
            _ -> 0
          catch
            exit:Reason ->
              logger:error("failed to get status for ~p: Reason: ~p", [{Hostname, Port}, Reason]),
              0
          end,
  [<<"tls_cert_expiry{hostname=\"">>, Hostname, <<"\", port=\"">>,
   io_lib:format("~p", [Port]), <<"\"} ">>, io_lib:format("~p\n", [Valid])
  ].

init(Req0, State) ->
  {targets, Targets} = proplists:lookup(targets, State),
  Req = cowboy_req:reply(200,
                         #{<<"content-type">> => <<"text/plain; version=0.0.4">>},
                         lists:map(fun format_metric/1, Targets),
                         Req0),
  {ok, Req, State}.