/
main.go
157 lines (144 loc) · 3.57 KB
/
main.go
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"regexp"
"strings"
"github.com/crooks/xclarity_extract/xcapi"
"github.com/tidwall/gjson"
"gopkg.in/yaml.v2"
)
var (
cfg *Config
cpuCodeRE = regexp.MustCompile(`[\s-]([2568]\d{3})\s`)
)
// Configuration structure
type Config struct {
API struct {
BaseURL string `yaml:"base_url"`
CertFile string `yaml:"certfile"`
Password string `yaml:"password"`
Username string `yaml:"username"`
}
}
type Flag struct {
configFile string
chassis bool
rawOutput bool
}
func newConfig(filename string) (*Config, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
d := yaml.NewDecoder(file)
config := &Config{}
if err := d.Decode(&config); err != nil {
return nil, err
}
return config, nil
}
func parseFlags() *Flag {
f := new(Flag)
flag.StringVar(
&f.configFile,
"config",
path.Join("/etc/xclarity", "xclarity_extract.yml"),
"Path to xclarity_extract configuration file",
)
flag.BoolVar(
&f.chassis,
"chassis",
false,
"Output chassis details instead of node",
)
flag.BoolVar(
&f.rawOutput,
"raw",
false,
"Output raw JSON",
)
flag.Parse()
return f
}
// parser is the main loop that endlessly fetches URLs and parses them into
// Prometheus metrics
func parser(url, jsonPath string) gjson.Result {
client := xcapi.NewBasicAuthClient(cfg.API.Username, cfg.API.Password, cfg.API.CertFile)
bytes, err := client.GetJSON(url)
if err != nil {
log.Fatal("Parsing %s returned: %v", url, err)
}
return gjson.GetBytes(bytes, jsonPath)
}
// nodeMemory iterates over all the memory modules and returns a total
func nodeMemory(modules gjson.Result) (memory int64) {
for _, module := range modules.Array() {
memory += module.Get("capacity").Int()
}
return
}
// cpuDesc2Code attempts to extract a meaningful CPU code from the unstructured displayName field
func cpuDesc2Code(desc string) string {
// Highest priority: Look for a 4 digit Xeon-style CPU code
res := cpuCodeRE.FindAllStringSubmatch(desc, -1)
if len(res) == 1 {
// One match is excellent, return the submatch
return res[0][1]
} else if len(res) > 1 {
// More than a single match suggests Regex refinement is required.
// Make the bold assumption that the first match is good.
log.Printf("Multiple CPU code matches: %v", res)
return res[0][1]
}
// No matches found
return "Unknown"
}
// nodeParser parses the json output from the XClarity API (https://<xclarity_server>/nodes)
func nodeParser(j gjson.Result) {
for _, jn := range j.Array() {
sockets := jn.Get("processors").Array()
// If there aren't any CPUs, we don't want it
if len(sockets) < 1 {
continue
}
// Name Serial Model CPU Speed Sockets Cores Memory
fmt.Printf(
"%-20s %-10s %-10s %-8s %1.2f %1d %2d %4d\n",
strings.ToLower(jn.Get("name").String()),
jn.Get("serialNumber").String(),
jn.Get("model").String(),
cpuDesc2Code(jn.Get("processors.0.displayName").String()),
jn.Get("processors.0.speed").Float(),
len(sockets),
jn.Get("processors.0.cores").Int(),
nodeMemory(jn.Get("memoryModules")),
)
}
}
func main() {
var err error
f := parseFlags()
cfg, err = newConfig(f.configFile)
if err != nil {
log.Fatalf("Unable to parse config file: %v", err)
}
var j gjson.Result
if f.chassis {
apiURL := fmt.Sprintf("%s/chassis", cfg.API.BaseURL)
j = parser(apiURL, "chassisList")
} else {
apiURL := fmt.Sprintf("%s/nodes", cfg.API.BaseURL)
j = parser(apiURL, "nodeList")
}
// TODO: Add chassis processing code.
if f.rawOutput || f.chassis {
fmt.Println(j)
} else {
nodeParser(j)
}
}