summaryrefslogtreecommitdiff
path: root/metrics.php
blob: f4f7b50461699ae74d7cd6f820006fd5325c048e (plain) (blame)
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
30
31
32
33
34
35
36
37
38
39
40
41
<?php

header('Content-Type: application/json; charset=utf-8');

if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
    http_response_code(405);
    header('Allow: GET');
    echo json_encode(['error' => 'method not allowed']);
    exit;
}

function get_snowflake_connections()
{
    $metric = 'tor_snowflake_proxy_connections_total';
    $body = file_get_contents(
        'http://127.0.0.1:9999/internal/metrics',
        false
    );
    if (!$body) {
        return 0;
    }
    $conns = 0;
    foreach (preg_split('/\r\n|\n|\r/', $body) as $line) {
        $line = trim($line);
        if (!$line || $line[0] === '#')
            continue;
        if (preg_match(
            '/^'
                . preg_quote($metric, '/')
                . '(?:\{[^}]*\})?\s+([0-9]+(?:\.[0-9]+)?)$/', $line, $m
        )) {
            $conns = $m[1];
            break;
        }
    }
    return $conns;
}

$snowflake_connections = get_snowflake_connections();

echo json_encode(['snowflake_connections' => $snowflake_connections]);