monMain.c 20.6 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#define _DEFAULT_SOURCE
#include "monInt.h"
S
monitor  
Shengliang Guan 已提交
18 19
#include "taoserror.h"
#include "thttp.h"
20
#include "ttime.h"
S
Shengliang Guan 已提交
21

S
monitor  
Shengliang Guan 已提交
22 23
static SMonitor tsMonitor = {0};

S
Shengliang Guan 已提交
24
void monRecordLog(int64_t ts, ELogLevel level, const char *content) {
wafwerar's avatar
wafwerar 已提交
25
  taosThreadMutexLock(&tsMonitor.lock);
S
Shengliang Guan 已提交
26
  int32_t size = taosArrayGetSize(tsMonitor.logs);
27
  if (size < tsMonitor.cfg.maxLogs) {
S
Shengliang Guan 已提交
28 29 30
    SMonLogItem  item = {.ts = ts, .level = level};
    SMonLogItem *pItem = taosArrayPush(tsMonitor.logs, &item);
    if (pItem != NULL) {
S
Shengliang Guan 已提交
31
      tstrncpy(pItem->content, content, MON_LOG_LEN);
S
Shengliang Guan 已提交
32 33
    }
  }
wafwerar's avatar
wafwerar 已提交
34
  taosThreadMutexUnlock(&tsMonitor.lock);
S
Shengliang Guan 已提交
35 36
}

