From cf433ec94c40615e98d10b2a4ccd8dd5f51f18e3 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 19 Apr 2022 16:27:20 +0800 Subject: [PATCH] feat: check server status --- source/client/src/clientImpl.c | 10 ++++- source/dnode/mgmt/interface/src/dmInt.c | 4 +- tools/shell/inc/shell.h | 3 +- tools/shell/src/shellMain.c | 49 ++++++++++++++++++------- 4 files changed, 49 insertions(+), 17 deletions(-) diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index c26d45dbce..526dec4df9 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -844,6 +844,14 @@ TSDB_SERVER_STATUS taos_check_server_status(const char* fqdn, int port, char* de goto _OVER; } + if (fqdn == NULL) { + fqdn = tsLocalFqdn; + } + + if (port == 0) { + port = tsServerPort; + } + tstrncpy(epSet.eps[0].fqdn, fqdn, TSDB_FQDN_LEN); epSet.eps[0].port = (uint16_t)port; rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp); @@ -859,7 +867,7 @@ TSDB_SERVER_STATUS taos_check_server_status(const char* fqdn, int port, char* de } code = statusRsp.statusCode; - if (details != NULL) { + if (details != NULL && statusRsp.details != NULL) { tstrncpy(details, statusRsp.details, maxlen); } diff --git a/source/dnode/mgmt/interface/src/dmInt.c b/source/dnode/mgmt/interface/src/dmInt.c index 88fd5eab17..067617dd52 100644 --- a/source/dnode/mgmt/interface/src/dmInt.c +++ b/source/dnode/mgmt/interface/src/dmInt.c @@ -145,7 +145,7 @@ void dmReportStartup(SDnode *pDnode, const char *pName, const char *pDesc) { static void dmGetServerStatus(SDnode *pDnode, SServerStatusRsp *pStatus) { if (pDnode->status == DND_STAT_INIT) { pStatus->statusCode = TSDB_SRV_STATUS_NETWORK_OK; - } else if (pDnode->status != DND_STAT_STOPPED) { + } else if (pDnode->status == DND_STAT_STOPPED) { pStatus->statusCode = TSDB_SRV_STATUS_EXTING; } else { pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_OK; @@ -172,7 +172,7 @@ void dmProcessServerStatusReq(SDnode *pDnode, SRpcMsg *pReq) { SServerStatusRsp statusRsp = {0}; dmGetServerStatus(pDnode, &statusRsp); - SRpcMsg rspMsg = {.handle = pReq->handle, .handle = pReq->ahandle}; + SRpcMsg rspMsg = {.handle = pReq->handle, .ahandle = pReq->ahandle}; int32_t rspLen = tSerializeSServerStatusRsp(NULL, 0, &statusRsp); if (rspLen < 0) { rspMsg.code = TSDB_CODE_OUT_OF_MEMORY; diff --git a/tools/shell/inc/shell.h b/tools/shell/inc/shell.h index 7a16ee9d2c..866cd63bdb 100644 --- a/tools/shell/inc/shell.h +++ b/tools/shell/inc/shell.h @@ -50,6 +50,8 @@ typedef struct SShellArguments { char dir[TSDB_FILENAME_LEN]; int threadNum; int check; + bool status; + bool verbose; char* commands; int abort; int port; @@ -72,7 +74,6 @@ void read_history(); void write_history(); void source_file(TAOS* con, char* fptr); void source_dir(TAOS* con, SShellArguments* args); -void shellCheck(TAOS* con, SShellArguments* args); void get_history_path(char* history); void shellCheck(TAOS* con, SShellArguments* args); void cleanup_handler(void* arg); diff --git a/tools/shell/src/shellMain.c b/tools/shell/src/shellMain.c index 0d397eb80b..c19a285a74 100644 --- a/tools/shell/src/shellMain.c +++ b/tools/shell/src/shellMain.c @@ -35,6 +35,7 @@ static char args_doc[] = ""; TdThread pid; static tsem_t cancelSem; +extern void taos_init(); static struct argp_option options[] = { {"host", 'h', "HOST", 0, "TDengine server FQDN to connect. The default host is localhost."}, @@ -52,6 +53,8 @@ static struct argp_option options[] = { {"check", 'k', "CHECK", 0, "Check tables."}, {"database", 'd', "DATABASE", 0, "Database to use when connecting to the server."}, {"timezone", 'z', "TIMEZONE", 0, "Time zone of the shell, default is local."}, + {"status", 't', NULL, 0, "Check the service status."}, + {"verbose", 'v', NULL, 0, "Check the details of the service status."}, {"netrole", 'n', "NETROLE", 0, "Net role when network connectivity test, default is startup, options: client|server|rpc|startup|sync|speed|fqdn."}, {"pktlen", 'l', "PKTLEN", 0, "Packet length used for net test, default is 1000 bytes."}, {"pktnum", 'N', "PKTNUM", 0, "Packet numbers used for net test, default is 100."}, @@ -138,6 +141,12 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) { case 'k': arguments->check = atoi(arg); break; + case 't': + arguments->status = true; + break; + case 'v': + arguments->verbose = true; + break; case 'd': arguments->database = arg; break; @@ -608,10 +617,9 @@ int main(int argc, char *argv[]) { } shellParseArgument(argc, argv, &args); + taos_init(); if (args.dump_config) { - taosInitCfg(configDir, NULL, NULL, NULL, 1); - SConfig *pCfg = taosGetCfg(); if (NULL == pCfg) { printf("TDengine read global config failed!\n"); @@ -621,21 +629,36 @@ int main(int argc, char *argv[]) { exit(0); } - if (args.netTestRole && args.netTestRole[0] != 0) { - TAOS *con = NULL; - if (args.auth == NULL) { - con = taos_connect(args.host, args.user, args.password, args.database, args.port); - } else { - con = taos_connect_auth(args.host, args.user, args.auth, args.database, args.port); + if (args.status || args.verbose) { + char details[1024] = {0}; + + TSDB_SERVER_STATUS code = taos_check_server_status(args.host, args.port, details, args.verbose ? 1024 : 0); + switch (code) { + case TSDB_SRV_STATUS_UNAVAILABLE: + printf("0: unavailable\n"); + break; + case TSDB_SRV_STATUS_NETWORK_OK: + printf("1: network ok\n"); + break; + case TSDB_SRV_STATUS_SERVICE_OK: + printf("2: service ok\n"); + break; + case TSDB_SRV_STATUS_SERVICE_DEGRADED: + printf("3: service degradedk\n"); + break; + case TSDB_SRV_STATUS_EXTING: + printf("4: exiting\n"); + break; } - // if (taos_init()) { - // printf("Failed to init taos"); - // exit(EXIT_FAILURE); - // } + if (strlen(details) != 0) { + printf("detail info:\n%s\n", details); + } + exit(0); + } + if (args.netTestRole && args.netTestRole[0] != 0) { taosNetTest(args.netTestRole, args.host, args.port, args.pktLen, args.pktNum, args.pktType); - taos_close(con); exit(0); } -- GitLab