forked from HewlettPackard/lustre_exporter
/
proc_common.go
144 lines (127 loc) · 4 KB
/
proc_common.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
// (C) Copyright 2017 Hewlett Packard Enterprise Development LP
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sources
import (
"fmt"
"math"
"regexp"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
)
const (
core string = "core"
extended string = "extended"
disabled string = "disabled"
)
var (
numRegexPattern = regexp.MustCompile(`[0-9]*\.[0-9]+|[0-9]+`)
jobidRegexPattern = regexp.MustCompile(`job_id:\s*(.*\.[0-9]+|[0-9_]+)`)
)
type prometheusType func([]string, []string, string, string, float64) prometheus.Metric
type lustreProcMetric struct {
filename string
promName string
source string //The parent data source (OST, MDS, MGS, etc)
path string //Path to retrieve metric from
helpText string
hasMultipleVals bool
metricFunc prometheusType
}
type lustreStatsMetric struct {
title string
help string
value float64
extraLabel string
extraLabelValue string
}
type lustreHelpStruct struct {
filename string
promName string // Name to be used in Prometheus
helpText string
metricFunc prometheusType
hasMultipleVals bool
priorityLevel string
}
func newLustreProcMetric(filename string, promName string, source string, path string, helpText string, hasMultipleVals bool, metricFunc prometheusType) lustreProcMetric {
var m lustreProcMetric
m.filename = filename
m.promName = promName
m.source = source
m.path = path
m.helpText = helpText
m.hasMultipleVals = hasMultipleVals
m.metricFunc = metricFunc
return m
}
func regexCaptureString(pattern string, textToMatch string) (matchedString string) {
// Return the first string in a list of matched strings if found
strings := regexCaptureStrings(pattern, textToMatch)
if len(strings) < 1 {
return ""
}
return strings[0]
}
func regexCaptureStrings(pattern string, textToMatch string) (matchedStrings []string) {
re := regexp.MustCompile(pattern)
matchedStrings = re.FindAllString(textToMatch, -1)
return matchedStrings
}
func regexCaptureNumbers(textToMatch string) (matchedNumbers []string) {
matchedNumbers = numRegexPattern.FindAllString(textToMatch, -1)
return matchedNumbers
}
func regexCaptureJobids(textToMatch string) (matchedJobids []string) {
matchedJobids = jobidRegexPattern.FindStringSubmatch(textToMatch)
return
}
func parseFileElements(path string, directoryDepth int) (name string, nodeName string, err error) {
pathElements := strings.Split(path, "/")
pathLen := len(pathElements)
if pathLen < 1 {
return "", "", fmt.Errorf("path did not return at least one element")
}
name = pathElements[pathLen-1]
nodeName = pathElements[pathLen-2-directoryDepth]
nodeName = strings.TrimPrefix(nodeName, "filter-")
nodeName = strings.TrimSuffix(nodeName, "_UUID")
return name, nodeName, nil
}
func convertToBytes(s string) string {
if len(s) < 1 {
return s
}
numericS := ""
uppercaseS := strings.ToUpper(s)
var multiplier float64
switch finalChar := uppercaseS[len(uppercaseS)-1:]; finalChar {
case "K":
numericS = strings.TrimSuffix(uppercaseS, "K")
multiplier = math.Pow(2, 10)
case "M":
numericS = strings.TrimSuffix(uppercaseS, "M")
multiplier = math.Pow(2, 20)
case "G":
numericS = strings.TrimSuffix(uppercaseS, "G")
multiplier = math.Pow(2, 30)
default:
//passthrough integers and IO sizes that we don't expect
return s
}
byteVal, err := strconv.ParseUint(numericS, 10, 64)
if err != nil {
return s
}
byteVal *= uint64(multiplier)
return strconv.FormatUint(byteVal, 10)
}