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

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

S
Shengliang Guan 已提交
25
void monRecordLog(int64_t ts, ELogLevel level, const char *content) {
S
Shengliang Guan 已提交
26
  pthread_mutex_lock(&tsMonitor.lock);
S
Shengliang Guan 已提交
27 28 29 30 31 32 33 34 35 36
  int32_t size = taosArrayGetSize(tsMonitor.logs);
  if (size >= tsMonitor.maxLogs) {
    uInfo("too many logs for monitor");
  } else {
    SMonLogItem  item = {.ts = ts, .level = level};
    SMonLogItem *pItem = taosArrayPush(tsMonitor.logs, &item);
    if (pItem != NULL) {
      tstrncpy(pItem->content, content, sizeof(item.content));
    }
  }
S
Shengliang Guan 已提交
37
  pthread_mutex_unlock(&tsMonitor.lock);
S
Shengliang Guan 已提交
38 39
}

S
monitor  
Shengliang Guan 已提交
40
int32_t monInit(const SMonCfg *pCfg) {
S
monitor  
Shengliang Guan 已提交
41
  tsMonitor.logs = taosArrayInit(16, sizeof(SMonLogItem));
S
monitor  
Shengliang Guan 已提交
42 43 44 45 46 47 48 49
  if (tsMonitor.logs == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  tsMonitor.maxLogs = pCfg->maxLogs;
  tsMonitor.server = pCfg->server;
  tsMonitor.port = pCfg->port;
S
Shengliang Guan 已提交
50
  tsLogFp = monRecordLog;
S
Shengliang Guan 已提交
51
  pthread_mutex_init(&tsMonitor.lock, NULL);
S
monitor  
Shengliang Guan 已提交
52 53 54 55 56 57
  return 0;
}

void monCleanup() {
  taosArrayDestroy(tsMonitor.logs);
  tsMonitor.logs = NULL;
S
Shengliang Guan 已提交
58
  pthread_mutex_destroy(&tsMonitor.lock);
S
monitor  
Shengliang Guan 已提交
59 60 61 62
}

SMonInfo *monCreateMonitorInfo() {
  SMonInfo *pMonitor = calloc(1, sizeof(SMonInfo));
S
monitor  
Shengliang Guan 已提交
63 64 65 66
  if (pMonitor == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
S
monitor  
Shengliang Guan 已提交
67

S
Shengliang Guan 已提交
68
  pthread_mutex_lock(&tsMonitor.lock);
69
  pMonitor->logs = taosArrayDup(tsMonitor.logs);
S
monitor  
Shengliang Guan 已提交
70
  taosArrayClear(tsMonitor.logs);
S
Shengliang Guan 已提交
71
  pthread_mutex_unlock(&tsMonitor.lock);
S
monitor  
Shengliang Guan 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

  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 已提交
90
  SJson  *pJson = pMonitor->pJson;
91 92 93
  int64_t ms = taosGetTimestampMs();
  char    buf[40] = {0};
  taosFormatUtcTime(buf, sizeof(buf), ms, TSDB_TIME_PRECISION_MILLI);
S
monitor  
Shengliang Guan 已提交
94

95
  tjsonAddStringToObject(pJson, "ts", buf);
S
monitor  
Shengliang Guan 已提交
96 97
  tjsonAddDoubleToObject(pJson, "dnode_id", pInfo->dnode_id);
  tjsonAddStringToObject(pJson, "dnode_ep", pInfo->dnode_ep);
S
monitor  
Shengliang Guan 已提交
98 99
}

S
monitor  
Shengliang Guan 已提交
100
void monSetClusterInfo(SMonInfo *pMonitor, SMonClusterInfo *pInfo) {
S
monitor  
Shengliang Guan 已提交
101 102
  SJson *pJson = tjsonCreateObject();
  if (pJson == NULL) return;
S
monitor  
Shengliang Guan 已提交
103 104 105 106
  if (tjsonAddItemToObject(pMonitor->pJson, "cluster_info", pJson) != 0) {
    tjsonDelete(pJson);
    return;
  }
S
monitor  
Shengliang Guan 已提交
107

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

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

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

S
Shengliang Guan 已提交
137
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->mnodes); ++i) {
S
monitor  
Shengliang Guan 已提交
138 139 140
    SJson *pMnodeJson = tjsonCreateObject();
    if (pMnodeJson == NULL) continue;

S
Shengliang Guan 已提交
141
    SMonMnodeDesc *pMnodeDesc = taosArrayGet(pInfo->mnodes, i);
S
monitor  
Shengliang Guan 已提交
142 143 144
    tjsonAddDoubleToObject(pMnodeJson, "mnode_id", pMnodeDesc->mnode_id);
    tjsonAddStringToObject(pMnodeJson, "mnode_ep", pMnodeDesc->mnode_ep);
    tjsonAddStringToObject(pMnodeJson, "role", pMnodeDesc->role);
S
monitor  
Shengliang Guan 已提交
145 146 147

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

void monSetVgroupInfo(SMonInfo *pMonitor, SMonVgroupInfo *pInfo) {
S
monitor  
Shengliang Guan 已提交
151
  SJson *pJson = tjsonAddArrayToObject(pMonitor->pJson, "vgroup_infos");
S
monitor  
Shengliang Guan 已提交
152 153 154 155 156
  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 已提交
157 158 159 160
    if (tjsonAddItemToArray(pJson, pVgroupJson) != 0) {
      tjsonDelete(pVgroupJson);
      continue;
    }
S
monitor  
Shengliang Guan 已提交
161

S
monitor  
Shengliang Guan 已提交
162
    SMonVgroupDesc *pVgroupDesc = taosArrayGet(pInfo->vgroups, i);
S
monitor  
Shengliang Guan 已提交
163 164 165 166
    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 已提交
167 168

    SJson *pVnodesJson = tjsonAddArrayToObject(pVgroupJson, "vnodes");
S
monitor  
Shengliang Guan 已提交
169
    if (pVnodesJson == NULL) continue;
S
monitor  
Shengliang Guan 已提交
170 171 172

    for (int32_t j = 0; j < TSDB_MAX_REPLICA; ++j) {
      SMonVnodeDesc *pVnodeDesc = &pVgroupDesc->vnodes[j];
S
monitor  
Shengliang Guan 已提交
173
      if (pVnodeDesc->dnode_id <= 0) continue;
S
monitor  
Shengliang Guan 已提交
174 175 176 177

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

S
monitor  
Shengliang Guan 已提交
178 179
      tjsonAddDoubleToObject(pVnodeJson, "dnode_id", pVnodeDesc->dnode_id);
      tjsonAddStringToObject(pVnodeJson, "vnode_role", pVnodeDesc->vnode_role);
S
monitor  
Shengliang Guan 已提交
180 181 182 183

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

void monSetGrantInfo(SMonInfo *pMonitor, SMonGrantInfo *pInfo) {
S
monitor  
Shengliang Guan 已提交
187 188
  SJson *pJson = tjsonCreateObject();
  if (pJson == NULL) return;
S
monitor  
Shengliang Guan 已提交
189 190 191 192
  if (tjsonAddItemToObject(pMonitor->pJson, "grant_info", pJson) != 0) {
    tjsonDelete(pJson);
    return;
  }
S
monitor  
Shengliang Guan 已提交
193

S
monitor  
Shengliang Guan 已提交
194 195 196
  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 已提交
197 198 199
}

void monSetDnodeInfo(SMonInfo *pMonitor, SMonDnodeInfo *pInfo) {
S
monitor  
Shengliang Guan 已提交
200 201
  SJson *pJson = tjsonCreateObject();
  if (pJson == NULL) return;
S
monitor  
Shengliang Guan 已提交
202 203 204 205
  if (tjsonAddItemToObject(pMonitor->pJson, "dnode_info", pJson) != 0) {
    tjsonDelete(pJson);
    return;
  }
S
monitor  
Shengliang Guan 已提交
206

S
monitor  
Shengliang Guan 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  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 已提交
235 236 237
}

void monSetDiskInfo(SMonInfo *pMonitor, SMonDiskInfo *pInfo) {
S
monitor  
Shengliang Guan 已提交
238 239
  SJson *pJson = tjsonCreateObject();
  if (pJson == NULL) return;
S
monitor  
Shengliang Guan 已提交
240
  if (tjsonAddItemToObject(pMonitor->pJson, "disk_infos", pJson) != 0) {
S
monitor  
Shengliang Guan 已提交
241 242 243
    tjsonDelete(pJson);
    return;
  }
S
monitor  
Shengliang Guan 已提交
244

S
monitor  
Shengliang Guan 已提交
245
  SJson *pDatadirsJson = tjsonAddArrayToObject(pJson, "datadir");
S
monitor  
Shengliang Guan 已提交
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 271 272 273 274 275 276
  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 已提交
277
}
S
monitor  
Shengliang Guan 已提交
278

279
static const char *monLogLevelStr(ELogLevel level) {
S
Shengliang Guan 已提交
280
  switch (level) {
281
    case DEBUG_ERROR:
S
Shengliang Guan 已提交
282
      return "error";
283
    case DEBUG_INFO:
S
Shengliang Guan 已提交
284
      return "info";
285
    case DEBUG_DEBUG:
S
Shengliang Guan 已提交
286
      return "debug";
287
    case DEBUG_TRACE:
S
Shengliang Guan 已提交
288 289 290 291 292 293
      return "trace";
    default:
      return "undefine";
  }
}

S
monitor  
Shengliang Guan 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
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 已提交
314
    tjsonAddStringToObject(pLogJson, "ts", buf);
S
Shengliang Guan 已提交
315
    tjsonAddStringToObject(pLogJson, "level", monLogLevelStr(pLogItem->level));
S
monitor  
Shengliang Guan 已提交
316
    tjsonAddStringToObject(pLogJson, "content", pLogItem->content);
S
monitor  
Shengliang Guan 已提交
317 318 319 320 321 322 323 324 325 326

    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");
327
  tjsonAddDoubleToObject(pLogError, "total", tsNumOfErrorLogs);
S
monitor  
Shengliang Guan 已提交
328 329 330 331 332
  if (tjsonAddItemToArray(pSummaryJson, pLogError) != 0) tjsonDelete(pLogError);

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

  SJson *pLogDebug = tjsonCreateObject();
  if (pLogDebug == NULL) return;
  tjsonAddStringToObject(pLogDebug, "level", "debug");
339
  tjsonAddDoubleToObject(pLogDebug, "total", tsNumOfDebugLogs);
S
monitor  
Shengliang Guan 已提交
340 341 342 343 344
  if (tjsonAddItemToArray(pSummaryJson, pLogDebug) != 0) tjsonDelete(pLogDebug);

  SJson *pLogTrace = tjsonCreateObject();
  if (pLogTrace == NULL) return;
  tjsonAddStringToObject(pLogTrace, "level", "trace");
345
  tjsonAddDoubleToObject(pLogTrace, "total", tsNumOfTraceLogs);
S
monitor  
Shengliang Guan 已提交
346 347 348 349 350 351 352 353 354 355 356 357
  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);
  }
}