Support Go agent side-by-side identity
This commit is contained in:
@@ -86,6 +86,21 @@ Run once against an existing config:
|
||||
./tmp/rmm-agent-go -config /etc/rmm-agent.conf -once
|
||||
```
|
||||
|
||||
For side-by-side testing, use a separate config and hostname suffix so the Go preview enrolls as a separate device:
|
||||
|
||||
```sh
|
||||
cp /etc/rmm-agent.conf /etc/rmm-agent-go.conf
|
||||
sed -i '/^DEVICE_ID=/d;/^DEVICE_TOKEN=/d' /etc/rmm-agent-go.conf
|
||||
cat >>/etc/rmm-agent-go.conf <<'EOF'
|
||||
HOSTNAME_SUFFIX="-go"
|
||||
LOCK_FILE="/tmp/rmm-agent-go.lock"
|
||||
SPOOL_DIR="/tmp/rmm-agent-go-results"
|
||||
BACKUP_DIR="/tmp/rmm-agent-go-backups"
|
||||
TUNNEL_STATE_DIR="/tmp/rmm-agent-go-tunnels"
|
||||
EOF
|
||||
./tmp/rmm-agent-go -config /etc/rmm-agent-go.conf -once
|
||||
```
|
||||
|
||||
## OpenWrt Package
|
||||
|
||||
Package skeleton:
|
||||
|
||||
@@ -35,6 +35,8 @@ type config struct {
|
||||
TunnelIdentity string
|
||||
TunnelStateDir string
|
||||
CheckTargets []string
|
||||
HostnameOverride string
|
||||
HostnameSuffix string
|
||||
ConfigFile string
|
||||
}
|
||||
|
||||
@@ -128,6 +130,8 @@ func loadConfig(path string) (config, error) {
|
||||
TunnelIdentity: envDefault("TUNNEL_IDENTITY_FILE", "/etc/rmm-agent/tunnel_key"),
|
||||
TunnelStateDir: envDefault("TUNNEL_STATE_DIR", "/tmp/rmm-agent-tunnels"),
|
||||
CheckTargets: splitWords(envDefault("CHECK_TARGETS", "1.1.1.1 8.8.8.8")),
|
||||
HostnameOverride: os.Getenv("HOSTNAME_OVERRIDE"),
|
||||
HostnameSuffix: os.Getenv("HOSTNAME_SUFFIX"),
|
||||
ConfigFile: path,
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
@@ -171,6 +175,12 @@ func loadConfig(path string) (config, error) {
|
||||
if value := values["CHECK_TARGETS"]; value != "" {
|
||||
cfg.CheckTargets = splitWords(value)
|
||||
}
|
||||
if value := values["HOSTNAME_OVERRIDE"]; value != "" {
|
||||
cfg.HostnameOverride = value
|
||||
}
|
||||
if value := values["HOSTNAME_SUFFIX"]; value != "" {
|
||||
cfg.HostnameSuffix = value
|
||||
}
|
||||
cfg.ServerURL = strings.TrimRight(cfg.ServerURL, "/")
|
||||
if cfg.IntervalSeconds <= 0 {
|
||||
cfg.IntervalSeconds = 30
|
||||
@@ -205,6 +215,12 @@ func saveConfig(cfg config) error {
|
||||
writeConfigLine(&b, "BACKUP_DIR", cfg.BackupDir)
|
||||
writeConfigLine(&b, "TUNNEL_IDENTITY_FILE", cfg.TunnelIdentity)
|
||||
writeConfigLine(&b, "TUNNEL_STATE_DIR", cfg.TunnelStateDir)
|
||||
if cfg.HostnameOverride != "" {
|
||||
writeConfigLine(&b, "HOSTNAME_OVERRIDE", cfg.HostnameOverride)
|
||||
}
|
||||
if cfg.HostnameSuffix != "" {
|
||||
writeConfigLine(&b, "HOSTNAME_SUFFIX", cfg.HostnameSuffix)
|
||||
}
|
||||
writeConfigLine(&b, "DEVICE_ID", cfg.DeviceID)
|
||||
writeConfigLine(&b, "DEVICE_TOKEN", cfg.DeviceToken)
|
||||
tmp := cfg.ConfigFile + ".tmp"
|
||||
@@ -228,7 +244,7 @@ func acquireLock(path string) (func(), error) {
|
||||
func enroll(ctx context.Context, client *http.Client, cfg *config) error {
|
||||
body := map[string]string{
|
||||
"enrollment_token": cfg.EnrollmentToken,
|
||||
"hostname": hostnameValue(),
|
||||
"hostname": cfg.displayHostname(),
|
||||
"openwrt_version": openwrtVersion(),
|
||||
}
|
||||
var resp enrollResponse
|
||||
@@ -249,7 +265,7 @@ func heartbeatOnce(ctx context.Context, client *http.Client, cfg config) error {
|
||||
}
|
||||
body := map[string]any{
|
||||
"device_id": cfg.DeviceID,
|
||||
"inventory": buildInventory(),
|
||||
"inventory": buildInventory(cfg),
|
||||
"metrics": buildMetrics(cfg.CheckTargets),
|
||||
}
|
||||
var resp heartbeatResponse
|
||||
@@ -262,9 +278,9 @@ func heartbeatOnce(ctx context.Context, client *http.Client, cfg config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildInventory() map[string]any {
|
||||
func buildInventory(cfg config) map[string]any {
|
||||
return map[string]any{
|
||||
"hostname": hostnameValue(),
|
||||
"hostname": cfg.displayHostname(),
|
||||
"openwrt_version": openwrtVersion(),
|
||||
"agent_version": agentVersion,
|
||||
"agent_runtime": "go",
|
||||
@@ -277,6 +293,13 @@ func buildInventory() map[string]any {
|
||||
}
|
||||
}
|
||||
|
||||
func (cfg config) displayHostname() string {
|
||||
if strings.TrimSpace(cfg.HostnameOverride) != "" {
|
||||
return strings.TrimSpace(cfg.HostnameOverride)
|
||||
}
|
||||
return hostnameValue() + strings.TrimSpace(cfg.HostnameSuffix)
|
||||
}
|
||||
|
||||
func buildMetrics(targets []string) map[string]any {
|
||||
return map[string]any{
|
||||
"system": jsonObjectOrEmpty(commandOutput("ubus", "call", "system", "info")),
|
||||
|
||||
Reference in New Issue
Block a user