mirror of
https://github.com/Luzifer/elb-instance-status.git
synced 2024-09-08 01:39:07 +00:00
Modernize code, fix linter errors
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
153f0a30ab
commit
0ff894bbad
176
.golangci.yml
Normal file
176
.golangci.yml
Normal file
@ -0,0 +1,176 @@
|
||||
# Derived from https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml
|
||||
|
||||
---
|
||||
|
||||
run:
|
||||
# timeout for analysis, e.g. 30s, 5m, default is 1m
|
||||
timeout: 5m
|
||||
# Force readonly modules usage for checking
|
||||
modules-download-mode: readonly
|
||||
|
||||
output:
|
||||
formats:
|
||||
- format: tab
|
||||
path: stdout
|
||||
|
||||
issues:
|
||||
# This disables the included exclude-list in golangci-lint as that
|
||||
# list for example fully hides G304 gosec rule, errcheck, exported
|
||||
# rule of revive and other errors one really wants to see.
|
||||
# Smme detail: https://github.com/golangci/golangci-lint/issues/456
|
||||
exclude-use-default: false
|
||||
# Don't limit the number of shown issues: Report ALL of them
|
||||
max-issues-per-linter: 0
|
||||
max-same-issues: 0
|
||||
|
||||
linters:
|
||||
disable-all: true
|
||||
enable:
|
||||
- asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers [fast: true, auto-fix: false]
|
||||
- bidichk # Checks for dangerous unicode character sequences [fast: true, auto-fix: false]
|
||||
- bodyclose # checks whether HTTP response body is closed successfully [fast: true, auto-fix: false]
|
||||
- containedctx # containedctx is a linter that detects struct contained context.Context field [fast: true, auto-fix: false]
|
||||
- contextcheck # check the function whether use a non-inherited context [fast: false, auto-fix: false]
|
||||
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) [fast: true, auto-fix: false]
|
||||
- durationcheck # check for two durations multiplied together [fast: false, auto-fix: false]
|
||||
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: false, auto-fix: false]
|
||||
- errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occations, where the check for the returned error can be omitted. [fast: false, auto-fix: false]
|
||||
- exportloopref # checks for pointers to enclosing loop variables [fast: true, auto-fix: false]
|
||||
- forbidigo # Forbids identifiers [fast: true, auto-fix: false]
|
||||
- funlen # Tool for detection of long functions [fast: true, auto-fix: false]
|
||||
- gocognit # Computes and checks the cognitive complexity of functions [fast: true, auto-fix: false]
|
||||
- goconst # Finds repeated strings that could be replaced by a constant [fast: true, auto-fix: false]
|
||||
- gocritic # The most opinionated Go source code linter [fast: true, auto-fix: false]
|
||||
- gocyclo # Computes and checks the cyclomatic complexity of functions [fast: true, auto-fix: false]
|
||||
- godox # Tool for detection of FIXME, TODO and other comment keywords [fast: true, auto-fix: false]
|
||||
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification [fast: true, auto-fix: true]
|
||||
- gofumpt # Gofumpt checks whether code was gofumpt-ed. [fast: true, auto-fix: true]
|
||||
- goimports # Goimports does everything that gofmt does. Additionally it checks unused imports [fast: true, auto-fix: true]
|
||||
- gomnd # An analyzer to detect magic numbers. [fast: true, auto-fix: false]
|
||||
- gosec # Inspects source code for security problems [fast: true, auto-fix: false]
|
||||
- gosimple # Linter for Go source code that specializes in simplifying a code [fast: true, auto-fix: false]
|
||||
- govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: true, auto-fix: false]
|
||||
- ineffassign # Detects when assignments to existing variables are not used [fast: true, auto-fix: false]
|
||||
- misspell # Finds commonly misspelled English words in comments [fast: true, auto-fix: true]
|
||||
- nakedret # Finds naked returns in functions greater than a specified function length [fast: true, auto-fix: false]
|
||||
- nilerr # Finds the code that returns nil even if it checks that the error is not nil. [fast: false, auto-fix: false]
|
||||
- nilnil # Checks that there is no simultaneous return of `nil` error and an invalid value. [fast: false, auto-fix: false]
|
||||
- noctx # noctx finds sending http request without context.Context [fast: true, auto-fix: false]
|
||||
- nolintlint # Reports ill-formed or insufficient nolint directives [fast: true, auto-fix: false]
|
||||
- revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint. [fast: false, auto-fix: false]
|
||||
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks [fast: true, auto-fix: false]
|
||||
- stylecheck # Stylecheck is a replacement for golint [fast: true, auto-fix: false]
|
||||
- tenv # tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17 [fast: false, auto-fix: false]
|
||||
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code [fast: true, auto-fix: false]
|
||||
- unconvert # Remove unnecessary type conversions [fast: true, auto-fix: false]
|
||||
- unused # Checks Go code for unused constants, variables, functions and types [fast: false, auto-fix: false]
|
||||
- wastedassign # wastedassign finds wasted assignment statements. [fast: false, auto-fix: false]
|
||||
- wrapcheck # Checks that errors returned from external packages are wrapped [fast: false, auto-fix: false]
|
||||
|
||||
linters-settings:
|
||||
funlen:
|
||||
lines: 100
|
||||
statements: 60
|
||||
|
||||
gocyclo:
|
||||
# minimal code complexity to report, 30 by default (but we recommend 10-20)
|
||||
min-complexity: 15
|
||||
|
||||
gomnd:
|
||||
settings:
|
||||
mnd:
|
||||
ignored-functions: 'strconv.(?:Format|Parse)\B+'
|
||||
|
||||
revive:
|
||||
rules:
|
||||
#- name: add-constant # Suggests using constant for magic numbers and string literals
|
||||
# Opinion: Makes sense for strings, not for numbers but checks numbers
|
||||
#- name: argument-limit # Specifies the maximum number of arguments a function can receive | Opinion: Don't need this
|
||||
- name: atomic # Check for common mistaken usages of the `sync/atomic` package
|
||||
- name: banned-characters # Checks banned characters in identifiers
|
||||
arguments:
|
||||
- ';' # Greek question mark
|
||||
- name: bare-return # Warns on bare returns
|
||||
- name: blank-imports # Disallows blank imports
|
||||
- name: bool-literal-in-expr # Suggests removing Boolean literals from logic expressions
|
||||
- name: call-to-gc # Warns on explicit call to the garbage collector
|
||||
#- name: cognitive-complexity # Sets restriction for maximum Cognitive complexity.
|
||||
# There is a dedicated linter for this
|
||||
- name: confusing-naming # Warns on methods with names that differ only by capitalization
|
||||
- name: confusing-results # Suggests to name potentially confusing function results
|
||||
- name: constant-logical-expr # Warns on constant logical expressions
|
||||
- name: context-as-argument # `context.Context` should be the first argument of a function.
|
||||
- name: context-keys-type # Disallows the usage of basic types in `context.WithValue`.
|
||||
#- name: cyclomatic # Sets restriction for maximum Cyclomatic complexity.
|
||||
# There is a dedicated linter for this
|
||||
#- name: datarace # Spots potential dataraces
|
||||
# Is not (yet) available?
|
||||
- name: deep-exit # Looks for program exits in funcs other than `main()` or `init()`
|
||||
- name: defer # Warns on some [defer gotchas](https://blog.learngoprogramming.com/5-gotchas-of-defer-in-go-golang-part-iii-36a1ab3d6ef1)
|
||||
- name: dot-imports # Forbids `.` imports.
|
||||
- name: duplicated-imports # Looks for packages that are imported two or more times
|
||||
- name: early-return # Spots if-then-else statements that can be refactored to simplify code reading
|
||||
- name: empty-block # Warns on empty code blocks
|
||||
- name: empty-lines # Warns when there are heading or trailing newlines in a block
|
||||
- name: errorf # Should replace `errors.New(fmt.Sprintf())` with `fmt.Errorf()`
|
||||
- name: error-naming # Naming of error variables.
|
||||
- name: error-return # The error return parameter should be last.
|
||||
- name: error-strings # Conventions around error strings.
|
||||
- name: exported # Naming and commenting conventions on exported symbols.
|
||||
arguments: ['sayRepetitiveInsteadOfStutters']
|
||||
#- name: file-header # Header which each file should have.
|
||||
# Useless without config, have no config for it
|
||||
- name: flag-parameter # Warns on boolean parameters that create a control coupling
|
||||
#- name: function-length # Warns on functions exceeding the statements or lines max
|
||||
# There is a dedicated linter for this
|
||||
#- name: function-result-limit # Specifies the maximum number of results a function can return
|
||||
# Opinion: Don't need this
|
||||
- name: get-return # Warns on getters that do not yield any result
|
||||
- name: identical-branches # Spots if-then-else statements with identical `then` and `else` branches
|
||||
- name: if-return # Redundant if when returning an error.
|
||||
#- name: imports-blacklist # Disallows importing the specified packages
|
||||
# Useless without config, have no config for it
|
||||
- name: import-shadowing # Spots identifiers that shadow an import
|
||||
- name: increment-decrement # Use `i++` and `i--` instead of `i += 1` and `i -= 1`.
|
||||
- name: indent-error-flow # Prevents redundant else statements.
|
||||
#- name: line-length-limit # Specifies the maximum number of characters in a lined
|
||||
# There is a dedicated linter for this
|
||||
#- name: max-public-structs # The maximum number of public structs in a file.
|
||||
# Opinion: Don't need this
|
||||
- name: modifies-parameter # Warns on assignments to function parameters
|
||||
- name: modifies-value-receiver # Warns on assignments to value-passed method receivers
|
||||
#- name: nested-structs # Warns on structs within structs
|
||||
# Opinion: Don't need this
|
||||
- name: optimize-operands-order # Checks inefficient conditional expressions
|
||||
#- name: package-comments # Package commenting conventions.
|
||||
# Opinion: Don't need this
|
||||
- name: range # Prevents redundant variables when iterating over a collection.
|
||||
- name: range-val-address # Warns if address of range value is used dangerously
|
||||
- name: range-val-in-closure # Warns if range value is used in a closure dispatched as goroutine
|
||||
- name: receiver-naming # Conventions around the naming of receivers.
|
||||
- name: redefines-builtin-id # Warns on redefinitions of builtin identifiers
|
||||
#- name: string-format # Warns on specific string literals that fail one or more user-configured regular expressions
|
||||
# Useless without config, have no config for it
|
||||
- name: string-of-int # Warns on suspicious casts from int to string
|
||||
- name: struct-tag # Checks common struct tags like `json`,`xml`,`yaml`
|
||||
- name: superfluous-else # Prevents redundant else statements (extends indent-error-flow)
|
||||
- name: time-equal # Suggests to use `time.Time.Equal` instead of `==` and `!=` for equality check time.
|
||||
- name: time-naming # Conventions around the naming of time variables.
|
||||
- name: unconditional-recursion # Warns on function calls that will lead to (direct) infinite recursion
|
||||
- name: unexported-naming # Warns on wrongly named un-exported symbols
|
||||
- name: unexported-return # Warns when a public return is from unexported type.
|
||||
- name: unhandled-error # Warns on unhandled errors returned by funcion calls
|
||||
arguments:
|
||||
- "fmt.(Fp|P)rint(f|ln|)"
|
||||
- name: unnecessary-stmt # Suggests removing or simplifying unnecessary statements
|
||||
- name: unreachable-code # Warns on unreachable code
|
||||
- name: unused-parameter # Suggests to rename or remove unused function parameters
|
||||
- name: unused-receiver # Suggests to rename or remove unused method receivers
|
||||
#- name: use-any # Proposes to replace `interface{}` with its alias `any`
|
||||
# Is not (yet) available?
|
||||
- name: useless-break # Warns on useless `break` statements in case clauses
|
||||
- name: var-declaration # Reduces redundancies around variable declaration.
|
||||
- name: var-naming # Naming rules.
|
||||
- name: waitgroup-by-value # Warns on functions taking sync.WaitGroup as a by-value parameter
|
||||
|
||||
...
|
151
checks.go
Normal file
151
checks.go
Normal file
@ -0,0 +1,151 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"golang.org/x/net/context"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const (
|
||||
checkKillWait = 100 * time.Millisecond
|
||||
remoteConfigFetchTimeout = 2 * time.Second
|
||||
)
|
||||
|
||||
type (
|
||||
checkCommand struct {
|
||||
Name string `yaml:"name"`
|
||||
Command string `yaml:"command"`
|
||||
WarnOnly bool `yaml:"warn_only"`
|
||||
}
|
||||
|
||||
checkResult struct {
|
||||
Check checkCommand
|
||||
IsSuccess bool
|
||||
Streak int64
|
||||
}
|
||||
)
|
||||
|
||||
func executeAndRegisterCheck(ctx context.Context, checkID string) {
|
||||
var (
|
||||
check = checks[checkID]
|
||||
logger = logrus.WithField("check_id", checkID)
|
||||
)
|
||||
|
||||
cmd := exec.Command("/bin/bash", "-e", "-o", "pipefail", "-c", check.Command) //#nosec G204 // Intended to run an user-defined command
|
||||
cmd.Stderr = logger.WithField("stream", "STDERR").Writer()
|
||||
if cfg.Verbose {
|
||||
cmd.Stdout = logger.WithField("stream", "STDOUT").Writer()
|
||||
}
|
||||
err := cmd.Start()
|
||||
|
||||
if err == nil {
|
||||
cmdDone := make(chan error)
|
||||
go func(cmdDone chan error, cmd *exec.Cmd) { cmdDone <- cmd.Wait() }(cmdDone, cmd)
|
||||
loop := true
|
||||
for loop {
|
||||
select {
|
||||
case err = <-cmdDone:
|
||||
loop = false
|
||||
case <-ctx.Done():
|
||||
logger.Error("execution of check will be killed through context timeout")
|
||||
if err := cmd.Process.Kill(); err != nil {
|
||||
logger.WithError(err).Error("killing check command")
|
||||
}
|
||||
time.Sleep(checkKillWait)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
success := err == nil
|
||||
|
||||
checkResultsLock.Lock()
|
||||
|
||||
if _, ok := checkResults[checkID]; !ok {
|
||||
checkResults[checkID] = &checkResult{
|
||||
Check: check,
|
||||
}
|
||||
}
|
||||
|
||||
if success == checkResults[checkID].IsSuccess {
|
||||
checkResults[checkID].Streak++
|
||||
} else {
|
||||
checkResults[checkID].IsSuccess = success
|
||||
checkResults[checkID].Streak = 1
|
||||
}
|
||||
|
||||
if !success {
|
||||
logger.WithError(err).WithField("streak", checkResults[checkID].Streak).Warn("check failed, streak increased")
|
||||
}
|
||||
|
||||
lastResultRegistered = time.Now()
|
||||
|
||||
checkResultsLock.Unlock()
|
||||
}
|
||||
|
||||
func loadChecks() error {
|
||||
var rawChecks io.Reader
|
||||
|
||||
if _, err := os.Stat(cfg.CheckDefinitionsFile); err == nil {
|
||||
// We got a local file, read it
|
||||
f, err := os.Open(cfg.CheckDefinitionsFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("opening file: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
if err := f.Close(); err != nil {
|
||||
logrus.WithError(err).Error("closing configfile (leaked fd)")
|
||||
}
|
||||
}()
|
||||
rawChecks = f
|
||||
} else {
|
||||
// Check whether we got an URL
|
||||
if _, err := url.Parse(cfg.CheckDefinitionsFile); err != nil {
|
||||
return errors.New("definitions file is neither a local file nor a URL")
|
||||
}
|
||||
|
||||
// We got an URL, fetch and read it
|
||||
ctx, cancel := context.WithTimeout(context.TODO(), remoteConfigFetchTimeout)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, cfg.CheckDefinitionsFile, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("executing request: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
if err := resp.Body.Close(); err != nil {
|
||||
logrus.WithError(err).Error("closing configfile request body (leaked fd)")
|
||||
}
|
||||
}()
|
||||
rawChecks = resp.Body
|
||||
}
|
||||
|
||||
tmpResult := map[string]checkCommand{}
|
||||
if err := yaml.NewDecoder(rawChecks).Decode(&tmpResult); err != nil {
|
||||
return fmt.Errorf("decoding checks file: %w", err)
|
||||
}
|
||||
|
||||
checks = tmpResult
|
||||
return nil
|
||||
}
|
||||
|
||||
func spawnChecks() {
|
||||
ctx, _ := context.WithTimeout(context.Background(), cfg.CheckInterval-time.Second)
|
||||
|
||||
for id := range checks {
|
||||
go executeAndRegisterCheck(ctx, id)
|
||||
}
|
||||
}
|
6
go.mod
6
go.mod
@ -4,14 +4,14 @@ go 1.22.1
|
||||
|
||||
require (
|
||||
github.com/Luzifer/rconfig/v2 v2.5.0
|
||||
github.com/gorilla/mux v1.8.1
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
golang.org/x/net v0.22.0
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
golang.org/x/sys v0.18.0 // indirect
|
||||
gopkg.in/validator.v2 v2.0.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
13
go.sum
13
go.sum
@ -1,9 +1,8 @@
|
||||
github.com/Luzifer/rconfig/v2 v2.5.0 h1:zx5lfQbNX3za4VegID97IeY+M+BmfgHxWJTYA94sxok=
|
||||
github.com/Luzifer/rconfig/v2 v2.5.0/go.mod h1:eGWUPQeCPv/Pr/p0hjmwFgI20uqvwi/Szen69hUzGzU=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
||||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
||||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
@ -12,18 +11,24 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
|
||||
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
||||
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/validator.v2 v2.0.1 h1:xF0KWyGWXm/LM2G1TrEjqOu4pa6coO9AlWSf3msVfDY=
|
||||
gopkg.in/validator.v2 v2.0.1/go.mod h1:lIUZBlB3Im4s/eYp39Ry/wkR02yOPhZ9IwIRBjuPuG8=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
201
main.go
201
main.go
@ -2,22 +2,16 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/robfig/cron/v3"
|
||||
"golang.org/x/net/context"
|
||||
"gopkg.in/yaml.v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/Luzifer/rconfig/v2"
|
||||
)
|
||||
@ -30,7 +24,8 @@ var (
|
||||
CheckInterval time.Duration `flag:"check-interval" default:"1m" description:"How often to execute checks (do not set below 10s!)"`
|
||||
ConfigRefreshInterval time.Duration `flag:"config-refresh" default:"10m" description:"How often to update checks from definitions file / url"`
|
||||
|
||||
Verbose bool `flag:"verbose,v" default:"false" description:"Attach stdout of the executed commands"`
|
||||
Verbose bool `flag:"verbose,v" default:"false" description:"Attach stdout of the executed commands"`
|
||||
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
|
||||
|
||||
Listen string `flag:"listen" default:":3000" description:"IP/Port to listen on for ELB health checks"`
|
||||
VersionAndExit bool `flag:"version" default:"false" description:"Print version and exit"`
|
||||
@ -44,157 +39,73 @@ var (
|
||||
lastResultRegistered time.Time
|
||||
)
|
||||
|
||||
type checkCommand struct {
|
||||
Name string `yaml:"name"`
|
||||
Command string `yaml:"command"`
|
||||
WarnOnly bool `yaml:"warn_only"`
|
||||
WarnOnlyOld *bool `yaml:"warn-only"`
|
||||
}
|
||||
|
||||
type checkResult struct {
|
||||
Check checkCommand
|
||||
IsSuccess bool
|
||||
Streak int64
|
||||
}
|
||||
|
||||
func init() {
|
||||
rconfig.Parse(&cfg)
|
||||
|
||||
if cfg.VersionAndExit {
|
||||
fmt.Printf("elb-instance-status %s\n", version)
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
func loadChecks() error {
|
||||
var rawChecks io.Reader
|
||||
|
||||
if _, err := os.Stat(cfg.CheckDefinitionsFile); err == nil {
|
||||
// We got a local file, read it
|
||||
f, err := os.Open(cfg.CheckDefinitionsFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
rawChecks = f
|
||||
} else {
|
||||
// Check whether we got an URL
|
||||
if _, err := url.Parse(cfg.CheckDefinitionsFile); err != nil {
|
||||
return errors.New("Definitions file is neither a local file nor a URL")
|
||||
}
|
||||
|
||||
// We got an URL, fetch and read it
|
||||
resp, err := http.Get(cfg.CheckDefinitionsFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
rawChecks = resp.Body
|
||||
func initApp() (err error) {
|
||||
rconfig.AutoEnv(true)
|
||||
if err = rconfig.Parse(&cfg); err != nil {
|
||||
return fmt.Errorf("parsing CLI options: %w", err)
|
||||
}
|
||||
|
||||
tmpResult := map[string]checkCommand{}
|
||||
if err := yaml.NewDecoder(rawChecks).Decode(&tmpResult); err != nil {
|
||||
return err
|
||||
l, err := logrus.ParseLevel(cfg.LogLevel)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parsing log-level: %w", err)
|
||||
}
|
||||
logrus.SetLevel(l)
|
||||
|
||||
for name, check := range tmpResult {
|
||||
if check.WarnOnlyOld != nil {
|
||||
log.Printf("Parameter 'warn-only' in check %q is deprecated: It's now named 'warn_only'", name)
|
||||
check.WarnOnly = *check.WarnOnlyOld
|
||||
}
|
||||
}
|
||||
|
||||
checks = tmpResult
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := loadChecks(); err != nil {
|
||||
log.Fatalf("Unable to read definitions file: %s", err)
|
||||
var err error
|
||||
if err = initApp(); err != nil {
|
||||
logrus.WithError(err).Fatal("initializing app")
|
||||
}
|
||||
|
||||
if cfg.VersionAndExit {
|
||||
fmt.Printf("elb-instance-status %s\n", version) //nolint:forbidigo
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if err = loadChecks(); err != nil {
|
||||
logrus.WithError(err).Fatal("reading definitions file")
|
||||
}
|
||||
|
||||
c := cron.New()
|
||||
c.AddFunc("@every "+cfg.CheckInterval.String(), spawnChecks)
|
||||
c.AddFunc("@every "+cfg.ConfigRefreshInterval.String(), func() {
|
||||
|
||||
if _, err = c.AddFunc(fmt.Sprintf("@every %s", cfg.CheckInterval), spawnChecks); err != nil {
|
||||
logrus.WithError(err).Fatal("registering spawn function")
|
||||
}
|
||||
|
||||
if _, err = c.AddFunc(fmt.Sprintf("@every %s", cfg.ConfigRefreshInterval), func() {
|
||||
if err := loadChecks(); err != nil {
|
||||
log.Printf("Unable to refresh checks: %s", err)
|
||||
logrus.WithError(err).Error("refreshing checks")
|
||||
}
|
||||
})
|
||||
}); err != nil {
|
||||
logrus.WithError(err).Fatal("registering config-refresh function")
|
||||
}
|
||||
|
||||
c.Start()
|
||||
|
||||
spawnChecks()
|
||||
|
||||
r := mux.NewRouter()
|
||||
r.HandleFunc("/status", handleELBHealthCheck)
|
||||
if err := http.ListenAndServe(cfg.Listen, r); err != nil {
|
||||
log.Fatalf("Unable to listen: %s", err)
|
||||
http.HandleFunc("/status", handleELBHealthCheck)
|
||||
|
||||
server := &http.Server{
|
||||
Addr: cfg.Listen,
|
||||
Handler: http.DefaultServeMux,
|
||||
ReadHeaderTimeout: time.Second,
|
||||
}
|
||||
|
||||
if err = server.ListenAndServe(); err != nil {
|
||||
logrus.WithError(err).Fatal("listening for HTTP traffic")
|
||||
}
|
||||
}
|
||||
|
||||
func spawnChecks() {
|
||||
ctx, _ := context.WithTimeout(context.Background(), cfg.CheckInterval-time.Second)
|
||||
|
||||
for id := range checks {
|
||||
go executeAndRegisterCheck(ctx, id)
|
||||
}
|
||||
}
|
||||
|
||||
func executeAndRegisterCheck(ctx context.Context, checkID string) {
|
||||
check := checks[checkID]
|
||||
|
||||
cmd := exec.Command("/bin/bash", "-e", "-o", "pipefail", "-c", check.Command)
|
||||
cmd.Stderr = newPrefixedLogger(os.Stderr, checkID+":STDERR")
|
||||
if cfg.Verbose {
|
||||
cmd.Stdout = newPrefixedLogger(os.Stderr, checkID+":STDOUT")
|
||||
}
|
||||
err := cmd.Start()
|
||||
|
||||
if err == nil {
|
||||
cmdDone := make(chan error)
|
||||
go func(cmdDone chan error, cmd *exec.Cmd) { cmdDone <- cmd.Wait() }(cmdDone, cmd)
|
||||
loop := true
|
||||
for loop {
|
||||
select {
|
||||
case err = <-cmdDone:
|
||||
loop = false
|
||||
case <-ctx.Done():
|
||||
log.Printf("Execution of check '%s' was killed through context timeout.", checkID)
|
||||
cmd.Process.Kill()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
success := err == nil
|
||||
|
||||
checkResultsLock.Lock()
|
||||
|
||||
if _, ok := checkResults[checkID]; !ok {
|
||||
checkResults[checkID] = &checkResult{
|
||||
Check: check,
|
||||
}
|
||||
}
|
||||
|
||||
if success == checkResults[checkID].IsSuccess {
|
||||
checkResults[checkID].Streak++
|
||||
} else {
|
||||
checkResults[checkID].IsSuccess = success
|
||||
checkResults[checkID].Streak = 1
|
||||
}
|
||||
|
||||
if !success {
|
||||
log.Printf("Check %q failed, streak now at %d, error was: %s", checkID, checkResults[checkID].Streak, err)
|
||||
}
|
||||
|
||||
lastResultRegistered = time.Now()
|
||||
|
||||
checkResultsLock.Unlock()
|
||||
}
|
||||
|
||||
func handleELBHealthCheck(res http.ResponseWriter, r *http.Request) {
|
||||
healthy := true
|
||||
start := time.Now()
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
func handleELBHealthCheck(w http.ResponseWriter, _ *http.Request) {
|
||||
var (
|
||||
healthy = true
|
||||
start = time.Now()
|
||||
buf = new(bytes.Buffer)
|
||||
)
|
||||
|
||||
checkResultsLock.RLock()
|
||||
for _, cr := range checkResults {
|
||||
@ -214,13 +125,15 @@ func handleELBHealthCheck(res http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
checkResultsLock.RUnlock()
|
||||
|
||||
res.Header().Set("X-Collection-Parsed-In", strconv.FormatInt(time.Since(start).Nanoseconds()/int64(time.Microsecond), 10)+"ms")
|
||||
res.Header().Set("X-Last-Result-Registered-At", lastResultRegistered.Format(time.RFC1123))
|
||||
w.Header().Set("X-Collection-Parsed-In", strconv.FormatInt(time.Since(start).Nanoseconds()/int64(time.Microsecond), 10)+"ms")
|
||||
w.Header().Set("X-Last-Result-Registered-At", lastResultRegistered.Format(time.RFC1123))
|
||||
if healthy {
|
||||
res.WriteHeader(http.StatusOK)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
} else {
|
||||
res.WriteHeader(http.StatusInternalServerError)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
io.Copy(res, buf)
|
||||
if _, err := io.Copy(w, buf); err != nil {
|
||||
logrus.WithError(err).Error("writing HTTP response body")
|
||||
}
|
||||
}
|
||||
|
@ -1,51 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type prefixedLogger struct {
|
||||
channel string
|
||||
wrappedWriter io.Writer
|
||||
|
||||
buffer []byte
|
||||
bufferLock sync.Mutex
|
||||
}
|
||||
|
||||
func newPrefixedLogger(wrappedWriter io.Writer, channel string) *prefixedLogger {
|
||||
return &prefixedLogger{
|
||||
channel: channel,
|
||||
wrappedWriter: wrappedWriter,
|
||||
buffer: []byte{},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *prefixedLogger) dropCR(data []byte) []byte {
|
||||
if len(data) > 0 && data[len(data)-1] == '\r' {
|
||||
return data[0 : len(data)-1]
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func (p *prefixedLogger) Write(in []byte) (n int, err error) {
|
||||
p.bufferLock.Lock()
|
||||
defer p.bufferLock.Unlock()
|
||||
|
||||
n = len(in)
|
||||
p.buffer = append(p.buffer, in...)
|
||||
|
||||
for {
|
||||
if i := bytes.IndexByte(p.buffer, '\n'); i >= 0 {
|
||||
// We have a full newline-terminated line.
|
||||
fmt.Fprintf(p.wrappedWriter, "[%s] %s\n", p.channel, string(p.dropCR(p.buffer[0:i])))
|
||||
p.buffer = p.buffer[i+1 : len(p.buffer)]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPrefixedLogger(t *testing.T) {
|
||||
var (
|
||||
buf = bytes.NewBuffer([]byte{})
|
||||
pl = newPrefixedLogger(buf, "baum")
|
||||
n int
|
||||
err error
|
||||
)
|
||||
|
||||
n, err = pl.Write([]byte("non-newline terminated string"))
|
||||
if n != 29 || err != nil {
|
||||
t.Fatalf("Write to prefixedLogger had unexpected results: n=29 != %d, err=nil != %s", n, err)
|
||||
}
|
||||
|
||||
if n = len(buf.Bytes()); n != 0 {
|
||||
t.Fatalf("Buffer contains %d characters, should contain 0", n)
|
||||
}
|
||||
|
||||
pl.Write([]byte("now a newline\nand something"))
|
||||
if n = len(buf.Bytes()); n != 50 {
|
||||
t.Fatalf("Buffer contains %d characters, should contain 50", n)
|
||||
}
|
||||
|
||||
pl.Write([]byte(" more to log\nwith multiple\nnewlines\n"))
|
||||
if n = len(buf.Bytes()); n != 120 {
|
||||
t.Fatalf("Buffer contains %d characters, should contain 120", n)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user