diff --git a/include/util/thttp.h b/include/util/thttp.h new file mode 100644 index 0000000000000000000000000000000000000000..f211b2615d8d166ad82bf0125655a7efdb58ff94 --- /dev/null +++ b/include/util/thttp.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_UTIL_HTTP_H_ +#define _TD_UTIL_HTTP_H_ + +#include "os.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t taosSendHttpReport(const char* server, uint16_t port, const char* pCont, int32_t contLen); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_UTIL_UTIL_H_*/ diff --git a/source/dnode/mnode/impl/src/mndTelem.c b/source/dnode/mnode/impl/src/mndTelem.c index 744cefcb4f0d8147cbfdb1940526e22e0d93ab47..1cd7593846716f54b7cbb491dc7e207280821c80 100644 --- a/source/dnode/mnode/impl/src/mndTelem.c +++ b/source/dnode/mnode/impl/src/mndTelem.c @@ -19,8 +19,9 @@ #include "mndSync.h" #include "tbuffer.h" #include "tjson.h" +#include "thttp.h" -#define TELEMETRY_SERVER "localhost" +#define TELEMETRY_SERVER "telemetry.taosdata.com" #define TELEMETRY_PORT 80 static void mndBuildRuntimeInfo(SMnode* pMnode, SJson* pJson) { @@ -81,62 +82,6 @@ static char* mndBuildTelemetryReport(SMnode* pMnode) { return pCont; } -static void mndSendTelemetryReport(const char* pCont) { - int32_t code = -1; - char buf[128] = {0}; - SOCKET fd = 0; - int32_t contLen = strlen(pCont); - - uint32_t ip = taosGetIpv4FromFqdn(TELEMETRY_SERVER); - if (ip == 0xffffffff) { - mError("failed to get telemetry server ip"); - goto SEND_OVER; - } - - fd = taosOpenTcpClientSocket(ip, TELEMETRY_PORT, 0); - if (fd < 0) { - mError("failed to create telemetry socket"); - goto SEND_OVER; - } - - const char* header = - "POST /report HTTP/1.1\n" - "Host: " TELEMETRY_SERVER - "\n" - "Content-Type: application/json\n" - "Content-Length: "; - if (taosWriteSocket(fd, (void*)header, (int32_t)strlen(header)) < 0) { - mError("failed to send telemetry header"); - goto SEND_OVER; - } - - snprintf(buf, sizeof(buf), "%d\n\n", contLen); - if (taosWriteSocket(fd, buf, (int32_t)strlen(buf)) < 0) { - mError("failed to send telemetry contlen"); - goto SEND_OVER; - } - - if (taosWriteSocket(fd, (void*)pCont, contLen) < 0) { - mError("failed to send telemetry content"); - goto SEND_OVER; - } - - // read something to avoid nginx error 499 - if (taosReadSocket(fd, buf, 10) < 0) { - mError("failed to receive telemetry response"); - goto SEND_OVER; - } - - mInfo("send telemetry to %s:%d, len:%d content: %s", TELEMETRY_SERVER, TELEMETRY_PORT, contLen, pCont); - code = 0; - -SEND_OVER: - if (code != 0) { - mError("failed to send telemetry to %s:%d since %s", TELEMETRY_SERVER, TELEMETRY_PORT, terrstr()); - } - taosCloseSocket(fd); -} - static int32_t mndProcessTelemTimer(SMnodeMsg* pReq) { SMnode* pMnode = pReq->pMnode; STelemMgmt* pMgmt = &pMnode->telemMgmt; @@ -145,7 +90,7 @@ static int32_t mndProcessTelemTimer(SMnodeMsg* pReq) { taosWLockLatch(&pMgmt->lock); char* pCont = mndBuildTelemetryReport(pMnode); if (pCont != NULL) { - mndSendTelemetryReport(pCont); + taosSendHttpReport(TELEMETRY_SERVER, TELEMETRY_PORT, pCont, strlen(pCont)); free(pCont); } taosWUnLockLatch(&pMgmt->lock); diff --git a/source/dnode/mnode/impl/src/mnode.c b/source/dnode/mnode/impl/src/mnode.c index 879f8662149f87a0f15185188902f2eec605c005..ab5d0d794b6f0416056f418dbc83d104f7362d69 100644 --- a/source/dnode/mnode/impl/src/mnode.c +++ b/source/dnode/mnode/impl/src/mnode.c @@ -132,7 +132,7 @@ static int32_t mndInitTimer(SMnode *pMnode) { return -1; } - if (taosTmrReset(mndPullupTelem, 300, pMnode, pMnode->timer, &pMnode->telemTimer)) { + if (taosTmrReset(mndPullupTelem, 60000, pMnode, pMnode->timer, &pMnode->telemTimer)) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } diff --git a/source/util/src/thttp.c b/source/util/src/thttp.c new file mode 100644 index 0000000000000000000000000000000000000000..14c39d3f03f6a59e921de174abe4425bec6f7441 --- /dev/null +++ b/source/util/src/thttp.c @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "thttp.h" +#include "taoserror.h" +#include "tlog.h" + +int32_t taosSendHttpReport(const char* server, uint16_t port, const char* pCont, int32_t contLen) { + int32_t code = -1; + SOCKET fd = 0; + + uint32_t ip = taosGetIpv4FromFqdn(server); + if (ip == 0xffffffff) { + terrno = TAOS_SYSTEM_ERROR(errno); + uError("failed to get http server:%s ip since %s", server, terrstr()); + goto SEND_OVER; + } + + fd = taosOpenTcpClientSocket(ip, port, 0); + if (fd < 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + uError("failed to create http socket since %s", terrstr()); + goto SEND_OVER; + } + + char header[4096] = {0}; + int32_t headLen = snprintf(header, sizeof(header), + "POST /report HTTP/1.1\n" + "Host: %s\n" + "Content-Type: application/json\n" + "Content-Length: %d\n\n", + server, contLen); + + if (taosWriteSocket(fd, (void*)header, headLen) < 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + uError("failed to send http header since %s", terrstr()); + goto SEND_OVER; + } + + if (taosWriteSocket(fd, (void*)pCont, contLen) < 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + uError("failed to send http content since %s", terrstr()); + goto SEND_OVER; + } + + // read something to avoid nginx error 499 + if (taosReadSocket(fd, header, 10) < 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + uError("failed to receive response since %s", terrstr()); + goto SEND_OVER; + } + + uInfo("send http to %s:%d, len:%d content: %s", server, port, contLen, pCont); + code = 0; + +SEND_OVER: + if (fd != 0) { + taosCloseSocket(fd); + } + + return code; +}