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

S
Shengliang Guan 已提交
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 61
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 已提交
62 63 64 65 66 67

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

70
static void mndBuildRuntimeInfo(SMnode* pMnode, SJson* pJson) {
S
Shengliang Guan 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83
  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);
84 85
}

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

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

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

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

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

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

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

118
  mndBuildRuntimeInfo(pMnode, pJson);
119

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

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

S
Shengliang Guan 已提交
130
  taosWLockLatch(&pMgmt->lock);
131 132
  char* pCont = mndBuildTelemetryReport(pMnode);
  if (pCont != NULL) {
S
Shengliang Guan 已提交
133
    if (taosSendHttpReport(tsTelemServer, tsTelemPort, pCont, strlen(pCont), HTTP_FLAT) != 0) {
H
Haojun Liao 已提交
134 135 136
      mError("failed to send telemetry report");
    } else {
      mTrace("succeed to send telemetry report");
S
Shengliang Guan 已提交
137
    }
wafwerar's avatar
wafwerar 已提交
138
    taosMemoryFree(pCont);
139
  }
S
Shengliang Guan 已提交
140 141
  taosWUnLockLatch(&pMgmt->lock);
  return 0;
142 143
}

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

147 148
  taosInitRWLatch(&pMgmt->lock);
  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
void mndCleanupTelem(SMnode* pMnode) {}