37 38
int32_t monGetLogs(SMonLogs *logs) {
  taosThreadMutexLock(&tsMonitor.lock);
H
Haojun Liao 已提交
39
  logs->logs = taosArrayDup(tsMonitor.logs, NULL);
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
  logs->numOfInfoLogs = tsNumOfInfoLogs;
  logs->numOfErrorLogs = tsNumOfErrorLogs;
  logs->numOfDebugLogs = tsNumOfDebugLogs;
  logs->numOfTraceLogs = tsNumOfTraceLogs;
  tsNumOfInfoLogs = 0;
  tsNumOfErrorLogs = 0;
  tsNumOfDebugLogs = 0;
  tsNumOfTraceLogs = 0;
  taosArrayClear(tsMonitor.logs);
  taosThreadMutexUnlock(&tsMonitor.lock);
  if (logs->logs == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  return 0;
}

void monSetDmInfo(SMonDmInfo *pInfo) {
  taosThreadMutexLock(&tsMonitor.lock);
  memcpy(&tsMonitor.dmInfo, pInfo, sizeof(SMonDmInfo));
  taosThreadMutexUnlock(&tsMonitor.lock);
  memset(pInfo, 0, sizeof(SMonDmInfo));
}

void monSetMmInfo(SMonMmInfo *pInfo) {
  taosThreadMutexLock(&tsMonitor.lock);
  memcpy(&tsMonitor.mmInfo, pInfo, sizeof(SMonMmInfo));
  taosThreadMutexUnlock(&tsMonitor.lock);
  memset(pInfo, 0, sizeof(SMonMmInfo));
}

void monSetVmInfo(SMonVmInfo *pInfo) {
  taosThreadMutexLock(&tsMonitor.lock);
  memcpy(&tsMonitor.vmInfo, pInfo, sizeof(SMonVmInfo));
  taosThreadMutexUnlock(&tsMonitor.lock);
  memset(pInfo, 0, sizeof(SMonVmInfo));
}

void monSetQmInfo(SMonQmInfo *pInfo) {
  taosThreadMutexLock(&tsMonitor.lock);
  memcpy(&tsMonitor.qmInfo, pInfo, sizeof(SMonQmInfo));
  taosThreadMutexUnlock(&tsMonitor.lock);
  memset(pInfo, 0, sizeof(SMonQmInfo));
}

void monSetSmInfo(SMonSmInfo *pInfo) {
  taosThreadMutexLock(&tsMonitor.lock);
  memcpy(&tsMonitor.smInfo, pInfo, sizeof(SMonSmInfo));
  taosThreadMutexUnlock(&tsMonitor.lock);
  memset(pInfo, 0, sizeof(SMonSmInfo));
}

void monSetBmInfo(SMonBmInfo *pInfo) {
  taosThreadMutexLock(&tsMonitor.lock);
  memcpy(&tsMonitor.bmInfo, pInfo, sizeof(SMonBmInfo));
  taosThreadMutexUnlock(&tsMonitor.lock);
  memset(pInfo, 0, sizeof(SMonBmInfo));
}

S
monitor  
Shengliang Guan 已提交
99
int32_t monInit(const SMonCfg *pCfg) {
S
monitor  
Shengliang Guan 已提交
100
  tsMonitor.logs = taosArrayInit(16, sizeof(SMonLogItem));
S
monitor  
Shengliang Guan 已提交
101 102 103 104 105
  if (tsMonitor.logs == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

106
  tsMonitor.cfg = *pCfg;
S
Shengliang Guan 已提交
107
  tsLogFp = monRecordLog;
108
  tsMonitor.lastTime = taosGetTimestampMs();
wafwerar's avatar
wafwerar 已提交
109
  taosThreadMutexInit(&tsMonitor.lock, NULL);
S
monitor  
Shengliang Guan 已提交
110 111 112 113
  return 0;
}

void monCleanup() {
S
Shengliang Guan 已提交
114
  tsLogFp = NULL;
S
monitor  
Shengliang Guan 已提交
115 116
  taosArrayDestroy(tsMonitor.logs);
  tsMonitor.logs = NULL;
117 118 119 120 121
  tFreeSMonMmInfo(&tsMonitor.mmInfo);
  tFreeSMonVmInfo(&tsMonitor.vmInfo);
  tFreeSMonSmInfo(&tsMonitor.smInfo);
  tFreeSMonQmInfo(&tsMonitor.qmInfo);
  tFreeSMonBmInfo(&tsMonitor.bmInfo);
wafwerar's avatar
wafwerar 已提交
122
  taosThreadMutexDestroy(&tsMonitor.lock);
S
monitor  
Shengliang Guan 已提交
123 124
}

125 126
static void monCleanupMonitorInfo(SMonInfo *pMonitor) {
  tsMonitor.lastTime = pMonitor->curTime;
127
  taosArrayDestroy(pMonitor->log.logs);
128 129 130 131 132 133 134 135 136 137
  tFreeSMonMmInfo(&pMonitor->mmInfo);
  tFreeSMonVmInfo(&pMonitor->vmInfo);
  tFreeSMonSmInfo(&pMonitor->smInfo);
  tFreeSMonQmInfo(&pMonitor->qmInfo);
  tFreeSMonBmInfo(&pMonitor->bmInfo);
  tjsonDelete(pMonitor->pJson);
  taosMemoryFree(pMonitor);
}

static SMonInfo *monCreateMonitorInfo() {
wafwerar's avatar
wafwerar 已提交
138
  SMonInfo *pMonitor = taosMemoryCalloc(1, sizeof(SMonInfo));
S
monitor  
Shengliang Guan 已提交
139 140 141 142
  if (pMonitor == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
S
monitor  
Shengliang Guan 已提交
143

144
  monGetLogs(&pMonitor->log);
145

wafwerar's avatar
wafwerar 已提交
146
  taosThreadMutexLock(&tsMonitor.lock);
147 148 149 150 151 152 153 154 155 156 157 158
  memcpy(&pMonitor->dmInfo, &tsMonitor.dmInfo, sizeof(SMonDmInfo));
  memcpy(&pMonitor->mmInfo, &tsMonitor.mmInfo, sizeof(SMonMmInfo));
  memcpy(&pMonitor->vmInfo, &tsMonitor.vmInfo, sizeof(SMonVmInfo));
  memcpy(&pMonitor->smInfo, &tsMonitor.smInfo, sizeof(SMonSmInfo));
  memcpy(&pMonitor->qmInfo, &tsMonitor.qmInfo, sizeof(SMonQmInfo));
  memcpy(&pMonitor->bmInfo, &tsMonitor.bmInfo, sizeof(SMonBmInfo));
  memset(&tsMonitor.dmInfo, 0, sizeof(SMonDmInfo));
  memset(&tsMonitor.mmInfo, 0, sizeof(SMonMmInfo));
  memset(&tsMonitor.vmInfo, 0, sizeof(SMonVmInfo));
  memset(&tsMonitor.smInfo, 0, sizeof(SMonSmInfo));
  memset(&tsMonitor.qmInfo, 0, sizeof(SMonQmInfo));
  memset(&tsMonitor.bmInfo, 0, sizeof(SMonBmInfo));
wafwerar's avatar
wafwerar 已提交
159
  taosThreadMutexUnlock(&tsMonitor.lock);
S
monitor  
Shengliang Guan 已提交
160 161

  pMonitor->pJson = tjsonCreateObject();
162
  if (pMonitor->pJson == NULL || pMonitor->log.logs == NULL) {
S
monitor  
Shengliang Guan 已提交
163 164 165 166 167
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    monCleanupMonitorInfo(pMonitor);
    return NULL;
  }

S
monitor  
Shengliang Guan 已提交
168
  pMonitor->curTime = taosGetTimestampMs();
169
  pMonitor->lastTime = tsMonitor.lastTime;
S
monitor  
Shengliang Guan 已提交
170 171 172
  return pMonitor;
}

173 174
static void monGenBasicJson(SMonInfo *pMonitor) {
  SMonBasicInfo *pInfo = &pMonitor->dmInfo.basic;
S
monitor  
Shengliang Guan 已提交
175

S
monitor  
Shengliang Guan 已提交
176 177 178
  SJson *pJson = pMonitor->pJson;
  char   buf[40] = {0};
  taosFormatUtcTime(buf, sizeof(buf), pMonitor->curTime, TSDB_TIME_PRECISION_MILLI);
S
monitor  
Shengliang Guan 已提交
179

180
  tjsonAddStringToObject(pJson, "ts", buf);
S
monitor  
Shengliang Guan 已提交
181 182
  tjsonAddDoubleToObject(pJson, "dnode_id", pInfo->dnode_id);
  tjsonAddStringToObject(pJson, "dnode_ep", pInfo->dnode_ep);
S
Shengliang Guan 已提交
183 184 185
  snprintf(buf, sizeof(buf), "%" PRId64, pInfo->cluster_id);
  tjsonAddStringToObject(pJson, "cluster_id", buf);
  tjsonAddDoubleToObject(pJson, "protocol", pInfo->protocol);
S
monitor  
Shengliang Guan 已提交
186 187
}

188 189
static void monGenClusterJson(SMonInfo *pMonitor) {
  SMonClusterInfo *pInfo = &pMonitor->mmInfo.cluster;
190
  if (pMonitor->mmInfo.cluster.first_ep_dnode_id == 0) return;
191

S
monitor  
Shengliang Guan 已提交
192 193
  SJson *pJson = tjsonCreateObject();
  if (pJson == NULL) return;
S
monitor  
Shengliang Guan 已提交
194 195 196 197
  if (tjsonAddItemToObject(pMonitor->pJson, "cluster_info", pJson) != 0) {
    tjsonDelete(pJson);
    return;
  }
S
monitor  
Shengliang Guan 已提交
198

S
monitor  
Shengliang Guan 已提交
199 200 201 202 203
  tjsonAddStringToObject(pJson, "first_ep", pInfo->first_ep);
  tjsonAddDoubleToObject(pJson, "first_ep_dnode_id", pInfo->first_ep_dnode_id);
  tjsonAddStringToObject(pJson, "version", pInfo->version);
  tjsonAddDoubleToObject(pJson, "master_uptime", pInfo->master_uptime);
  tjsonAddDoubleToObject(pJson, "monitor_interval", pInfo->monitor_interval);
204 205 206
  tjsonAddDoubleToObject(pJson, "dbs_total", pInfo->dbs_total);
  tjsonAddDoubleToObject(pJson, "tbs_total", pInfo->tbs_total);
  tjsonAddDoubleToObject(pJson, "stbs_total", pInfo->stbs_total);
S
monitor  
Shengliang Guan 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220
  tjsonAddDoubleToObject(pJson, "vgroups_total", pInfo->vgroups_total);
  tjsonAddDoubleToObject(pJson, "vgroups_alive", pInfo->vgroups_alive);
  tjsonAddDoubleToObject(pJson, "vnodes_total", pInfo->vnodes_total);
  tjsonAddDoubleToObject(pJson, "vnodes_alive", pInfo->vnodes_alive);
  tjsonAddDoubleToObject(pJson, "connections_total", pInfo->connections_total);

  SJson *pDnodesJson = tjsonAddArrayToObject(pJson, "dnodes");
  if (pDnodesJson == NULL) return;

  for (int32_t i = 0; i < taosArrayGetSize(pInfo->dnodes); ++i) {
    SJson *pDnodeJson = tjsonCreateObject();
    if (pDnodeJson == NULL) continue;

    SMonDnodeDesc *pDnodeDesc = taosArrayGet(pInfo->dnodes, i);
S
monitor  
Shengliang Guan 已提交
221 222 223
    tjsonAddDoubleToObject(pDnodeJson, "dnode_id", pDnodeDesc->dnode_id);
    tjsonAddStringToObject(pDnodeJson, "dnode_ep", pDnodeDesc->dnode_ep);
    tjsonAddStringToObject(pDnodeJson, "status", pDnodeDesc->status);
S
monitor  
Shengliang Guan 已提交
224 225 226 227 228 229 230

    if (tjsonAddItemToArray(pDnodesJson, pDnodeJson) != 0) tjsonDelete(pDnodeJson);
  }

  SJson *pMnodesJson = tjsonAddArrayToObject(pJson, "mnodes");
  if (pMnodesJson == NULL) return;

S
Shengliang Guan 已提交
231
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->mnodes); ++i) {
S
monitor  
Shengliang Guan 已提交
232 233 234
    SJson *pMnodeJson = tjsonCreateObject();
    if (pMnodeJson == NULL) continue;

S
Shengliang Guan 已提交
235
    SMonMnodeDesc *pMnodeDesc = taosArrayGet(pInfo->mnodes, i);
S
monitor  
Shengliang Guan 已提交
236 237 238
    tjsonAddDoubleToObject(pMnodeJson, "mnode_id", pMnodeDesc->mnode_id);
    tjsonAddStringToObject(pMnodeJson, "mnode_ep", pMnodeDesc->mnode_ep);
    tjsonAddStringToObject(pMnodeJson, "role", pMnodeDesc->role);
S
monitor  
Shengliang Guan 已提交
239 240 241

    if (tjsonAddItemToArray(pMnodesJson, pMnodeJson) != 0) tjsonDelete(pMnodeJson);
  }
S
monitor  
Shengliang Guan 已提交
242 243
}

244 245
static void monGenVgroupJson(SMonInfo *pMonitor) {
  SMonVgroupInfo *pInfo = &pMonitor->mmInfo.vgroup;
246
  if (pMonitor->mmInfo.cluster.first_ep_dnode_id == 0) return;
247

S
monitor  
Shengliang Guan 已提交
248
  SJson *pJson = tjsonAddArrayToObject(pMonitor->pJson, "vgroup_infos");
S
monitor  
Shengliang Guan 已提交
249 250 251 252 253
  if (pJson == NULL) return;

  for (int32_t i = 0; i < taosArrayGetSize(pInfo->vgroups); ++i) {
    SJson *pVgroupJson = tjsonCreateObject();
    if (pVgroupJson == NULL) continue;
S
monitor  
Shengliang Guan 已提交
254 255 256 257
    if (tjsonAddItemToArray(pJson, pVgroupJson) != 0) {
      tjsonDelete(pVgroupJson);
      continue;
    }
S
monitor  
Shengliang Guan 已提交
258

S
monitor  
Shengliang Guan 已提交
259
    SMonVgroupDesc *pVgroupDesc = taosArrayGet(pInfo->vgroups, i);
S
monitor  
Shengliang Guan 已提交
260 261 262 263
    tjsonAddDoubleToObject(pVgroupJson, "vgroup_id", pVgroupDesc->vgroup_id);
    tjsonAddStringToObject(pVgroupJson, "database_name", pVgroupDesc->database_name);
    tjsonAddDoubleToObject(pVgroupJson, "tables_num", pVgroupDesc->tables_num);
    tjsonAddStringToObject(pVgroupJson, "status", pVgroupDesc->status);
S
monitor  
Shengliang Guan 已提交
264 265

    SJson *pVnodesJson = tjsonAddArrayToObject(pVgroupJson, "vnodes");
S
monitor  
Shengliang Guan 已提交
266
    if (pVnodesJson == NULL) continue;
S
monitor  
Shengliang Guan 已提交
267 268 269

    for (int32_t j = 0; j < TSDB_MAX_REPLICA; ++j) {
      SMonVnodeDesc *pVnodeDesc = &pVgroupDesc->vnodes[j];
S
monitor  
Shengliang Guan 已提交
270
      if (pVnodeDesc->dnode_id <= 0) continue;
S
monitor  
Shengliang Guan 已提交
271 272 273 274

      SJson *pVnodeJson = tjsonCreateObject();
      if (pVnodeJson == NULL) continue;

S
monitor  
Shengliang Guan 已提交
275 276
      tjsonAddDoubleToObject(pVnodeJson, "dnode_id", pVnodeDesc->dnode_id);
      tjsonAddStringToObject(pVnodeJson, "vnode_role", pVnodeDesc->vnode_role);
S
monitor  
Shengliang Guan 已提交
277 278 279 280

      if (tjsonAddItemToArray(pVnodesJson, pVnodeJson) != 0) tjsonDelete(pVnodeJson);
    }
  }
S
monitor  
Shengliang Guan 已提交
281 282
}

S
Shengliang Guan 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
static void monGenStbJson(SMonInfo *pMonitor) {
  SMonStbInfo *pInfo = &pMonitor->mmInfo.stb;
  if (pMonitor->mmInfo.cluster.first_ep_dnode_id == 0) return;

  SJson *pJson = tjsonAddArrayToObject(pMonitor->pJson, "stb_infos");
  if (pJson == NULL) return;

  for (int32_t i = 0; i < taosArrayGetSize(pInfo->stbs); ++i) {
    SJson *pStbJson = tjsonCreateObject();
    if (pStbJson == NULL) continue;
    if (tjsonAddItemToArray(pJson, pStbJson) != 0) {
      tjsonDelete(pStbJson);
      continue;
    }

    SMonStbDesc *pStbDesc = taosArrayGet(pInfo->stbs, i);
    tjsonAddStringToObject(pStbJson, "stb_name", pStbDesc->stb_name);
    tjsonAddStringToObject(pStbJson, "database_name", pStbDesc->database_name);
  }
}

304 305
static void monGenGrantJson(SMonInfo *pMonitor) {
  SMonGrantInfo *pInfo = &pMonitor->mmInfo.grant;
306
  if (pMonitor->mmInfo.cluster.first_ep_dnode_id == 0) return;
307

S
monitor  
Shengliang Guan 已提交
308 309
  SJson *pJson = tjsonCreateObject();
  if (pJson == NULL) return;
S
monitor  
Shengliang Guan 已提交
310 311 312 313
  if (tjsonAddItemToObject(pMonitor->pJson, "grant_info", pJson) != 0) {
    tjsonDelete(pJson);
    return;
  }
S
monitor  
Shengliang Guan 已提交
314

S
monitor  
Shengliang Guan 已提交
315 316 317
  tjsonAddDoubleToObject(pJson, "expire_time", pInfo->expire_time);
  tjsonAddDoubleToObject(pJson, "timeseries_used", pInfo->timeseries_used);
  tjsonAddDoubleToObject(pJson, "timeseries_total", pInfo->timeseries_total);
S
monitor  
Shengliang Guan 已提交
318 319
}

320 321 322 323 324
static void monGenDnodeJson(SMonInfo *pMonitor) {
  SMonDnodeInfo *pInfo = &pMonitor->dmInfo.dnode;
  SMonSysInfo   *pSys = &pMonitor->dmInfo.sys;
  SVnodesStat   *pStat = &pMonitor->vmInfo.vstat;

S
monitor  
Shengliang Guan 已提交
325 326
  SJson *pJson = tjsonCreateObject();
  if (pJson == NULL) return;
S
monitor  
Shengliang Guan 已提交
327 328 329 330
  if (tjsonAddItemToObject(pMonitor->pJson, "dnode_info", pJson) != 0) {
    tjsonDelete(pJson);
    return;
  }
S
monitor  
Shengliang Guan 已提交
331

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
  double interval = (pMonitor->curTime - pMonitor->lastTime) / 1000.0;
  if (pMonitor->curTime - pMonitor->lastTime == 0) {
    interval = 1;
  }

  double cpu_engine = 0;
  double mem_engine = 0;
  double net_in = 0;
  double net_out = 0;
  double io_read = 0;
  double io_write = 0;
  double io_read_disk = 0;
  double io_write_disk = 0;

  SMonSysInfo *sysArrays[6];
  sysArrays[0] = &pMonitor->dmInfo.sys;
  sysArrays[1] = &pMonitor->mmInfo.sys;
  sysArrays[2] = &pMonitor->vmInfo.sys;
  sysArrays[3] = &pMonitor->qmInfo.sys;
  sysArrays[4] = &pMonitor->smInfo.sys;
  sysArrays[5] = &pMonitor->bmInfo.sys;
  for (int32_t i = 0; i < 6; ++i) {
    cpu_engine += sysArrays[i]->cpu_engine;
    mem_engine += sysArrays[i]->mem_engine;
    net_in += sysArrays[i]->net_in;
    net_out += sysArrays[i]->net_out;
    io_read += sysArrays[i]->io_read;
    io_write += sysArrays[i]->io_write;
    io_read_disk += sysArrays[i]->io_read_disk;
    io_write_disk += sysArrays[i]->io_write_disk;
  }

  double req_select_rate = pStat->numOfSelectReqs / interval;
  double req_insert_rate = pStat->numOfInsertReqs / interval;
  double req_insert_batch_rate = pStat->numOfBatchInsertReqs / interval;
  double net_in_rate = net_in / interval;
  double net_out_rate = net_out / interval;
  double io_read_rate = io_read / interval;
  double io_write_rate = io_write / interval;
  double io_read_disk_rate = io_read_disk / interval;
  double io_write_disk_rate = io_write_disk / interval;
S
monitor  
Shengliang Guan 已提交
373

S
monitor  
Shengliang Guan 已提交
374
  tjsonAddDoubleToObject(pJson, "uptime", pInfo->uptime);
375 376 377 378 379 380 381 382 383
  tjsonAddDoubleToObject(pJson, "cpu_engine", cpu_engine);
  tjsonAddDoubleToObject(pJson, "cpu_system", pSys->cpu_system);
  tjsonAddDoubleToObject(pJson, "cpu_cores", pSys->cpu_cores);
  tjsonAddDoubleToObject(pJson, "mem_engine", mem_engine);
  tjsonAddDoubleToObject(pJson, "mem_system", pSys->mem_system);
  tjsonAddDoubleToObject(pJson, "mem_total", pSys->mem_total);
  tjsonAddDoubleToObject(pJson, "disk_engine", pSys->disk_engine);
  tjsonAddDoubleToObject(pJson, "disk_used", pSys->disk_used);
  tjsonAddDoubleToObject(pJson, "disk_total", pSys->disk_total);
S
monitor  
Shengliang Guan 已提交
384 385 386 387 388 389
  tjsonAddDoubleToObject(pJson, "net_in", net_in_rate);
  tjsonAddDoubleToObject(pJson, "net_out", net_out_rate);
  tjsonAddDoubleToObject(pJson, "io_read", io_read_rate);
  tjsonAddDoubleToObject(pJson, "io_write", io_write_rate);
  tjsonAddDoubleToObject(pJson, "io_read_disk", io_read_disk_rate);
  tjsonAddDoubleToObject(pJson, "io_write_disk", io_write_disk_rate);
390
  tjsonAddDoubleToObject(pJson, "req_select", pStat->numOfSelectReqs);
S
monitor  
Shengliang Guan 已提交
391
  tjsonAddDoubleToObject(pJson, "req_select_rate", req_select_rate);
392 393
  tjsonAddDoubleToObject(pJson, "req_insert", pStat->numOfInsertReqs);
  tjsonAddDoubleToObject(pJson, "req_insert_success", pStat->numOfInsertSuccessReqs);
S
monitor  
Shengliang Guan 已提交
394
  tjsonAddDoubleToObject(pJson, "req_insert_rate", req_insert_rate);
395 396
  tjsonAddDoubleToObject(pJson, "req_insert_batch", pStat->numOfBatchInsertReqs);
  tjsonAddDoubleToObject(pJson, "req_insert_batch_success", pStat->numOfBatchInsertSuccessReqs);
S
monitor  
Shengliang Guan 已提交
397
  tjsonAddDoubleToObject(pJson, "req_insert_batch_rate", req_insert_batch_rate);
398 399 400
  tjsonAddDoubleToObject(pJson, "errors", pStat->errors);
  tjsonAddDoubleToObject(pJson, "vnodes_num", pStat->totalVnodes);
  tjsonAddDoubleToObject(pJson, "masters", pStat->masterNum);
S
monitor  
Shengliang Guan 已提交
401
  tjsonAddDoubleToObject(pJson, "has_mnode", pInfo->has_mnode);
402 403
  tjsonAddDoubleToObject(pJson, "has_qnode", pInfo->has_qnode);
  tjsonAddDoubleToObject(pJson, "has_snode", pInfo->has_snode);
S
monitor  
Shengliang Guan 已提交
404 405
}

406 407 408 409 410
static void monGenDiskJson(SMonInfo *pMonitor) {
  SMonDiskInfo *pInfo = &pMonitor->vmInfo.tfs;
  SMonDiskDesc *pLogDesc = &pMonitor->dmInfo.dnode.logdir;
  SMonDiskDesc *pTempDesc = &pMonitor->dmInfo.dnode.tempdir;

S
monitor  
Shengliang Guan 已提交
411 412
  SJson *pJson = tjsonCreateObject();
  if (pJson == NULL) return;
S
monitor  
Shengliang Guan 已提交
413
  if (tjsonAddItemToObject(pMonitor->pJson, "disk_infos", pJson) != 0) {
S
monitor  
Shengliang Guan 已提交
414 415 416
    tjsonDelete(pJson);
    return;
  }
S
monitor  
Shengliang Guan 已提交
417

S
monitor  
Shengliang Guan 已提交
418
  SJson *pDatadirsJson = tjsonAddArrayToObject(pJson, "datadir");
S
monitor  
Shengliang Guan 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
  if (pDatadirsJson == NULL) return;

  for (int32_t i = 0; i < taosArrayGetSize(pInfo->datadirs); ++i) {
    SJson *pDatadirJson = tjsonCreateObject();
    if (pDatadirJson == NULL) continue;

    SMonDiskDesc *pDatadirDesc = taosArrayGet(pInfo->datadirs, i);
    if (tjsonAddStringToObject(pDatadirJson, "name", pDatadirDesc->name) != 0) tjsonDelete(pDatadirJson);
    if (tjsonAddDoubleToObject(pDatadirJson, "level", pDatadirDesc->level) != 0) tjsonDelete(pDatadirJson);
    if (tjsonAddDoubleToObject(pDatadirJson, "avail", pDatadirDesc->size.avail) != 0) tjsonDelete(pDatadirJson);
    if (tjsonAddDoubleToObject(pDatadirJson, "used", pDatadirDesc->size.used) != 0) tjsonDelete(pDatadirJson);
    if (tjsonAddDoubleToObject(pDatadirJson, "total", pDatadirDesc->size.total) != 0) tjsonDelete(pDatadirJson);

    if (tjsonAddItemToArray(pDatadirsJson, pDatadirJson) != 0) tjsonDelete(pDatadirJson);
  }

  SJson *pLogdirJson = tjsonCreateObject();
  if (pLogdirJson == NULL) return;
  if (tjsonAddItemToObject(pJson, "logdir", pLogdirJson) != 0) return;
438 439 440 441
  tjsonAddStringToObject(pLogdirJson, "name", pLogDesc->name);
  tjsonAddDoubleToObject(pLogdirJson, "avail", pLogDesc->size.avail);
  tjsonAddDoubleToObject(pLogdirJson, "used", pLogDesc->size.used);
  tjsonAddDoubleToObject(pLogdirJson, "total", pLogDesc->size.total);
S
monitor  
Shengliang Guan 已提交
442 443 444 445

  SJson *pTempdirJson = tjsonCreateObject();
  if (pTempdirJson == NULL) return;
  if (tjsonAddItemToObject(pJson, "tempdir", pTempdirJson) != 0) return;
446 447 448 449
  tjsonAddStringToObject(pTempdirJson, "name", pTempDesc->name);
  tjsonAddDoubleToObject(pTempdirJson, "avail", pTempDesc->size.avail);
  tjsonAddDoubleToObject(pTempdirJson, "used", pTempDesc->size.used);
  tjsonAddDoubleToObject(pTempdirJson, "total", pTempDesc->size.total);
S
monitor  
Shengliang Guan 已提交
450
}
S
monitor  
Shengliang Guan 已提交
451

452
static const char *monLogLevelStr(ELogLevel level) {
S
Shengliang Guan 已提交
453 454 455 456
  if (level == DEBUG_ERROR) {
    return "error";
  } else {
    return "info";
S
Shengliang Guan 已提交
457 458 459
  }
}

460
static void monGenLogJson(SMonInfo *pMonitor) {
S
monitor  
Shengliang Guan 已提交
461 462 463 464 465 466 467 468 469 470
  SJson *pJson = tjsonCreateObject();
  if (pJson == NULL) return;
  if (tjsonAddItemToObject(pMonitor->pJson, "log_infos", pJson) != 0) {
    tjsonDelete(pJson);
    return;
  }

  SJson *pLogsJson = tjsonAddArrayToObject(pJson, "logs");
  if (pLogsJson == NULL) return;

471
  SMonLogs *logs[6];
472 473 474 475 476 477
  logs[0] = &pMonitor->log;
  logs[1] = &pMonitor->mmInfo.log;
  logs[2] = &pMonitor->vmInfo.log;
  logs[3] = &pMonitor->smInfo.log;
  logs[4] = &pMonitor->qmInfo.log;
  logs[5] = &pMonitor->bmInfo.log;
478 479 480 481 482 483 484

  int32_t numOfErrorLogs = 0;
  int32_t numOfInfoLogs = 0;
  int32_t numOfDebugLogs = 0;
  int32_t numOfTraceLogs = 0;

  for (int32_t j = 0; j < 6; j++) {
485 486 487 488 489
    SMonLogs *pLog = logs[j];
    numOfErrorLogs += pLog->numOfErrorLogs;
    numOfInfoLogs += pLog->numOfInfoLogs;
    numOfDebugLogs += pLog->numOfDebugLogs;
    numOfTraceLogs += pLog->numOfTraceLogs;
490

491
    for (int32_t i = 0; i < taosArrayGetSize(pLog->logs); ++i) {
492 493
      SJson *pLogJson = tjsonCreateObject();
      if (pLogJson == NULL) continue;
S
monitor  
Shengliang Guan 已提交
494

495
      SMonLogItem *pLogItem = taosArrayGet(pLog->logs, i);
S
monitor  
Shengliang Guan 已提交
496

497 498
      char buf[40] = {0};
      taosFormatUtcTime(buf, sizeof(buf), pLogItem->ts, TSDB_TIME_PRECISION_MILLI);
S
monitor  
Shengliang Guan 已提交
499

500 501 502
      tjsonAddStringToObject(pLogJson, "ts", buf);
      tjsonAddStringToObject(pLogJson, "level", monLogLevelStr(pLogItem->level));
      tjsonAddStringToObject(pLogJson, "content", pLogItem->content);
S
monitor  
Shengliang Guan 已提交
503

504 505
      if (tjsonAddItemToArray(pLogsJson, pLogJson) != 0) tjsonDelete(pLogJson);
    }
S
monitor  
Shengliang Guan 已提交
506 507 508 509 510 511 512 513
  }

  SJson *pSummaryJson = tjsonAddArrayToObject(pJson, "summary");
  if (pSummaryJson == NULL) return;

  SJson *pLogError = tjsonCreateObject();
  if (pLogError == NULL) return;
  tjsonAddStringToObject(pLogError, "level", "error");
514
  tjsonAddDoubleToObject(pLogError, "total", numOfErrorLogs);
S
monitor  
Shengliang Guan 已提交
515 516 517 518 519
  if (tjsonAddItemToArray(pSummaryJson, pLogError) != 0) tjsonDelete(pLogError);

  SJson *pLogInfo = tjsonCreateObject();
  if (pLogInfo == NULL) return;
  tjsonAddStringToObject(pLogInfo, "level", "info");
520
  tjsonAddDoubleToObject(pLogInfo, "total", numOfInfoLogs);
S
monitor  
Shengliang Guan 已提交
521 522 523 524 525
  if (tjsonAddItemToArray(pSummaryJson, pLogInfo) != 0) tjsonDelete(pLogInfo);

  SJson *pLogDebug = tjsonCreateObject();
  if (pLogDebug == NULL) return;
  tjsonAddStringToObject(pLogDebug, "level", "debug");
526
  tjsonAddDoubleToObject(pLogDebug, "total", numOfDebugLogs);
S
monitor  
Shengliang Guan 已提交
527 528 529 530 531
  if (tjsonAddItemToArray(pSummaryJson, pLogDebug) != 0) tjsonDelete(pLogDebug);

  SJson *pLogTrace = tjsonCreateObject();
  if (pLogTrace == NULL) return;
  tjsonAddStringToObject(pLogTrace, "level", "trace");
532
  tjsonAddDoubleToObject(pLogTrace, "total", numOfTraceLogs);
S
monitor  
Shengliang Guan 已提交
533 534 535
  if (tjsonAddItemToArray(pSummaryJson, pLogTrace) != 0) tjsonDelete(pLogTrace);
}

536 537 538 539 540 541 542
void monSendReport() {
  SMonInfo *pMonitor = monCreateMonitorInfo();
  if (pMonitor == NULL) return;

  monGenBasicJson(pMonitor);
  monGenClusterJson(pMonitor);
  monGenVgroupJson(pMonitor);
S
Shengliang Guan 已提交
543
  monGenStbJson(pMonitor);
544 545 546 547
  monGenGrantJson(pMonitor);
  monGenDnodeJson(pMonitor);
  monGenDiskJson(pMonitor);
  monGenLogJson(pMonitor);
S
monitor  
Shengliang Guan 已提交
548 549

  char *pCont = tjsonToString(pMonitor->pJson);
550
  // uDebugL("report cont:%s\n", pCont);
H
Hongze Cheng 已提交
551
  if (pCont != NULL) {
552
    EHttpCompFlag flag = tsMonitor.cfg.comp ? HTTP_GZIP : HTTP_FLAT;
553
    if (taosSendHttpReport(tsMonitor.cfg.server, tsMonitor.cfg.port, pCont, strlen(pCont), flag) != 0) {
554
      uError("failed to send monitor msg");
555
    }
wafwerar's avatar
wafwerar 已提交
556
    taosMemoryFree(pCont);
S
monitor  
Shengliang Guan 已提交
557
  }
558 559

  monCleanupMonitorInfo(pMonitor);
S
monitor  
Shengliang Guan 已提交
560
}