Skip to content

Commit

Permalink
Initial commit with functional tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Herbert Fischer committed Oct 19, 2017
0 parents commit 3e56538
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
g_*.go
/dist/

# Binaries for programs and plugins
cassandra-keyspaces-checker
*.exe
*.dll
*.so
*.dylib

# Temp files
*.tmp
*.swp
*.test
*.out
*.log
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) 2017 CrossEngage

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.
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
APPNAME := cassandra-keyspaces-checker
DIST_DIR := ./dist
PLATFORMS := linux-386 linux-amd64 linux-arm

RELEASE := $(shell git describe --tags --always)

build:
go generate
go get -v -t ./...
go test -v ./...
go build -v


cleanup_dist:
rm -rfv $(DIST_DIR)
mkdir -p $(DIST_DIR)

dist: cleanup_dist $(PLATFORMS)
$(PLATFORMS):
$(eval GOOS := $(firstword $(subst -, ,$@)))
$(eval GOARCH := $(lastword $(subst -, ,$@)))
env GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o $(DIST_DIR)/$(APPNAME).$(RELEASE).$@

md5: dist
cd $(DIST_DIR) && md5sum $(APPNAME).$(RELEASE).* | tee MD5SUM

release: md5
go get github.com/tcnksm/ghr
if [ "x$$(git config --global --get github.token)" = "x" ]; then echo "Missing github.token in your git config"; fi
ghr -recreate -u CrossEngage $(RELEASE) $(DIST_DIR)
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# disk-health-checker

Simple tool to run smartctl and print InfluxDB compatible output.

This tool is meant to be used with Telegraf's `inputs.exec` plugin.

The tool needs to have setuid to be able to run `smartctl`.
3 changes: 3 additions & 0 deletions g_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
version=`git describe --all --always --dirty --long`
printf "package main\nconst (\nversion=\`$version\`\n)\n" | gofmt | tee g_version.go
137 changes: 137 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
//go:generate bash ./g_version.sh
package main

import (
"encoding/json"
"fmt"
"log"
"log/syslog"
"net/http"
"net/url"
"os"
"path"
"strings"
"time"

"gopkg.in/alecthomas/kingpin.v2"
)

var (
appName = path.Base(os.Args[0])
app = kingpin.New(appName, "A telegraf input plugin that gatters metrics for every keyspace and table, by CrossEngage")
checkName = app.Flag("name", "Check name").Default(appName).String()
jolokiaBaseURL = app.Flag("jolokia", "The base URL of the jolokia agent running on Cassandra JVM").Default("http://localhost:1778/jolokia").URL()
debug = app.Flag("debug", "If set, enables debug logs").Default("false").Bool()
stderr = app.Flag("stderr", "If set, enables logging to stderr instead of syslog").Default("false").Bool()
)

func main() {
app.Version(version)
kingpin.MustParse(app.Parse(os.Args[1:]))

if *debug {
log.SetFlags(log.LstdFlags | log.Lshortfile)
}

if *stderr {
log.SetOutput(os.Stderr)
} else {
slog, err := syslog.New(syslog.LOG_NOTICE|syslog.LOG_DAEMON, appName)
if err != nil {
log.Fatal(err)
}
log.SetOutput(slog)

}

hostname, err := os.Hostname()
if err != nil {
log.Fatal(err)
}

keys := []string{*checkName, "host=" + hostname}

loc, err := url.Parse((*jolokiaBaseURL).String() + "/read/org.apache.cassandra.metrics:type=ColumnFamily,keyspace=*,scope=*,name=*")
if err != nil {
log.Fatal(err)
}

// TODO timeouts
tr := &http.Transport{}
client := &http.Client{Transport: tr}
resp, err := client.Get(loc.String())
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatalf("%s %s", loc, resp.Status)
}

jsonResp := &jsonResp{}
if err := json.NewDecoder(resp.Body).Decode(jsonResp); err != nil {
log.Fatal(err)
}

if jsonResp.Status != 200 || jsonResp.Error != nil {
log.Fatal(jsonResp.Error)
}

timestamp := time.Unix(jsonResp.TimeStamp, 0)
commonKey := strings.Join(keys, ",")

for keyPath, valueMap := range jsonResp.Value {
tags := []string{}
keyPath = strings.Replace(keyPath, "org.apache.cassandra.metrics:", "", 1)
keyParts := strings.Split(keyPath, ",")
for _, part := range keyParts {
kv := strings.Split(part, "=")
switch kv[0] {
case "keyspace":
tags = append(tags, "keyspace="+kv[1])
case "name":
tags = append(tags, "metric="+kv[1])
case "scope":
tags = append(tags, "cf="+kv[1])
}
}

values := []string{}
for valueKey, value := range valueMap {
if value == nil {
continue
}
switch v := value.(type) {
case []int64, []int32, []int16, []int8, []int, []uint64, []uint32, []uint16, []uint8, []uint:
continue
case int64, int32, int16, int8, int, uint64, uint32, uint16, uint8, uint:
values = append(values, fmt.Sprintf(`%s=%di`, valueKey, v))
case string:
values = append(values, fmt.Sprintf(`%s="%s"`, valueKey, v))
default:
values = append(values, fmt.Sprintf(`%s=%v`, valueKey, v))
}
}

if len(values) > 0 {
fmt.Print(commonKey, ",", strings.Join(tags, ","))
fmt.Print(" ")
fmt.Print(strings.Join(values, ","))
fmt.Print(" ")
fmt.Println(timestamp.UnixNano())
}
}
}

type jsonResp struct {
Request struct {
MBean string `json:"mbean"`
Type string `json:"type"`
} `json:"request"`
Status int `json:"status"`
Error error `json:"error"`
ErrorType string `json:"error_type"`
StackTrace string `json:"stacktrace"`
TimeStamp int64 `json:"timestamp"`
Value map[string]map[string]interface{} `json:"value"`
}

0 comments on commit 3e56538

Please sign in to comment.