~cypheon/ecertmon

ref: c41253492b6d9ba83cec931602aaa355908bdaed ecertmon/src/metrics_handler.erl -rw-r--r-- 1.0 KiB
c4125349 — Johann Rudloff Allow a certain threshold of errors before reporting invalid cert 8 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
28
29
-module(metrics_handler).
-behavior(cowboy_handler).

-export([init/2]).

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

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}.