monMain.c 20.8 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
static SMonitor tsMonitor = {0};
D
dapan1121 已提交
23
static char* tsMonUri = "/report";
S
monitor  
Shengliang Guan 已提交
24

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

38 39
int32_t monGetLogs(SMonLogs *logs) {
  taosThreadMutexLock(&tsMonitor.lock);
H
Haojun Liao 已提交
40
  logs->logs = taosArrayDup(tsMonitor.logs, NULL);
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 99
  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 已提交
100
int32_t monInit(const SMonCfg *pCfg) {
S
monitor  
Shengliang Guan 已提交
101
  tsMonitor.logs = taosArrayInit(16, sizeof(SMonLogItem));
S
monitor  
Shengliang Guan 已提交
102 103 104 105 106
  if (tsMonitor.logs == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

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

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

126 127
static void monCleanupMonitorInfo(SMonInfo *pMonitor) {
  tsMonitor.lastTime = pMonitor->curTime;
128
  taosArrayDestroy(pMonitor->log.logs);
129 130 131 132 133 134 135 136 137 138
  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 已提交
139
  SMonInfo *pMonitor = taosMemoryCalloc(1, sizeof(SMonInfo));
S
monitor  
Shengliang Guan 已提交
140 141 142 143
  if (pMonitor == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
S
monitor  
Shengliang Guan 已提交
144

145
  monGetLogs(&pMonitor->log);
146

wafwerar's avatar
wafwerar 已提交
147
  taosThreadMutexLock(&tsMonitor.lock);
148 149 150 151 152 153 154 155 156 157 158 159
  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 已提交
160
  taosThreadMutexUnlock(&tsMonitor.lock);
S
monitor  
Shengliang Guan 已提交
161 162

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

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

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

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

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

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

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

S
monitor  
Shengliang Guan 已提交
200 201 202 203 204
  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);
205 206 207
  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 已提交
208 209 210 211 212
  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);
213 214
  tjsonAddDoubleToObject(pJson, "topics_total", pInfo->topics_toal);
  tjsonAddDoubleToObject(pJson, "streams_total", pInfo->streams_total);
S
monitor  
Shengliang Guan 已提交
215 216 217 218 219 220 221 222 223

  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 已提交
224 225 226
    tjsonAddDoubleToObject(pDnodeJson, "dnode_id", pDnodeDesc->dnode_id);
    tjsonAddStringToObject(pDnodeJson, "dnode_ep", pDnodeDesc->dnode_ep);
    tjsonAddStringToObject(pDnodeJson, "status", pDnodeDesc->status);
S
monitor  
Shengliang Guan 已提交
227 228 229 230 231 232 233

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

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

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

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

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

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

S
monitor  
Shengliang Guan 已提交
251
  SJson *pJson = tjsonAddArrayToObject(pMonitor->pJson, "vgroup_infos");
S
monitor  
Shengliang Guan 已提交
252 253 254 255 256
  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 已提交
257 258 259 260
    if (tjsonAddItemToArray(pJson, pVgroupJson) != 0) {
      tjsonDelete(pVgroupJson);
      continue;
    }
S
monitor  
Shengliang Guan 已提交
261

S
monitor  
Shengliang Guan 已提交
262
    SMonVgroupDesc *pVgroupDesc = taosArrayGet(pInfo->vgroups, i);
S
monitor  
Shengliang Guan 已提交
263 264 265 266
    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 已提交
267 268

    SJson *pVnodesJson = tjsonAddArrayToObject(pVgroupJson, "vnodes");
S
monitor  
Shengliang Guan 已提交
269
    if (pVnodesJson == NULL) continue;
S
monitor  
Shengliang Guan 已提交
270 271 272

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

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

S
monitor  
Shengliang Guan 已提交
278 279
      tjsonAddDoubleToObject(pVnodeJson, "dnode_id", pVnodeDesc->dnode_id);
      tjsonAddStringToObject(pVnodeJson, "vnode_role", pVnodeDesc->vnode_role);
S
monitor  
Shengliang Guan 已提交
280 281 282 283

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

S
Shengliang Guan 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
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);
  }
}

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

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

S
monitor  
Shengliang Guan 已提交
318 319 320
  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 已提交
321 322
}

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

S
monitor  
Shengliang Guan 已提交
328 329
  SJson *pJson = tjsonCreateObject();
  if (pJson == NULL) return;
S
monitor  
Shengliang Guan 已提交
330 331 332 333
  if (tjsonAddItemToObject(pMonitor->pJson, "dnode_info", pJson) != 0) {
    tjsonDelete(pJson);
    return;
  }
