mndTelem.c 4.9 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 "thttp.h"
S
Shengliang Guan 已提交
21
#include "tjson.h"
22

S
Shengliang Guan 已提交
23 24 25 26 27 28 29 30 31 32 33 34 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
typedef struct {
  int64_t numOfDnode;
  int64_t numOfMnode;
  int64_t numOfVgroup;
  int64_t numOfDatabase;
  int64_t numOfSuperTable;
  int64_t numOfChildTable;
  int64_t numOfNormalTable;
  int64_t numOfColumn;
  int64_t totalPoints;
  int64_t totalStorage;
  int64_t compStorage;
} SMnodeStat;

static void mndGetStat(SMnode* pMnode, SMnodeStat* pStat) {
  memset(pStat, 0, sizeof(SMnodeStat));

  SSdb* pSdb = pMnode->pSdb;
  pStat->numOfDnode = sdbGetSize(pSdb, SDB_DNODE);
  pStat->numOfMnode = sdbGetSize(pSdb, SDB_MNODE);
  pStat->numOfVgroup = sdbGetSize(pSdb, SDB_VGROUP);
  pStat->numOfDatabase = sdbGetSize(pSdb, SDB_DB);
  pStat->numOfSuperTable = sdbGetSize(pSdb, SDB_STB);

  void* pIter = NULL;
  while (1) {
    SVgObj* pVgroup = NULL;
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
    if (pIter == NULL) break;

    pStat->numOfChildTable += pVgroup->numOfTables;
    pStat->numOfColumn += pVgroup->numOfTimeSeries;
    pStat->totalPoints += pVgroup->pointsWritten;
    pStat->totalStorage += pVgroup->totalStorage;
    pStat->compStorage += pVgroup->compStorage;

    sdbRelease(pSdb, pVgroup);
  }
S
Shengliang Guan 已提交
61 62 63 64 65 66

  pStat->numOfChildTable = 100;
  pStat->numOfColumn = 200;
  pStat->totalPoints = 300;
  pStat->totalStorage = 400;
  pStat->compStorage = 500;
S
Shengliang Guan 已提交
67 68
}

69
static void mndBuildRuntimeInfo(SMnode* pMnode, SJson* pJson) {
S
Shengliang Guan 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82
  SMnodeStat mstat = {0};
  mndGetStat(pMnode, &mstat);

  tjsonAddDoubleToObject(pJson, "numOfDnode", mstat.numOfDnode);
  tjsonAddDoubleToObject(pJson, "numOfMnode", mstat.numOfMnode);
  tjsonAddDoubleToObject(pJson, "numOfVgroup", mstat.numOfVgroup);
  tjsonAddDoubleToObject(pJson, "numOfDatabase", mstat.numOfDatabase);
  tjsonAddDoubleToObject(pJson, "numOfSuperTable", mstat.numOfSuperTable);
  tjsonAddDoubleToObject(pJson, "numOfChildTable", mstat.numOfChildTable);
  tjsonAddDoubleToObject(pJson, "numOfColumn", mstat.numOfColumn);
  tjsonAddDoubleToObject(pJson, "numOfPoint", mstat.totalPoints);
  tjsonAddDoubleToObject(pJson, "totalStorage", mstat.totalStorage);
  tjsonAddDoubleToObject(pJson, "compStorage", mstat.compStorage);
83 84
}

85 86 87
static char* mndBuildTelemetryReport(SMnode* pMnode) {
  char        tmp[4096] = {0};
  STelemMgmt* pMgmt = &pMnode->telemMgmt;
88

89 90
  SJson* pJson = tjsonCreateObject();
  if (pJson == NULL) return NULL;
91

92 93 94 95
  char clusterName[64] = {0};
  mndGetClusterName(pMnode, clusterName, sizeof(clusterName));
  tjsonAddStringToObject(pJson, "instanceId", clusterName);
  tjsonAddDoubleToObject(pJson, "reportVersion", 1);
96

S
Shengliang Guan 已提交
97
  if (taosGetOsReleaseName(tmp, sizeof(tmp)) == 0) {
98
    tjsonAddStringToObject(pJson, "os", tmp);
99 100
  }

S
Shengliang Guan 已提交
101
  float numOfCores = 0;
S
Shengliang Guan 已提交
102
  if (taosGetCpuInfo(tmp, sizeof(tmp), &numOfCores) == 0) {
103 104 105
    tjsonAddStringToObject(pJson, "cpuModel", tmp);
    tjsonAddDoubleToObject(pJson, "numOfCpu", numOfCores);
  } else {
S
Shengliang Guan 已提交
106
    tjsonAddDoubleToObject(pJson, "numOfCpu", tsNumOfCores);
107 108
  }

S
Shengliang Guan 已提交
109 110
  snprintf(tmp, sizeof(tmp), "%" PRId64 " kB", tsTotalMemoryKB);
  tjsonAddStringToObject(pJson, "memory", tmp);
111

112 113 114 115
  tjsonAddStringToObject(pJson, "version", version);
  tjsonAddStringToObject(pJson, "buildInfo", buildinfo);
  tjsonAddStringToObject(pJson, "gitInfo", gitinfo);
  tjsonAddStringToObject(pJson, "email", pMgmt->email);
116

117
  mndBuildRuntimeInfo(pMnode, pJson);
118

119 120 121
  char* pCont = tjsonToString(pJson);
  tjsonDelete(pJson);
  return pCont;
122 123
}

S
Shengliang Guan 已提交
124 125
static int32_t mndProcessTelemTimer(SRpcMsg* pReq) {
  SMnode*     pMnode = pReq->info.node;
S
Shengliang Guan 已提交
126
  STelemMgmt* pMgmt = &pMnode->telemMgmt;
S
Shengliang Guan 已提交
127
  if (!tsEnableTelem) return 0;
S
Shengliang Guan 已提交
128

S
Shengliang Guan 已提交
129
  taosThreadMutexLock(&pMgmt->lock);
130
  char* pCont = mndBuildTelemetryReport(pMnode);
S
Shengliang Guan 已提交
131 132
  taosThreadMutexUnlock(&pMgmt->lock);

133
  if (pCont != NULL) {
S
Shengliang Guan 已提交
134
    if (taosSendHttpReport(tsTelemServer, tsTelemPort, pCont, strlen(pCont), HTTP_FLAT) != 0) {
135 136
      mError("failed to send telemetry report");
    } else {
137
      mInfo("succeed to send telemetry report");
S
Shengliang Guan 已提交
138
    }
wafwerar's avatar
wafwerar 已提交
139
    taosMemoryFree(pCont);
140
  }
S
Shengliang Guan 已提交
141
  return 0;
142 143
}

S
Shengliang Guan 已提交
144 145
int32_t mndInitTelem(SMnode* pMnode) {
  STelemMgmt* pMgmt = &pMnode->telemMgmt;
146

S
Shengliang Guan 已提交
147
  taosThreadMutexInit(&pMgmt->lock, NULL);
148
  taosGetEmail(pMgmt->email, sizeof(pMgmt->email));
S
Shengliang Guan 已提交
149
  mndSetMsgHandle(pMnode, TDMT_MND_TELEM_TIMER, mndProcessTelemTimer);
150

151 152 153
  return 0;
}

S
Shengliang Guan 已提交
154 155 156 157
void mndCleanupTelem(SMnode* pMnode) {
  STelemMgmt* pMgmt = &pMnode->telemMgmt;
  taosThreadMutexDestroy(&pMgmt->lock);
}