monMain.c 19.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 39 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
int32_t monGetLogs(SMonLogs *logs) {
  taosThreadMutexLock(&tsMonitor.lock);
  logs->logs = taosArrayDup(tsMonitor.logs);
  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 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  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);
  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 已提交
218 219 220
    tjsonAddDoubleToObject(pDnodeJson, "dnode_id", pDnodeDesc->dnode_id);
    tjsonAddStringToObject(pDnodeJson, "dnode_ep", pDnodeDesc->dnode_ep);
    tjsonAddStringToObject(pDnodeJson, "status", pDnodeDesc->status);
S
monitor  
Shengliang Guan 已提交
221 222 223 224 225 226 227

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

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

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

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

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

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

S
monitor  
Shengliang Guan 已提交
245
  SJson *pJson = tjsonAddArrayToObject(pMonitor->pJson, "vgroup_infos");
S
monitor  
Shengliang Guan 已提交
246 247 248 249 250
  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 已提交
251 252 253 254
    if (tjsonAddItemToArray(pJson, pVgroupJson) != 0) {
      tjsonDelete(pVgroupJson);
      continue;
    }
S
monitor  
Shengliang Guan 已提交
255

S
monitor  
Shengliang Guan 已提交
256
    SMonVgroupDesc *pVgroupDesc = taosArrayGet(pInfo->vgroups, i);
S
monitor  
Shengliang Guan 已提交
257 258 259 260
    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 已提交
261 262

    SJson *pVnodesJson = tjsonAddArrayToObject(pVgroupJson, "vnodes");
S
monitor  
Shengliang Guan 已提交
263
    if (pVnodesJson == NULL) continue;
S
monitor  
Shengliang Guan 已提交
264 265 266

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

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

S
monitor  
Shengliang Guan 已提交
272 273
      tjsonAddDoubleToObject(pVnodeJson, "dnode_id", pVnodeDesc->dnode_id);
      tjsonAddStringToObject(pVnodeJson, "vnode_role", pVnodeDesc->vnode_role);
S
monitor  
Shengliang Guan 已提交
274 275 276 277

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

280 281
static void monGenGrantJson(SMonInfo *pMonitor) {
  SMonGrantInfo *pInfo = &pMonitor->mmInfo.grant;
282
  if (pMonitor->mmInfo.cluster.first_ep_dnode_id == 0) return;
283

S
monitor  
Shengliang Guan 已提交
284 285
  SJson *pJson = tjsonCreateObject();
  if (pJson == NULL) return;
S
monitor  
Shengliang Guan 已提交
286 287 288 289
  if (tjsonAddItemToObject(pMonitor->pJson, "grant_info", pJson) != 0) {
    tjsonDelete(pJson);
    return;
  }
S
monitor  
Shengliang Guan 已提交
290

S
monitor  
Shengliang Guan 已提交
291 292 293
  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 已提交
294 295
}

296 297 298 299 300
static void monGenDnodeJson(SMonInfo *pMonitor) {
  SMonDnodeInfo *pInfo = &pMonitor->dmInfo.dnode;
  SMonSysInfo   *pSys = &pMonitor->dmInfo.sys;
  SVnodesStat   *pStat = &pMonitor->vmInfo.vstat;

S
monitor  
Shengliang Guan 已提交
301 302
  SJson *pJson = tjsonCreateObject();
  if (pJson == NULL) return;
S
monitor  
Shengliang Guan 已提交
303 304 305 306
  if (tjsonAddItemToObject(pMonitor->pJson, "dnode_info", pJson) != 0) {
    tjsonDelete(pJson);
    return;
  }
S
monitor  
Shengliang Guan 已提交
307

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
  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 已提交
349

S
monitor  
Shengliang Guan 已提交
350
  tjsonAddDoubleToObject(pJson, "uptime", pInfo->uptime);
351 352 353 354 355 356 357 358 359
  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 已提交
360 361 362 363 364 365
  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);
366
  tjsonAddDoubleToObject(pJson, "req_select", pStat->numOfSelectReqs);
S
monitor  
Shengliang Guan 已提交
367
  tjsonAddDoubleToObject(pJson, "req_select_rate", req_select_rate);
368 369
  tjsonAddDoubleToObject(pJson, "req_insert", pStat->numOfInsertReqs);
  tjsonAddDoubleToObject(pJson, "req_insert_success", pStat->numOfInsertSuccessReqs);
S
monitor  
Shengliang Guan 已提交
370
  tjsonAddDoubleToObject(pJson, "req_insert_rate", req_insert_rate);
371 372
  tjsonAddDoubleToObject(pJson, "req_insert_batch", pStat->numOfBatchInsertReqs);
  tjsonAddDoubleToObject(pJson, "req_insert_batch_success", pStat->numOfBatchInsertSuccessReqs);
S
monitor  
Shengliang Guan 已提交
373
  tjsonAddDoubleToObject(pJson, "req_insert_batch_rate", req_insert_batch_rate);
374 375 376
  tjsonAddDoubleToObject(pJson, "errors", pStat->errors);
  tjsonAddDoubleToObject(pJson, "vnodes_num", pStat->totalVnodes);
  tjsonAddDoubleToObject(pJson, "masters", pStat->masterNum);
S
monitor  
Shengliang Guan 已提交
377
  tjsonAddDoubleToObject(pJson, "has_mnode", pInfo->has_mnode);
S
monitor  
Shengliang Guan 已提交
378 379
}

380 381 382 383 384
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 已提交
385 386
  SJson *pJson = tjsonCreateObject();
  if (pJson == NULL) return;
