From 0de8e2d81899ee719e3726beb74031120bbb0295 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Tue, 2 Jul 2024 13:04:02 +0100 Subject: [PATCH] chore: Add hostname to server monitor report This commit adds the hostname field to the server monitor report in order to include the hostname of the server in the collected metrics. This information is useful for identifying and distinguishing between different servers in the monitoring system. --- InfrastructureAgent/README.md | 38 +++++++++++++++---- InfrastructureAgent/agent.go | 1 + .../model/server_monitor_report.go | 1 + InfrastructureAgent/utils/hostname.go | 16 ++++++++ 4 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 InfrastructureAgent/utils/hostname.go diff --git a/InfrastructureAgent/README.md b/InfrastructureAgent/README.md index 48e7747212..0b3a4af693 100644 --- a/InfrastructureAgent/README.md +++ b/InfrastructureAgent/README.md @@ -2,13 +2,13 @@ The OneUptime Infrastructure Agent is a lightweight, open-source agent that collects system metrics and sends them to the OneUptime platform. It is designed to be easy to install and use, and to be extensible. -## Installation +### Installation ``` curl -s https://oneuptime.com/docs/static/scripts/infrastructure-agent/install.sh | bash ``` -## Configure the agent +### Configure the agent Configure the agent as a system service - You can change the host to your own host if you're self hosting the OneUptime platform. @@ -18,7 +18,7 @@ Configure the agent as a system service oneuptime-infrastructure-agent configure --secret-key=YOUR_SECRET_KEY --oneuptime-url=https://oneuptime.com ``` -## Starting the agent +### Starting the agent ``` oneuptime-infrastructure-agent start @@ -26,26 +26,48 @@ oneuptime-infrastructure-agent start Once its up and running you should see the metrics on the OneUptime Dashboard. -## Stopping the agent +### Stopping the agent ``` oneuptime-infrastructure-agent stop ``` -## Restarting the agent +### Restarting the agent ``` oneuptime-infrastructure-agent restart ``` -## Uninstalling the agent +### Uninstalling the agent ``` oneuptime-infrastructure-agent uninstall && rm -rf /usr/bin/oneuptime-infrastructure-agent ``` -## Supported Platforms +### Supported Platforms - Linux - MacOS -- Windows \ No newline at end of file +- Windows + +## Development + +This section is for developers who want to contribute to the agent. The agent is written in Go. + +### Building the agent + +```bash +go build +``` + +### Configure the agent + +```bash +sudo ./oneuptime-infrastructure-agent configure --secret-key=YOUR_SECRET_KEY --oneuptime-url=https://localhost +``` + +### Starting the agent + +```bash +sudo ./oneuptime-infrastructure-agent start +``` \ No newline at end of file diff --git a/InfrastructureAgent/agent.go b/InfrastructureAgent/agent.go index f8845bcaf2..1bf6a56641 100644 --- a/InfrastructureAgent/agent.go +++ b/InfrastructureAgent/agent.go @@ -110,6 +110,7 @@ func collectMetricsJob(secretKey string, oneuptimeURL string) { RequestReceivedAt: time.Now().UTC().Format("2006-01-02T15:04:05.000Z"), OnlyCheckRequestReceivedAt: false, Processes: servProcesses, + Hostname: utils.GetHostname(), } reqData := struct { diff --git a/InfrastructureAgent/model/server_monitor_report.go b/InfrastructureAgent/model/server_monitor_report.go index 47a57909b8..8d9c958c19 100644 --- a/InfrastructureAgent/model/server_monitor_report.go +++ b/InfrastructureAgent/model/server_monitor_report.go @@ -6,4 +6,5 @@ type ServerMonitorReport struct { RequestReceivedAt string `json:"requestReceivedAt"` OnlyCheckRequestReceivedAt bool `json:"onlyCheckRequestReceivedAt"` Processes []*ServerProcess `json:"processes"` + Hostname string `json:"hostname"` } diff --git a/InfrastructureAgent/utils/hostname.go b/InfrastructureAgent/utils/hostname.go new file mode 100644 index 0000000000..ee694ae3c7 --- /dev/null +++ b/InfrastructureAgent/utils/hostname.go @@ -0,0 +1,16 @@ +package utils + +import ( + "log/slog" + "os" +) + +func GetHostname() string { + hostname, err := os.Hostname() + if err != nil { + slog.Error("Failed to fetch hostname", err) + return "" + } + + return hostname +}