|
| 1 | +package health |
| 2 | + |
| 3 | +import ( |
| 4 | + "os/exec" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/shirou/gopsutil/v3/cpu" |
| 10 | + "github.com/shirou/gopsutil/v3/disk" |
| 11 | + "github.com/shirou/gopsutil/v3/mem" |
| 12 | +) |
| 13 | + |
| 14 | +type SystemStats struct { |
| 15 | + CpuUsagePercent float64 `json:"cpuUsagePercent"` |
| 16 | + MemoryUsagePercent float64 `json:"memoryUsagePercent"` |
| 17 | + MemoryUsedMb int `json:"memoryUsedMb"` |
| 18 | + DiskUsagePercent float64 `json:"diskUsagePercent"` |
| 19 | + DiskUsedGb int `json:"diskUsedGb"` |
| 20 | +} |
| 21 | + |
| 22 | +type NetworkPeerHealth struct { |
| 23 | + ID string `json:"id"` |
| 24 | + LastSeenSecs int `json:"lastSeenSecs"` |
| 25 | + Reachable bool `json:"reachable"` |
| 26 | +} |
| 27 | + |
| 28 | +type NetworkHealth struct { |
| 29 | + TunnelUp bool `json:"tunnelUp"` |
| 30 | + PeerCount int `json:"peerCount"` |
| 31 | + Peers []NetworkPeerHealth `json:"peers"` |
| 32 | +} |
| 33 | + |
| 34 | +type ContainerHealth struct { |
| 35 | + RuntimeResponsive bool `json:"runtimeResponsive"` |
| 36 | + RunningContainers int `json:"runningContainers"` |
| 37 | + StoppedContainers int `json:"stoppedContainers"` |
| 38 | + StorageUsedGb float64 `json:"storageUsedGb"` |
| 39 | +} |
| 40 | + |
| 41 | +type AgentHealthInfo struct { |
| 42 | + Version string `json:"version"` |
| 43 | + UptimeSecs int64 `json:"uptimeSecs"` |
| 44 | + LastSyncSuccess bool `json:"lastSyncSuccess"` |
| 45 | + LastSyncAt string `json:"lastSyncAt"` |
| 46 | +} |
| 47 | + |
| 48 | +func CollectSystemStats() *SystemStats { |
| 49 | + stats := &SystemStats{} |
| 50 | + |
| 51 | + cpuPercent, err := cpu.Percent(time.Second, false) |
| 52 | + if err == nil && len(cpuPercent) > 0 { |
| 53 | + stats.CpuUsagePercent = cpuPercent[0] |
| 54 | + } |
| 55 | + |
| 56 | + memInfo, err := mem.VirtualMemory() |
| 57 | + if err == nil { |
| 58 | + stats.MemoryUsagePercent = memInfo.UsedPercent |
| 59 | + stats.MemoryUsedMb = int(memInfo.Used / 1024 / 1024) |
| 60 | + } |
| 61 | + |
| 62 | + diskInfo, err := disk.Usage("/") |
| 63 | + if err == nil { |
| 64 | + stats.DiskUsagePercent = diskInfo.UsedPercent |
| 65 | + stats.DiskUsedGb = int(diskInfo.Used / 1024 / 1024 / 1024) |
| 66 | + } |
| 67 | + |
| 68 | + return stats |
| 69 | +} |
| 70 | + |
| 71 | +func CollectNetworkHealth(interfaceName string) *NetworkHealth { |
| 72 | + health := &NetworkHealth{ |
| 73 | + TunnelUp: false, |
| 74 | + Peers: []NetworkPeerHealth{}, |
| 75 | + } |
| 76 | + |
| 77 | + cmd := exec.Command("wg", "show", interfaceName, "dump") |
| 78 | + output, err := cmd.CombinedOutput() |
| 79 | + if err != nil { |
| 80 | + return health |
| 81 | + } |
| 82 | + |
| 83 | + health.TunnelUp = true |
| 84 | + |
| 85 | + lines := strings.Split(strings.TrimSpace(string(output)), "\n") |
| 86 | + if len(lines) < 1 { |
| 87 | + return health |
| 88 | + } |
| 89 | + |
| 90 | + for i, line := range lines { |
| 91 | + if i == 0 { |
| 92 | + continue |
| 93 | + } |
| 94 | + |
| 95 | + fields := strings.Split(line, "\t") |
| 96 | + if len(fields) < 5 { |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + publicKey := fields[0] |
| 101 | + lastHandshake := fields[4] |
| 102 | + |
| 103 | + var lastSeenSecs int |
| 104 | + reachable := false |
| 105 | + |
| 106 | + if lastHandshake != "0" { |
| 107 | + ts, err := parseUnixTimestamp(lastHandshake) |
| 108 | + if err == nil { |
| 109 | + lastSeenSecs = int(time.Since(ts).Seconds()) |
| 110 | + reachable = lastSeenSecs < 180 |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + health.Peers = append(health.Peers, NetworkPeerHealth{ |
| 115 | + ID: publicKey[:8], |
| 116 | + LastSeenSecs: lastSeenSecs, |
| 117 | + Reachable: reachable, |
| 118 | + }) |
| 119 | + } |
| 120 | + |
| 121 | + health.PeerCount = len(health.Peers) |
| 122 | + |
| 123 | + return health |
| 124 | +} |
| 125 | + |
| 126 | +func parseUnixTimestamp(s string) (time.Time, error) { |
| 127 | + ts, err := strconv.ParseInt(s, 10, 64) |
| 128 | + if err != nil { |
| 129 | + return time.Time{}, err |
| 130 | + } |
| 131 | + return time.Unix(ts, 0), nil |
| 132 | +} |
| 133 | + |
| 134 | +func CollectContainerHealth() *ContainerHealth { |
| 135 | + health := &ContainerHealth{ |
| 136 | + RuntimeResponsive: false, |
| 137 | + } |
| 138 | + |
| 139 | + cmd := exec.Command("podman", "ps", "-a", "--format", "{{.State}}") |
| 140 | + output, err := cmd.CombinedOutput() |
| 141 | + if err != nil { |
| 142 | + return health |
| 143 | + } |
| 144 | + |
| 145 | + health.RuntimeResponsive = true |
| 146 | + |
| 147 | + states := strings.Split(strings.TrimSpace(string(output)), "\n") |
| 148 | + for _, state := range states { |
| 149 | + if state == "" { |
| 150 | + continue |
| 151 | + } |
| 152 | + if state == "running" { |
| 153 | + health.RunningContainers++ |
| 154 | + } else { |
| 155 | + health.StoppedContainers++ |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + infoCmd := exec.Command("podman", "system", "info", "--format", "{{.Store.GraphRoot}}") |
| 160 | + infoOutput, err := infoCmd.CombinedOutput() |
| 161 | + if err == nil { |
| 162 | + graphRoot := strings.TrimSpace(string(infoOutput)) |
| 163 | + if graphRoot != "" { |
| 164 | + diskInfo, err := disk.Usage(graphRoot) |
| 165 | + if err == nil { |
| 166 | + health.StorageUsedGb = float64(diskInfo.Used) / 1024 / 1024 / 1024 |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return health |
| 172 | +} |
0 commit comments