|
| 1 | +package providers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os/exec" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "observability-hub/internal/telemetry" |
| 11 | +) |
| 12 | + |
| 13 | +// CommandRunner defines the interface for executing shell commands. |
| 14 | +type CommandRunner interface { |
| 15 | + Run(ctx context.Context, name string, arg ...string) ([]byte, error) |
| 16 | +} |
| 17 | + |
| 18 | +// RealCommandRunner is the production implementation. |
| 19 | +type RealCommandRunner struct{} |
| 20 | + |
| 21 | +func (r *RealCommandRunner) Run(ctx context.Context, name string, arg ...string) ([]byte, error) { |
| 22 | + cmd := exec.CommandContext(ctx, name, arg...) |
| 23 | + return cmd.CombinedOutput() |
| 24 | +} |
| 25 | + |
| 26 | +// HubProvider provides tools for host-level introspection and platform status. |
| 27 | +type HubProvider struct { |
| 28 | + runner CommandRunner |
| 29 | + targetServices []string |
| 30 | +} |
| 31 | + |
| 32 | +// NewHubProvider creates a new HubProvider. |
| 33 | +func NewHubProvider() *HubProvider { |
| 34 | + return &HubProvider{ |
| 35 | + runner: &RealCommandRunner{}, |
| 36 | + targetServices: []string{ |
| 37 | + "ingestion.service", |
| 38 | + "proxy.service", |
| 39 | + "openbao.service", |
| 40 | + "tailscale-gate.service", |
| 41 | + }, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// HostResource represents physical resource usage on the host. |
| 46 | +type HostResource struct { |
| 47 | + CPUUsage string `json:"cpu_usage"` |
| 48 | + MemoryTotal string `json:"memory_total"` |
| 49 | + MemoryUsed string `json:"memory_used"` |
| 50 | + DiskUsage string `json:"disk_usage"` |
| 51 | + LoadAverage string `json:"load_average"` |
| 52 | +} |
| 53 | + |
| 54 | +// ServiceStatus represents the state of a systemd unit. |
| 55 | +type ServiceStatus struct { |
| 56 | + Name string `json:"name"` |
| 57 | + Active string `json:"active"` |
| 58 | + Sub string `json:"sub"` |
| 59 | + Since string `json:"since"` |
| 60 | +} |
| 61 | + |
| 62 | +// ListHostServices returns the status of target systemd services. |
| 63 | +func (p *HubProvider) ListHostServices(ctx context.Context) ([]ServiceStatus, error) { |
| 64 | + var statuses []ServiceStatus |
| 65 | + |
| 66 | + for _, svc := range p.targetServices { |
| 67 | + out, err := p.runner.Run(ctx, "systemctl", "show", svc, "--property=ActiveState,SubState,ActiveEnterTimestamp") |
| 68 | + if err != nil { |
| 69 | + telemetry.Warn("systemctl_show_failed", "service", svc, "error", err) |
| 70 | + continue |
| 71 | + } |
| 72 | + |
| 73 | + status := ServiceStatus{Name: svc} |
| 74 | + lines := strings.Split(string(out), "\n") |
| 75 | + for _, line := range lines { |
| 76 | + parts := strings.SplitN(line, "=", 2) |
| 77 | + if len(parts) < 2 { |
| 78 | + continue |
| 79 | + } |
| 80 | + val := strings.TrimSpace(parts[1]) |
| 81 | + switch parts[0] { |
| 82 | + case "ActiveState": |
| 83 | + status.Active = val |
| 84 | + case "SubState": |
| 85 | + status.Sub = val |
| 86 | + case "ActiveEnterTimestamp": |
| 87 | + status.Since = val |
| 88 | + } |
| 89 | + } |
| 90 | + statuses = append(statuses, status) |
| 91 | + } |
| 92 | + |
| 93 | + return statuses, nil |
| 94 | +} |
| 95 | + |
| 96 | +// QueryServiceLogs retrieves journal logs for a specific service since a relative time. |
| 97 | +func (p *HubProvider) QueryServiceLogs(ctx context.Context, service string, since string) (string, error) { |
| 98 | + if since == "" { |
| 99 | + since = "5m" |
| 100 | + } |
| 101 | + |
| 102 | + argSince := fmt.Sprintf("%s ago", since) |
| 103 | + out, err := p.runner.Run(ctx, "journalctl", "-u", service, "--since", argSince, "--no-pager", "-n", "50") |
| 104 | + if err != nil { |
| 105 | + return "", fmt.Errorf("failed to fetch logs for %s: %w", service, err) |
| 106 | + } |
| 107 | + |
| 108 | + return string(out), nil |
| 109 | +} |
| 110 | + |
| 111 | +// InspectHost retrieves physical resource statistics. |
| 112 | +func (p *HubProvider) InspectHost(ctx context.Context) (*HostResource, error) { |
| 113 | + res := &HostResource{} |
| 114 | + |
| 115 | + // Load Average |
| 116 | + loadOut, _ := p.runner.Run(ctx, "uptime") |
| 117 | + res.LoadAverage = strings.TrimSpace(string(loadOut)) |
| 118 | + |
| 119 | + // Memory (free -h) |
| 120 | + memOut, _ := p.runner.Run(ctx, "free", "-h") |
| 121 | + lines := strings.Split(string(memOut), "\n") |
| 122 | + if len(lines) > 1 { |
| 123 | + fields := strings.Fields(lines[1]) // Mem row |
| 124 | + if len(fields) > 2 { |
| 125 | + res.MemoryTotal = fields[1] |
| 126 | + res.MemoryUsed = fields[2] |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Disk (df -h /) |
| 131 | + diskOut, _ := p.runner.Run(ctx, "df", "-h", "/") |
| 132 | + dLines := strings.Split(string(diskOut), "\n") |
| 133 | + if len(dLines) > 1 { |
| 134 | + dFields := strings.Fields(dLines[1]) |
| 135 | + if len(dFields) > 4 { |
| 136 | + res.DiskUsage = dFields[4] |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return res, nil |
| 141 | +} |
| 142 | + |
| 143 | +// InspectPlatform returns an executive summary of the entire hub. |
| 144 | +func (p *HubProvider) InspectPlatform(ctx context.Context) (map[string]interface{}, error) { |
| 145 | + summary := make(map[string]interface{}) |
| 146 | + summary["timestamp"] = time.Now().Format(time.RFC3339) |
| 147 | + summary["node"] = "server2" |
| 148 | + |
| 149 | + if _, err := p.runner.Run(ctx, "kubectl", "get", "nodes"); err != nil { |
| 150 | + summary["k3s_status"] = "unreachable" |
| 151 | + } else { |
| 152 | + summary["k3s_status"] = "healthy" |
| 153 | + } |
| 154 | + |
| 155 | + services, _ := p.ListHostServices(ctx) |
| 156 | + runningCount := 0 |
| 157 | + for _, s := range services { |
| 158 | + if s.Active == "active" { |
| 159 | + runningCount++ |
| 160 | + } |
| 161 | + } |
| 162 | + summary["host_services_running"] = fmt.Sprintf("%d/%d", runningCount, len(p.targetServices)) |
| 163 | + |
| 164 | + return summary, nil |
| 165 | +} |
0 commit comments