Skip to content

Commit

Permalink
Fix all wsl linter issues
Browse files Browse the repository at this point in the history
Signed-off-by: Mario Trangoni <mjtrangoni@gmail.com>
  • Loading branch information
mjtrangoni committed Nov 9, 2019
1 parent f20eef6 commit 814d058
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .golanci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ linters-settings:
min-confidence: 0
gocyclo:
min-complexity: 40
gocognit:
min-complexity: 60
maligned:
suggest-new: true
dupl:
Expand Down Expand Up @@ -32,7 +34,7 @@ linters-settings:
disabled-checks:
- wrapperFunc
funlen:
lines: 100
lines: 120
statements: 50
wsl:
# If true append is only allowed to be cuddled if appending value is
Expand Down
7 changes: 7 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type FlexlmCollector struct {
// NewFlexlmCollector creates a new FlexlmCollector
func NewFlexlmCollector(filters ...string) (*FlexlmCollector, error) {
f := make(map[string]bool)

for _, filter := range filters {
enabled, exist := collectorState[filter]
if !exist {
Expand All @@ -87,10 +88,12 @@ func NewFlexlmCollector(filters ...string) (*FlexlmCollector, error) {
if !*enabled {
return nil, fmt.Errorf("disabled collector: %s", filter)
}

f[filter] = true
}

collectors := make(map[string]Collector)

for key, enabled := range collectorState {
if *enabled {
collector, err := factories[key]()
Expand All @@ -102,6 +105,7 @@ func NewFlexlmCollector(filters ...string) (*FlexlmCollector, error) {
}
}
}

return &FlexlmCollector{Collectors: collectors}, nil
}

Expand All @@ -116,12 +120,14 @@ func (n FlexlmCollector) Collect(ch chan<- prometheus.Metric) {
wg := sync.WaitGroup{}

wg.Add(len(n.Collectors))

for name, c := range n.Collectors {
go func(name string, c Collector) {
execute(name, c, ch)
wg.Done()
}(name, c)
}

wg.Wait()
}

Expand All @@ -136,6 +142,7 @@ func execute(name string, c Collector, ch chan<- prometheus.Metric) {

if err != nil {
log.Errorf("ERROR: %s collector failed after %fs: %s", name, duration.Seconds(), err)

success = 0
} else {
log.Debugf("OK: %s collector succeeded after %fs.", name, duration.Seconds())
Expand Down
1 change: 1 addition & 0 deletions collector/lmstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,6 @@ func (c *lmstatCollector) Update(ch chan<- prometheus.Metric) error {
if err != nil {
return fmt.Errorf("couldn't get licenses information: %s", err)
}

return nil
}
1 change: 1 addition & 0 deletions collector/lmstat_feature_exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ func (c *lmstatFeatureExpCollector) Update(ch chan<- prometheus.Metric) error {
if err != nil {
return fmt.Errorf("couldn't get licenses feature expiration date: %s", err)
}

return nil
}
4 changes: 4 additions & 0 deletions collector/lmstat_feature_exp_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func parseLmstatLicenseFeatureExpDate(outStr [][]string) map[int]*featureExp {
if !lmutilLicenseFeatureExpRegex.MatchString(lineJoined) {
continue
}

matches := lmutilLicenseFeatureExpRegex.FindStringSubmatch(lineJoined)
// Parse date, month has to be capitalized.
slice := strings.Split(matches[4], "-")
Expand All @@ -64,6 +65,7 @@ func parseLmstatLicenseFeatureExpDate(outStr [][]string) map[int]*featureExp {
}

index++

featuresExp[index] = &featureExp{
name: matches[1],
expires: expires,
Expand All @@ -72,6 +74,7 @@ func parseLmstatLicenseFeatureExpDate(outStr [][]string) map[int]*featureExp {
version: matches[2],
}
}

return featuresExp
}

Expand Down Expand Up @@ -140,5 +143,6 @@ func (c *lmstatFeatureExpCollector) getLmstatFeatureExpDate(ch chan<- prometheus
feature.version)
}
}

return nil
}
2 changes: 1 addition & 1 deletion collector/lmstat_feature_exp_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func TestParseLmstatLicenseFeatureExpDate(t *testing.T) {
}

featuresExp := parseLmstatLicenseFeatureExpDate(dataStr)

found := false

for index, feature := range featuresExp {
if feature.name == "feature_11" {
if feature.version != v201812String ||
Expand Down
19 changes: 19 additions & 0 deletions collector/lmstat_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func contains(slice []string, item string) bool {
}

_, ok := set[item]

return ok
}

Expand All @@ -71,6 +72,7 @@ func lmutilOutput(args ...string) ([]byte, error) {
*lmutilPath, strings.Join(args, " "), err)
}
}

return out, err
}