S
monitor  
Shengliang Guan 已提交
387
  if (tjsonAddItemToObject(pMonitor->pJson, "disk_infos", pJson) != 0) {
S
monitor  
Shengliang Guan 已提交
388 389 390
    tjsonDelete(pJson);
    return;
  }
S
monitor  
Shengliang Guan 已提交
391

S
monitor  
Shengliang Guan 已提交
392
  SJson *pDatadirsJson = tjsonAddArrayToObject(pJson, "datadir");
S
monitor  
Shengliang Guan 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
  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;
412 413 414 415
  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 已提交
416 417 418 419

  SJson *pTempdirJson = tjsonCreateObject();
  if (pTempdirJson == NULL) return;
  if (tjsonAddItemToObject(pJson, "tempdir", pTempdirJson) != 0) return;
420 421 422 423
  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 已提交
424
}
S
monitor  
Shengliang Guan 已提交
425

426
static const char *monLogLevelStr(ELogLevel level) {
S
Shengliang Guan 已提交
427
  switch (level) {
428
    case DEBUG_ERROR:
S
Shengliang Guan 已提交
429
      return "error";
430
    case DEBUG_INFO:
S
Shengliang Guan 已提交
431
      return "info";
432
    case DEBUG_DEBUG:
S
Shengliang Guan 已提交
433
      return "debug";
434
    case DEBUG_TRACE:
S
Shengliang Guan 已提交
435 436 437 438 439 440
      return "trace";
    default:
      return "undefine";
  }
}

441
static void monGenLogJson(SMonInfo *pMonitor) {
S
monitor  
Shengliang Guan 已提交
442 443 444 445 446 447 448 449 450 451
  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;

452
  SMonLogs *logs[6];
453 454 455 456 457 458
  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;
459 460 461 462 463 464 465

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

  for (int32_t j = 0; j < 6; j++) {
466 467 468 469 470
    SMonLogs *pLog = logs[j];
    numOfErrorLogs += pLog->numOfErrorLogs;
    numOfInfoLogs += pLog->numOfInfoLogs;
    numOfDebugLogs += pLog->numOfDebugLogs;
    numOfTraceLogs += pLog->numOfTraceLogs;
471

472
    for (int32_t i = 0; i < taosArrayGetSize(pLog->logs); ++i) {
473 474
      SJson *pLogJson = tjsonCreateObject();
      if (pLogJson == NULL) continue;
S
monitor  
Shengliang Guan 已提交
475

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

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

481 482 483
      tjsonAddStringToObject(pLogJson, "ts", buf);
      tjsonAddStringToObject(pLogJson, "level", monLogLevelStr(pLogItem->level));
      tjsonAddStringToObject(pLogJson, "content", pLogItem->content);
S
monitor  
Shengliang Guan 已提交
484

485 486
      if (tjsonAddItemToArray(pLogsJson, pLogJson) != 0) tjsonDelete(pLogJson);
    }
S
monitor  
Shengliang Guan 已提交
487 488 489 490 491 492 493 494
  }

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

  SJson *pLogError = tjsonCreateObject();
  if (pLogError == NULL) return;
  tjsonAddStringToObject(pLogError, "level", "error");
495
  tjsonAddDoubleToObject(pLogError, "total", numOfErrorLogs);
S
monitor  
Shengliang Guan 已提交
496 497 498 499 500
  if (tjsonAddItemToArray(pSummaryJson, pLogError) != 0) tjsonDelete(pLogError);

  SJson *pLogInfo = tjsonCreateObject();
  if (pLogInfo == NULL) return;
  tjsonAddStringToObject(pLogInfo, "level", "info");
501
  tjsonAddDoubleToObject(pLogInfo, "total", numOfInfoLogs);
S
monitor  
Shengliang Guan 已提交
502 503 504 505 506
  if (tjsonAddItemToArray(pSummaryJson, pLogInfo) != 0) tjsonDelete(pLogInfo);

  SJson *pLogDebug = tjsonCreateObject();
  if (pLogDebug == NULL) return;
  tjsonAddStringToObject(pLogDebug, "level", "debug");
507
  tjsonAddDoubleToObject(pLogDebug, "total", numOfDebugLogs);
S
monitor  
Shengliang Guan 已提交
508 509 510 511 512
  if (tjsonAddItemToArray(pSummaryJson, pLogDebug) != 0) tjsonDelete(pLogDebug);

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

517 518 519 520 521 522 523 524 525 526 527
void monSendReport() {
  SMonInfo *pMonitor = monCreateMonitorInfo();
  if (pMonitor == NULL) return;

  monGenBasicJson(pMonitor);
  monGenClusterJson(pMonitor);
  monGenVgroupJson(pMonitor);
  monGenGrantJson(pMonitor);
  monGenDnodeJson(pMonitor);
  monGenDiskJson(pMonitor);
  monGenLogJson(pMonitor);
S
monitor  
Shengliang Guan 已提交
528 529 530

  char *pCont = tjsonToString(pMonitor->pJson);
  if (pCont != NULL) {
531 532
    EHttpCompFlag flag = tsMonitor.cfg.comp ? HTTP_GZIP : HTTP_FLAT;
    taosSendHttpReport(tsMonitor.cfg.server, tsMonitor.cfg.port, pCont, strlen(pCont), flag);
wafwerar's avatar
wafwerar 已提交
533
    taosMemoryFree(pCont);
S
monitor  
Shengliang Guan 已提交
534
  }
535 536

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