From f26fa3da69340d69db865bd226e469fae0f25812 Mon Sep 17 00:00:00 2001 From: d3m0k1d Date: Sun, 5 Apr 2026 08:24:57 +0300 Subject: [PATCH] feat: add logif for checl alive func --- agent/internal/config/config.go | 7 +++-- agent/main.go | 50 +++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/agent/internal/config/config.go b/agent/internal/config/config.go index 32d3d90..e273c9b 100644 --- a/agent/internal/config/config.go +++ b/agent/internal/config/config.go @@ -8,9 +8,10 @@ import ( ) type ServiceConfig struct { - Name string `yaml:"name"` - Type string `yaml:"type"` - Path *string `yaml:"path"` + Name string `yaml:"name"` + Type string `yaml:"type"` + Path *string `yaml:"path"` + SystemdUnit *string `yaml:"systemd_unit"` // Optional: systemd unit name for health check } type AgentConfig struct { diff --git a/agent/main.go b/agent/main.go index b6b7d0f..b7f3c3d 100644 --- a/agent/main.go +++ b/agent/main.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "os" + "os/exec" "strings" "time" @@ -329,7 +330,6 @@ func reconnectStream( } // reportServices periodically sends service status updates to the backend via gRPC. -// For now, all configured services are reported as "up" every 5 seconds. func reportServices( ctx context.Context, grpcAddr string, @@ -352,9 +352,10 @@ func reportServices( for { svcUpdates := make([]*proto.ServicesUpdate_ServiceUpdate, 0, len(services)) for _, svc := range services { + status := checkServiceStatus(svc, lgr) svcUpdates = append(svcUpdates, &proto.ServicesUpdate_ServiceUpdate{ Name: svc.Name, - Status: "up", + Status: status, }) } @@ -377,6 +378,51 @@ func reportServices( } } +// checkServiceStatus checks if a service is alive based on its type. +func checkServiceStatus(svc config.ServiceConfig, lgr *logger.Logger) string { + // If systemd_unit is specified, check systemd first + if svc.SystemdUnit != nil && *svc.SystemdUnit != "" { + status := checkSystemdService(*svc.SystemdUnit) + if status != "up" { + lgr.Debug("Systemd service check", "unit", *svc.SystemdUnit, "status", status) + return status + } + } + + // For docker type, check container is running + if svc.Type == "docker" { + status := checkDockerContainer(svc.Name) + if status != "up" { + lgr.Debug("Docker container check", "container", svc.Name, "status", status) + return status + } + } + + return "up" +} + +// checkSystemdService checks if a systemd service is active. +func checkSystemdService(unit string) string { + cmd := exec.Command("systemctl", "is-active", "--quiet", unit) + if err := cmd.Run(); err != nil { + return "down" + } + return "up" +} + +// checkDockerContainer checks if a Docker container is running. +func checkDockerContainer(name string) string { + cmd := exec.Command("docker", "inspect", "-f", "{{.State.Running}}", name) + out, err := cmd.Output() + if err != nil { + return "down" + } + if strings.TrimSpace(string(out)) == "true" { + return "up" + } + return "down" +} + // reportSystemMetrics periodically collects and sends system metrics to the backend via gRPC. func reportSystemMetrics( ctx context.Context,