S
monitor  
Shengliang Guan 已提交
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 373 374 375
  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 已提交
376

S
monitor  
Shengliang Guan 已提交
377
  tjsonAddDoubleToObject(pJson, "uptime", pInfo->uptime);
378 379 380 381 382 383 384 385 386
  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 已提交
387 388 389 390 391 392
  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);
393
  tjsonAddDoubleToObject(pJson, "req_select", pStat->numOfSelectReqs);
S
monitor  
Shengliang Guan 已提交
394
  tjsonAddDoubleToObject(pJson, "req_select_rate", req_select_rate);
395 396
  tjsonAddDoubleToObject(pJson, "req_insert", pStat->numOfInsertReqs);
  tjsonAddDoubleToObject(pJson, "req_insert_success", pStat->numOfInsertSuccessReqs);
S
monitor  
Shengliang Guan 已提交
397
  tjsonAddDoubleToObject(pJson, "req_insert_rate", req_insert_rate);
398 399
  tjsonAddDoubleToObject(pJson, "req_insert_batch", pStat->numOfBatchInsertReqs);
  tjsonAddDoubleToObject(pJson, "req_insert_batch_success", pStat->numOfBatchInsertSuccessReqs);
S
monitor  
Shengliang Guan 已提交
400
  tjsonAddDoubleToObject(pJson, "req_insert_batch_rate", req_insert_batch_rate);
401 402 403
  tjsonAddDoubleToObject(pJson, "errors", pStat->errors);
  tjsonAddDoubleToObject(pJson, "vnodes_num", pStat->totalVnodes);
  tjsonAddDoubleToObject(pJson, "masters", pStat->masterNum);
S
monitor  
Shengliang Guan 已提交
404
  tjsonAddDoubleToObject(pJson, "has_mnode", pInfo->has_mnode);
405 406
  tjsonAddDoubleToObject(pJson, "has_qnode", pInfo->has_qnode);
  tjsonAddDoubleToObject(pJson, "has_snode", pInfo->has_snode);
S
monitor  
Shengliang Guan 已提交
407 408
}

409 410 411 412 413
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 已提交
414 415
  SJson *pJson = tjsonCreateObject();
  if (pJson == NULL) return;
S
monitor  
Shengliang Guan 已提交
416
  if (tjsonAddItemToObject(pMonitor->pJson, "disk_infos", pJson) != 0) {
S
monitor  
Shengliang Guan 已提交
417 418 419
    tjsonDelete(pJson);
    return;
  }
S
monitor  
Shengliang Guan 已提交
420

S
monitor  
Shengliang Guan 已提交
421
  SJson *pDatadirsJson = tjsonAddArrayToObject(pJson, "datadir");
S
monitor  
Shengliang Guan 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
  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;
441 442 443 444
  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 已提交
445 446 447 448

  SJson *pTempdirJson = tjsonCreateObject();
  if (pTempdirJson == NULL) return;
  if (tjsonAddItemToObject(pJson, "tempdir", pTempdirJson) != 0) return;
449 450 451 452
  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 已提交
453
}
S
monitor  
Shengliang Guan 已提交
454

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

463
static void monGenLogJson(SMonInfo *pMonitor) {
S
monitor  
Shengliang Guan 已提交
464 465 466 467 468 469 470 471 472 473
  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;

474
  SMonLogs *logs[6];
475 476 477 478 479 480
  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;
481 482 483 484 485 486 487

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

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

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

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

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

503 504 505
      tjsonAddStringToObject(pLogJson, "ts", buf);
      tjsonAddStringToObject(pLogJson, "level", monLogLevelStr(pLogItem->level));
      tjsonAddStringToObject(pLogJson, "content", pLogItem->content);
S
monitor  
Shengliang Guan 已提交
506

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

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

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

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

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

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

539 540 541 542 543 544 545
void monSendReport() {
  SMonInfo *pMonitor = monCreateMonitorInfo();
  if (pMonitor == NULL) return;

  monGenBasicJson(pMonitor);
  monGenClusterJson(pMonitor);
  monGenVgroupJson(pMonitor);
S
Shengliang Guan 已提交
546
  monGenStbJson(pMonitor);
547 548 549 550
  monGenGrantJson(pMonitor);
  monGenDnodeJson(pMonitor);
  monGenDiskJson(pMonitor);
  monGenLogJson(pMonitor);
S
monitor  
Shengliang Guan 已提交
551 552

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

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