-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
cmd: Add 'caddy status' command and core StatusReporter interface #7506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pauloappbr
wants to merge
1
commit into
caddyserver:master
Choose a base branch
from
pauloappbr:feature/issue-7453-status-command
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package caddycmd | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "flag" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/caddyserver/caddy/v2" | ||
| ) | ||
|
|
||
| func init() { | ||
| RegisterCommand(Command{ | ||
| Name: "status", | ||
| Func: cmdStatus, | ||
| Usage: "[--json] [--address <admin-api-address>] [--config <path>] [--adapter <name>]", | ||
| Short: "Prints the status of the running Caddy instance", | ||
| Flags: func() *flag.FlagSet { | ||
| fs := flag.NewFlagSet("status", flag.ExitOnError) | ||
| fs.Bool("json", false, "Output raw JSON instead of human-readable text") | ||
| fs.String("address", "", "The address to use to reach the admin API endpoint, if not the default") | ||
| fs.String("config", "", "Configuration file") | ||
| fs.String("adapter", "", "Name of config adapter to apply") | ||
| return fs | ||
| }(), | ||
| }) | ||
| } | ||
|
|
||
| // cmdStatus implements the 'caddy status' command. | ||
| func cmdStatus(fl Flags) (int, error) { | ||
| useJSON := fl.Bool("json") | ||
| addr := fl.String("address") | ||
| cfgFile := fl.String("config") | ||
| cfgAdapter := fl.String("adapter") | ||
|
|
||
| // Determine the admin API address based on provided flags or defaults | ||
| adminAddr, err := DetermineAdminAPIAddress(cfgFile, nil, cfgAdapter, addr) | ||
| if err != nil { | ||
| return caddy.ExitCodeFailedStartup, fmt.Errorf("could not determine admin API address: %v", err) | ||
| } | ||
|
|
||
| resp, err := AdminAPIRequest(adminAddr, http.MethodGet, "/status", nil, nil) | ||
| if err != nil { | ||
|
pauloappbr marked this conversation as resolved.
|
||
| return caddy.ExitCodeFailedStartup, fmt.Errorf("failed to query status: %v", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return caddy.ExitCodeFailedStartup, err | ||
| } | ||
|
|
||
| // Strictly output raw JSON to stdout for CLI pipelines (e.g., jq) | ||
| if useJSON { | ||
| os.Stdout.Write(body) | ||
| fmt.Println() | ||
| return caddy.ExitCodeSuccess, nil | ||
| } | ||
|
|
||
| var status map[string]any | ||
| if err := json.Unmarshal(body, &status); err != nil { | ||
| return caddy.ExitCodeFailedStartup, fmt.Errorf("failed to parse status JSON: %v\nRaw output: %s", err, string(body)) | ||
| } | ||
|
|
||
| // Format output to be human-readable | ||
| fmt.Printf("Caddy %v\n", status["version"]) | ||
| fmt.Println("Status: Running") | ||
|
|
||
| if uptime, ok := status["uptime_secs"].(float64); ok { | ||
| u := int64(uptime) | ||
| h := u / 3600 | ||
| m := (u % 3600) / 60 | ||
| s := u % 60 | ||
| if h > 0 { | ||
| fmt.Printf("Uptime: %dh %dm %ds\n", h, m, s) | ||
| } else if m > 0 { | ||
| fmt.Printf("Uptime: %dm %ds\n", m, s) | ||
| } else { | ||
| fmt.Printf("Uptime: %ds\n", s) | ||
| } | ||
| } | ||
|
|
||
| if apps, ok := status["apps"].(map[string]any); ok && len(apps) > 0 { | ||
| var appNames []string | ||
| for appName := range apps { | ||
| appNames = append(appNames, appName) | ||
| } | ||
| fmt.Printf("\nRunning apps: %s\n", strings.Join(appNames, ", ")) | ||
| } else { | ||
| fmt.Printf("\nRunning apps: none\n") | ||
| } | ||
|
|
||
| fmt.Println("\nMemory") | ||
| if mem, ok := status["memory"].(map[string]any); ok { | ||
| allocMB := mem["allocated_bytes"].(float64) / 1024 / 1024 | ||
| sysMB := mem["system_bytes"].(float64) / 1024 / 1024 | ||
| fmt.Printf("Allocated: %.0f MB\nSystem: %.0f MB\n", allocMB, sysMB) | ||
| } | ||
|
|
||
| fmt.Printf("Goroutines: %v\n", status["goroutines"]) | ||
|
|
||
| return caddy.ExitCodeSuccess, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.