Skip to content

Commit

Permalink
Merge pull request #14 from fmotrifork/patch-1
Browse files Browse the repository at this point in the history
Upgrade to newest golangci-lint
  • Loading branch information
Lesterpig committed Mar 17, 2020
2 parents 2cd31e5 + faffb16 commit b7c0045
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 7 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ linters-settings:
linters:
enable-all: true
disable:
- gomnd
- gochecknoinits
- gochecknoglobals

Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ env:
- GO111MODULE=on

before_script:
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.17.1
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.24.0

script:
- golangci-lint run # Run a bunch of code checkers/linters in parallel
Expand Down
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func parseConfigString(cnf string) (dir string, name string) {
dir = filepath.Dir(cnf)
basename := filepath.Base(cnf)
name = strings.TrimSuffix(basename, filepath.Ext(basename))

return
}

Expand All @@ -62,13 +63,15 @@ func loadConfig(configPath, configName string) (*Manager, error) {
}

sc := make([]serviceConfig, 0)

err = viper.UnmarshalKey("services", &sc)
if err != nil {
return nil, err
}

manager := Manager{}
manager.Services = make(map[string]([]*Service))

for _, c := range sc {
constructor := probeConstructors[c.Probe]
if constructor == nil {
Expand All @@ -78,6 +81,7 @@ func loadConfig(configPath, configName string) (*Manager, error) {
c.Config = setProbeConfigDefaults(c.Config)

prober := constructor()

err = prober.Init(c.Config)
if err != nil {
return nil, err
Expand All @@ -91,12 +95,14 @@ func loadConfig(configPath, configName string) (*Manager, error) {
}

ac := make([]alertConfig, 0)

err = viper.UnmarshalKey("alerts", &ac)
if err != nil {
return nil, err
}

alerters = make([]alert.Alerter, 0)

for _, c := range ac {
constructor := alertConstructors[c.Type]
if constructor == nil {
Expand Down
1 change: 1 addition & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func Test_parseConfigString(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.args, func(t *testing.T) {
gotDir, gotName := parseConfigString(tt.args)
if gotDir != tt.wantDir {
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var log = logrus.StandardLogger()

func main() {
flag.Parse()

interval := getInterval()

log.Infof("Probe interval: %d", interval)
Expand All @@ -48,11 +49,13 @@ func main() {

func getInterval() int {
intervalEnv := getInt(envy.Get("INTERVAL", ""))

if *intervalCli != 0 {
return *intervalCli
} else if intervalEnv != 0 {
return intervalEnv
}

return 10
}

Expand All @@ -61,5 +64,6 @@ func getInt(s string) int {
if nil != err {
return 0
}

return int(i)
}
4 changes: 4 additions & 0 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Manager struct {
// ProbeLoop starts the main loop that will call ProbeAll regularly.
func (manager *Manager) ProbeLoop(interval time.Duration) {
manager.ProbeAll()

c := time.Tick(interval)
for range c {
manager.ProbeAll()
Expand All @@ -34,7 +35,9 @@ func (manager *Manager) ProbeLoop(interval time.Duration) {
// Everything is done asynchronously.
func (manager *Manager) ProbeAll() {
log.Debug("Probing all")

manager.LastUpdate = time.Now()

for category, services := range manager.Services {
for _, service := range services {
go func(category string, service *Service) {
Expand All @@ -57,6 +60,7 @@ func (manager *Manager) ProbeAll() {
// It uses global configuration for list of alert (`A` variable).
func AlertAll(category string, service *Service) {
date := time.Now().Format("15:04:05 MST")

for _, alert := range alerters {
alert.Alert(service.Status, category, service.Name, service.Message, service.Target, date)
}
Expand Down
2 changes: 2 additions & 0 deletions probe/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type DNS struct {
func (d *DNS) Init(c Config) error {
err := mapstructure.Decode(c.Options, d)
d.Config = c

return err
}

Expand All @@ -29,6 +30,7 @@ func (d *DNS) Probe() (status Status, message string) {

c := new(dns.Client)
r, rtt, err := c.Exchange(m, d.Target+":53")

if err != nil {
return StatusError, err.Error()
}
Expand Down
3 changes: 3 additions & 0 deletions probe/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ type HTTPOptions struct {
// Init configures the probe.
func (h *HTTP) Init(c Config) error {
h.Config = c

var opts HTTPOptions

err := mapstructure.Decode(c.Options, &opts)
if err != nil {
return err
Expand All @@ -45,6 +47,7 @@ func (h *HTTP) Init(c Config) error {
Transport: tr,
}
h.regex, err = regexp.Compile(opts.Regex)

return err
}

Expand Down
14 changes: 13 additions & 1 deletion probe/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ func TestHTTPSuccess(t *testing.T) {
Fatal: 10 * time.Second,
Options: map[string]interface{}{"Regex": "Loïck"},
}))

s, m := h.Probe()

assert.True(t, StatusOK == s)
t.Log(m)
}
Expand All @@ -27,7 +29,9 @@ func TestHTTPWarning(t *testing.T) {
Warning: time.Microsecond,
Fatal: 10 * time.Second,
}))

s, _ := h.Probe()

assert.True(t, StatusWarning == s)
}

Expand All @@ -38,7 +42,9 @@ func TestHTTP404(t *testing.T) {
Warning: 5 * time.Second,
Fatal: 10 * time.Second,
}))

s, _ := h.Probe()

assert.True(t, StatusError == s)
}

Expand All @@ -49,7 +55,9 @@ func TestHTTPError(t *testing.T) {
Warning: 5 * time.Second,
Fatal: 10 * time.Second,
}))

s, _ := h.Probe()

assert.True(t, StatusError == s)
}

Expand All @@ -60,7 +68,9 @@ func TestHTTPTimeout(t *testing.T) {
Warning: 5 * time.Second,
Fatal: time.Microsecond,
}))

s, _ := h.Probe()

assert.True(t, StatusError == s)
}

Expand All @@ -72,7 +82,9 @@ func TestHTTPUnexpected(t *testing.T) {
Fatal: 10 * time.Second,
Options: map[string]interface{}{"Regex": "Unexpected"},
}))

s, m := h.Probe()

assert.True(t, StatusError == s)
assert.True(t, "Unexpected result" == m)
assert.True(t, m == "Unexpected result")
}
22 changes: 17 additions & 5 deletions probe/minecraft.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (m *Minecraft) Probe() (status Status, message string) {
if err != nil {
return StatusError, defaultConnectErrorMsg
}

defer func() { _ = conn.Close() }()

// Handshake
Expand All @@ -40,36 +41,41 @@ func (m *Minecraft) Probe() (status Status, message string) {
0x00,
0x01, // Ask for status
}

_, err = conn.Write(handshake)
if err != nil {
return StatusError, "Error during handshake"
}

// Status
stat := []byte{0x01, 0x00}

_, err = conn.Write(stat)
if err != nil {
return StatusError, "Error during status"
}

// Result
_, _ = readVarInt(conn) // Packet length
_, _ = readVarInt(conn) // PacketID
_ = readVarInt(conn) // Packet length
_ = readVarInt(conn) // PacketID
data := make([]byte, 10000)

read, err := conn.Read(data)
if err != nil || read < 2 {
return StatusError, "No stat received"
}

// Try to parse data
result := new(minecraftServerStats)

err = json.Unmarshal(data[2:read], result)
if err != nil {
return StatusError, "Invalid stats"
}

message = fmt.Sprintf("%d / %d - %s", result.Players.Online, result.Players.Max, result.Version.Name)
status = StatusOK

if result.Players.Online == result.Players.Max {
status = StatusWarning
}
Expand All @@ -87,18 +93,24 @@ type minecraftServerStats struct {
} `json:"players"`
}

func readVarInt(c io.Reader) (res int, err error) {
func readVarInt(c io.Reader) (err error) {
buf := []byte{0x00}
res := 0

for i := 0; i < 5; i++ {
_, err := c.Read(buf)
if err != nil {
return 0, err
return err
}

res |= int((buf[0] & 0x7F) << uint(i*7))
if 0x00 == buf[0]&0x80 {

if buf[0]&0x80 == 0x00 {
break
}
}

_ = res

return
}
4 changes: 4 additions & 0 deletions probe/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ type Port struct {
// Init configures the probe.
func (p *Port) Init(c Config) error {
p.Config = c

u, err := url.Parse(p.Target)
if err != nil {
return err
}

p.network = u.Scheme
p.addrport = u.Host

return nil
}

Expand All @@ -30,11 +32,13 @@ func (p *Port) Init(c Config) error {
// Otherwise, an error message is returned.
func (p *Port) Probe() (status Status, message string) {
start := time.Now()

conn, err := net.DialTimeout(p.network, p.addrport, p.Fatal)
if err != nil {
return StatusError, defaultConnectErrorMsg
}

_ = conn.Close()

return EvaluateDuration(time.Since(start), p.Warning)
}
2 changes: 2 additions & 0 deletions probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func EvaluateDuration(duration time.Duration, warning time.Duration) (status Sta
} else {
status = StatusOK
}

message = fmt.Sprintf("%d ms", duration.Nanoseconds()/1000000)

return
}
4 changes: 4 additions & 0 deletions probe/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,26 @@ func (s *SMTP) Init(c Config) error {
// Otherwise, an error message is returned.
func (s *SMTP) Probe() (status Status, message string) {
start := time.Now()

conn, err := net.DialTimeout("tcp", s.Target, s.Fatal)
if err != nil {
return StatusError, defaultConnectErrorMsg
}

defer func() { _ = conn.Close() }()

host, _, _ := net.SplitHostPort(s.Target)
secure := tls.Client(conn, &tls.Config{
ServerName: host,
})

data := make([]byte, 4)

_, err = secure.Read(data)
if err != nil {
return StatusError, "TLS Error"
}

if fmt.Sprintf("%s", data) != "220 " {
return StatusError, "Unexpected reply"
}
Expand Down

0 comments on commit b7c0045

Please sign in to comment.