diff options
Diffstat (limited to 'cmd/ship/host/status.go')
| -rw-r--r-- | cmd/ship/host/status.go | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/cmd/ship/host/status.go b/cmd/ship/host/status.go new file mode 100644 index 0000000..eb2de53 --- /dev/null +++ b/cmd/ship/host/status.go | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | package host | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | |||
| 6 | "github.com/bdw/ship/internal/ssh" | ||
| 7 | "github.com/bdw/ship/internal/state" | ||
| 8 | "github.com/spf13/cobra" | ||
| 9 | ) | ||
| 10 | |||
| 11 | var statusCmd = &cobra.Command{ | ||
| 12 | Use: "status", | ||
| 13 | Short: "Show VPS health (uptime, disk, memory)", | ||
| 14 | RunE: runStatus, | ||
| 15 | } | ||
| 16 | |||
| 17 | func runStatus(cmd *cobra.Command, args []string) error { | ||
| 18 | st, err := state.Load() | ||
| 19 | if err != nil { | ||
| 20 | return fmt.Errorf("error loading state: %w", err) | ||
| 21 | } | ||
| 22 | |||
| 23 | host, _ := cmd.Flags().GetString("host") | ||
| 24 | if host == "" { | ||
| 25 | host = st.GetDefaultHost() | ||
| 26 | } | ||
| 27 | |||
| 28 | if host == "" { | ||
| 29 | return fmt.Errorf("--host is required (no default host set)") | ||
| 30 | } | ||
| 31 | |||
| 32 | fmt.Printf("Connecting to %s...\n\n", host) | ||
| 33 | |||
| 34 | client, err := ssh.Connect(host) | ||
| 35 | if err != nil { | ||
| 36 | return fmt.Errorf("error connecting to VPS: %w", err) | ||
| 37 | } | ||
| 38 | defer client.Close() | ||
| 39 | |||
| 40 | fmt.Println("UPTIME") | ||
| 41 | if output, err := client.Run("uptime -p"); err == nil { | ||
| 42 | fmt.Printf(" %s", output) | ||
| 43 | } | ||
| 44 | if output, err := client.Run("uptime -s"); err == nil { | ||
| 45 | fmt.Printf(" Since: %s", output) | ||
| 46 | } | ||
| 47 | fmt.Println() | ||
| 48 | |||
| 49 | fmt.Println("LOAD") | ||
| 50 | if output, err := client.Run("cat /proc/loadavg | awk '{print $1, $2, $3}'"); err == nil { | ||
| 51 | fmt.Printf(" 1m, 5m, 15m: %s", output) | ||
| 52 | } | ||
| 53 | fmt.Println() | ||
| 54 | |||
| 55 | fmt.Println("MEMORY") | ||
| 56 | if output, err := client.Run("free -h | awk 'NR==2 {print \" Used: \" $3 \" / \" $2}'"); err == nil { | ||
| 57 | fmt.Print(output) | ||
| 58 | } | ||
| 59 | if output, err := client.Run("free -h | awk 'NR==2 {printf \" Available: %s\\n\", $7}'"); err == nil { | ||
| 60 | fmt.Print(output) | ||
| 61 | } | ||
| 62 | fmt.Println() | ||
| 63 | |||
| 64 | fmt.Println("DISK") | ||
| 65 | if output, err := client.Run("df -h / | awk 'NR==2 {print \" Used: \" $3 \" / \" $2 \" (\" $5 \")\"}'"); err == nil { | ||
| 66 | fmt.Print(output) | ||
| 67 | } | ||
| 68 | if output, err := client.Run("df -h / | awk 'NR==2 {print \" Available: \" $4}'"); err == nil { | ||
| 69 | fmt.Print(output) | ||
| 70 | } | ||
| 71 | fmt.Println() | ||
| 72 | |||
| 73 | fmt.Println("UPDATES") | ||
| 74 | if output, err := client.Run("[ -f /var/lib/update-notifier/updates-available ] && cat /var/lib/update-notifier/updates-available | head -2 || echo ' (update info not available)'"); err == nil { | ||
| 75 | fmt.Print(output) | ||
| 76 | } | ||
| 77 | fmt.Println() | ||
| 78 | |||
| 79 | fmt.Println("SERVICES") | ||
| 80 | if output, err := client.Run("systemctl is-active caddy 2>/dev/null && echo ' Caddy: active' || echo ' Caddy: inactive'"); err == nil { | ||
| 81 | if output == "active\n" { | ||
| 82 | fmt.Println(" Caddy: active") | ||
| 83 | } else { | ||
| 84 | fmt.Println(" Caddy: inactive") | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | hostState := st.GetHost(host) | ||
| 89 | if hostState != nil && len(hostState.Apps) > 0 { | ||
| 90 | activeCount := 0 | ||
| 91 | for name, app := range hostState.Apps { | ||
| 92 | if app.Type == "app" { | ||
| 93 | if output, err := client.Run(fmt.Sprintf("systemctl is-active %s 2>/dev/null", name)); err == nil && output == "active\n" { | ||
| 94 | activeCount++ | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | appCount := 0 | ||
| 99 | for _, app := range hostState.Apps { | ||
| 100 | if app.Type == "app" { | ||
| 101 | appCount++ | ||
| 102 | } | ||
| 103 | } | ||
| 104 | fmt.Printf(" Deployed apps: %d/%d active\n", activeCount, appCount) | ||
| 105 | } | ||
| 106 | |||
| 107 | return nil | ||
| 108 | } | ||
