monitor.c 13.3 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 20
#include "taoserror.h"
#include "thttp.h"
#include "tlog.h"
21
#include "ttime.h"
S
Shengliang Guan 已提交
22

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

int32_t monInit(const SMonCfg *pCfg) {
S
monitor  
Shengliang Guan 已提交
26
  tsMonitor.logs = taosArrayInit(16, sizeof(SMonLogItem));
S
monitor  
Shengliang Guan 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  if (tsMonitor.logs == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  tsMonitor.maxLogs = pCfg->maxLogs;
  tsMonitor.server = pCfg->server;
  tsMonitor.port = pCfg->port;
  taosInitRWLatch(&tsMonitor.lock);
  return 0;
}

void monCleanup() {
  taosArrayDestroy(tsMonitor.logs);
  tsMonitor.logs = NULL;
}

void monAddLogItem(SMonLogItem *pItem) {
  taosWLockLatch(&tsMonitor.lock);
  int32_t size = taosArrayGetSize(tsMonitor.logs);
S
monitor  
Shengliang Guan 已提交
47
  if (size >= tsMonitor.maxLogs) {
S
monitor  
Shengliang Guan 已提交
48 49 50 51 52 53 54 55 56
    uInfo("too many logs for monitor");
  } else {
    taosArrayPush(tsMonitor.logs, pItem);
  }
  taosWUnLockLatch(&tsMonitor.lock);
}

SMonInfo *monCreateMonitorInfo() {
  SMonInfo *pMonitor = calloc(1, sizeof(SMonInfo));
S
monitor  
Shengliang Guan 已提交
57 58 59 60
  if (pMonitor == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
S
monitor  
Shengliang Guan 已提交
61 62

  taosWLockLatch(&tsMonitor.lock);
63
  pMonitor->logs = taosArrayDup(tsMonitor.logs);
S
monitor  
Shengliang Guan 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  taosArrayClear(tsMonitor.logs);
  taosWUnLockLatch(&tsMonitor.lock);

  pMonitor->pJson = tjsonCreateObject();
  if (pMonitor->pJson == NULL || pMonitor->logs == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    monCleanupMonitorInfo(pMonitor);
    return NULL;
  }

  return pMonitor;
}

void monCleanupMonitorInfo(SMonInfo *pMonitor) {
  taosArrayDestroy(pMonitor->logs);
  tjsonDelete(pMonitor->pJson);
  free(pMonitor);
}

void monSetBasicInfo(SMonInfo *pMonitor, SMonBasicInfo *pInfo) {
S
monitor  
Shengliang Guan 已提交
84
  SJson  *pJson = pMonitor->pJson;
85 86 87
  int64_t ms = taosGetTimestampMs();
  char    buf[40] = {0};
  taosFormatUtcTime(buf, sizeof(buf), ms, TSDB_TIME_PRECISION_MILLI);
S
monitor  
Shengliang Guan 已提交
88

89
  tjsonAddStringToObject(pJson, "ts", buf);
S
monitor  
Shengliang Guan 已提交
90 91
  tjsonAddDoubleToObject(pJson, "dnode_id", pInfo->dnode_id);
  tjsonAddStringToObject(pJson, "dnode_ep", pInfo->dnode_ep);
S
monitor  
Shengliang Guan 已提交
92 93
}

S
monitor  
Shengliang Guan 已提交
94
void monSetClusterInfo(SMonInfo *pMonitor, SMonClusterInfo *pInfo) {
S
monitor  
Shengliang Guan 已提交
95 96
  SJson *pJson = tjsonCreateObject();
  if (pJson == NULL) return;
S
monitor  
Shengliang Guan 已提交
97 98 99 100
  if (tjsonAddItemToObject(pMonitor->pJson, "cluster_info", pJson) != 0) {
    tjsonDelete(pJson);
    return;
  }
S
monitor  
Shengliang Guan 已提交
101

S
monitor  
Shengliang Guan 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  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 已提交
121 122 123
    tjsonAddDoubleToObject(pDnodeJson, "dnode_id", pDnodeDesc->dnode_id);
    tjsonAddStringToObject(pDnodeJson, "dnode_ep", pDnodeDesc->dnode_ep);
    tjsonAddStringToObject(pDnodeJson, "status", pDnodeDesc->status);
S
monitor  
Shengliang Guan 已提交
124 125 126 127 128 129 130

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

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

S
Shengliang Guan 已提交
131
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->mnodes); ++i) {
S
monitor  
Shengliang Guan 已提交
132 133 134
    SJson *pMnodeJson = tjsonCreateObject();
    if (pMnodeJson == NULL) continue;

S
Shengliang Guan 已提交
135
    SMonMnodeDesc *pMnodeDesc = taosArrayGet(pInfo->mnodes, i);
S
monitor  
Shengliang Guan 已提交
136 137 138
    tjsonAddDoubleToObject(pMnodeJson, "mnode_id", pMnodeDesc->mnode_id);
    tjsonAddStringToObject(pMnodeJson, "mnode_ep", pMnodeDesc->mnode_ep);
    tjsonAddStringToObject(pMnodeJson, "role", pMnodeDesc->role);
S
monitor  
Shengliang Guan 已提交
139 140 141

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

void monSetVgroupInfo(SMonInfo *pMonitor, SMonVgroupInfo *pInfo) {
S
monitor  
Shengliang Guan 已提交
145
  SJson *pJson = tjsonAddArrayToObject(pMonitor->pJson, "vgroup_infos");
S
monitor  
Shengliang Guan 已提交
146 147 148 149 150
  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 已提交
151 152 153 154
    if (tjsonAddItemToArray(pJson, pVgroupJson) != 0) {
      tjsonDelete(pVgroupJson);
      continue;
    }
S
monitor  
Shengliang Guan 已提交
155

S
monitor  
Shengliang Guan 已提交
156
    SMonVgroupDesc *pVgroupDesc = taosArrayGet(pInfo->vgroups, i);
S
monitor  
Shengliang Guan 已提交
157 158 159 160
    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 已提交
161 162

    SJson *pVnodesJson = tjsonAddArrayToObject(pVgroupJson, "vnodes");
S
monitor  
Shengliang Guan 已提交
163
    if (pVnodesJson == NULL) continue;
S
monitor  
Shengliang Guan 已提交
164 165 166

    for (int32_t j = 0; j < TSDB_MAX_REPLICA; ++j) {
      SMonVnodeDesc *pVnodeDesc = &pVgroupDesc->vnodes[j];
S
monitor  
Shengliang Guan 已提交
167
      if (pVnodeDesc->dnode_id <= 0) continue;
S
monitor  
Shengliang Guan 已提交
168 169 170 171

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

S
monitor  
Shengliang Guan 已提交
172 173
      tjsonAddDoubleToObject(pVnodeJson, "dnode_id", pVnodeDesc->dnode_id);
      tjsonAddStringToObject(pVnodeJson, "vnode_role", pVnodeDesc->vnode_role);
S
monitor  
Shengliang Guan 已提交
174 175 176 177

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

void monSetGrantInfo(SMonInfo *pMonitor, SMonGrantInfo *pInfo) {
S
monitor  
Shengliang Guan 已提交
181 182
  SJson *pJson = tjsonCreateObject();
  if (pJson == NULL) return;
S
monitor  
Shengliang Guan 已提交
183 184 185 186
  if (tjsonAddItemToObject(pMonitor->pJson, "grant_info", pJson) != 0) {
    tjsonDelete(pJson);
    return;
  }
S
monitor  
Shengliang Guan 已提交
187

S
monitor  
Shengliang Guan 已提交
188 189 190
  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 已提交
191 192 193
}

void monSetDnodeInfo(SMonInfo *pMonitor, SMonDnodeInfo *pInfo) {
S
monitor  
Shengliang Guan 已提交
194 195
  SJson *pJson = tjsonCreateObject();
  if (pJson == NULL) return;
S
monitor  
Shengliang Guan 已提交
196 197 198 199
  if (tjsonAddItemToObject(pMonitor->pJson, "dnode_info", pJson) != 0) {
    tjsonDelete(pJson);
    return;
  }
S
monitor  
Shengliang Guan 已提交
200

S
monitor  
Shengliang Guan 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  tjsonAddDoubleToObject(pJson, "uptime", pInfo->uptime);
  tjsonAddDoubleToObject(pJson, "cpu_engine", pInfo->cpu_engine);
  tjsonAddDoubleToObject(pJson, "cpu_system", pInfo->cpu_system);
  tjsonAddDoubleToObject(pJson, "cpu_cores", pInfo->cpu_cores);
  tjsonAddDoubleToObject(pJson, "mem_engine", pInfo->mem_engine);
  tjsonAddDoubleToObject(pJson, "mem_system", pInfo->mem_system);
  tjsonAddDoubleToObject(pJson, "mem_total", pInfo->mem_total);
  tjsonAddDoubleToObject(pJson, "disk_engine", pInfo->disk_engine);
  tjsonAddDoubleToObject(pJson, "disk_used", pInfo->disk_used);
  tjsonAddDoubleToObject(pJson, "disk_total", pInfo->disk_total);
  tjsonAddDoubleToObject(pJson, "net_in", pInfo->net_in);
  tjsonAddDoubleToObject(pJson, "net_out", pInfo->net_out);
  tjsonAddDoubleToObject(pJson, "io_read", pInfo->io_read);
  tjsonAddDoubleToObject(pJson, "io_write", pInfo->io_write);
  tjsonAddDoubleToObject(pJson, "io_read_disk", pInfo->io_read_disk);
  tjsonAddDoubleToObject(pJson, "io_write_disk", pInfo->io_write_disk);
  tjsonAddDoubleToObject(pJson, "req_select", pInfo->req_select);
  tjsonAddDoubleToObject(pJson, "req_select_rate", pInfo->req_select_rate);
  tjsonAddDoubleToObject(pJson, "req_insert", pInfo->req_insert);
  tjsonAddDoubleToObject(pJson, "req_insert_success", pInfo->req_insert_success);
  tjsonAddDoubleToObject(pJson, "req_insert_rate", pInfo->req_insert_rate);
  tjsonAddDoubleToObject(pJson, "req_insert_batch", pInfo->req_insert_batch);
  tjsonAddDoubleToObject(pJson, "req_insert_batch_success", pInfo->req_insert_batch_success);
  tjsonAddDoubleToObject(pJson, "req_insert_batch_rate", pInfo->req_insert_batch_rate);
  tjsonAddDoubleToObject(pJson, "errors", pInfo->errors);
  tjsonAddDoubleToObject(pJson, "vnodes_num", pInfo->vnodes_num);
  tjsonAddDoubleToObject(pJson, "masters", pInfo->masters);
  tjsonAddDoubleToObject(pJson, "has_mnode", pInfo->has_mnode);
S
monitor  
Shengliang Guan 已提交
229 230 231
}

void monSetDiskInfo(SMonInfo *pMonitor, SMonDiskInfo *pInfo) {
S
monitor  
Shengliang Guan 已提交
232 233
  SJson *pJson = tjsonCreateObject();
  if (pJson == NULL) return;
S
monitor  
Shengliang Guan 已提交
234
  if (tjsonAddItemToObject(pMonitor->pJson, "disk_infos", pJson) != 0) {
S
monitor  
Shengliang Guan 已提交
235 236 237
    tjsonDelete(pJson);
    return;
  }
S
monitor  
Shengliang Guan 已提交
238

S
monitor  
Shengliang Guan 已提交
239
  SJson *pDatadirsJson = tjsonAddArrayToObject(pJson, "datadir");
S
monitor  
Shengliang Guan 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
  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;
  tjsonAddStringToObject(pLogdirJson, "name", pInfo->logdir.name);
  tjsonAddDoubleToObject(pLogdirJson, "avail", pInfo->logdir.size.avail);
  tjsonAddDoubleToObject(pLogdirJson, "used", pInfo->logdir.size.used);
  tjsonAddDoubleToObject(pLogdirJson, "total", pInfo->logdir.size.total);

  SJson *pTempdirJson = tjsonCreateObject();
  if (pTempdirJson == NULL) return;
  if (tjsonAddItemToObject(pJson, "tempdir", pTempdirJson) != 0) return;
  tjsonAddStringToObject(pTempdirJson, "name", pInfo->tempdir.name);
  tjsonAddDoubleToObject(pTempdirJson, "avail", pInfo->tempdir.size.avail);
  tjsonAddDoubleToObject(pTempdirJson, "used", pInfo->tempdir.size.used);
  tjsonAddDoubleToObject(pTempdirJson, "total", pInfo->tempdir.size.total);
S
monitor  
Shengliang Guan 已提交
271
}
S
monitor  
Shengliang Guan 已提交
272

273
static const char *monLogLevelStr(ELogLevel level) {
S
Shengliang Guan 已提交
274
  switch (level) {
275
    case DEBUG_ERROR:
S
Shengliang Guan 已提交
276
      return "error";
277
    case DEBUG_INFO:
S
Shengliang Guan 已提交
278
      return "info";
279
    case DEBUG_DEBUG:
S
Shengliang Guan 已提交
280
      return "debug";
281
    case DEBUG_TRACE:
S
Shengliang Guan 已提交
282 283 284 285 286 287
      return "trace";
    default:
      return "undefine";
  }
}

S
monitor  
Shengliang Guan 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
static void monSetLogInfo(SMonInfo *pMonitor) {
  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;

  for (int32_t i = 0; i < taosArrayGetSize(pMonitor->logs); ++i) {
    SJson *pLogJson = tjsonCreateObject();
    if (pLogJson == NULL) continue;

    SMonLogItem *pLogItem = taosArrayGet(pMonitor->logs, i);

    char buf[40] = {0};
    taosFormatUtcTime(buf, sizeof(buf), pLogItem->ts, TSDB_TIME_PRECISION_MILLI);

S
monitor  
Shengliang Guan 已提交
308
    tjsonAddStringToObject(pLogJson, "ts", buf);
S
Shengliang Guan 已提交
309
    tjsonAddStringToObject(pLogJson, "level", monLogLevelStr(pLogItem->level));
S
monitor  
Shengliang Guan 已提交
310
    tjsonAddStringToObject(pLogJson, "content", pLogItem->content);
S
monitor  
Shengliang Guan 已提交
311 312 313 314 315 316 317 318 319 320

    if (tjsonAddItemToArray(pLogsJson, pLogJson) != 0) tjsonDelete(pLogJson);
  }

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

  SJson *pLogError = tjsonCreateObject();
  if (pLogError == NULL) return;
  tjsonAddStringToObject(pLogError, "level", "error");
321
  tjsonAddDoubleToObject(pLogError, "total", tsNumOfErrorLogs);
S
monitor  
Shengliang Guan 已提交
322 323 324 325 326
  if (tjsonAddItemToArray(pSummaryJson, pLogError) != 0) tjsonDelete(pLogError);

  SJson *pLogInfo = tjsonCreateObject();
  if (pLogInfo == NULL) return;
  tjsonAddStringToObject(pLogInfo, "level", "info");
327
  tjsonAddDoubleToObject(pLogInfo, "total", tsNumOfInfoLogs);
S
monitor  
Shengliang Guan 已提交
328 329 330 331 332
  if (tjsonAddItemToArray(pSummaryJson, pLogInfo) != 0) tjsonDelete(pLogInfo);

  SJson *pLogDebug = tjsonCreateObject();
  if (pLogDebug == NULL) return;
  tjsonAddStringToObject(pLogDebug, "level", "debug");
333
  tjsonAddDoubleToObject(pLogDebug, "total", tsNumOfDebugLogs);
S
monitor  
Shengliang Guan 已提交
334 335 336 337 338
  if (tjsonAddItemToArray(pSummaryJson, pLogDebug) != 0) tjsonDelete(pLogDebug);

  SJson *pLogTrace = tjsonCreateObject();
  if (pLogTrace == NULL) return;
  tjsonAddStringToObject(pLogTrace, "level", "trace");
339
  tjsonAddDoubleToObject(pLogTrace, "total", tsNumOfTraceLogs);
S
monitor  
Shengliang Guan 已提交
340 341 342 343 344 345 346 347 348 349 350 351
  if (tjsonAddItemToArray(pSummaryJson, pLogTrace) != 0) tjsonDelete(pLogTrace);
}

void monSendReport(SMonInfo *pMonitor) {
  monSetLogInfo(pMonitor);

  char *pCont = tjsonToString(pMonitor->pJson);
  if (pCont != NULL) {
    taosSendHttpReport(tsMonitor.server, tsMonitor.port, pCont, strlen(pCont));
    free(pCont);
  }
}