Skip to content

Commit

Permalink
Feature/trigger checks (#53)
Browse files Browse the repository at this point in the history
* Existing control endpoints should be POST
* Add endpoint to trigger check at any time
  • Loading branch information
jonog authored Mar 24, 2017
1 parent ee9aa04 commit d697429
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ For monitoring your infrastructure and sending notifications if stuff is not ok.
* *Execute remote commands via SSH* & capture output (check type: `remote-command`)
* *Run test suite and capture report metrics* via `JUnit XML` format (check type: `test-report`)

Checks will happen at specified intervals or explicit trigger (i.e. trigger check API endpoint).

#### Dashboard and Alerts
* Alert notifications available on several channels:
* sending email (`gmail`)
Expand Down Expand Up @@ -49,7 +51,14 @@ For monitoring your infrastructure and sending notifications if stuff is not ok.
* source: `json`

#### API
* Event stats available via `/v1/stats`

| Endpoint | Description |
| --- | --- |
| `GET /v1/stats` | Retrieve stats for all checks |
| `POST /v1/checks/{check_id}/disable` | Disable check |
| `POST /v1/checks/{check_id}/enable` | Enable check |
| `POST /v1/checks/{check_id}/trigger` | Trigger check |


### Design

Expand All @@ -61,7 +70,7 @@ For monitoring your infrastructure and sending notifications if stuff is not ok.
│ │ │
│ └──────────────────────────────┘
│ │
@interval ┌──────────────────────┐
│ @interval or ->trigger ┌──────────────────────┐
│ │ ┌▶│ error during check │
│ ▼ │ └──────────────────────┘
│ ┌──────────────────────┐ │ ┌──────────────────────┐
Expand Down
8 changes: 5 additions & 3 deletions core/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ type Check struct {

ConfigRank int

stopChan chan bool
wait sync.WaitGroup
triggerChan chan bool
stopChan chan bool
wait sync.WaitGroup
}

func NewCheck(cfg checks.Config, eventStorage storage.EventStorage, preferences config.Preferences) (*Check, error) {
Expand Down Expand Up @@ -91,7 +92,8 @@ func NewCheck(cfg checks.Config, eventStorage storage.EventStorage, preferences
preferences.Notifications.RepeatFailAlerts,
config.DefaultRepeatFailAlerts),

stopChan: make(chan bool),
triggerChan: make(chan bool),
stopChan: make(chan bool),
}, nil
}

Expand Down
5 changes: 5 additions & 0 deletions core/check_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (c *Check) Stop() {
c.stopChan <- true
}

func (c *Check) Trigger() {
c.triggerChan <- true
}

func (c *Check) cleanup() {
c.wait.Done()
}
Expand Down Expand Up @@ -120,6 +124,7 @@ func (c *Check) run(serviceStop chan bool) {

select {
case <-time.After(delay):
case <-c.triggerChan:
case <-c.stopChan:
c.cleanup()
return
Expand Down
21 changes: 21 additions & 0 deletions web/check_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,24 @@ func checkEnableHandler(c *appCtx, w http.ResponseWriter, r *http.Request) {

w.Write([]byte(`OK`))
}

func checkTriggerHandler(c *appCtx, w http.ResponseWriter, r *http.Request) {

vars := mux.Vars(r)
id := vars["check_id"]

check, err := c.service.CheckByID(id)
if err != nil {
http.Error(w, "Not Found", http.StatusNotFound)
return
}

if !check.Data.Enabled {
http.Error(w, "Check is not enabled", http.StatusPreconditionFailed)
return
}

check.Trigger()

w.Write([]byte(`OK`))
}
5 changes: 3 additions & 2 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func Run(service *core.Service, port int, disableBrand bool) {
router.Handle("/api/put", appHandler{context, metricsReceiverHandler})

router.Handle("/v1/stats", appHandler{context, statsHandler})
router.Handle("/v1/checks/{check_id}/disable", appHandler{context, checkDisableHandler})
router.Handle("/v1/checks/{check_id}/enable", appHandler{context, checkEnableHandler})
router.Handle("/v1/checks/{check_id}/disable", appHandler{context, checkDisableHandler}).Methods("POST")
router.Handle("/v1/checks/{check_id}/enable", appHandler{context, checkEnableHandler}).Methods("POST")
router.Handle("/v1/checks/{check_id}/trigger", appHandler{context, checkTriggerHandler}).Methods("POST")

router.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
Expand Down

0 comments on commit d697429

Please sign in to comment.