Skip to content

Commit 2ad212f

Browse files
author
Philipp Dorschner
committed
git init
0 parents  commit 2ad212f

12 files changed

+1778
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

LICENSE

+674
Large diffs are not rendered by default.

cmd/config.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cmd
2+
3+
import (
4+
"check_elasticsearch/internal/client"
5+
"net/url"
6+
"strconv"
7+
)
8+
9+
type Config struct {
10+
Hostname string
11+
Port int
12+
TLS bool
13+
Username string
14+
Password string
15+
Insecure bool
16+
}
17+
18+
var cliConfig Config
19+
20+
func (c *Config) Client() *client.Client {
21+
u := url.URL{
22+
Scheme: "http",
23+
Host: c.Hostname + ":" + strconv.Itoa(c.Port),
24+
}
25+
26+
if c.TLS {
27+
u.Scheme = "https"
28+
}
29+
30+
cl := client.NewClient(u.String(), c.Username, c.Password)
31+
cl.Insecure = c.Insecure
32+
33+
return cl
34+
}

cmd/health.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cmd
2+
3+
import (
4+
"github.com/NETWAYS/go-check"
5+
"github.com/NETWAYS/go-check/perfdata"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// healthCmd represents the health command
10+
var healthCmd = &cobra.Command{
11+
Use: "health",
12+
Short: "A brief description of your command",
13+
Long: `A longer description that spans multiple lines and likely contains examples
14+
and usage of using your command. For example:
15+
16+
Cobra is a CLI library for Go that empowers applications.
17+
This application is a tool to generate the needed files
18+
to quickly create a Cobra application.`,
19+
Run: func(cmd *cobra.Command, args []string) {
20+
client := cliConfig.Client()
21+
err := client.Connect()
22+
if err != nil {
23+
check.ExitError(err)
24+
}
25+
26+
health, err := client.Health()
27+
if err != nil {
28+
check.ExitError(err)
29+
}
30+
31+
rc := 3
32+
output := "Cluster " + health.ClusterName + " is " + health.Status
33+
34+
switch health.Status {
35+
case "green":
36+
rc = 0
37+
case "yellow":
38+
rc = 1
39+
default:
40+
rc = 2
41+
}
42+
43+
// green = 0
44+
// yellow = 1
45+
// red = 2
46+
p := perfdata.PerfdataList{
47+
{Label: "status", Value: rc},
48+
{Label: "nodes", Value: health.NumberOfNodes},
49+
{Label: "data_nodes", Value: health.NumberOfDataNodes},
50+
{Label: "active_primary_shards", Value: health.ActivePrimaryShards},
51+
{Label: "active_shards", Value: health.ActiveShards},
52+
}
53+
54+
check.ExitRaw(rc, output, "|", p.String())
55+
},
56+
}
57+
58+
func init() {
59+
rootCmd.AddCommand(healthCmd)
60+
healthCmd.DisableFlagsInUseLine = true
61+
}

cmd/query.go

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"github.com/NETWAYS/go-check"
6+
"github.com/NETWAYS/go-check/perfdata"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
type QueryConfig struct {
11+
Index string
12+
Query string
13+
MessageKey string
14+
MessageLen int
15+
Critical uint
16+
Warning uint
17+
}
18+
19+
var cliQueryConfig QueryConfig
20+
21+
// queryCmd represents the query command
22+
var queryCmd = &cobra.Command{
23+
Use: "query",
24+
Short: "A brief description of your command",
25+
Long: `A longer description that spans multiple lines and likely contains examples
26+
and usage of using your command. For example:
27+
28+
Cobra is a CLI library for Go that empowers applications.
29+
This application is a tool to generate the needed files
30+
to quickly create a Cobra application.`,
31+
Run: func(cmd *cobra.Command, args []string) {
32+
client := cliConfig.Client()
33+
err := client.Connect()
34+
if err != nil {
35+
check.ExitError(err)
36+
}
37+
38+
total, messages, err := client.SearchMessages(
39+
cliQueryConfig.Index,
40+
cliQueryConfig.Query,
41+
cliQueryConfig.MessageKey)
42+
43+
rc := 3
44+
output := fmt.Sprintf("Total hits: %d", total)
45+
46+
if len(messages) > 0 {
47+
output += "\n"
48+
for _, msg := range messages {
49+
if len(msg) > cliQueryConfig.MessageLen {
50+
msg = msg[0:cliQueryConfig.MessageLen]
51+
}
52+
output += msg + "\n"
53+
}
54+
}
55+
56+
if total >= cliQueryConfig.Critical {
57+
rc = check.Critical
58+
} else if total >= cliQueryConfig.Warning {
59+
rc = check.Warning
60+
} else if total == 0 {
61+
rc = check.OK
62+
}
63+
64+
p := perfdata.PerfdataList{
65+
{Label: "total", Value: total,
66+
Warn: &check.Threshold{Upper: float64(cliQueryConfig.Warning)},
67+
Crit: &check.Threshold{Upper: float64(cliQueryConfig.Critical)},
68+
},
69+
}
70+
71+
check.ExitRaw(rc, output, "|", p.String())
72+
},
73+
}
74+
75+
func init() {
76+
rootCmd.AddCommand(queryCmd)
77+
78+
fs := queryCmd.Flags()
79+
fs.StringVarP(&cliQueryConfig.Query, "query", "q", "", "")
80+
fs.StringVarP(&cliQueryConfig.Index, "index", "I", "", "")
81+
fs.StringVarP(&cliQueryConfig.MessageKey, "msgkey", "k", "", "")
82+
fs.IntVarP(&cliQueryConfig.MessageLen, "msglen", "m", 80, "")
83+
fs.UintVarP(&cliQueryConfig.Warning, "warning", "w", 20, "")
84+
fs.UintVarP(&cliQueryConfig.Critical, "critical", "w", 50, "")
85+
}

cmd/root.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package cmd
2+
3+
import (
4+
"github.com/NETWAYS/go-check"
5+
"github.com/spf13/cobra"
6+
"os"
7+
)
8+
9+
var rootCmd = &cobra.Command{
10+
Use: "check_elasticsearch",
11+
Short: "A brief description of your application",
12+
Long: `A longer description that spans multiple lines and likely contains
13+
examples and usage of using your application. For example:
14+
15+
Cobra is a CLI library for Go that empowers applications.
16+
This application is a tool to generate the needed files
17+
to quickly create a Cobra application.`,
18+
// Uncomment the following line if your bare application
19+
// has an action associated with it:
20+
Run: Help,
21+
}
22+
23+
func Execute(version string) {
24+
defer check.CatchPanic()
25+
26+
rootCmd.Version = version
27+
rootCmd.VersionTemplate()
28+
29+
if err := rootCmd.Execute(); err != nil {
30+
check.ExitError(err)
31+
}
32+
}
33+
34+
func init() {
35+
rootCmd.CompletionOptions.DisableDefaultCmd = true
36+
rootCmd.DisableAutoGenTag = true
37+
38+
rootCmd.SetHelpCommand(&cobra.Command{
39+
Use: "no-help",
40+
Hidden: true,
41+
})
42+
43+
pfs := rootCmd.PersistentFlags()
44+
pfs.StringVarP(&cliConfig.Hostname, "hostname", "H", "localhost", "")
45+
pfs.StringVarP(&cliConfig.Username, "username", "U", "", "")
46+
pfs.StringVarP(&cliConfig.Password, "password", "P", "", "")
47+
pfs.IntVarP(&cliConfig.Port, "port", "p", 9200, "")
48+
pfs.BoolVarP(&cliConfig.TLS, "tls", "S", false, "")
49+
pfs.BoolVar(&cliConfig.Insecure, "insecure", false, "")
50+
51+
pfs.SortFlags = false
52+
}
53+
54+
func Help(cmd *cobra.Command, strings []string) {
55+
_ = cmd.Usage()
56+
57+
os.Exit(3)
58+
}

go.mod

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module check_elasticsearch
2+
3+
go 1.16
4+
5+
require (
6+
github.com/NETWAYS/go-check v0.2.0
7+
github.com/elastic/go-elasticsearch/v7 v7.15.1
8+
github.com/spf13/cobra v1.2.1
9+
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect
10+
)

0 commit comments

Comments
 (0)