monservice.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "monservice.h"
  2. #include <library/cpp/malloc/api/malloc.h>
  3. #include <library/cpp/string_utils/base64/base64.h>
  4. #include <library/cpp/svnversion/svnversion.h>
  5. #include <util/generic/map.h>
  6. #include <util/generic/ptr.h>
  7. #include <util/system/hostname.h>
  8. #include <google/protobuf/text_format.h>
  9. using namespace NMonitoring;
  10. TMonService2::TMonService2(ui16 port, const TString& host, ui32 threads, const TString& title, THolder<IAuthProvider> auth)
  11. : TMonService2(HttpServerOptions(port, host, threads), title, std::move(auth))
  12. {
  13. }
  14. TMonService2::TMonService2(const THttpServerOptions& options, const TString& title, THolder<IAuthProvider> auth)
  15. : NMonitoring::TMtHttpServer(options, std::bind(&TMonService2::ServeRequest, this, std::placeholders::_1, std::placeholders::_2))
  16. , Title(title)
  17. , IndexMonPage(new TIndexMonPage("", Title))
  18. , AuthProvider_{std::move(auth)}
  19. {
  20. Y_ABORT_UNLESS(!!title);
  21. time_t t = time(nullptr);
  22. ctime_r(&t, StartTime);
  23. }
  24. TMonService2::TMonService2(const THttpServerOptions& options, TSimpleSharedPtr<IThreadPool> pool, const TString& title, THolder<IAuthProvider> auth)
  25. : NMonitoring::TMtHttpServer(options, std::bind(&TMonService2::ServeRequest, this, std::placeholders::_1, std::placeholders::_2), std::move(pool))
  26. , Title(title)
  27. , IndexMonPage(new TIndexMonPage("", Title))
  28. , AuthProvider_{std::move(auth)}
  29. {
  30. Y_ABORT_UNLESS(!!title);
  31. time_t t = time(nullptr);
  32. ctime_r(&t, StartTime);
  33. }
  34. TMonService2::TMonService2(ui16 port, ui32 threads, const TString& title, THolder<IAuthProvider> auth)
  35. : TMonService2(port, TString(), threads, title, std::move(auth))
  36. {
  37. }
  38. TMonService2::TMonService2(ui16 port, const TString& title, THolder<IAuthProvider> auth)
  39. : TMonService2(port, TString(), 0, title, std::move(auth))
  40. {
  41. }
  42. void TMonService2::OutputIndex(IOutputStream& out) {
  43. IndexMonPage->OutputIndex(out, true);
  44. }
  45. void TMonService2::OutputIndexPage(IOutputStream& out) {
  46. out << HTTPOKHTML;
  47. out << "<html>\n";
  48. IndexMonPage->OutputHead(out);
  49. OutputIndexBody(out);
  50. out << "</html>\n";
  51. }
  52. void TMonService2::OutputIndexBody(IOutputStream& out) {
  53. out << "<body>\n";
  54. // part of common navbar
  55. out << "<ol class='breadcrumb'>\n";
  56. out << "<li class='active'>" << Title << "</li>\n";
  57. out << "</ol>\n";
  58. out << "<div class='container'>\n"
  59. << "<h2>" << Title << "</h2>\n";
  60. OutputIndex(out);
  61. out
  62. << "<div>\n"
  63. << "</body>\n";
  64. }
  65. void TMonService2::ServeRequest(IOutputStream& out, const NMonitoring::IHttpRequest& request) {
  66. TString path = request.GetPath();
  67. Y_ABORT_UNLESS(path.StartsWith('/'));
  68. if (AuthProvider_) {
  69. const auto authResult = AuthProvider_->Check(request);
  70. switch (authResult.Status) {
  71. case TAuthResult::EStatus::NoCredentials:
  72. out << HTTPUNAUTHORIZED;
  73. return;
  74. case TAuthResult::EStatus::Denied:
  75. out << HTTPFORBIDDEN;
  76. return;
  77. case TAuthResult::EStatus::Ok:
  78. break;
  79. }
  80. }
  81. if (path == "/") {
  82. OutputIndexPage(out);
  83. } else {
  84. TMonService2HttpRequest monService2HttpRequest(
  85. &out, &request, this, IndexMonPage.Get(), path, nullptr);
  86. IndexMonPage->Output(monService2HttpRequest);
  87. }
  88. }
  89. void TMonService2::Register(IMonPage* page) {
  90. IndexMonPage->Register(page);
  91. }
  92. void TMonService2::Register(TMonPagePtr page) {
  93. IndexMonPage->Register(std::move(page));
  94. }
  95. TIndexMonPage* TMonService2::RegisterIndexPage(const TString& path, const TString& title) {
  96. return IndexMonPage->RegisterIndexPage(path, title);
  97. }
  98. IMonPage* TMonService2::FindPage(const TString& relativePath) {
  99. return IndexMonPage->FindPage(relativePath);
  100. }
  101. TIndexMonPage* TMonService2::FindIndexPage(const TString& relativePath) {
  102. return IndexMonPage->FindIndexPage(relativePath);
  103. }
  104. void TMonService2::SortPages() {
  105. IndexMonPage->SortPages();
  106. }