- immediate tick flag
- reword Start -> ClockStart etc
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,5 +1,3 @@
|
|||||||
gin-bin
|
|
||||||
example.config.local.json
|
|
||||||
.idea
|
|
||||||
/config.yml
|
/config.yml
|
||||||
/config.json
|
/config.json
|
||||||
|
examples/
|
||||||
12
cli/main.go
12
cli/main.go
@@ -21,7 +21,7 @@ import (
|
|||||||
const usage = `cachet-monitor
|
const usage = `cachet-monitor
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
cachet-monitor (-c PATH | --config PATH) [--log=LOGPATH] [--name=NAME]
|
cachet-monitor (-c PATH | --config PATH) [--log=LOGPATH] [--name=NAME] [--immediate]
|
||||||
cachet-monitor -h | --help | --version
|
cachet-monitor -h | --help | --version
|
||||||
cachet-monitor print-config
|
cachet-monitor print-config
|
||||||
|
|
||||||
@@ -38,6 +38,7 @@ Options:
|
|||||||
-c PATH.json --config PATH Path to configuration file
|
-c PATH.json --config PATH Path to configuration file
|
||||||
-h --help Show this screen.
|
-h --help Show this screen.
|
||||||
--version Show version
|
--version Show version
|
||||||
|
--immediate Tick immediately (by default waits for first defined interval)
|
||||||
print-config Print example configuration
|
print-config Print example configuration
|
||||||
|
|
||||||
Environment varaibles:
|
Environment varaibles:
|
||||||
@@ -53,6 +54,10 @@ func main() {
|
|||||||
logrus.Panicf("Unable to start (reading config): %v", err)
|
logrus.Panicf("Unable to start (reading config): %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if immediate, ok := arguments["--immediate"]; ok {
|
||||||
|
cfg.Immediate = immediate.(bool)
|
||||||
|
}
|
||||||
|
|
||||||
if name := arguments["--name"]; name != nil {
|
if name := arguments["--name"]; name != nil {
|
||||||
cfg.SystemName = name.(string)
|
cfg.SystemName = name.(string)
|
||||||
}
|
}
|
||||||
@@ -73,6 +78,7 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logrus.Debug("Configuration valid")
|
||||||
logrus.Infof("System: %s", cfg.SystemName)
|
logrus.Infof("System: %s", cfg.SystemName)
|
||||||
logrus.Infof("API: %s", cfg.API.URL)
|
logrus.Infof("API: %s", cfg.API.URL)
|
||||||
logrus.Infof("Monitors: %d\n", len(cfg.Monitors))
|
logrus.Infof("Monitors: %d\n", len(cfg.Monitors))
|
||||||
@@ -89,7 +95,7 @@ func main() {
|
|||||||
logrus.Infof("Starting Monitor #%d:", index)
|
logrus.Infof("Starting Monitor #%d:", index)
|
||||||
logrus.Infof("Features: \n - %v", strings.Join(monitor.Describe(), "\n - "))
|
logrus.Infof("Features: \n - %v", strings.Join(monitor.Describe(), "\n - "))
|
||||||
|
|
||||||
// go mon.Start(cfg, wg)
|
go monitor.ClockStart(cfg, wg)
|
||||||
}
|
}
|
||||||
|
|
||||||
signals := make(chan os.Signal, 1)
|
signals := make(chan os.Signal, 1)
|
||||||
@@ -98,7 +104,7 @@ func main() {
|
|||||||
|
|
||||||
logrus.Warnf("Abort: Waiting monitors to finish")
|
logrus.Warnf("Abort: Waiting monitors to finish")
|
||||||
for _, mon := range cfg.Monitors {
|
for _, mon := range cfg.Monitors {
|
||||||
mon.GetMonitor().Stop()
|
mon.GetMonitor().ClockStop()
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ type CachetMonitor struct {
|
|||||||
RawMonitors []map[string]interface{} `json:"monitors" yaml:"monitors"`
|
RawMonitors []map[string]interface{} `json:"monitors" yaml:"monitors"`
|
||||||
|
|
||||||
Monitors []MonitorInterface `json:"-" yaml:"-"`
|
Monitors []MonitorInterface `json:"-" yaml:"-"`
|
||||||
|
|
||||||
|
Immediate bool `json:"-" yaml:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate configuration
|
// Validate configuration
|
||||||
|
|||||||
39
http.go
39
http.go
@@ -40,7 +40,7 @@ type HTTPMonitor struct {
|
|||||||
bodyRegexp *regexp.Regexp
|
bodyRegexp *regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (monitor *HTTPMonitor) do() bool {
|
func (monitor *HTTPMonitor) test() bool {
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: time.Duration(monitor.Timeout * time.Second),
|
Timeout: time.Duration(monitor.Timeout * time.Second),
|
||||||
}
|
}
|
||||||
@@ -90,45 +90,30 @@ func (monitor *HTTPMonitor) do() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (monitor *HTTPMonitor) Validate() []string {
|
func (mon *HTTPMonitor) Validate() []string {
|
||||||
errs := []string{}
|
errs := mon.AbstractMonitor.Validate()
|
||||||
if len(monitor.ExpectedBody) > 0 {
|
|
||||||
exp, err := regexp.Compile(monitor.ExpectedBody)
|
if len(mon.ExpectedBody) > 0 {
|
||||||
|
exp, err := regexp.Compile(mon.ExpectedBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errs = append(errs, "Regexp compilation failure: "+err.Error())
|
errs = append(errs, "Regexp compilation failure: "+err.Error())
|
||||||
} else {
|
} else {
|
||||||
monitor.bodyRegexp = exp
|
mon.bodyRegexp = exp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(monitor.ExpectedBody) == 0 && monitor.ExpectedStatusCode == 0 {
|
if len(mon.ExpectedBody) == 0 && mon.ExpectedStatusCode == 0 {
|
||||||
errs = append(errs, "Both 'expected_body' and 'expected_status_code' fields empty")
|
errs = append(errs, "Both 'expected_body' and 'expected_status_code' fields empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
if monitor.Interval < 1 {
|
mon.Method = strings.ToUpper(mon.Method)
|
||||||
monitor.Interval = DefaultInterval
|
switch mon.Method {
|
||||||
}
|
|
||||||
|
|
||||||
if monitor.Timeout < 1 {
|
|
||||||
monitor.Timeout = DefaultTimeout
|
|
||||||
}
|
|
||||||
|
|
||||||
monitor.Method = strings.ToUpper(monitor.Method)
|
|
||||||
switch monitor.Method {
|
|
||||||
case "GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD":
|
case "GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD":
|
||||||
break
|
break
|
||||||
case "":
|
case "":
|
||||||
monitor.Method = "GET"
|
mon.Method = "GET"
|
||||||
default:
|
default:
|
||||||
errs = append(errs, "Unsupported check method: "+monitor.Method)
|
errs = append(errs, "Unsupported HTTP method: "+mon.Method)
|
||||||
}
|
|
||||||
|
|
||||||
if monitor.ComponentID == 0 && monitor.MetricID == 0 {
|
|
||||||
errs = append(errs, "component_id & metric_id are unset")
|
|
||||||
}
|
|
||||||
|
|
||||||
if monitor.Threshold <= 0 {
|
|
||||||
monitor.Threshold = 100
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return errs
|
return errs
|
||||||
|
|||||||
82
monitor.go
82
monitor.go
@@ -13,7 +13,11 @@ const DefaultTimeFormat = "15:04:05 Jan 2 MST"
|
|||||||
const HistorySize = 10
|
const HistorySize = 10
|
||||||
|
|
||||||
type MonitorInterface interface {
|
type MonitorInterface interface {
|
||||||
do() bool
|
ClockStart(*CachetMonitor, *sync.WaitGroup)
|
||||||
|
ClockStop()
|
||||||
|
tick()
|
||||||
|
test() bool
|
||||||
|
|
||||||
Validate() []string
|
Validate() []string
|
||||||
GetMonitor() *AbstractMonitor
|
GetMonitor() *AbstractMonitor
|
||||||
Describe() []string
|
Describe() []string
|
||||||
@@ -26,8 +30,6 @@ type AbstractMonitor struct {
|
|||||||
|
|
||||||
// (default)http, tcp, dns, icmp
|
// (default)http, tcp, dns, icmp
|
||||||
Type string
|
Type string
|
||||||
|
|
||||||
// defaults true
|
|
||||||
Strict bool
|
Strict bool
|
||||||
|
|
||||||
Interval time.Duration
|
Interval time.Duration
|
||||||
@@ -54,16 +56,34 @@ type AbstractMonitor struct {
|
|||||||
stopC chan bool
|
stopC chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mon *AbstractMonitor) do() bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
func (mon *AbstractMonitor) Validate() []string {
|
func (mon *AbstractMonitor) Validate() []string {
|
||||||
return []string{}
|
errs := []string{}
|
||||||
|
|
||||||
|
if len(mon.Name) == 0 {
|
||||||
|
errs = append(errs, "Name is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
if mon.Interval < 1 {
|
||||||
|
mon.Interval = DefaultInterval
|
||||||
|
}
|
||||||
|
if mon.Timeout < 1 {
|
||||||
|
mon.Timeout = DefaultTimeout
|
||||||
|
}
|
||||||
|
|
||||||
|
if mon.ComponentID == 0 && mon.MetricID == 0 {
|
||||||
|
errs = append(errs, "component_id & metric_id are unset")
|
||||||
|
}
|
||||||
|
|
||||||
|
if mon.Threshold <= 0 {
|
||||||
|
mon.Threshold = 100
|
||||||
|
}
|
||||||
|
|
||||||
|
return errs
|
||||||
}
|
}
|
||||||
func (mon *AbstractMonitor) GetMonitor() *AbstractMonitor {
|
func (mon *AbstractMonitor) GetMonitor() *AbstractMonitor {
|
||||||
return mon
|
return mon
|
||||||
}
|
}
|
||||||
func (mon AbstractMonitor) Describe() []string {
|
func (mon *AbstractMonitor) Describe() []string {
|
||||||
features := []string{"Type: " + mon.Type}
|
features := []string{"Type: " + mon.Type}
|
||||||
|
|
||||||
if len(mon.Name) > 0 {
|
if len(mon.Name) > 0 {
|
||||||
@@ -73,17 +93,19 @@ func (mon AbstractMonitor) Describe() []string {
|
|||||||
return features
|
return features
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mon *AbstractMonitor) Start(cfg *CachetMonitor, wg *sync.WaitGroup) {
|
func (mon *AbstractMonitor) ClockStart(cfg *CachetMonitor, wg *sync.WaitGroup) {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
mon.config = cfg
|
mon.config = cfg
|
||||||
mon.stopC = make(chan bool)
|
mon.stopC = make(chan bool)
|
||||||
mon.Tick()
|
if cfg.Immediate {
|
||||||
|
mon.tick()
|
||||||
|
}
|
||||||
|
|
||||||
ticker := time.NewTicker(mon.Interval * time.Second)
|
ticker := time.NewTicker(mon.Interval * time.Second)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
mon.Tick()
|
mon.tick()
|
||||||
case <-mon.stopC:
|
case <-mon.stopC:
|
||||||
wg.Done()
|
wg.Done()
|
||||||
return
|
return
|
||||||
@@ -91,41 +113,35 @@ func (mon *AbstractMonitor) Start(cfg *CachetMonitor, wg *sync.WaitGroup) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (monitor *AbstractMonitor) Stop() {
|
func (mon *AbstractMonitor) ClockStop() {
|
||||||
if monitor.Stopped() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
close(monitor.stopC)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (monitor *AbstractMonitor) Stopped() bool {
|
|
||||||
select {
|
select {
|
||||||
case <-monitor.stopC:
|
case <-mon.stopC:
|
||||||
return true
|
return
|
||||||
default:
|
default:
|
||||||
return false
|
close(mon.stopC)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (monitor *AbstractMonitor) Tick() {
|
func (mon *AbstractMonitor) test() bool { return false }
|
||||||
|
|
||||||
|
func (mon *AbstractMonitor) tick() {
|
||||||
reqStart := getMs()
|
reqStart := getMs()
|
||||||
up := monitor.do()
|
up := mon.test()
|
||||||
lag := getMs() - reqStart
|
lag := getMs() - reqStart
|
||||||
|
|
||||||
if len(monitor.history) == HistorySize-1 {
|
if len(mon.history) == HistorySize-1 {
|
||||||
logrus.Warnf("%v is now saturated\n", monitor.Name)
|
logrus.Warnf("%v is now saturated\n", mon.Name)
|
||||||
}
|
}
|
||||||
if len(monitor.history) >= HistorySize {
|
if len(mon.history) >= HistorySize {
|
||||||
monitor.history = monitor.history[len(monitor.history)-(HistorySize-1):]
|
mon.history = mon.history[len(mon.history)-(HistorySize-1):]
|
||||||
}
|
}
|
||||||
monitor.history = append(monitor.history, up)
|
mon.history = append(mon.history, up)
|
||||||
monitor.AnalyseData()
|
mon.AnalyseData()
|
||||||
|
|
||||||
// report lag
|
// report lag
|
||||||
if up && monitor.MetricID > 0 {
|
if up && mon.MetricID > 0 {
|
||||||
logrus.Infof("%v", lag)
|
logrus.Infof("%v", lag)
|
||||||
// monitor.SendMetric(lag)
|
// mon.SendMetric(lag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user