| 41 | } |
| 42 | |
| 43 | func main() { |
| 44 | es, err := elasticsearch.New() |
| 45 | if err != nil { |
| 46 | log.Fatalf("Error creating client: %s", err) |
| 47 | } |
| 48 | defer func() { |
| 49 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 50 | defer cancel() |
| 51 | if err := es.Close(ctx); err != nil { |
| 52 | fmt.Printf("Error closing the client: %s\n", err) |
| 53 | } |
| 54 | }() |
| 55 | |
| 56 | res, err := es.Cluster.Stats(es.Cluster.Stats.WithHuman()) |
| 57 | if err != nil { |
| 58 | log.Fatalf("Error getting response: %s", err) |
| 59 | } |
| 60 | defer res.Body.Close() |
| 61 | |
| 62 | json := read(res.Body) |
| 63 | |
| 64 | fmt.Println(strings.Repeat("─", 50)) |
| 65 | faint.Print("cluster ") |
| 66 | // Get cluster name |
| 67 | bold.Print(gjson.Get(json, "cluster_name")) |
| 68 | |
| 69 | faint.Print(" status=") |
| 70 | // Get cluster health status |
| 71 | status := gjson.Get(json, "status") |
| 72 | switch status.Str { |
| 73 | case "green": |
| 74 | bold.Add(color.FgHiGreen).Print(status) |
| 75 | case "yellow": |
| 76 | bold.Add(color.FgHiYellow).Print(status) |
| 77 | case "red": |
| 78 | bold.Add(color.FgHiRed).Print(status) |
| 79 | default: |
| 80 | bold.Add(color.FgHiRed, color.Underline).Print(status) |
| 81 | } |
| 82 | fmt.Println("\n" + strings.Repeat("─", 50)) |
| 83 | |
| 84 | stats := []string{ |
| 85 | "indices.count", |
| 86 | "indices.docs.count", |
| 87 | "indices.store.size", |
| 88 | "nodes.count.total", |
| 89 | "nodes.os.mem.used_percent", |
| 90 | "nodes.process.cpu.percent", |
| 91 | "nodes.jvm.versions.#.version", |
| 92 | "nodes.jvm.mem.heap_used", |
| 93 | "nodes.jvm.mem.heap_max", |
| 94 | "nodes.fs.free", |
| 95 | } |
| 96 | |
| 97 | var maxwidth int |
| 98 | for _, item := range stats { |
| 99 | if len(item) > maxwidth { |
| 100 | maxwidth = len(item) |