| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package rabbitmq |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | |
| 8 | "github.com/netdata/netdata/go/plugins/pkg/stm" |
| 9 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 10 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/oldmetrix" |
| 11 | ) |
| 12 | |
| 13 | func (c *Collector) collectVhosts(mx map[string]int64) error { |
| 14 | req, err := web.NewHTTPRequestWithPath(c.RequestConfig, urlPathAPIVhosts) |
| 15 | if err != nil { |
| 16 | return fmt.Errorf("failed to create vhosts stats request: %w", err) |
| 17 | } |
| 18 | |
| 19 | var resp []apiVhostResp |
| 20 | |
| 21 | if err := c.webClient().RequestJSON(req, &resp); err != nil { |
| 22 | return err |
| 23 | } |
| 24 | |
| 25 | for _, vhost := range resp { |
| 26 | c.cache.getVhost(vhost.Name).seen = true |
| 27 | |
| 28 | px := fmt.Sprintf("vhost_%s_", vhost.Name) |
| 29 | |
| 30 | for k, v := range stm.ToMap(vhost) { |
| 31 | mx[px+k] = v |
| 32 | } |
| 33 | |
| 34 | st := getVhostStatus(vhost) |
| 35 | for _, v := range []string{"running", "stopped", "partial"} { |
| 36 | mx[px+"status_"+v] = oldmetrix.Bool(v == st) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | return nil |
| 41 | } |
| 42 | |
| 43 | func getVhostStatus(vhost apiVhostResp) string { |
| 44 | // https://github.com/rabbitmq/rabbitmq-server/blob/c8394095990c2eb9e2f4b142e7816a653c9e5011/deps/rabbitmq_management/priv/www/js/formatters.js#L1058 |
| 45 | |
| 46 | var ok, nok int |
| 47 | |
| 48 | for _, v := range vhost.ClusterState { |
| 49 | switch v { |
| 50 | case "stopped", "nodedown": |
| 51 | nok++ |
| 52 | case "running": |
| 53 | ok++ |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | switch { |
| 58 | case nok == 0: |
| 59 | return "running" |
| 60 | case ok == 0: |
| 61 | return "stopped" |
| 62 | default: |
| 63 | return "partial" |
| 64 | } |
| 65 | } |