Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Key commands inside the shell:
| `ftp download <alias> <remote> <local>`| Download a file via FTP/FTPS. |
| `help` | Print the command catalogue. |
| `clear` | Clear the terminal and reprint the banner. |
| `htop` | Launch `htop` with the ServerCommander color theme. |
| `exit` | Gracefully shut down ServerCommander. |

> **Note:** When prompted for the authentication method during `session add`, enter `password` or `private_key`. Passwords are never stored—if you choose `password` you will be asked for it when connecting.
Expand Down
14 changes: 4 additions & 10 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,15 @@ server-commander logs server_id

This command shows the most recent logs from the specified server.

### 11. Process Management
### 11. System Monitoring with `htop`

To list running processes on the server:
Launch the interactive `htop` monitor using the ServerCommander color palette:

```bash
server-commander processes server_id
server-commander htop
```

To kill a running process:

```bash
server-commander kill-process server_id --pid <process_id>
```

This allows you to monitor and manage processes on the server remotely.
This opens `htop` in the current terminal session. Press `q` to exit the monitor and return to the ServerCommander console.

## Additional Options

Expand Down
19 changes: 19 additions & 0 deletions src/cmd/assets/htoprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ServerCommander htop theme
# Generated configuration to favor cyan and green hues matching the console palette.
fields=0 48 17 18 38 39 46 47
sort_key=46
sort_direction=1
hide_threads=0
hide_kernel_threads=1
hide_userland_threads=0
shadow_other_users=0
highlight_base_name=1
highlight_megabytes=1
highlight_threads=1
tree_view=0
header_margin=1
color_scheme=5
left_meters=AllCPUs2 Memory Swap
left_meter_modes=1 1 1
right_meters=Tasks LoadAverage Uptime
right_meter_modes=2 2 2
74 changes: 74 additions & 0 deletions src/cmd/htop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cmd

import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"

"servercommander/src/utils"
)

func init() {
RegisterCommand("htop", "Launch the system monitor with ServerCommander colors", htopCommand)
}

func htopCommand(args []string) error {
if err := ensureUsage(args, 0, 0, "htop"); err != nil {
return err
}

if runtime.GOOS == "windows" {
return errors.New("htop is not available on Windows. Please install an alternative process monitor.")
}

binary, err := exec.LookPath("htop")
if err != nil {
return fmt.Errorf("htop executable not found in PATH: %w", err)
}

themePath, cleanup, err := prepareHtopTheme()
if err != nil {
return err
}
if cleanup != nil {
defer cleanup()
}

fmt.Printf("%sLaunching htop with ServerCommander theme. Press 'q' to return to the console.%s\n", utils.Green, utils.Reset)

cmd := exec.Command(binary)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if themePath != "" {
cmd.Env = append(os.Environ(), fmt.Sprintf("HTOPRC=%s", themePath))
}

return cmd.Run()
}

func prepareHtopTheme() (string, func(), error) {
if len(htopTheme) == 0 {
return "", nil, nil
}

dir, err := os.MkdirTemp("", "servercommander-htop")
if err != nil {
return "", nil, fmt.Errorf("failed to create temporary configuration directory: %w", err)
}

path := filepath.Join(dir, "htoprc")
if err := os.WriteFile(path, htopTheme, 0o600); err != nil {
os.RemoveAll(dir)
return "", nil, fmt.Errorf("failed to prepare htop theme: %w", err)
}

cleanup := func() {
_ = os.RemoveAll(dir)
}

return path, cleanup, nil
}
6 changes: 6 additions & 0 deletions src/cmd/htop_theme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cmd

import "embed"

Check failure on line 3 in src/cmd/htop_theme.go

View workflow job for this annotation

GitHub Actions / build

"embed" imported and not used) (typecheck)

Check failure on line 3 in src/cmd/htop_theme.go

View workflow job for this annotation

GitHub Actions / build

"embed" imported and not used (typecheck)

//go:embed assets/htoprc
var htopTheme []byte
Loading