Skip to content

Commit

Permalink
Basic config and some initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
csmith committed Sep 16, 2020
1 parent c3da24b commit 728ecd5
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cmd/goplum/goplum.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/csmith/goplum"
"github.com/csmith/goplum/internal"
"log"
"os"
Expand All @@ -14,4 +15,6 @@ func main() {
for i := range plugins {
log.Printf("Plugin %d is '%s' with %d checks, %d notifiers\n", i, plugins[i].Name(), len(plugins[i].Checks()), len(plugins[i].Notifiers()))
}

goplum.Initialise(plugins, "config.json")
}
2 changes: 2 additions & 0 deletions cmd/goplumdev/goplumdev.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ func main() {
for i := range plugins {
log.Printf("Plugin %d is '%s' with %d checks, %d notifiers\n", i, plugins[i].Name(), len(plugins[i].Checks()), len(plugins[i].Notifiers()))
}

goplum.Initialise(plugins, "config.json")
}
29 changes: 29 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package goplum

import (
"encoding/json"
"os"
"time"
)

type Config struct {
Checks []ConfiguredCheck `json:"checks"`
}

type ConfiguredCheck struct {
Name string `json:"name"`
Check string `json:"check"`
Interval time.Duration `json:"interval"`
Params json.RawMessage `json:"params"`
}

func LoadConfig(path string) (*Config, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()

config := &Config{}
return config, json.NewDecoder(f).Decode(config)
}
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"checks": [
{
"name": "Google",
"check": "http",
"params": {
"url": "https://www.google.com/"
}
}
]
}
3 changes: 1 addition & 2 deletions plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ type Plugin interface {
// e.g. making a HTTP request, or opening a TCP socket.
type Check interface {
Name() string
Help() string
Create(config json.RawMessage) Task
Create(config json.RawMessage) (Task, error)
}

type Task interface {
Expand Down
38 changes: 36 additions & 2 deletions plugins/http/http.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package http

import "github.com/csmith/goplum"
import (
"encoding/json"
"github.com/csmith/goplum"
"net/http"
)

type Plugin struct{}

Expand All @@ -9,10 +13,40 @@ func (h Plugin) Name() string {
}

func (h Plugin) Checks() []goplum.Check {
return nil
return []goplum.Check{Check{}}
}

func (h Plugin) Notifiers() []goplum.Notifier {
return nil
}

type params struct {
Url string `json:"url"`
}

type Check struct{}

func (c Check) Name() string {
return "http"
}

func (c Check) Create(config json.RawMessage) (goplum.Task, error) {
p := params{}
err := json.Unmarshal(config, &p)
if err != nil {
return nil, err
}

return Task{p}, nil
}

type Task struct {
params params
}

func (t Task) Execute() goplum.Result {
r, err := http.Get(t.params.Url)
return goplum.Result{
Good: err == nil && r.StatusCode >= 400,
}
}
35 changes: 35 additions & 0 deletions plum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package goplum

import "log"

func Initialise(plugins []Plugin, configPath string) {
config, err := LoadConfig(configPath)
if err != nil {
log.Fatalf("Unable to read config: %v", err)
}

checks := make(map[string]Check)
for i := range plugins {
cs := plugins[i].Checks()
for j := range cs {
checks[cs[j].Name()] = cs[j]
}
}
log.Printf("Found %d checks from %d plugins\n", len(checks), len(plugins))

tasks := make([]Task, 0)
for i := range config.Checks {
cc := config.Checks[i]
check, ok := checks[cc.Check]
if !ok {
log.Fatalf("Invalid check name in config: %s", cc.Check)
}

t, err := check.Create(cc.Params)
if err != nil {
log.Fatalf("Unable to create check '%s': %v", cc.Name, err)
}

tasks = append(tasks, t)
}
}

0 comments on commit 728ecd5

Please sign in to comment.