dmMonitor.c 6.0 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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
17
#include "dmInt.h"
S
shm  
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19
static void dmGetMonitorBasicInfo(SDnode *pDnode, SMonBasicInfo *pInfo) {
S
Shengliang Guan 已提交
20
  pInfo->protocol = 1;
S
shm  
Shengliang Guan 已提交
21 22 23
  pInfo->dnode_id = pDnode->dnodeId;
  pInfo->cluster_id = pDnode->clusterId;
  tstrncpy(pInfo->dnode_ep, tsLocalEp, TSDB_EP_LEN);
S
Shengliang Guan 已提交
24 25
}

S
Shengliang Guan 已提交
26
static void dmGetMonitorDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) {
S
shm  
Shengliang Guan 已提交
27
  pInfo->uptime = (taosGetTimestampMs() - pDnode->rebootTime) / (86400000.0f);
28
  SMgmtWrapper *pWrapper = dndAcquireWrapper(pDnode, MNODE);
S
Shengliang Guan 已提交
29 30 31 32
  if (pWrapper != NULL) {
    pInfo->has_mnode = pWrapper->required;
    dndReleaseWrapper(pWrapper);
  }
33 34 35 36 37 38 39 40 41 42
  tstrncpy(pInfo->logdir.name, tsLogDir, sizeof(pInfo->logdir.name));
  pInfo->logdir.size = tsLogSpace.size;
  tstrncpy(pInfo->tempdir.name, tsTempDir, sizeof(pInfo->tempdir.name));
  pInfo->tempdir.size = tsTempSpace.size;
}

static void dmGetMonitorInfo(SDnode *pDnode, SMonDmInfo *pInfo) {
  dmGetMonitorBasicInfo(pDnode, &pInfo->basic);
  dmGetMonitorSysInfo(&pInfo->sys);
  dmGetMonitorDnodeInfo(pDnode, &pInfo->dnode);
S
Shengliang Guan 已提交
43 44
}

