aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/service/monservice.cpp
blob: d1b9cda1d21e29298c44cc1b8a2b4f391ba72d66 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "monservice.h"

#include <library/cpp/malloc/api/malloc.h>
#include <library/cpp/string_utils/base64/base64.h>
#include <library/cpp/svnversion/svnversion.h>

#include <util/generic/map.h>
#include <util/generic/ptr.h>
#include <util/system/hostname.h>

#include <google/protobuf/text_format.h>

using namespace NMonitoring;

TMonService2::TMonService2(ui16 port, const TString& host, ui32 threads, const TString& title, THolder<IAuthProvider> auth)
    : TMonService2(HttpServerOptions(port, host, threads), title, std::move(auth))
{
}

TMonService2::TMonService2(const THttpServerOptions& options, const TString& title, THolder<IAuthProvider> auth)
    : NMonitoring::TMtHttpServer(options, std::bind(&TMonService2::ServeRequest, this, std::placeholders::_1, std::placeholders::_2))
    , Title(title)
    , IndexMonPage(new TIndexMonPage("", Title))
    , AuthProvider_{std::move(auth)}
{
    Y_VERIFY(!!title);
    time_t t = time(nullptr);
    ctime_r(&t, StartTime);
}

TMonService2::TMonService2(const THttpServerOptions& options, TSimpleSharedPtr<IThreadPool> pool, const TString& title, THolder<IAuthProvider> auth)
    : NMonitoring::TMtHttpServer(options, std::bind(&TMonService2::ServeRequest, this, std::placeholders::_1, std::placeholders::_2), std::move(pool))
    , Title(title)
    , IndexMonPage(new TIndexMonPage("", Title))
    , AuthProvider_{std::move(auth)}
{
    Y_VERIFY(!!title);
    time_t t = time(nullptr);
    ctime_r(&t, StartTime);
}

TMonService2::TMonService2(ui16 port, ui32 threads, const TString& title, THolder<IAuthProvider> auth)
    : TMonService2(port, TString(), threads, title, std::move(auth))
{
}

TMonService2::TMonService2(ui16 port, const TString& title, THolder<IAuthProvider> auth)
    : TMonService2(port, TString(), 0, title, std::move(auth))
{
}

void TMonService2::OutputIndex(IOutputStream& out) {
    IndexMonPage->OutputIndex(out, true);
}

void TMonService2::OutputIndexPage(IOutputStream& out) {
    out << HTTPOKHTML;
    out << "<html>\n";
    IndexMonPage->OutputHead(out);
    OutputIndexBody(out);
    out << "</html>\n";
}

void TMonService2::OutputIndexBody(IOutputStream& out) {
    out << "<body>\n";

    // part of common navbar
    out << "<ol class='breadcrumb'>\n";
    out << "<li class='active'>" << Title << "</li>\n";
    out << "</ol>\n";

    out << "<div class='container'>\n"
        << "<h2>" << Title << "</h2>\n";
    OutputIndex(out);
    out
        << "<div>\n"
        << "</body>\n";
}

void TMonService2::ServeRequest(IOutputStream& out, const NMonitoring::IHttpRequest& request) {
    TString path = request.GetPath();
    Y_VERIFY(path.StartsWith('/'));

    if (AuthProvider_) {
        const auto authResult = AuthProvider_->Check(request);
        switch (authResult.Status) {
            case TAuthResult::EStatus::NoCredentials:
                out << HTTPUNAUTHORIZED;
                return;
            case TAuthResult::EStatus::Denied:
                out << HTTPFORBIDDEN;
                return;
            case TAuthResult::EStatus::Ok:
                break;
        }
    }

    if (path == "/") {
        OutputIndexPage(out);
    } else {
        TMonService2HttpRequest monService2HttpRequest(
            &out, &request, this, IndexMonPage.Get(), path, nullptr);
        IndexMonPage->Output(monService2HttpRequest);
    }
}

void TMonService2::Register(IMonPage* page) {
    IndexMonPage->Register(page);
}

void TMonService2::Register(TMonPagePtr page) {
    IndexMonPage->Register(std::move(page));
}

TIndexMonPage* TMonService2::RegisterIndexPage(const TString& path, const TString& title) {
    return IndexMonPage->RegisterIndexPage(path, title);
}

IMonPage* TMonService2::FindPage(const TString& relativePath) {
    return IndexMonPage->FindPage(relativePath);
}

TIndexMonPage* TMonService2::FindIndexPage(const TString& relativePath) {
    return IndexMonPage->FindIndexPage(relativePath);
}

void TMonService2::SortPages() {
    IndexMonPage->SortPages();
}