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 17 18 19 20 21 22 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
/*
 * 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
#include "dmMgmt.h"
#include "dmNodes.h"

#define dmSendLocalRecv(pDnode, mtype, func, pInfo)         \
  SRpcMsg rsp = {0};                                        \
  SRpcMsg req = {.msgType = mtype};                         \
  SEpSet  epset = {.inUse = 0, .numOfEps = 1};              \
  tstrncpy(epset.eps[0].fqdn, tsLocalFqdn, TSDB_FQDN_LEN);  \
  epset.eps[0].port = tsServerPort;                         \
  rpcSendRecv(pDnode->trans.clientRpc, &epset, &req, &rsp); \
  if (rsp.code == 0 && rsp.contLen > 0) {                   \
    func(rsp.pCont, rsp.contLen, pInfo);                    \
  }                                                         \
  rpcFreeCont(rsp.pCont);

static void dmGetMonitorBasicInfo(SDnode *pDnode, SMonBasicInfo *pInfo) {
  pInfo->protocol = 1;
  pInfo->dnode_id = pDnode->data.dnodeId;
  pInfo->cluster_id = pDnode->data.clusterId;
  tstrncpy(pInfo->dnode_ep, tsLocalEp, TSDB_EP_LEN);
}

static void dmGetMonitorDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) {
  pInfo->uptime = (taosGetTimestampMs() - pDnode->data.rebootTime) / (86400000.0f);
  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;
  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 dmGetDmMonitorInfo(SDnode *pDnode) {
  SMonDmInfo dmInfo = {0};
  dmGetMonitorBasicInfo(pDnode, &dmInfo.basic);
  dmGetMonitorDnodeInfo(pDnode, &dmInfo.dnode);
  dmGetMonitorSystemInfo(&dmInfo.sys);
  monSetDmInfo(&dmInfo);
}

static void dmGetMmMonitorInfo(SDnode *pDnode) {
  SMonMmInfo    mmInfo = {0};
  SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, MNODE);
  if (pWrapper != NULL && pWrapper->required) {
    if (tsMultiProcess) {
      dmSendLocalRecv(pDnode, TDMT_MON_MM_INFO, tDeserializeSMonMmInfo, &mmInfo);
    } else if (pWrapper->pMgmt != NULL) {
      mmGetMonitorInfo(pWrapper->pMgmt, &mmInfo);
    }
  }
  dmReleaseWrapper(pWrapper);
  monSetMmInfo(&mmInfo);
  tFreeSMonMmInfo(&mmInfo);
}

static void dmGetVmMonitorInfo(SDnode *pDnode) {
  SMonVmInfo    vmInfo = {0};
  SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, VNODE);
  if (pWrapper != NULL && pWrapper->required) {
    if (tsMultiProcess) {
      dmSendLocalRecv(pDnode, TDMT_MON_VM_INFO, tDeserializeSMonVmInfo, &vmInfo);
    } else if (pWrapper->pMgmt != NULL) {
      vmGetMonitorInfo(pWrapper->pMgmt, &vmInfo);
    }
  }
  dmReleaseWrapper(pWrapper);
  monSetVmInfo(&vmInfo);
  tFreeSMonVmInfo(&vmInfo);
}

static void dmGetQmMonitorInfo(SDnode *pDnode) {
  SMonQmInfo    qmInfo = {0};
  SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, VNODE);
  if (pWrapper != NULL && pWrapper->required) {
    if (tsMultiProcess) {
      dmSendLocalRecv(pDnode, TDMT_MON_QM_INFO, tDeserializeSMonQmInfo, &qmInfo);
    } else if (pWrapper->pMgmt != NULL) {
      qmGetMonitorInfo(pWrapper->pMgmt, &qmInfo);
    }
  }
  dmReleaseWrapper(pWrapper);
  monSetQmInfo(&qmInfo);
  tFreeSMonQmInfo(&qmInfo);
}

static void dmGetSmMonitorInfo(SDnode *pDnode) {
  SMonSmInfo    smInfo = {0};
  SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, VNODE);
  if (pWrapper != NULL && pWrapper->required) {
    if (tsMultiProcess) {
      dmSendLocalRecv(pDnode, TDMT_MON_SM_INFO, tDeserializeSMonSmInfo, &smInfo);
    } else if (pWrapper->pMgmt != NULL) {
      smGetMonitorInfo(pWrapper->pMgmt, &smInfo);
    }
  }
  dmReleaseWrapper(pWrapper);
  monSetSmInfo(&smInfo);
  tFreeSMonSmInfo(&smInfo);
}

static void dmGetBmMonitorInfo(SDnode *pDnode) {
  SMonBmInfo    bmInfo = {0};
  SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, VNODE);
  if (pWrapper != NULL && pWrapper->required) {
    if (tsMultiProcess) {
      dmSendLocalRecv(pDnode, TDMT_MON_BM_INFO, tDeserializeSMonBmInfo, &bmInfo);
    } else if (pWrapper->pMgmt != NULL) {
      bmGetMonitorInfo(pWrapper->pMgmt, &bmInfo);
    }
  }
  dmReleaseWrapper(pWrapper);
  monSetBmInfo(&bmInfo);
  tFreeSMonBmInfo(&bmInfo);
}

void dmSendMonitorReport() {
  if (!tsEnableMonitor || tsMonitorFqdn[0] == 0 || tsMonitorPort == 0) return;
  dTrace("send monitor report to %s:%u", tsMonitorFqdn, tsMonitorPort);

  SDnode *pDnode = dmInstance();
  dmGetDmMonitorInfo(pDnode);
  dmGetMmMonitorInfo(pDnode);
  dmGetVmMonitorInfo(pDnode);
  dmGetQmMonitorInfo(pDnode);
  dmGetSmMonitorInfo(pDnode);
  dmGetBmMonitorInfo(pDnode);
  monSendReport();
}

void dmGetVnodeLoads(SMonVloadInfo *pInfo) {
  SDnode       *pDnode = dmInstance();
  SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, VNODE);
  if (pWrapper != NULL && pWrapper->required) {
    if (tsMultiProcess) {
      dmSendLocalRecv(pDnode, TDMT_MON_VM_LOAD, tDeserializeSMonVloadInfo, pInfo);
    } else if (pWrapper->pMgmt != NULL) {
      vmGetVnodeLoads(pWrapper->pMgmt, pInfo);
    }
  }
  dmReleaseWrapper(pWrapper);
}

void dmGetMnodeLoads(SMonMloadInfo *pInfo) {
  SDnode       *pDnode = dmInstance();
  SMgmtWrapper *pWrapper = dmAcquireWrapper(pDnode, MNODE);
  if (pWrapper != NULL && pWrapper->required) {
    if (tsMultiProcess) {
      dmSendLocalRecv(pDnode, TDMT_MON_MM_LOAD, tDeserializeSMonMloadInfo, pInfo);
    } else if (pWrapper->pMgmt != NULL) {
      mmGetMnodeLoads(pWrapper->pMgmt, pInfo);
    }
  }
  dmReleaseWrapper(pWrapper);
}