S
Shengliang Guan 已提交
45
void dmSendMonitorReport(SDnode *pDnode) {
S
Shengliang Guan 已提交
46
  if (!tsEnableMonitor || tsMonitorFqdn[0] == 0 || tsMonitorPort == 0) return;
S
shm  
Shengliang Guan 已提交
47
  dTrace("send monitor report to %s:%u", tsMonitorFqdn, tsMonitorPort);
S
Shengliang Guan 已提交
48

49 50 51 52 53 54
  SMonDmInfo dmInfo = {0};
  SMonMmInfo mmInfo = {0};
  SMonVmInfo vmInfo = {0};
  SMonQmInfo qmInfo = {0};
  SMonSmInfo smInfo = {0};
  SMonBmInfo bmInfo = {0};
S
Shengliang Guan 已提交
55

56 57 58 59 60
  SRpcMsg req = {0};
  SRpcMsg rsp;
  SEpSet  epset = {.inUse = 0, .numOfEps = 1};
  tstrncpy(epset.eps[0].fqdn, tsLocalFqdn, TSDB_FQDN_LEN);
  epset.eps[0].port = tsServerPort;
S
shm  
Shengliang Guan 已提交
61

62
  SMgmtWrapper *pWrapper = NULL;
63
  dmGetMonitorInfo(pDnode, &dmInfo);
S
Shengliang Guan 已提交
64

65
  bool getFromAPI = !tsMultiProcess;
66
  pWrapper = &pDnode->wrappers[MNODE];
67
  if (getFromAPI) {
68
    if (dndMarkWrapper(pWrapper) != 0) {
69
      mmGetMonitorInfo(pWrapper, &mmInfo);
70 71 72 73
      dndReleaseWrapper(pWrapper);
    }
  } else {
    if (pWrapper->required) {
74 75
      req.msgType = TDMT_MON_MM_INFO;
      dndSendRecv(pDnode, &epset, &req, &rsp);
76
      if (rsp.code == 0 && rsp.contLen > 0) {
77 78
        tDeserializeSMonMmInfo(rsp.pCont, rsp.contLen, &mmInfo);
      }
79
      rpcFreeCont(rsp.pCont);
S
Shengliang Guan 已提交
80
    }
S
Shengliang Guan 已提交
81 82
  }

83
  pWrapper = &pDnode->wrappers[VNODES];
84
  if (getFromAPI) {
85
    if (dndMarkWrapper(pWrapper) != 0) {
86
      vmGetMonitorInfo(pWrapper, &vmInfo);
87 88 89 90
      dndReleaseWrapper(pWrapper);
    }
  } else {
    if (pWrapper->required) {
91 92
      req.msgType = TDMT_MON_VM_INFO;
      dndSendRecv(pDnode, &epset, &req, &rsp);
93
      if (rsp.code == 0 && rsp.contLen > 0) {
94 95
        tDeserializeSMonVmInfo(rsp.pCont, rsp.contLen, &vmInfo);
      }
96 97 98 99
      rpcFreeCont(rsp.pCont);
    }
  }

100
  pWrapper = &pDnode->wrappers[QNODE];
101
  if (getFromAPI) {
102
    if (dndMarkWrapper(pWrapper) != 0) {
103
      qmGetMonitorInfo(pWrapper, &qmInfo);
104 105 106 107
      dndReleaseWrapper(pWrapper);
    }
  } else {
    if (pWrapper->required) {
108 109
      req.msgType = TDMT_MON_QM_INFO;
      dndSendRecv(pDnode, &epset, &req, &rsp);
110
      if (rsp.code == 0 && rsp.contLen > 0) {
111 112
        tDeserializeSMonQmInfo(rsp.pCont, rsp.contLen, &qmInfo);
      }
113 114 115 116
      rpcFreeCont(rsp.pCont);
    }
  }

117
  pWrapper = &pDnode->wrappers[SNODE];
118
  if (getFromAPI) {
119
    if (dndMarkWrapper(pWrapper) != 0) {
120
      smGetMonitorInfo(pWrapper, &smInfo);
121 122 123 124
      dndReleaseWrapper(pWrapper);
    }
  } else {
    if (pWrapper->required) {
125 126
      req.msgType = TDMT_MON_SM_INFO;
      dndSendRecv(pDnode, &epset, &req, &rsp);
127
      if (rsp.code == 0 && rsp.contLen > 0) {
128 129
        tDeserializeSMonSmInfo(rsp.pCont, rsp.contLen, &smInfo);
      }
130 131 132 133
      rpcFreeCont(rsp.pCont);
    }
  }

134
  pWrapper = &pDnode->wrappers[BNODE];
135
  if (getFromAPI) {
136
    if (dndMarkWrapper(pWrapper) != 0) {
137
      bmGetMonitorInfo(pWrapper, &bmInfo);
138 139 140 141
      dndReleaseWrapper(pWrapper);
    }
  } else {
    if (pWrapper->required) {
142 143
      req.msgType = TDMT_MON_BM_INFO;
      dndSendRecv(pDnode, &epset, &req, &rsp);
144
      if (rsp.code == 0 && rsp.contLen > 0) {
145 146
        tDeserializeSMonBmInfo(rsp.pCont, rsp.contLen, &bmInfo);
      }
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
      rpcFreeCont(rsp.pCont);
    }
  }

  monSetDmInfo(&dmInfo);
  monSetMmInfo(&mmInfo);
  monSetVmInfo(&vmInfo);
  monSetQmInfo(&qmInfo);
  monSetSmInfo(&smInfo);
  monSetBmInfo(&bmInfo);
  tFreeSMonMmInfo(&mmInfo);
  tFreeSMonVmInfo(&vmInfo);
  tFreeSMonQmInfo(&qmInfo);
  tFreeSMonSmInfo(&smInfo);
  tFreeSMonBmInfo(&bmInfo);
  monSendReport();
}

void dmGetVnodeLoads(SMgmtWrapper *pWrapper, SMonVloadInfo *pInfo) {
166 167
  bool getFromAPI = !tsMultiProcess;
  if (getFromAPI) {
168 169 170 171 172 173 174 175 176
    vmGetVnodeLoads(pWrapper, pInfo);
  } else {
    SRpcMsg req = {.msgType = TDMT_MON_VM_LOAD};
    SRpcMsg rsp = {0};
    SEpSet  epset = {.inUse = 0, .numOfEps = 1};
    tstrncpy(epset.eps[0].fqdn, tsLocalFqdn, TSDB_FQDN_LEN);
    epset.eps[0].port = tsServerPort;

    dndSendRecv(pWrapper->pDnode, &epset, &req, &rsp);
177
    if (rsp.code == 0 && rsp.contLen > 0) {
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
      tDeserializeSMonVloadInfo(rsp.pCont, rsp.contLen, pInfo);
    }
    rpcFreeCont(rsp.pCont);
  }
}

void dmGetMonitorSysInfo(SMonSysInfo *pInfo) {
  taosGetCpuUsage(&pInfo->cpu_engine, &pInfo->cpu_system);
  taosGetCpuCores(&pInfo->cpu_cores);
  taosGetProcMemory(&pInfo->mem_engine);
  taosGetSysMemory(&pInfo->mem_system);
  pInfo->mem_total = tsTotalMemoryKB;
  pInfo->disk_engine = 0;
  pInfo->disk_used = tsDataSpace.size.used;
  pInfo->disk_total = tsDataSpace.size.total;
  taosGetCardInfoDelta(&pInfo->net_in, &pInfo->net_out);
  taosGetProcIODelta(&pInfo->io_read, &pInfo->io_write, &pInfo->io_read_disk, &pInfo->io_write_disk);
}