System logger
This commit is contained in:
@@ -5,7 +5,9 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/castawaylabs/cachet-monitor/system"
|
"github.com/castawaylabs/cachet-monitor/system"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
@@ -13,6 +15,8 @@ import (
|
|||||||
|
|
||||||
// Static config
|
// Static config
|
||||||
var Config CachetConfig
|
var Config CachetConfig
|
||||||
|
// Central logger
|
||||||
|
var Logger *log.Logger
|
||||||
|
|
||||||
// CachetConfig is the monitoring tool configuration
|
// CachetConfig is the monitoring tool configuration
|
||||||
type CachetConfig struct {
|
type CachetConfig struct {
|
||||||
@@ -20,13 +24,16 @@ type CachetConfig struct {
|
|||||||
APIToken string `json:"api_token"`
|
APIToken string `json:"api_token"`
|
||||||
Monitors []*Monitor `json:"monitors"`
|
Monitors []*Monitor `json:"monitors"`
|
||||||
SystemName string `json:"system_name"`
|
SystemName string `json:"system_name"`
|
||||||
|
LogPath string `json:"log_path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var configPath string
|
var configPath string
|
||||||
var systemName string
|
var systemName string
|
||||||
|
var logPath string
|
||||||
flag.StringVar(&configPath, "c", "/etc/cachet-monitor.config.json", "Config path")
|
flag.StringVar(&configPath, "c", "/etc/cachet-monitor.config.json", "Config path")
|
||||||
flag.StringVar(&systemName, "name", "", "System Name")
|
flag.StringVar(&systemName, "name", "", "System Name")
|
||||||
|
flag.StringVar(&logPath, "log", "", "Log path")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
var data []byte
|
var data []byte
|
||||||
@@ -42,7 +49,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
|
|
||||||
data, _ = ioutil.ReadAll(response.Body)
|
data, _ = ioutil.ReadAll(response.Body)
|
||||||
|
|
||||||
fmt.Println("Downloaded network configuration.")
|
fmt.Println("Downloaded network configuration.")
|
||||||
@@ -85,4 +91,20 @@ func init() {
|
|||||||
fmt.Printf("No monitors defined!\nSee sample configuration: https://github.com/CastawayLabs/cachet-monitor/blob/master/example.config.json\n")
|
fmt.Printf("No monitors defined!\nSee sample configuration: https://github.com/CastawayLabs/cachet-monitor/blob/master/example.config.json\n")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(logPath) > 0 {
|
||||||
|
Config.LogPath = logPath
|
||||||
|
}
|
||||||
|
|
||||||
|
var logWriter io.Writer
|
||||||
|
logWriter = os.Stdout
|
||||||
|
if len(Config.LogPath) > 0 {
|
||||||
|
logWriter, err = os.Create(Config.LogPath)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Unable to open file '%v' for logging\n", Config.LogPath)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger = log.New(logWriter, "", log.Llongfile | log.Ldate | log.Ltime)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package cachet
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,7 +12,7 @@ type Incident struct {
|
|||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Status int `json:"status"` // 4?
|
Status int `json:"status"` // 4?
|
||||||
HumanStatus string `json:"human_status"`
|
HumanStatus string `json:"human_status"`
|
||||||
Component *Component `json:"component"`
|
Component *Component `json:"-"`
|
||||||
ComponentID *int `json:"component_id"`
|
ComponentID *int `json:"component_id"`
|
||||||
CreatedAt int `json:"created_at"`
|
CreatedAt int `json:"created_at"`
|
||||||
UpdatedAt int `json:"updated_at"`
|
UpdatedAt int `json:"updated_at"`
|
||||||
@@ -33,13 +32,14 @@ type IncidentList struct {
|
|||||||
func GetIncidents() []Incident {
|
func GetIncidents() []Incident {
|
||||||
_, body, err := makeRequest("GET", "/incidents", nil)
|
_, body, err := makeRequest("GET", "/incidents", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
Logger.Printf("Cannot get incidents: %v\n", err)
|
||||||
|
return []Incident{}
|
||||||
}
|
}
|
||||||
|
|
||||||
var data IncidentList
|
var data IncidentList
|
||||||
err = json.Unmarshal(body, &data)
|
err = json.Unmarshal(body, &data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Cannot parse incidents.")
|
Logger.Printf("Cannot parse incidents: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return data.Incidents
|
return data.Incidents
|
||||||
@@ -49,7 +49,8 @@ func GetIncidents() []Incident {
|
|||||||
func (incident *Incident) Send() {
|
func (incident *Incident) Send() {
|
||||||
jsonBytes, err := json.Marshal(incident)
|
jsonBytes, err := json.Marshal(incident)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
Logger.Printf("Cannot encode incident: %v\n", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
requestType := "POST"
|
requestType := "POST"
|
||||||
@@ -61,24 +62,25 @@ func (incident *Incident) Send() {
|
|||||||
|
|
||||||
resp, body, err := makeRequest(requestType, requestURL, jsonBytes)
|
resp, body, err := makeRequest(requestType, requestURL, jsonBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
Logger.Printf("Cannot create/update incident: %v\n", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(strconv.Itoa(resp.StatusCode) + " " + string(body))
|
Logger.Println(strconv.Itoa(resp.StatusCode) + " " + string(body))
|
||||||
|
|
||||||
var data IncidentData
|
var data IncidentData
|
||||||
err = json.Unmarshal(body, &data)
|
err = json.Unmarshal(body, &data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Cannot parse incident body.")
|
Logger.Println("Cannot parse incident body.")
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
incident.ID = data.Incident.ID
|
incident.ID = data.Incident.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("ID:" + strconv.Itoa(incident.ID))
|
Logger.Println("ID:" + strconv.Itoa(incident.ID))
|
||||||
|
|
||||||
if resp.StatusCode != 200 {
|
if resp.StatusCode != 200 {
|
||||||
fmt.Println("Could not create/update incident!")
|
Logger.Println("Could not create/update incident!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +92,7 @@ func (incident *Incident) GetSimilarIncidentID() {
|
|||||||
for _, inc := range incidents {
|
for _, inc := range incidents {
|
||||||
if incident.Name == inc.Name && incident.Message == inc.Message && incident.Status == inc.Status && incident.HumanStatus == inc.HumanStatus {
|
if incident.Name == inc.Name && incident.Message == inc.Message && incident.Status == inc.Status && incident.HumanStatus == inc.HumanStatus {
|
||||||
incident.ID = inc.ID
|
incident.ID = inc.ID
|
||||||
fmt.Printf("Updated incident id to %v\n", inc.ID)
|
Logger.Printf("Updated incident id to %v\n", inc.ID)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package cachet
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,7 +17,7 @@ func SendMetric(metricID int, delay int64) {
|
|||||||
|
|
||||||
resp, _, err := makeRequest("POST", "/metrics/"+strconv.Itoa(metricID)+"/points", jsonBytes)
|
resp, _, err := makeRequest("POST", "/metrics/"+strconv.Itoa(metricID)+"/points", jsonBytes)
|
||||||
if err != nil || resp.StatusCode != 200 {
|
if err != nil || resp.StatusCode != 200 {
|
||||||
fmt.Printf("Could not log data point!\n%v\n", err)
|
Logger.Printf("Could not log data point!\n%v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package cachet
|
package cachet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -66,7 +65,7 @@ func (monitor *Monitor) AnalyseData() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t := (float32(numDown) / float32(len(monitor.History))) * 100
|
t := (float32(numDown) / float32(len(monitor.History))) * 100
|
||||||
fmt.Printf("%s %.2f%% Down at %v. Threshold: %.2f%%\n", monitor.URL, t, time.Now().UnixNano()/int64(time.Second), monitor.Threshold)
|
Logger.Printf("%s %.2f%% Down at %v. Threshold: %.2f%%\n", monitor.URL, t, time.Now().UnixNano()/int64(time.Second), monitor.Threshold)
|
||||||
|
|
||||||
if len(monitor.History) != 10 {
|
if len(monitor.History) != 10 {
|
||||||
// not enough data
|
// not enough data
|
||||||
@@ -75,7 +74,7 @@ func (monitor *Monitor) AnalyseData() {
|
|||||||
|
|
||||||
if t > monitor.Threshold && monitor.Incident == nil {
|
if t > monitor.Threshold && monitor.Incident == nil {
|
||||||
// is down, create an incident
|
// is down, create an incident
|
||||||
fmt.Println("Creating incident...")
|
Logger.Println("Creating incident...")
|
||||||
|
|
||||||
monitor.Incident = &Incident{
|
monitor.Incident = &Incident{
|
||||||
Name: monitor.Name + " - " + Config.SystemName,
|
Name: monitor.Name + " - " + Config.SystemName,
|
||||||
@@ -96,7 +95,7 @@ func (monitor *Monitor) AnalyseData() {
|
|||||||
monitor.Incident.Send()
|
monitor.Incident.Send()
|
||||||
} else if t < monitor.Threshold && monitor.Incident != nil {
|
} else if t < monitor.Threshold && monitor.Incident != nil {
|
||||||
// was down, created an incident, its now ok, make it resolved.
|
// was down, created an incident, its now ok, make it resolved.
|
||||||
fmt.Println("Updating incident to resolved...")
|
Logger.Println("Updating incident to resolved...")
|
||||||
|
|
||||||
monitor.Incident.SetFixed()
|
monitor.Incident.SetFixed()
|
||||||
monitor.Incident.Send()
|
monitor.Incident.Send()
|
||||||
|
|||||||
12
main.go
12
main.go
@@ -1,24 +1,24 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/castawaylabs/cachet-monitor/cachet"
|
"github.com/castawaylabs/cachet-monitor/cachet"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config := cachet.Config
|
config := cachet.Config
|
||||||
|
log := cachet.Logger
|
||||||
|
|
||||||
fmt.Printf("System: %s, API: %s\n", config.SystemName, config.APIUrl)
|
log.Printf("System: %s, API: %s\n", config.SystemName, config.APIUrl)
|
||||||
fmt.Printf("Starting %d monitors:\n", len(config.Monitors))
|
log.Printf("Starting %d monitors:\n", len(config.Monitors))
|
||||||
for _, mon := range config.Monitors {
|
for _, mon := range config.Monitors {
|
||||||
fmt.Printf(" %s: GET %s & Expect HTTP %d\n", mon.Name, mon.URL, mon.ExpectedStatusCode)
|
log.Printf(" %s: GET %s & Expect HTTP %d\n", mon.Name, mon.URL, mon.ExpectedStatusCode)
|
||||||
if mon.MetricID > 0 {
|
if mon.MetricID > 0 {
|
||||||
fmt.Printf(" - Logs lag to metric id: %d\n", mon.MetricID)
|
log.Printf(" - Logs lag to metric id: %d\n", mon.MetricID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println()
|
log.Println()
|
||||||
|
|
||||||
ticker := time.NewTicker(time.Second)
|
ticker := time.NewTicker(time.Second)
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
|
|||||||
Reference in New Issue
Block a user