This commit is contained in:
@@ -8,9 +8,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ServiceConfig struct {
|
type ServiceConfig struct {
|
||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
Type string `yaml:"type"`
|
Type string `yaml:"type"`
|
||||||
Path *string `yaml:"path"`
|
Path *string `yaml:"path"`
|
||||||
|
SystemdUnit *string `yaml:"systemd_unit"` // Optional: systemd unit name for health check
|
||||||
}
|
}
|
||||||
|
|
||||||
type AgentConfig struct {
|
type AgentConfig struct {
|
||||||
|
|||||||
+48
-2
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -329,7 +330,6 @@ func reconnectStream(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// reportServices periodically sends service status updates to the backend via gRPC.
|
// 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(
|
func reportServices(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
grpcAddr string,
|
grpcAddr string,
|
||||||
@@ -352,9 +352,10 @@ func reportServices(
|
|||||||
for {
|
for {
|
||||||
svcUpdates := make([]*proto.ServicesUpdate_ServiceUpdate, 0, len(services))
|
svcUpdates := make([]*proto.ServicesUpdate_ServiceUpdate, 0, len(services))
|
||||||
for _, svc := range services {
|
for _, svc := range services {
|
||||||
|
status := checkServiceStatus(svc, lgr)
|
||||||
svcUpdates = append(svcUpdates, &proto.ServicesUpdate_ServiceUpdate{
|
svcUpdates = append(svcUpdates, &proto.ServicesUpdate_ServiceUpdate{
|
||||||
Name: svc.Name,
|
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.
|
// reportSystemMetrics periodically collects and sends system metrics to the backend via gRPC.
|
||||||
func reportSystemMetrics(
|
func reportSystemMetrics(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
|
|||||||
Reference in New Issue
Block a user