command/agent: Minor cleanups

This commit is contained in:
Armon Dadgar 2015-01-13 12:18:18 -08:00
parent 713d30c73e
commit 96c7ce120e
1 changed files with 23 additions and 20 deletions

View File

@ -2,8 +2,6 @@ package agent
import ( import (
"fmt" "fmt"
"github.com/armon/circbuf"
"github.com/hashicorp/consul/consul/structs"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
@ -11,6 +9,9 @@ import (
"sync" "sync"
"syscall" "syscall"
"time" "time"
"github.com/armon/circbuf"
"github.com/hashicorp/consul/consul/structs"
) )
const ( const (
@ -164,7 +165,7 @@ func (c *CheckMonitor) check() {
// Check if the check passed // Check if the check passed
if err == nil { if err == nil {
c.Logger.Printf("[DEBUG] Check '%v' is passing", c.CheckID) c.Logger.Printf("[DEBUG] agent: Check '%v' is passing", c.CheckID)
c.Notify.UpdateCheck(c.CheckID, structs.HealthPassing, outputStr) c.Notify.UpdateCheck(c.CheckID, structs.HealthPassing, outputStr)
return return
} }
@ -175,7 +176,7 @@ func (c *CheckMonitor) check() {
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok { if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
code := status.ExitStatus() code := status.ExitStatus()
if code == 1 { if code == 1 {
c.Logger.Printf("[WARN] Check '%v' is now warning", c.CheckID) c.Logger.Printf("[WARN] agent: Check '%v' is now warning", c.CheckID)
c.Notify.UpdateCheck(c.CheckID, structs.HealthWarning, outputStr) c.Notify.UpdateCheck(c.CheckID, structs.HealthWarning, outputStr)
return return
} }
@ -183,7 +184,7 @@ func (c *CheckMonitor) check() {
} }
// Set the health as critical // Set the health as critical
c.Logger.Printf("[WARN] Check '%v' is now critical", c.CheckID) c.Logger.Printf("[WARN] agent: Check '%v' is now critical", c.CheckID)
c.Notify.UpdateCheck(c.CheckID, structs.HealthCritical, outputStr) c.Notify.UpdateCheck(c.CheckID, structs.HealthCritical, outputStr)
} }
@ -230,7 +231,7 @@ func (c *CheckTTL) run() {
for { for {
select { select {
case <-c.timer.C: case <-c.timer.C:
c.Logger.Printf("[WARN] Check '%v' missed TTL, is now critical", c.Logger.Printf("[WARN] agent: Check '%v' missed TTL, is now critical",
c.CheckID) c.CheckID)
c.Notify.UpdateCheck(c.CheckID, structs.HealthCritical, "TTL expired") c.Notify.UpdateCheck(c.CheckID, structs.HealthCritical, "TTL expired")
@ -243,7 +244,7 @@ func (c *CheckTTL) run() {
// SetStatus is used to update the status of the check, // SetStatus is used to update the status of the check,
// and to renew the TTL. If expired, TTL is restarted. // and to renew the TTL. If expired, TTL is restarted.
func (c *CheckTTL) SetStatus(status, output string) { func (c *CheckTTL) SetStatus(status, output string) {
c.Logger.Printf("[DEBUG] Check '%v' status is now %v", c.Logger.Printf("[DEBUG] agent: Check '%v' status is now %v",
c.CheckID, status) c.CheckID, status)
c.Notify.UpdateCheck(c.CheckID, status, output) c.Notify.UpdateCheck(c.CheckID, status, output)
c.timer.Reset(c.TTL) c.timer.Reset(c.TTL)
@ -258,8 +259,8 @@ type persistedCheck struct {
// CheckHTTP is used to periodically make an HTTP request to // CheckHTTP is used to periodically make an HTTP request to
// determine the health of a given check. // determine the health of a given check.
// The check is passing if the response code is 200. // The check is passing if the response code is 2XX.
// The check is warning if the response code is 503. // The check is warning if the response code is 429.
// The check is critical if the response code is anything else // The check is critical if the response code is anything else
// or if the request returns an error // or if the request returns an error
type CheckHTTP struct { type CheckHTTP struct {
@ -334,27 +335,29 @@ func (c *CheckHTTP) check() {
} }
defer resp.Body.Close() defer resp.Body.Close()
// Format the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.Logger.Printf("[WARN] agent: check '%v': Get error while reading body: %s", c.CheckID, err)
body = []byte{}
}
result := fmt.Sprintf("HTTP GET %s: %s Output: %s", c.HTTP, resp.Status, body)
if resp.StatusCode >= 200 && resp.StatusCode <= 299 { if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
// PASSING (2xx) // PASSING (2xx)
body, err := ioutil.ReadAll(resp.Body) c.Logger.Printf("[DEBUG] agent: check '%v' is passing", c.CheckID)
if err != nil {
c.Logger.Printf("[WARN] check '%v': Get error while reading body: %s", c.CheckID, err)
body = []byte{}
}
result := fmt.Sprintf("HTTP GET %s: %s Output: %s", c.HTTP, resp.Status, body)
c.Logger.Printf("[DEBUG] agent: http check '%v' is passing: %s", c.CheckID, result)
c.Notify.UpdateCheck(c.CheckID, structs.HealthPassing, result) c.Notify.UpdateCheck(c.CheckID, structs.HealthPassing, result)
} else if resp.StatusCode == 429 { } else if resp.StatusCode == 429 {
// WARNING // WARNING
// 429 Too Many Requests (RFC 6585) // 429 Too Many Requests (RFC 6585)
// The user has sent too many requests in a given amount of time. // The user has sent too many requests in a given amount of time.
c.Logger.Printf("[WARN] check '%v' is now warning", c.CheckID) c.Logger.Printf("[WARN] agent: check '%v' is now warning", c.CheckID)
c.Notify.UpdateCheck(c.CheckID, structs.HealthWarning, resp.Status) c.Notify.UpdateCheck(c.CheckID, structs.HealthWarning, result)
} else { } else {
// CRITICAL // CRITICAL
c.Logger.Printf("[WARN] check '%v' is now critical", c.CheckID) c.Logger.Printf("[WARN] agent: check '%v' is now critical", c.CheckID)
c.Notify.UpdateCheck(c.CheckID, structs.HealthCritical, resp.Status) c.Notify.UpdateCheck(c.CheckID, structs.HealthCritical, result)
} }
} }