Add cloudflared tunnel health command

This commit is contained in:
Mads Jon Nielsen 2023-12-28 19:50:49 +01:00
parent 00cd7c333c
commit f10247db90
2 changed files with 27 additions and 0 deletions

View File

@ -128,6 +128,7 @@ func Commands() []*cli.Command {
buildVirtualNetworkSubcommand(false), buildVirtualNetworkSubcommand(false),
buildRunCommand(), buildRunCommand(),
buildListCommand(), buildListCommand(),
buildHealthCommand(),
buildInfoCommand(), buildInfoCommand(),
buildIngressSubcommand(), buildIngressSubcommand(),
buildDeleteCommand(), buildDeleteCommand(),

View File

@ -5,6 +5,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -397,6 +398,31 @@ func fmtConnections(connections []cfapi.Connection, showRecentlyDisconnected boo
return strings.Join(output, ", ") return strings.Join(output, ", ")
} }
func buildHealthCommand() *cli.Command {
return &cli.Command{
Name: "health",
Action: cliutil.ConfiguredAction(healthCommand),
Usage: "Tunnel health exit code",
UsageText: "cloudflared tunnel [tunnel command options] health [subcommand options]",
Description: "cloudflared tunnel health will return proper exit code if tunnel is healthy or unhealthy",
Flags: []cli.Flag{},
CustomHelpTemplate: commandHelpTemplate(),
}
}
func healthCommand(c *cli.Context) error {
metrics := strings.Split(c.String("metrics"), ":")
requestURL := fmt.Sprintf("http://%s:%s/ready", metrics[0], metrics[1])
res, err := http.Get(requestURL)
if err != nil {
return err
}
if res.StatusCode != 200 {
return fmt.Errorf("health /ready endpoint returned status code %d", res.StatusCode)
}
return nil
}
func buildInfoCommand() *cli.Command { func buildInfoCommand() *cli.Command {
return &cli.Command{ return &cli.Command{
Name: "info", Name: "info",