提交 29238fa4 编写于 作者: G Ganlin Zhao

[TD-6452]<feature>: taoskeeper metrics collector phase 1 taosd implementation

上级 65626152
...@@ -181,7 +181,7 @@ static void *monThreadFunc(void *param) { ...@@ -181,7 +181,7 @@ static void *monThreadFunc(void *param) {
} }
if (tsMonitor.state == MON_STATE_INITED) { if (tsMonitor.state == MON_STATE_INITED) {
if (accessTimes % tsMonitorInterval == 0) { if (accessTimes % tsMonitorInterval == 0 || accessTimes == 1) {
monSaveSystemInfo(); monSaveSystemInfo();
monSaveClusterInfo(); monSaveClusterInfo();
} }
...@@ -408,6 +408,17 @@ static void monSaveSystemInfo() { ...@@ -408,6 +408,17 @@ static void monSaveSystemInfo() {
} }
} }
static int32_t monGetRowElemCharLen(TAOS_FIELD field, char *rowElem) {
int32_t charLen = varDataLen(rowElem - VARSTR_HEADER_SIZE);
if (field.type == TSDB_DATA_TYPE_BINARY) {
assert(charLen <= field.bytes && charLen >= 0);
} else {
assert(charLen <= field.bytes * TSDB_NCHAR_SIZE && charLen >= 0);
}
return charLen;
}
static int32_t monBuildFirstEpSql(char *sql) { static int32_t monBuildFirstEpSql(char *sql) {
return sprintf(sql, ", \"%s\"", tsFirst); return sprintf(sql, ", \"%s\"", tsFirst);
} }
...@@ -417,7 +428,26 @@ static int32_t monBuildVersionSql(char *sql) { ...@@ -417,7 +428,26 @@ static int32_t monBuildVersionSql(char *sql) {
} }
static int32_t monBuildMasterUptimeSql(char *sql) { static int32_t monBuildMasterUptimeSql(char *sql) {
return sprintf(sql, ", %d", 0); int64_t masterUptime = 0;
TAOS_RES *result = taos_query(tsMonitor.conn, "show mnodes");
TAOS_ROW row;
int32_t num_fields = taos_num_fields(result);
TAOS_FIELD *fields = taos_fetch_fields(result);
while ((row = taos_fetch_row(result))) {
for (int i = 0; i < num_fields; ++i) {
if (strcmp(fields[i].name, "role_time") == 0) {
int64_t now = taosGetTimestamp(TSDB_TIME_PRECISION_MILLI);
//master uptime in seconds
masterUptime = (now - *(int64_t *)row[i]) / 1000;
}
}
}
taos_free_result(result);
return sprintf(sql, ", %" PRId64, masterUptime);
} }
static int32_t monBuildMonIntervalSql(char *sql) { static int32_t monBuildMonIntervalSql(char *sql) {
...@@ -436,12 +466,7 @@ static int32_t monBuildDnodesTotalSql(char *sql) { ...@@ -436,12 +466,7 @@ static int32_t monBuildDnodesTotalSql(char *sql) {
totalDnodes++; totalDnodes++;
for (int i = 0; i < num_fields; ++i) { for (int i = 0; i < num_fields; ++i) {
if (strcmp(fields[i].name, "status") == 0) { if (strcmp(fields[i].name, "status") == 0) {
int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE); int32_t charLen = monGetRowElemCharLen(fields[i], (char *)row[i]);
if (fields[i].type == TSDB_DATA_TYPE_BINARY) {
assert(charLen <= fields[i].bytes && charLen >= 0);
} else {
assert(charLen <= fields[i].bytes * TSDB_NCHAR_SIZE && charLen >= 0);
}
if (strncmp((char *)row[i], "ready", charLen) == 0) { if (strncmp((char *)row[i], "ready", charLen) == 0) {
totalDnodesAlive++; totalDnodesAlive++;
printf("Gavin0- row:%s, charLen:%d\n", (char *)row[i], charLen); printf("Gavin0- row:%s, charLen:%d\n", (char *)row[i], charLen);
...@@ -459,10 +484,22 @@ static int32_t monBuildDnodesTotalSql(char *sql) { ...@@ -459,10 +484,22 @@ static int32_t monBuildDnodesTotalSql(char *sql) {
static int32_t monBuildMnodesTotalSql(char *sql) { static int32_t monBuildMnodesTotalSql(char *sql) {
int32_t totalMnodes = 0, totalMnodesAlive= 0; int32_t totalMnodes = 0, totalMnodesAlive= 0;
TAOS_RES *result = taos_query(tsMonitor.conn, "show mnodes"); TAOS_RES *result = taos_query(tsMonitor.conn, "show mnodes");
TAOS_ROW row; TAOS_ROW row;
int32_t num_fields = taos_num_fields(result);
TAOS_FIELD *fields = taos_fetch_fields(result);
while ((row = taos_fetch_row(result))) { while ((row = taos_fetch_row(result))) {
totalMnodes++; totalMnodes++;
for (int i = 0; i < num_fields; ++i) {
if (strcmp(fields[i].name, "role") == 0) {
int32_t charLen = monGetRowElemCharLen(fields[i], (char *)row[i]);
if (strncmp((char *)row[i], "master", charLen) == 0 ||
strncmp((char *)row[i], "slave", charLen) == 0) {
totalMnodesAlive += 1;
}
}
}
} }
taos_free_result(result); taos_free_result(result);
...@@ -470,6 +507,7 @@ static int32_t monBuildMnodesTotalSql(char *sql) { ...@@ -470,6 +507,7 @@ static int32_t monBuildMnodesTotalSql(char *sql) {
return sprintf(sql, ", %d, %d", totalMnodes, totalMnodesAlive); return sprintf(sql, ", %d, %d", totalMnodes, totalMnodesAlive);
} }
static int32_t monGetVgroupsTotalStats(char *dbName, int32_t *totalVgroups, static int32_t monGetVgroupsTotalStats(char *dbName, int32_t *totalVgroups,
int32_t *totalVgroupsAlive) { int32_t *totalVgroupsAlive) {
char subsql[TSDB_DB_NAME_LEN + 14]; char subsql[TSDB_DB_NAME_LEN + 14];
...@@ -485,13 +523,7 @@ static int32_t monGetVgroupsTotalStats(char *dbName, int32_t *totalVgroups, ...@@ -485,13 +523,7 @@ static int32_t monGetVgroupsTotalStats(char *dbName, int32_t *totalVgroups,
*totalVgroups += 1; *totalVgroups += 1;
for (int i = 0; i < num_fields; ++i) { for (int i = 0; i < num_fields; ++i) {
if (strcmp(fields[i].name, "status") == 0) { if (strcmp(fields[i].name, "status") == 0) {
int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE); int32_t charLen = monGetRowElemCharLen(fields[i], (char *)row[i]);
if (fields[i].type == TSDB_DATA_TYPE_BINARY) {
assert(charLen <= fields[i].bytes && charLen >= 0);
} else {
assert(charLen <= fields[i].bytes * TSDB_NCHAR_SIZE && charLen >= 0);
}
if (strncmp((char *)row[i], "ready", charLen) == 0) { if (strncmp((char *)row[i], "ready", charLen) == 0) {
*totalVgroupsAlive += 1; *totalVgroupsAlive += 1;
} }
...@@ -515,12 +547,6 @@ static int32_t monBuildVgroupsTotalSql(char *sql) { ...@@ -515,12 +547,6 @@ static int32_t monBuildVgroupsTotalSql(char *sql) {
for (int i = 0; i < num_fields; ++i) { for (int i = 0; i < num_fields; ++i) {
//database name //database name
if (strcmp(fields[i].name, "name") == 0) { if (strcmp(fields[i].name, "name") == 0) {
int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
if (fields[i].type == TSDB_DATA_TYPE_BINARY) {
assert(charLen <= fields[i].bytes && charLen >= 0);
} else {
assert(charLen <= fields[i].bytes * TSDB_NCHAR_SIZE && charLen >= 0);
}
monGetVgroupsTotalStats((char *)row[i], &totalVgroups, &totalVgroupsAlive); monGetVgroupsTotalStats((char *)row[i], &totalVgroups, &totalVgroupsAlive);
} }
} }
...@@ -535,8 +561,8 @@ static int32_t monGetVnodesTotalStats(char *ep, int32_t *totalVnodes, ...@@ -535,8 +561,8 @@ static int32_t monGetVnodesTotalStats(char *ep, int32_t *totalVnodes,
int32_t *totalVnodesAlive) { int32_t *totalVnodesAlive) {
char subsql[TSDB_EP_LEN + 15]; char subsql[TSDB_EP_LEN + 15];
memset(subsql, 0, sizeof(subsql)); memset(subsql, 0, sizeof(subsql));
//sprintf(subsql, "show vnodes \"%s\"", ep); sprintf(subsql, "show vnodes \"%s\"", ep);
TAOS_RES *result = taos_query(tsMonitor.conn, "show vnodes 'ubuntu:6030'"); TAOS_RES *result = taos_query(tsMonitor.conn, subsql);
TAOS_ROW row; TAOS_ROW row;
int32_t num_fields = taos_num_fields(result); int32_t num_fields = taos_num_fields(result);
...@@ -546,13 +572,7 @@ static int32_t monGetVnodesTotalStats(char *ep, int32_t *totalVnodes, ...@@ -546,13 +572,7 @@ static int32_t monGetVnodesTotalStats(char *ep, int32_t *totalVnodes,
*totalVnodes += 1; *totalVnodes += 1;
for (int i = 0; i < num_fields; ++i) { for (int i = 0; i < num_fields; ++i) {
if (strcmp(fields[i].name, "status") == 0) { if (strcmp(fields[i].name, "status") == 0) {
int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE); int32_t charLen = monGetRowElemCharLen(fields[i], (char *)row[i]);
if (fields[i].type == TSDB_DATA_TYPE_BINARY) {
assert(charLen <= fields[i].bytes && charLen >= 0);
} else {
assert(charLen <= fields[i].bytes * TSDB_NCHAR_SIZE && charLen >= 0);
}
if (strncmp((char *)row[i], "master", charLen) == 0 || if (strncmp((char *)row[i], "master", charLen) == 0 ||
strncmp((char *)row[i], "slave", charLen) == 0) { strncmp((char *)row[i], "slave", charLen) == 0) {
*totalVnodesAlive += 1; *totalVnodesAlive += 1;
...@@ -577,12 +597,6 @@ static int32_t monBuildVnodesTotalSql(char *sql) { ...@@ -577,12 +597,6 @@ static int32_t monBuildVnodesTotalSql(char *sql) {
for (int i = 0; i < num_fields; ++i) { for (int i = 0; i < num_fields; ++i) {
//database name //database name
if (strcmp(fields[i].name, "end_point") == 0) { if (strcmp(fields[i].name, "end_point") == 0) {
int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
if (fields[i].type == TSDB_DATA_TYPE_BINARY) {
assert(charLen <= fields[i].bytes && charLen >= 0);
} else {
assert(charLen <= fields[i].bytes * TSDB_NCHAR_SIZE && charLen >= 0);
}
monGetVnodesTotalStats((char *)row[i], &totalVnodes, &totalVnodesAlive); monGetVnodesTotalStats((char *)row[i], &totalVnodes, &totalVnodesAlive);
} }
} }
...@@ -594,7 +608,16 @@ static int32_t monBuildVnodesTotalSql(char *sql) { ...@@ -594,7 +608,16 @@ static int32_t monBuildVnodesTotalSql(char *sql) {
} }
static int32_t monBuildConnsTotalSql(char *sql) { static int32_t monBuildConnsTotalSql(char *sql) {
return sprintf(sql, ", %d)", 0); int32_t totalConns = 0;
TAOS_RES *result = taos_query(tsMonitor.conn, "show connections");
TAOS_ROW row;
while ((row = taos_fetch_row(result))) {
totalConns++;
}
taos_free_result(result);
return sprintf(sql, ", %d)", totalConns);
} }
static void monSaveClusterInfo() { static void monSaveClusterInfo() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册