mndTelem.c 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2020 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
S
Shengliang Guan 已提交
17
#include "mndTelem.h"
S
Shengliang Guan 已提交
18
#include "mndCluster.h"
S
Shengliang Guan 已提交
19
#include "mndSync.h"
S
Shengliang Guan 已提交
20
#include "tbuffer.h"
21
#include "tjson.h"
S
Shengliang Guan 已提交
22
#include "thttp.h"
23

S
Shengliang Guan 已提交
24
#define TELEMETRY_SERVER "telemetry.taosdata.com"
S
Shengliang Guan 已提交
25
#define TELEMETRY_PORT   80
26

27 28 29 30 31 32 33 34 35 36 37 38 39 40
static void mndBuildRuntimeInfo(SMnode* pMnode, SJson* pJson) {
  SMnodeLoad load = {0};
  if (mndGetLoad(pMnode, &load) != 0) return;

  tjsonAddDoubleToObject(pJson, "numOfDnode", load.numOfDnode);
  tjsonAddDoubleToObject(pJson, "numOfMnode", load.numOfMnode);
  tjsonAddDoubleToObject(pJson, "numOfVgroup", load.numOfVgroup);
  tjsonAddDoubleToObject(pJson, "numOfDatabase", load.numOfDatabase);
  tjsonAddDoubleToObject(pJson, "numOfSuperTable", load.numOfSuperTable);
  tjsonAddDoubleToObject(pJson, "numOfChildTable", load.numOfChildTable);
  tjsonAddDoubleToObject(pJson, "numOfColumn", load.numOfColumn);
  tjsonAddDoubleToObject(pJson, "numOfPoint", load.totalPoints);
  tjsonAddDoubleToObject(pJson, "totalStorage", load.totalStorage);
  tjsonAddDoubleToObject(pJson, "compStorage", load.compStorage);
41 42
}

43 44 45
static char* mndBuildTelemetryReport(SMnode* pMnode) {
  char        tmp[4096] = {0};
  STelemMgmt* pMgmt = &pMnode->telemMgmt;
46

47 48
  SJson* pJson = tjsonCreateObject();
  if (pJson == NULL) return NULL;
49

50 51 52 53
  char clusterName[64] = {0};
  mndGetClusterName(pMnode, clusterName, sizeof(clusterName));
  tjsonAddStringToObject(pJson, "instanceId", clusterName);
  tjsonAddDoubleToObject(pJson, "reportVersion", 1);
54

55 56
  if (taosGetOsReleaseName(tmp, sizeof(tmp))) {
    tjsonAddStringToObject(pJson, "os", tmp);
57 58
  }

59 60 61 62 63 64
  int32_t numOfCores = 0;
  if (taosGetCpuInfo(tmp, sizeof(tmp), &numOfCores)) {
    tjsonAddStringToObject(pJson, "cpuModel", tmp);
    tjsonAddDoubleToObject(pJson, "numOfCpu", numOfCores);
  } else {
    tjsonAddDoubleToObject(pJson, "numOfCpu", taosGetCpuCores());
65 66
  }

67 68 69 70
  uint64_t memoryKB = 0;
  if (taosGetTotalSysMemoryKB(&memoryKB)) {
    snprintf(tmp, sizeof(tmp), "%" PRIu64 " kB", memoryKB);
    tjsonAddStringToObject(pJson, "memory", tmp);
71 72
  }

73 74 75 76
  tjsonAddStringToObject(pJson, "version", version);
  tjsonAddStringToObject(pJson, "buildInfo", buildinfo);
  tjsonAddStringToObject(pJson, "gitInfo", gitinfo);
  tjsonAddStringToObject(pJson, "email", pMgmt->email);
77

78
  mndBuildRuntimeInfo(pMnode, pJson);
79

80 81 82
  char* pCont = tjsonToString(pJson);
  tjsonDelete(pJson);
  return pCont;
83 84
}

S
Shengliang Guan 已提交
85 86
static int32_t mndProcessTelemTimer(SMnodeMsg* pReq) {
  SMnode*     pMnode = pReq->pMnode;
S
Shengliang Guan 已提交
87
  STelemMgmt* pMgmt = &pMnode->telemMgmt;
S
Shengliang Guan 已提交
88
  if (!pMgmt->enable) return 0;
S
Shengliang Guan 已提交
89

S
Shengliang Guan 已提交
90
  taosWLockLatch(&pMgmt->lock);
91 92
  char* pCont = mndBuildTelemetryReport(pMnode);
  if (pCont != NULL) {
S
Shengliang Guan 已提交
93
    taosSendHttpReport(TELEMETRY_SERVER, TELEMETRY_PORT, pCont, strlen(pCont));
94 95
    free(pCont);
  }
S
Shengliang Guan 已提交
96 97
  taosWUnLockLatch(&pMgmt->lock);
  return 0;
98 99
}

S
Shengliang Guan 已提交
100 101
int32_t mndInitTelem(SMnode* pMnode) {
  STelemMgmt* pMgmt = &pMnode->telemMgmt;
102

103 104 105
  taosInitRWLatch(&pMgmt->lock);
  pMgmt->enable = tsEnableTelemetryReporting;
  taosGetEmail(pMgmt->email, sizeof(pMgmt->email));
S
Shengliang Guan 已提交
106
  mndSetMsgHandle(pMnode, TDMT_MND_TELEM_TIMER, mndProcessTelemTimer);
107

S
Shengliang Guan 已提交
108
  mDebug("mnode telemetry is initialized");
109 110 111
  return 0;
}

S
Shengliang Guan 已提交
112
void mndCleanupTelem(SMnode* pMnode) {}