-
Notifications
You must be signed in to change notification settings - Fork 393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support host monitor #1890
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,11 @@ | |
#include "MachineInfoUtil.h" | ||
|
||
#include <string.h> | ||
|
||
#include <boost/filesystem.hpp> | ||
|
||
#include "AppConfig.h" | ||
#include "common/UUIDUtil.h" | ||
#if defined(__linux__) | ||
#include <arpa/inet.h> | ||
#include <ifaddrs.h> | ||
|
@@ -40,8 +45,10 @@ | |
|
||
#include "FileSystemUtil.h" | ||
#include "StringTools.h" | ||
#include "common/FileSystemUtil.h" | ||
#include "logger/Logger.h" | ||
|
||
DEFINE_FLAG_STRING(agent_host_id, "", ""); | ||
|
||
#if defined(_MSC_VER) | ||
typedef LONG NTSTATUS, *PNTSTATUS; | ||
|
@@ -496,6 +503,92 @@ size_t FetchECSMetaCallback(char* buffer, size_t size, size_t nmemb, std::string | |
return sizes; | ||
} | ||
|
||
std::string GetSerialNumberFromEcsAssist(const std::string& machineIdFile) { | ||
std::string sn; | ||
if (CheckExistance(machineIdFile)) { | ||
if (!ReadFileContent(machineIdFile, sn)) { | ||
return ""; | ||
} | ||
} | ||
return sn; | ||
} | ||
|
||
static std::string GetEcsAssistMachineIdFile() { | ||
#if defined(WIN32) | ||
return "C:\\ProgramData\\aliyun\\assist\\hybrid\\machine-id"; | ||
#else | ||
return "/usr/local/share/aliyun-assist/hybrid/machine-id"; | ||
#endif | ||
} | ||
|
||
std::string GetSerialNumberFromEcsAssist() { | ||
return GetSerialNumberFromEcsAssist(GetEcsAssistMachineIdFile()); | ||
} | ||
|
||
std::string RandomHostid() { | ||
static std::string hostId = CalculateRandomUUID(); | ||
return hostId; | ||
} | ||
|
||
const std::string& GetLocalHostId() { | ||
static std::string fileName = AppConfig::GetInstance()->GetLoongcollectorConfDir() + PATH_SEPARATOR + "host_id"; | ||
static std::string hostId; | ||
if (!hostId.empty()) { | ||
return hostId; | ||
} | ||
if (CheckExistance(fileName)) { | ||
if (!ReadFileContent(fileName, hostId)) { | ||
hostId = ""; | ||
} | ||
} | ||
if (hostId.empty()) { | ||
hostId = RandomHostid(); | ||
|
||
LOG_INFO(sLogger, ("save hostId file to local file system, hostId", hostId)); | ||
int fd = open(fileName.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0755); | ||
if (fd == -1) { | ||
int savedErrno = errno; | ||
if (savedErrno != EEXIST) { | ||
LOG_ERROR(sLogger, ("save hostId file fail", fileName)("errno", strerror(savedErrno))); | ||
} | ||
} else { | ||
// 文件成功创建,现在写入hostId | ||
ssize_t written = write(fd, hostId.c_str(), hostId.length()); | ||
if (written == static_cast<ssize_t>(hostId.length())) { | ||
LOG_INFO(sLogger, ("hostId saved successfully to", fileName)); | ||
} else { | ||
int writeErrno = errno; | ||
LOG_ERROR(sLogger, ("Failed to write hostId to file", fileName)("errno", strerror(writeErrno))); | ||
} | ||
close(fd); | ||
} | ||
} | ||
return hostId; | ||
} | ||
|
||
std::string FetchHostId() { | ||
static std::string hostId; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (!hostId.empty()) { | ||
return hostId; | ||
} | ||
hostId = STRING_FLAG(agent_host_id); | ||
if (!hostId.empty()) { | ||
return hostId; | ||
} | ||
ECSMeta meta = FetchECSMeta(); | ||
hostId = meta.instanceID; | ||
if (!hostId.empty()) { | ||
return hostId; | ||
} | ||
hostId = GetSerialNumberFromEcsAssist(); | ||
if (!hostId.empty()) { | ||
return hostId; | ||
} | ||
hostId = GetLocalHostId(); | ||
|
||
return hostId; | ||
} | ||
|
||
ECSMeta FetchECSMeta() { | ||
CURL* curl; | ||
for (size_t retryTimes = 1; retryTimes <= 5; retryTimes++) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,11 +34,21 @@ struct TimerEventCompare { | |
|
||
class Timer { | ||
public: | ||
Timer(const Timer&) = delete; | ||
Timer(Timer&&) = delete; | ||
Timer& operator=(const Timer&) = delete; | ||
Timer& operator=(Timer&&) = delete; | ||
~Timer() = default; | ||
static Timer* GetInstance() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 改成单例,之前prometheus等使用的地方没有问题? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 改完同步下Prometheus,测试 |
||
static Timer sInstance; | ||
return &sInstance; | ||
} | ||
void Init(); | ||
void Stop(); | ||
void PushEvent(std::unique_ptr<TimerEvent>&& e); | ||
|
||
private: | ||
Timer() = default; | ||
void Run(); | ||
|
||
mutable std::mutex mQueueMux; | ||
|
@@ -53,6 +63,7 @@ class Timer { | |
#ifdef APSARA_UNIT_TEST_MAIN | ||
friend class TimerUnittest; | ||
friend class ScrapeSchedulerUnittest; | ||
friend class HostMonitorInputRunnerUnittest; | ||
#endif | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2024 iLogtail Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "EntityConstants.h" | ||
|
||
namespace logtail { | ||
|
||
const std::string DEFAULT_ENV_KEY_HOST_TYPE = "HOST_TYPE"; | ||
const std::string DEFAULT_ENV_VALUE_ECS = "ecs"; | ||
const std::string DEFAULT_ENV_VALUE_HOST = "host"; | ||
const std::string DEFAULT_CONTENT_KEY_ENTITY_TYPE = "__entity_type__"; | ||
const std::string DEFAULT_CONTENT_KEY_ENTITY_ID = "__entity_id__"; | ||
const std::string DEFAULT_CONTENT_KEY_DOMAIN = "__domain__"; | ||
const std::string DEFAULT_CONTENT_VALUE_DOMAIN_ACS = "acs"; | ||
const std::string DEFAULT_CONTENT_VALUE_DOMAIN_INFRA = "infra"; | ||
const std::string DEFAULT_CONTENT_KEY_FIRST_OBSERVED_TIME = "__first_observed_time__"; | ||
const std::string DEFAULT_CONTENT_KEY_LAST_OBSERVED_TIME = "__last_observed_time__"; | ||
const std::string DEFAULT_CONTENT_KEY_KEEP_ALIVE_SECONDS = "__keep_alive_seconds__"; | ||
const std::string DEFAULT_CONTENT_KEY_METHOD = "__method__"; | ||
const std::string DEFAULT_CONTENT_VALUE_METHOD_UPDATE = "update"; | ||
const std::string DEFAULT_CONTENT_VALUE_METHOD_EXPIRE = "expire"; | ||
|
||
// for process entity | ||
const std::string DEFAULT_CONTENT_VALUE_ENTITY_TYPE_PROCESS = "process"; | ||
const std::string DEFAULT_CONTENT_KEY_PROCESS_PID = "process_pid"; | ||
Abingcbc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const std::string DEFAULT_CONTENT_KEY_PROCESS_PPID = "process_ppid"; | ||
const std::string DEFAULT_CONTENT_KEY_PROCESS_USER = "process_user"; | ||
const std::string DEFAULT_CONTENT_KEY_PROCESS_COMM = "process_comm"; | ||
const std::string DEFAULT_CONTENT_KEY_PROCESS_CREATE_TIME = "process_create_time"; | ||
const std::string DEFAULT_CONTENT_KEY_PROCESS_CWD = "process_cwd"; | ||
const std::string DEFAULT_CONTENT_KEY_PROCESS_BINARY = "process_binary"; | ||
const std::string DEFAULT_CONTENT_KEY_PROCESS_ARGUMENTS = "process_arguments"; | ||
const std::string DEFAULT_CONTENT_KEY_PROCESS_LANGUAGE = "process_language"; | ||
const std::string DEFAULT_CONTENT_KEY_PROCESS_CONTAINER_ID = "process_container_id"; | ||
} // namespace logtail |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
没必要了