Skip to content

Commit

Permalink
Implement custom endpoint for modules
Browse files Browse the repository at this point in the history
Every module now has a "endpoint" setting in its configuration file.
Also implemented some simple configuration validation for an easier
migration.
  • Loading branch information
fleaz committed Sep 21, 2019
1 parent 23a8f4e commit 797d474
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 41 deletions.
36 changes: 23 additions & 13 deletions cpthook.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ irc:
password: "VerySecure!"

modules:
prometheus:
enabled: true
my-prom-endpoint:
endpoint: "/prom"
type: "prometheus"
channel: "#prometheusChannel"
hostname_filter: "(\\w*)\\.company.com:\\d{4}"

gitlab:
enabled: true
endpoint: "/gitlab"
type: "gitlab"
default: "#defaultChannel"
commit_limit: 3
groups:
Expand All @@ -35,15 +38,22 @@ modules:
"myGitlabGroup/mySpecialGitlabProject":
- "#specificChannel"
simple:
enabled: True
endpoint: "/simple"
type: "simple"
default_channel: "#defaultChannel"
icinga2:
enabled: True
default: "#monitoring"
hostgroups:
"webservers":
- "#monitoring-web"
explicit:
"host.example.tld":
- "#monitoring-example"

another-simple:
endpoint: "/simple2"
type: "simple"
default_channel: "#defaultChannel"

icinga2:
endpoint: "/betterthannagios"
type: "icinga"
default: "#monitoring"
hostgroups:
"webservers":
- "#monitoring-web"
explicit:
"host.example.tld":
- "#monitoring-example"
4 changes: 0 additions & 4 deletions input/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ func (m GitlabModule) GetChannelList() []string {
return all
}

func (m GitlabModule) GetEndpoint() string {
return "/gitlab"
}

func (m GitlabModule) GetHandler() http.HandlerFunc {

const pushCompareString = "[\x0312{{ .Project.Name }}\x03] {{ .UserName }} pushed {{ .TotalCommits }} commits to \x0305{{ .Branch }}\x03 {{ .Project.WebURL }}/compare/{{ .BeforeCommit }}...{{ .AfterCommit }}"
Expand Down
1 change: 0 additions & 1 deletion input/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
type Module interface {
Init(c *viper.Viper, channel *chan IRCMessage)
GetChannelList() []string
GetEndpoint() string
GetHandler() http.HandlerFunc
}

Expand Down
9 changes: 3 additions & 6 deletions input/icinga2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package input
import (
"bytes"
"encoding/json"
"github.com/dustin/go-humanize"
log "github.com/sirupsen/logrus"
"net/http"
"strings"
"text/template"
"time"

"github.com/dustin/go-humanize"
log "github.com/sirupsen/logrus"

"github.com/spf13/viper"
)

Expand Down Expand Up @@ -201,10 +202,6 @@ func (m Icinga2Module) GetChannelList() []string {
return all
}

func (m Icinga2Module) GetEndpoint() string {
return "/icinga2"
}

func (m Icinga2Module) GetHandler() http.HandlerFunc {

const serviceStateChangeString = "Service \x0312{{ .Service.DisplayName }}\x03 (\x0314{{ .Host.DisplayName }}\x03) transitioned from state {{ .Service.ColoredLastState }} to {{ .Service.ColoredState }}"
Expand Down
4 changes: 0 additions & 4 deletions input/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ func shortenInstanceName(name string, pattern *regexp.Regexp) string {
return name
}

func (m PrometheusModule) GetEndpoint() string {
return "/prometheus"
}

func (m PrometheusModule) GetChannelList() []string {
return []string{m.defaultChannel}
}
Expand Down
4 changes: 0 additions & 4 deletions input/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ func (m SimpleModule) GetChannelList() []string {
return []string{m.defaultChannel}
}

func (m SimpleModule) GetEndpoint() string {
return "/simple"
}

func (m SimpleModule) GetHandler() http.HandlerFunc {

return func(wr http.ResponseWriter, req *http.Request) {
Expand Down
46 changes: 37 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"net/http"
"os"
"path"
"strings"
"time"
Expand Down Expand Up @@ -44,7 +45,8 @@ type Configuration struct {
}

type InputModule struct {
Enalbed string `yaml:"enabled"`
Type string `yaml:"type"`
Endpoint string `yaml:"endpoint"`
}

func createModuleObject(name string) (input.Module, error) {
Expand All @@ -60,12 +62,36 @@ func createModuleObject(name string) (input.Module, error) {
case "icinga2":
m = &input.Icinga2Module{}
default:
e = fmt.Errorf("found configuration for unknown module: %q", name)
e = fmt.Errorf("ignoring configuration for unknown module: %q", name)
}

return m, e
}

func validateConfig(c Configuration) {
var foundErrors []string

for blockName, blockConfig := range c.Modules {
if blockConfig.Type == "" {
foundErrors = append(foundErrors, fmt.Sprintf("Block %q is missing its type", blockName))
}
if blockConfig.Endpoint == "" {
foundErrors = append(foundErrors, fmt.Sprintf("Block %q is missing its endpoint", blockName))
}
}

if len(foundErrors) > 0 {
log.Error("Found the following errors in the configuration:")
for _, e := range foundErrors {
log.Error(e)
}
os.Exit(1)
} else {
log.Info("Configuration parsed without errors")
}

}

func main() {
confDirPtr := flag.String("config", "/etc/cpthook.yml", "Path to the configfile")
flag.Parse()
Expand All @@ -85,24 +111,26 @@ func main() {
configureLogLevel()

var channelList = []string{}
var configurtaion = Configuration{}
var config = Configuration{}

err = viper.Unmarshal(&configurtaion)
err = viper.Unmarshal(&config)
if err != nil {
log.Fatal(err)
}

for moduleName := range configurtaion.Modules {
module, err := createModuleObject(moduleName)
validateConfig(config)

for blockName, blockConfig := range config.Modules {
module, err := createModuleObject(blockConfig.Type)
if err != nil {
log.Warn(err)
continue
}
log.Infof("Loaded module %q", moduleName)
configPath := fmt.Sprintf("modules.%s", moduleName)
log.Infof("Loaded block %q (Type %q, Endpoint %q)", blockName, blockConfig.Type, blockConfig.Endpoint)
configPath := fmt.Sprintf("modules.%s", blockName)
module.Init(viper.Sub(configPath), &inputChannel)
channelList = append(channelList, module.GetChannelList()...)
http.HandleFunc(module.GetEndpoint(), module.GetHandler())
http.HandleFunc(blockConfig.Endpoint, module.GetHandler())

}

Expand Down

0 comments on commit 797d474

Please sign in to comment.