Skip to content

Commit

Permalink
done lvm_exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostiam committed Oct 7, 2020
1 parent 0830ac2 commit e23c26f
Show file tree
Hide file tree
Showing 15 changed files with 3,002 additions and 0 deletions.
70 changes: 70 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
version: 2.1

executors:
# Whenever the Go version is updated here, .promu.yml should also be updated.
golang:
docker:
- image: circleci/golang:1.15

jobs:
test:
executor: golang
steps:
- checkout
- run: make

build:
executor: golang
working_directory: /home/circleci/.go_workspace/src/github.com/ghostiam/lvm_exporter

steps:
- setup_remote_docker:
version: 18.09.3
- checkout
- run: make promu
- run: promu crossbuild
- persist_to_workspace:
root: .
paths:
- .build
- store_artifacts:
path: .build
destination: /build

release_tags:
executor: golang

steps:
- checkout
- attach_workspace:
at: .
- run: make promu
- run: promu crossbuild tarballs
- run: promu checksum .tarballs
- run: promu release .tarballs
- store_artifacts:
path: .tarballs
destination: releases

workflows:
version: 2
lvm_exporter:
jobs:
- test:
filters:
tags:
only: /.*/
- build:
filters:
tags:
only: /.*/
- release_tags:
requires:
- test
- build
filters:
tags:
only: /^v[0-9]+(\.[0-9]+){2}(-.+|[^-.]*)$/
branches:
ignore: /.*/
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
dependencies-stamp
/lvm_exporter
/.build
/.deps
/.release
/.tarballs

# Intellij

/.idea
*.iml

/vendor
50 changes: 50 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
run:
tests: true

output:
print-issued-lines: true

linters:
enable:
- bodyclose
- exhaustive
- exportloopref
- goconst
- gocritic
- goerr113
- goimports
- golint
- gosimple
- govet
- nakedret
- rowserrcheck
- staticcheck
- unconvert
- whitespace

issues:
exclude-rules:
- path: _test.go
linters:
- errcheck

linters-settings:
govet:
enable-all: true

errcheck:
exclude: scripts/errcheck_excludes.txt

whitespace:
multi-if: false # Enforces newlines (or comments) after every multi-line if statement
multi-func: false # Enforces newlines (or comments) after every multi-line function signature

exhaustive:
# indicates that switch statements are to be considered exhaustive if a
# 'default' case is present, even if all enum members aren't listed in the
# switch
# default-signifies-exhaustive: false

nakedret:
# make an issue if func has more lines of code than this setting and it has naked returns; default is 30
max-func-lines: 1
26 changes: 26 additions & 0 deletions .promu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
verbose: false
go:
# Whenever the Go version is updated here, .travis.yml and
# .circle/config.yml should also be updated.
version: 1.15
cgo: false
repository:
path: github.com/ghostiam/lvm_exporter
build:
binaries:
- name: lvm_exporter
prefix: .
flags: -a -tags netgo
ldflags: |
-X {{repoPath}}/vendor/github.com/prometheus/common/version.Version={{.Version}}
-X {{repoPath}}/vendor/github.com/prometheus/common/version.Revision={{.Revision}}
-X {{repoPath}}/vendor/github.com/prometheus/common/version.Branch={{.Branch}}
-X {{repoPath}}/vendor/github.com/prometheus/common/version.BuildUser={{user}}@{{host}}
-X {{repoPath}}/vendor/github.com/prometheus/common/version.BuildDate={{date "20060102-15:04:05"}}
tarball:
files:
- LICENSE
crossbuild:
platforms:
- linux/amd64
- linux/386
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Vladislav Fursov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
59 changes: 59 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
GO := go
PROMU := $(GOPATH)/bin/promu

PREFIX ?= $(shell pwd)
BIN_DIR ?= $(shell pwd)

GOLANGCI_LINT := $(GOPATH)/bin/golangci-lint
GOLANGCI_LINT_VERSION ?= v1.31.0

all: format lint build test

style:
@echo ">> checking code style"
@! gofmt -d $(shell find . -path ./vendor -prune -o -name '*.go' -print) | grep '^'

test:
@echo ">> running tests"
@$(GO) test -short -race ./...

format:
@echo ">> formatting code"
@$(GO) fmt $(pkgs)

vet:
@echo ">> vetting code"
@$(GO) vet $(pkgs)

build: $(PROMU)
@echo ">> building binaries"
$(PROMU) build --prefix $(PREFIX)

crossbuild: $(PROMU)
@echo ">> crossbuilding binaries"
$(PROMU) crossbuild -v

tarball: $(PROMU)
@echo ">> building release tarball"
@$(PROMU) tarball --prefix $(PREFIX) $(BIN_DIR)

lint: $(GOLANGCI_LINT)
@echo ">> running golangci-lint"
GO111MODULE=on $(GO) list -e -compiled -test=true -export=false -deps=true -find=false -tags= -- ./... > /dev/null
GO111MODULE=on $(GOLANGCI_LINT) run --sort-results ./...

# deps
promu: $(PROMU)

$(PROMU):
@GOOS=$(shell uname -s | tr A-Z a-z) \
GOARCH=$(subst x86_64,amd64,$(patsubst i%86,386,$(shell uname -m))) \
$(GO) get -u github.com/prometheus/promu

$(GOLANGCI_LINT):
mkdir -p $(GOPATH)/bin
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/$(GOLANGCI_LINT_VERSION)/install.sh \
| sed -e '/install -d/d' \
| sh -s -- -b $(GOPATH)/bin $(GOLANGCI_LINT_VERSION)

.PHONY: all style format build crossbuild test vet tarball promu lint $(PROMU) $(GOLANGCI_LINT)
93 changes: 93 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//go:generate go run ../generator/fields_generator.go -o fields_gen.go
package collector

import (
"os/exec"
"strings"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)

type Collector struct {
cfg *Config
logger log.Logger
}

type Config struct {
}

func New(cfg *Config, logger log.Logger) (*Collector, error) {
if cfg == nil {
cfg = &Config{}
}

return &Collector{
cfg: cfg,
logger: logger,
}, nil
}

func (c *Collector) Describe(ch chan<- *prometheus.Desc) {}

func (c *Collector) Collect(ch chan<- prometheus.Metric) {
err := execToMetrics("lvs", ch)
if err != nil {
level.Error(c.logger).Log("err", err)
}

err = execToMetrics("vgs", ch)
if err != nil {
level.Error(c.logger).Log("err", err)
}

err = execToMetrics("pvs", ch)
if err != nil {
level.Error(c.logger).Log("err", err)
}
}

func execToMetrics(exe string, ch chan<- prometheus.Metric) error {
cmd := exec.Command(exe,
"--reportformat",
"json",
"-o",
"all",
"--units",
"B",
"--binary",
)

out, err := cmd.Output()
if err != nil {
return err
}

metrics, err := ReportsToMetrics(out)
if err != nil {
return err
}

for _, ms := range metrics {
var labelsNames, labelsValues []string
for _, l := range ms.Labels {
labelsNames = append(labelsNames, l.Field)
labelsValues = append(labelsValues, l.Value)
}

for _, m := range ms.Metrics {
gauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "lvm",
Subsystem: ms.Namespace,
Name: strings.TrimPrefix(m.Field, ms.Namespace+"_"),
Help: m.Help,
ConstLabels: nil,
}, labelsNames).WithLabelValues(labelsValues...)
gauge.Set(m.Value)
ch <- gauge
}
}

return nil
}
Loading

0 comments on commit e23c26f

Please sign in to comment.