dmMonitor.c 6.3 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
S
Shengliang Guan 已提交
17
#include "dmImp.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
Shengliang Guan 已提交
21 22
  pInfo->dnode_id = pDnode->data.dnodeId;
  pInfo->cluster_id = pDnode->data.clusterId;
S
shm  
Shengliang Guan 已提交
23
  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
Shengliang Guan 已提交
27
  pInfo->uptime = (taosGetTimestampMs() - pDnode->data.rebootTime) / (86400000.0f);
28 29 30 31
  pInfo->has_mnode = pDnode->wrappers[MNODE].required;
  pInfo->has_qnode = pDnode->wrappers[QNODE].required;
  pInfo->has_snode = pDnode->wrappers[SNODE].required;
  pInfo->has_bnode = pDnode->wrappers[BNODE].required;
32 33 34 35 36 37 38 39
  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);
S
Shengliang Guan 已提交
40
  dmGetMonitorSysInfo(&pInfo->sys);
41
  dmGetMonitorDnodeInfo(pDnode, &pInfo->dnode);
S
Shengliang Guan 已提交
42 43
}

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

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

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

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

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

S
Shengliang Guan 已提交
82
  pWrapper = &pDnode->wrappers[VNODE];
83
  if (getFromAPI) {
S
Shengliang Guan 已提交
84
    if (dmMarkWrapper(pWrapper) == 0) {
85
      vmGetMonitorInfo(pWrapper, &vmInfo);
S
Shengliang Guan 已提交
86
      dmReleaseWrapper(pWrapper);
87 88 89
    }
  } else {
    if (pWrapper->required) {
90
      req.msgType = TDMT_MON_VM_INFO;
S
Shengliang Guan 已提交
91
      dmSendRecv(pDnode, &epset, &req, &rsp);
92
      if (rsp.code == 0 && rsp.contLen > 0) {
93 94
        tDeserializeSMonVmInfo(rsp.pCont, rsp.contLen, &vmInfo);
      }
95 96 97 98
      rpcFreeCont(rsp.pCont);
    }
  }

99
  pWrapper = &pDnode->wrappers[QNODE];
100
  if (getFromAPI) {
S
Shengliang Guan 已提交
101
    if (dmMarkWrapper(pWrapper) == 0) {
102
      qmGetMonitorInfo(pWrapper, &qmInfo);
S
Shengliang Guan 已提交
103
      dmReleaseWrapper(pWrapper);
104 105 106
    }
  } else {
    if (pWrapper->required) {
107
      req.msgType = TDMT_MON_QM_INFO;
S
Shengliang Guan 已提交
108
      dmSendRecv(pDnode, &epset, &req, &rsp);
109
      if (rsp.code == 0 && rsp.contLen > 0) {
110 111
        tDeserializeSMonQmInfo(rsp.pCont, rsp.contLen, &qmInfo);
      }
112 113 114 115
      rpcFreeCont(rsp.pCont);
    }
  }

116
  pWrapper = &pDnode->wrappers[SNODE];
117
  if (getFromAPI) {
S
Shengliang Guan 已提交
118
    if (dmMarkWrapper(pWrapper) == 0) {
119
      smGetMonitorInfo(pWrapper, &smInfo);
S
Shengliang Guan 已提交
120
      dmReleaseWrapper(pWrapper);
121 122 123
    }
  } else {
    if (pWrapper->required) {
124
      req.msgType = TDMT_MON_SM_INFO;
S
Shengliang Guan 已提交
125
      dmSendRecv(pDnode, &epset, &req, &rsp);
126
      if (rsp.code == 0 && rsp.contLen > 0) {
127 128
        tDeserializeSMonSmInfo(rsp.pCont, rsp.contLen, &smInfo);
      }
129 130 131 132
      rpcFreeCont(rsp.pCont);
    }
  }

133
  pWrapper = &pDnode->wrappers[BNODE];
134
  if (getFromAPI) {
S
Shengliang Guan 已提交
135
    if (dmMarkWrapper(pWrapper) == 0) {
136
      bmGetMonitorInfo(pWrapper, &bmInfo);
S
Shengliang Guan 已提交
137
      dmReleaseWrapper(pWrapper);
138 139 140
    }
  } else {
    if (pWrapper->required) {
141
      req.msgType = TDMT_MON_BM_INFO;
S
Shengliang Guan 已提交
142
      dmSendRecv(pDnode, &epset, &req, &rsp);
143
      if (rsp.code == 0 && rsp.contLen > 0) {
144 145
        tDeserializeSMonBmInfo(rsp.pCont, rsp.contLen, &bmInfo);
      }
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
      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();
}

S
Shengliang Guan 已提交
164 165 166 167
void dmGetVnodeLoads(SDnode *pDnode, SMonVloadInfo *pInfo) {
  SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, VNODE);
  if (pWrapper == NULL) return;

168 169
  bool getFromAPI = !tsMultiProcess;
  if (getFromAPI) {
170 171 172 173 174
    vmGetVnodeLoads(pWrapper, pInfo);
  } else {
    SRpcMsg req = {.msgType = TDMT_MON_VM_LOAD};
    SRpcMsg rsp = {0};
    SEpSet  epset = {.inUse = 0, .numOfEps = 1};
S
Shengliang Guan 已提交
175
    tstrncpy(epset.eps[0].fqdn, pDnode->data.localFqdn, TSDB_FQDN_LEN);
176 177
    epset.eps[0].port = tsServerPort;

S
Shengliang Guan 已提交
178
    dmSendRecv(pDnode, &epset, &req, &rsp);
179
    if (rsp.code == 0 && rsp.contLen > 0) {
180 181 182 183
      tDeserializeSMonVloadInfo(rsp.pCont, rsp.contLen, pInfo);
    }
    rpcFreeCont(rsp.pCont);
  }
S
Shengliang Guan 已提交
184
  dmReleaseWrapper(pWrapper);
185
}
S
Shengliang Guan 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

void dmGetMnodeLoads(SDnode *pDnode, SMonMloadInfo *pInfo) {
  SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, MNODE);
  if (pWrapper == NULL) {
    pInfo->isMnode = 0;
    return;
  }

  bool getFromAPI = !tsMultiProcess;
  if (getFromAPI) {
    mmGetMnodeLoads(pWrapper, pInfo);
  } else {
    SRpcMsg req = {.msgType = TDMT_MON_MM_LOAD};
    SRpcMsg rsp = {0};
    SEpSet  epset = {.inUse = 0, .numOfEps = 1};
    tstrncpy(epset.eps[0].fqdn, pDnode->data.localFqdn, TSDB_FQDN_LEN);
    epset.eps[0].port = tsServerPort;

    dmSendRecv(pDnode, &epset, &req, &rsp);
    if (rsp.code == 0 && rsp.contLen > 0) {
      tDeserializeSMonMloadInfo(rsp.pCont, rsp.contLen, pInfo);
    }
    rpcFreeCont(rsp.pCont);
  }
  dmReleaseWrapper(pWrapper);
}