diff options
author | pnv1 <pnv@ydb.tech> | 2022-09-06 15:57:32 +0300 |
---|---|---|
committer | pnv1 <pnv@ydb.tech> | 2022-09-06 15:57:32 +0300 |
commit | 505c8099fd67fafc495541d9451047730287d2a5 (patch) | |
tree | 68bab25013faa135ad4a2cb4f816288737048e2f | |
parent | 72277b0f60b65f7ff162686fcac08becc2adc0d1 (diff) | |
download | ydb-505c8099fd67fafc495541d9451047730287d2a5.tar.gz |
Add healthcheck to YDB CLI
%%
pnv1@mr-nvme-testing-04:~/arcadia$ kikimr/public/tools/ydb/ydb -p ydb-ru mon healthcheck
Healthcheck status: GOOD
pnv1@mr-nvme-testing-04:~/arcadia$ kikimr/public/tools/ydb/ydb -p ydb-ru mon healthcheck --format json
{
"self_check_result": "GOOD"
}
pnv1@mr-nvme-testing-04:~/arcadia$ kikimr/public/tools/ydb/ydb -p ydb-vla-dev02 mon healthcheck Healthcheck status: DEGRADED
For more info use "--format json" option
pnv1@mr-nvme-testing-04:~/arcadia$ kikimr/public/tools/ydb/ydb -p ydb-vla-dev02 mon healthcheck --format json
{
"self_check_result": "DEGRADED",
"issue_log": [
{
"id": "YELLOW-be81",
"status": "YELLOW",
"message": "Database has storage issues",
"location": {
"database": {
"name": "/dev02/home/xenoxeno/db1"
}
},
"reason": [
""
],
"type": "DATABASE",
"level": 1
},
{
"id": "",
"status": "YELLOW",
"message": "Storage degraded",
"location": {
"database": {
"name": "/dev02/home/xenoxeno/db1"
}
},
...
%%
4 files changed, 143 insertions, 0 deletions
diff --git a/ydb/public/lib/ydb_cli/commands/CMakeLists.txt b/ydb/public/lib/ydb_cli/commands/CMakeLists.txt index fe8e547a3ae..89c9fbba660 100644 --- a/ydb/public/lib/ydb_cli/commands/CMakeLists.txt +++ b/ydb/public/lib/ydb_cli/commands/CMakeLists.txt @@ -26,6 +26,7 @@ target_link_libraries(clicommands PUBLIC cpp-client-ydb_discovery cpp-client-ydb_export cpp-client-ydb_import + cpp-client-ydb_monitoring cpp-client-ydb_operation cpp-client-ydb_persqueue_public cpp-client-ydb_proto @@ -43,6 +44,7 @@ target_sources(clicommands PRIVATE ${CMAKE_SOURCE_DIR}/ydb/public/lib/ydb_cli/commands/ydb_service_discovery.cpp ${CMAKE_SOURCE_DIR}/ydb/public/lib/ydb_cli/commands/ydb_service_export.cpp ${CMAKE_SOURCE_DIR}/ydb/public/lib/ydb_cli/commands/ydb_service_import.cpp + ${CMAKE_SOURCE_DIR}/ydb/public/lib/ydb_cli/commands/ydb_service_monitoring.cpp ${CMAKE_SOURCE_DIR}/ydb/public/lib/ydb_cli/commands/ydb_service_operation.cpp ${CMAKE_SOURCE_DIR}/ydb/public/lib/ydb_cli/commands/ydb_service_scheme.cpp ${CMAKE_SOURCE_DIR}/ydb/public/lib/ydb_cli/commands/ydb_service_scripting.cpp diff --git a/ydb/public/lib/ydb_cli/commands/ydb_root_common.cpp b/ydb/public/lib/ydb_cli/commands/ydb_root_common.cpp index 31805fd27e6..3ba870aba0c 100644 --- a/ydb/public/lib/ydb_cli/commands/ydb_root_common.cpp +++ b/ydb/public/lib/ydb_cli/commands/ydb_root_common.cpp @@ -4,6 +4,7 @@ #include "ydb_service_export.h" #include "ydb_service_import.h" #include "ydb_service_operation.h" +#include "ydb_service_monitoring.h" #include "ydb_service_scheme.h" #include "ydb_service_scripting.h" #include "ydb_service_table.h" @@ -33,6 +34,7 @@ TClientCommandRootCommon::TClientCommandRootCommon(const TClientSettings& settin AddCommand(std::make_unique<TCommandTools>()); AddCommand(std::make_unique<TCommandExport>(Settings.UseExportToYt.GetRef())); AddCommand(std::make_unique<TCommandImport>()); + AddCommand(std::make_unique<TCommandMonitoring>()); AddCommand(std::make_unique<TCommandOperation>()); AddCommand(std::make_unique<TCommandConfig>()); AddCommand(std::make_unique<TCommandInit>()); diff --git a/ydb/public/lib/ydb_cli/commands/ydb_service_monitoring.cpp b/ydb/public/lib/ydb_cli/commands/ydb_service_monitoring.cpp new file mode 100644 index 00000000000..2b28ee7f76a --- /dev/null +++ b/ydb/public/lib/ydb_cli/commands/ydb_service_monitoring.cpp @@ -0,0 +1,108 @@ +#include "ydb_service_monitoring.h" + +#include <ydb/public/api/grpc/ydb_monitoring_v1.grpc.pb.h> +#include <ydb/public/sdk/cpp/client/ydb_proto/accessor.h> + +namespace NYdb { +namespace NConsoleClient { + +TCommandMonitoring::TCommandMonitoring() + : TClientCommandTree("monitoring", {"mon"}, "Monitoring service operations") +{ + AddCommand(std::make_unique<TCommandSelfCheck>()); +} + +TCommandSelfCheck::TCommandSelfCheck() + : TYdbSimpleCommand("healthcheck", {"selfcheck"}, "Self check. Returns status of the database") +{} + +void TCommandSelfCheck::Config(TConfig& config) { + TYdbSimpleCommand::Config(config); + config.SetFreeArgsNum(0); + + AddFormats(config, { EOutputFormat::Pretty, EOutputFormat::Json }); + + config.Opts->AddLongOption('v', "verbose", "Return detailed info about components checked with their statuses.") + .StoreTrue(&Verbose); +} + +void TCommandSelfCheck::Parse(TConfig& config) { + TYdbSimpleCommand::Parse(config); + ParseFormats(); +} + +int TCommandSelfCheck::Run(TConfig& config) { + NMonitoring::TMonitoringClient client(CreateDriver(config)); + NMonitoring::TSelfCheckSettings settings; + + if (Verbose) { + settings.ReturnVerboseStatus(true); + } + + NMonitoring::TSelfCheckResult result = client.SelfCheck( + FillSettings(settings) + ).GetValueSync(); + ThrowOnError(result); + return PrintResponse(result); +} + +int TCommandSelfCheck::PrintResponse(NMonitoring::TSelfCheckResult& result) { + const auto& proto = NYdb::TProtoAccessor::GetProto(result); + switch (OutputFormat) { + case EOutputFormat::Default: + case EOutputFormat::Pretty: + { + NColorizer::TColors colors = NColorizer::AutoColors(Cout); + TStringBuf statusColor; + auto hcResultString = SelfCheck_Result_Name(proto.Getself_check_result()); + switch (proto.Getself_check_result()) { + case Ydb::Monitoring::SelfCheck::GOOD: + statusColor = colors.GreenColor(); + break; + case Ydb::Monitoring::SelfCheck::DEGRADED: + statusColor = colors.YellowColor(); + break; + case Ydb::Monitoring::SelfCheck::MAINTENANCE_REQUIRED: + // Orange-ish + statusColor = colors.IsTTY() ? "\033[88;91m" : ""; + break; + case Ydb::Monitoring::SelfCheck::EMERGENCY: + statusColor = colors.RedColor(); + break; + case Ydb::Monitoring::SelfCheck::UNSPECIFIED: + statusColor = colors.OldColor(); + break; + default: + Cout << "Unknown healthcheck status: " << hcResultString << Endl; + return EXIT_FAILURE; + } + + Cout << "Healthcheck status: " << statusColor << hcResultString << colors.OldColor() << Endl; + if (proto.Getself_check_result() != Ydb::Monitoring::SelfCheck::GOOD) { + Cerr << "For more info use \"--format json\" option" << Endl; + } + break; + } + case EOutputFormat::Json: + { + TString json;
+ google::protobuf::util::JsonPrintOptions jsonOpts;
+ jsonOpts.preserve_proto_field_names = true;
+ jsonOpts.add_whitespace = true;
+ auto convertStatus = google::protobuf::util::MessageToJsonString(proto, &json, jsonOpts);
+ if (convertStatus.ok()) {
+ Cout << json;
+ } else {
+ Cerr << "Error occurred while converting result proto to json" << Endl;
+ return EXIT_FAILURE;
+ } + break; + }
+ default:
+ throw TMisuseException() << "This command doesn't support " << OutputFormat << " output format"; + } + return EXIT_SUCCESS; +} + +} +} diff --git a/ydb/public/lib/ydb_cli/commands/ydb_service_monitoring.h b/ydb/public/lib/ydb_cli/commands/ydb_service_monitoring.h new file mode 100644 index 00000000000..d5454593808 --- /dev/null +++ b/ydb/public/lib/ydb_cli/commands/ydb_service_monitoring.h @@ -0,0 +1,31 @@ +#pragma once + +#include "ydb_command.h" +#include "ydb_common.h" +
+#include <ydb/public/lib/ydb_cli/common/format.h> +#include <ydb/public/sdk/cpp/client/ydb_monitoring/monitoring.h> + +namespace NYdb { +namespace NConsoleClient { + +class TCommandMonitoring : public TClientCommandTree { +public: + TCommandMonitoring(); +}; + +class TCommandSelfCheck : public TYdbSimpleCommand, public TCommandWithFormat { +public: + TCommandSelfCheck(); + void Config(TConfig& config) override; + void Parse(TConfig& config) override; + int Run(TConfig& config) override; + +private: + int PrintResponse(NMonitoring::TSelfCheckResult& result); + + bool Verbose = false; +}; + +} +} |