Skip to content

Commit

Permalink
Remove ansible dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
martinhaus committed Mar 26, 2020
1 parent d0f91e7 commit 7949235
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 93 deletions.
32 changes: 2 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,13 @@ groups:

Only alert rules are altered, recording rules are left intact and are just passed to output without change.

## Ansible

Running the program
```
- name: Override default rules
become: false
delegate_to: localhost
prometheus_merge:
rulesPath: "/tmp/alerting_rules"
register: override
- name: Copy rules to files
become: false
delegate_to: localhost
copy:
content: "{{ override.alerts }}"
dest: "/tmp/processed_rules.rules"
```

## Dev/Testing

The application expects JSON file as it's input.

config.json
```
{
"rulesPath": "./rules"
}
```

```
go run main.go config.json
./prometheus_merget <path_to_rules>
```

## Build

```
CGO_ENABLED=0 GOOS=linux go build -o prometheus_alert_overrider main.go
```
71 changes: 8 additions & 63 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
Expand All @@ -12,17 +11,6 @@ import (
"gopkg.in/yaml.v2"
)

type ModuleArgs struct {
RulesPath string
}

type Response struct {
Msg string `json:"msg"`
Alerts string `json:"alerts"`
Changed bool `json:"changed"`
Failed bool `json:"failed"`
}

type AlertFile struct {
Groups []Group `yaml:"groups,omitempty"`
}
Expand Down Expand Up @@ -87,15 +75,6 @@ func (alertFile AlertFile) Override(overrideRule Rule) {
}
}

func ExitJson(responseBody Response) {
returnResponse(responseBody)
}

func failJson(responseBody Response) {
responseBody.Failed = true
returnResponse(responseBody)
}

func LoadRules(input []byte) (*AlertFile, error) {
var alertRules AlertFile
err := yaml.Unmarshal(input, &alertRules)
Expand All @@ -111,21 +90,6 @@ func containsOperator(needle string, haystack string) bool {
return result
}

func returnResponse(responseBody Response) {
var response []byte
var err error
response, err = json.Marshal(responseBody)
if err != nil {
response, _ = json.Marshal(Response{Msg: "Invalid response object"})
}
fmt.Println(string(response))
if responseBody.Failed {
os.Exit(1)
} else {
os.Exit(0)
}
}

func extractFilterExpressions(input string) string {
re := regexp.MustCompile("{.*}")
return re.FindAllString(input, 1)[0]
Expand Down Expand Up @@ -175,7 +139,7 @@ func NegateFilterExpression(input string) string {
return strings.Join(exprFilterBodyElements[:], ",")
}

func getFilePaths(rootPath string, response Response) []string {
func getFilePaths(rootPath string) []string {
var filePaths []string

files, err := ioutil.ReadDir(rootPath)
Expand All @@ -189,11 +153,10 @@ func getFilePaths(rootPath string, response Response) []string {
return filePaths
}

func loadAlertFile(filePath string, response Response) *AlertFile {
func loadAlertFile(filePath string) *AlertFile {
input, err := ioutil.ReadFile(filePath)
if err != nil {
response.Msg = "Failed to read file " + filePath
failJson(response)
fmt.Errorf("Failed to read file " + filePath)
return nil
}
alertFile, err := LoadRules(input)
Expand All @@ -204,34 +167,18 @@ func loadAlertFile(filePath string, response Response) *AlertFile {
}

func main() {
var response Response

if len(os.Args) != 2 {
response.Msg = "No argument file provided"
failJson(response)
}

argsFile := os.Args[1]

text, err := ioutil.ReadFile(argsFile)
if err != nil {
response.Msg = "Could not read configuration file: " + argsFile
failJson(response)
}

var moduleArgs ModuleArgs
err = json.Unmarshal(text, &moduleArgs)
if err != nil {
response.Msg = "Configuration file not valid JSON: " + argsFile
failJson(response)
panic("No arguments provided")
}

files := getFilePaths(moduleArgs.RulesPath, response)
filePaths := os.Args[1]
files := getFilePaths(filePaths)

var alertFiles []AlertFile

for _, file := range files {
alertFile := loadAlertFile(file, response)
alertFile := loadAlertFile(file)
if alertFile != nil {
alertFiles = append(alertFiles, *alertFile)
}
Expand All @@ -250,7 +197,5 @@ func main() {
}
}

response.Msg = "done"
response.Alerts = alertFile.Exporter()
ExitJson(response)
fmt.Print(alertFile.Exporter())
}

0 comments on commit 7949235

Please sign in to comment.