Skip to content

Commit

Permalink
add UpdateHostId
Browse files Browse the repository at this point in the history
  • Loading branch information
quzard committed Dec 31, 2024
1 parent d711a65 commit 98c02d7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
19 changes: 18 additions & 1 deletion core/common/MachineInfoUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,30 +551,47 @@ const std::string& HostIdentifier::GetLocalHostId() {
}

HostIdentifier::HostIdentifier() {
mMetadata = GetECSMetaFromFile();
UpdateHostId();
}

void HostIdentifier::UpdateHostId() {
std::string hostId;
Type type;
mMetadata = GetECSMetaFromFile();
hostId = STRING_FLAG(agent_host_id);
if (!hostId.empty()) {
type = Type::CUSTOM;
mHostid = Hostid{hostId, type};
SetHostId(mHostid);
return;
}
hostId = mMetadata.instanceID;
if (!hostId.empty()) {
type = Type::ECS;
mHostid = Hostid{hostId, type};
SetHostId(mHostid);
return;
}
hostId = GetSerialNumberFromEcsAssist();
if (!hostId.empty()) {
type = Type::ECS_ASSIST;
mHostid = Hostid{hostId, type};
SetHostId(mHostid);
return;
}
hostId = GetLocalHostId();
type = Type::LOCAL;
mHostid = Hostid{hostId, type};
SetHostId(mHostid);
}

void HostIdentifier::SetHostId(const Hostid& hostid) {
std::lock_guard<std::mutex> lock(mMutex);
if (mHostid.id == hostid.id && mHostid.type == hostid.type) {
return;
}
LOG_INFO(sLogger, ("change hostId, from", mHostid)("to", hostid));
mHostid = hostid;
}

bool ParseECSMeta(const std::string& meta, ECSMeta& metaObj) {
Expand Down
6 changes: 2 additions & 4 deletions core/common/MachineInfoUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ class HostIdentifier {
std::lock_guard<std::mutex> lock(mMutex);
mMetadata = meta;
}
void SetHostId(const Hostid& hostid) {
std::lock_guard<std::mutex> lock(mMutex);
mHostid = hostid;
}
void UpdateHostId();

HostIdentifier();
static HostIdentifier* Instance() {
Expand Down Expand Up @@ -108,6 +105,7 @@ class HostIdentifier {
}
const std::string& GetLocalHostId();

void SetHostId(const Hostid& hostid);
ECSMeta GetECSMetaFromFile();
};

Expand Down

0 comments on commit 98c02d7

Please sign in to comment.