Expand All @@ -92,21 +94,25 @@ func splitOutput(lmutilOutput []byte) ([][]string, error) {
keys := make(map[string]int)

res := make([][]string, len(result))

for _, v := range result {
key := v[0]
if _, ok := keys[key]; ok {
keys[key]++

v[0] = strings.TrimSpace(v[0]) + strconv.Itoa(keys[key])
} else {
keys[key] = 1
}
res = append(res, v)
}

return res, err
}

func parseLmstatVersion(outStr [][]string) lmstatInformation {
lmstatInfo = lmstatInformation{arch: notFound, build: notFound, version: notFound}

for _, line := range outStr {
lineJoined := strings.Join(line, "")
if lmutilVersionRegex.MatchString(lineJoined) {
Expand All @@ -117,18 +123,21 @@ func parseLmstatVersion(outStr [][]string) lmstatInformation {
for i, n := range matches {
md[names[i]] = n
}

lmstatInfo = lmstatInformation{
arch: md["arch"],
build: md["build"],
version: md["version"],
}
}
}

return lmstatInfo
}

func parseLmstatLicenseInfoServer(outStr [][]string) map[string]*server {
servers = make(map[string]*server)

for _, line := range outStr {
lineJoined := strings.Join(line, "")
if lmutilLicenseServersRegex.MatchString(lineJoined) {
Expand All @@ -150,11 +159,13 @@ func parseLmstatLicenseInfoServer(outStr [][]string) map[string]*server {
}
}
}

return servers
}

func parseLmstatLicenseInfoVendor(outStr [][]string) map[string]*vendor {
vendors = make(map[string]*vendor)

for _, line := range outStr {
lineJoined := strings.Join(line, "")
if lmutilLicenseVendorStatusRegex.MatchString(lineJoined) {
Expand All @@ -170,6 +181,7 @@ func parseLmstatLicenseInfoVendor(outStr [][]string) map[string]*vendor {
}
}
}

return vendors
}

Expand All @@ -180,6 +192,7 @@ func parseLmstatLicenseInfoFeature(outStr [][]string) (map[string]*feature,
reservGroupByFeature = make(map[string]map[string]float64)
// featureName saved here as index for the user and reservation information.
var featureName string

for _, line := range outStr {
lineJoined := strings.Join(line, "")
if lmutilLicenseFeatureUsageRegex.MatchString(lineJoined) {
Expand All @@ -190,13 +203,15 @@ func parseLmstatLicenseInfoFeature(outStr [][]string) (map[string]*feature,
log.Errorf("could not convert %s to integer: %v", matches[2],
err)
}

featureName = matches[1]

used, err := strconv.Atoi(matches[3])
if err != nil {
log.Errorf("could not convert %s to integer: %v", matches[3],
err)
}

features[featureName] = &feature{
issued: float64(issued),
used: float64(used),
Expand Down Expand Up @@ -236,6 +251,7 @@ func parseLmstatLicenseInfoFeature(outStr [][]string) (map[string]*feature,
reservGroupByFeature[featureName][matches[4]] = float64(groupReserv)
}
}

return features, licUsersByFeature, reservGroupByFeature
}

Expand All @@ -252,9 +268,11 @@ func (c *lmstatCollector) getLmstatInfo(ch chan<- prometheus.Metric) error {
log.Errorln(err)
return err
}

lmstatInfo = parseLmstatVersion(outStr)

ch <- prometheus.MustNewConstMetric(c.lmstatInfo, prometheus.GaugeValue, 1.0, lmstatInfo.arch, lmstatInfo.build, lmstatInfo.version)

return nil
}

Expand Down Expand Up @@ -357,5 +375,6 @@ func (c *lmstatCollector) getLmstatLicensesInfo(ch chan<- prometheus.Metric) err
}
}
}

return nil
}
1 change: 1 addition & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestLoad(t *testing.T) {
}

appRegex := regexp.MustCompile(`^app\d`)

for _, licenses := range testLicenseConfig.Licenses {
if !appRegex.MatchString(licenses.Name) {
t.Fatalf("'%s' not matching expected app name.", licenses.Name)
Expand Down
3 changes: 3 additions & 0 deletions flexlm_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func init() {

func handler(w http.ResponseWriter, r *http.Request) {
var num int

filters := r.URL.Query()["collect[]"]
log.Debugln("collect query:", filters)

Expand All @@ -45,6 +46,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Fatal(num, err)
}

return
}

Expand Down Expand Up @@ -98,6 +100,7 @@ func main() {
}

log.Infof("Enabled collectors:")

for n := range nc.Collectors {
log.Infof(" - %s", n)
}
Expand Down

0 comments on commit 814d058

Please sign in to comment.