| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package vcsa |
| 4 | |
| 5 | import ( |
| 6 | "sync" |
| 7 | |
| 8 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/oldmetrix" |
| 9 | ) |
| 10 | |
| 11 | var componentHealthStatuses = []string{"green", "red", "yellow", "orange", "gray"} |
| 12 | var softwareHealthStatuses = []string{"green", "red", "orange", "gray"} |
| 13 | |
| 14 | type vcsaHealthStatus struct { |
| 15 | System *string |
| 16 | ApplMgmt *string |
| 17 | Load *string |
| 18 | Mem *string |
| 19 | Swap *string |
| 20 | DatabaseStorage *string |
| 21 | Storage *string |
| 22 | SoftwarePackages *string |
| 23 | } |
| 24 | |
| 25 | func (c *Collector) collect() (map[string]int64, error) { |
| 26 | err := c.client.Ping() |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | |
| 31 | var status vcsaHealthStatus |
| 32 | c.scrapeHealth(&status) |
| 33 | |
| 34 | mx := make(map[string]int64) |
| 35 | |
| 36 | writeStatus(mx, "system", componentHealthStatuses, status.System) |
| 37 | writeStatus(mx, "applmgmt", componentHealthStatuses, status.ApplMgmt) |
| 38 | writeStatus(mx, "load", componentHealthStatuses, status.Load) |
| 39 | writeStatus(mx, "mem", componentHealthStatuses, status.Mem) |
| 40 | writeStatus(mx, "swap", componentHealthStatuses, status.Swap) |
| 41 | writeStatus(mx, "database_storage", componentHealthStatuses, status.DatabaseStorage) |
| 42 | writeStatus(mx, "storage", componentHealthStatuses, status.Storage) |
| 43 | writeStatus(mx, "software_packages", softwareHealthStatuses, status.SoftwarePackages) |
| 44 | |
| 45 | return mx, nil |
| 46 | } |
| 47 | |
| 48 | func (c *Collector) scrapeHealth(status *vcsaHealthStatus) { |
| 49 | wg := &sync.WaitGroup{} |
| 50 | |
| 51 | scrape := func(fn func() (string, error), value **string) { |
| 52 | v, err := fn() |
| 53 | if err != nil { |
| 54 | c.Error(err) |
| 55 | return |
| 56 | } |
| 57 | *value = &v |
| 58 | } |
| 59 | |
| 60 | for _, fn := range []func(){ |
| 61 | func() { scrape(c.client.System, &status.System) }, |
| 62 | func() { scrape(c.client.ApplMgmt, &status.ApplMgmt) }, |
| 63 | func() { scrape(c.client.Load, &status.Load) }, |
| 64 | func() { scrape(c.client.DatabaseStorage, &status.DatabaseStorage) }, |
| 65 | func() { scrape(c.client.Storage, &status.Storage) }, |
| 66 | func() { scrape(c.client.Mem, &status.Mem) }, |
| 67 | func() { scrape(c.client.Swap, &status.Swap) }, |
| 68 | func() { scrape(c.client.SoftwarePackages, &status.SoftwarePackages) }, |
| 69 | } { |
| 70 | |
| 71 | wg.Go(func() { fn() }) |
| 72 | } |
| 73 | |
| 74 | wg.Wait() |
| 75 | } |
| 76 | |
| 77 | func writeStatus(mx map[string]int64, key string, statuses []string, status *string) { |
| 78 | if status == nil { |
| 79 | return |
| 80 | } |
| 81 | |
| 82 | var found bool |
| 83 | for _, s := range statuses { |
| 84 | mx[key+"_status_"+s] = oldmetrix.Bool(s == *status) |
| 85 | found = found || s == *status |
| 86 | } |
| 87 | mx[key+"_status_unknown"] = oldmetrix.Bool(!found) |
| 88 | } |