summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/vcsa/collect.go
blob: 8a734d9e89ea6c8713f411385fe770272ac608a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// SPDX-License-Identifier: GPL-3.0-or-later

package vcsa

import (
	"sync"
)

var componentHealthStatuses = []string{"green", "red", "yellow", "orange", "gray"}
var softwareHealthStatuses = []string{"green", "red", "orange", "gray"}

type vcsaHealthStatus struct {
	System           *string
	ApplMgmt         *string
	Load             *string
	Mem              *string
	Swap             *string
	DatabaseStorage  *string
	Storage          *string
	SoftwarePackages *string
}

func (vc *VCSA) collect() (map[string]int64, error) {
	err := vc.client.Ping()
	if err != nil {
		return nil, err
	}

	var status vcsaHealthStatus
	vc.scrapeHealth(&status)

	mx := make(map[string]int64)

	writeStatus(mx, "system", componentHealthStatuses, status.System)
	writeStatus(mx, "applmgmt", componentHealthStatuses, status.ApplMgmt)
	writeStatus(mx, "load", componentHealthStatuses, status.Load)
	writeStatus(mx, "mem", componentHealthStatuses, status.Mem)
	writeStatus(mx, "swap", componentHealthStatuses, status.Swap)
	writeStatus(mx, "database_storage", componentHealthStatuses, status.DatabaseStorage)
	writeStatus(mx, "storage", componentHealthStatuses, status.Storage)
	writeStatus(mx, "software_packages", softwareHealthStatuses, status.SoftwarePackages)

	return mx, nil
}

func (vc *VCSA) scrapeHealth(status *vcsaHealthStatus) {
	wg := &sync.WaitGroup{}

	scrape := func(fn func() (string, error), value **string) {
		v, err := fn()
		if err != nil {
			vc.Error(err)
			return
		}
		*value = &v
	}

	for _, fn := range []func(){
		func() { scrape(vc.client.System, &status.System) },
		func() { scrape(vc.client.ApplMgmt, &status.ApplMgmt) },
		func() { scrape(vc.client.Load, &status.Load) },
		func() { scrape(vc.client.DatabaseStorage, &status.DatabaseStorage) },
		func() { scrape(vc.client.Storage, &status.Storage) },
		func() { scrape(vc.client.Mem, &status.Mem) },
		func() { scrape(vc.client.Swap, &status.Swap) },
		func() { scrape(vc.client.SoftwarePackages, &status.SoftwarePackages) },
	} {
		fn := fn

		wg.Add(1)
		go func() { defer wg.Done(); fn() }()
	}

	wg.Wait()
}

func writeStatus(mx map[string]int64, key string, statuses []string, status *string) {
	if status == nil {
		return
	}

	var found bool
	for _, s := range statuses {
		mx[key+"_status_"+s] = boolToInt(s == *status)
		found = found || s == *status
	}
	mx[key+"_status_unknown"] = boolToInt(!found)
}

func boolToInt(v bool) int64 {
	if v {
		return 1
	}
	return 0
}