Skip to content

Commit

Permalink
saving state. WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tbauriedel committed Oct 10, 2024
1 parent dfa7619 commit 3edb9c3
Show file tree
Hide file tree
Showing 13 changed files with 392 additions and 412 deletions.
18 changes: 18 additions & 0 deletions doc/answer-file.yml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
general:
outputFile: support-collector_icinga-dev_20241010-1037.zip
enabledModules:
- all
disabledModules: [ ]
extraObfuscators: [ ]
detailedCollection: true
commandTimeout: 1m0s
icinga2:
endpoints:
- address: localhost
port: 5665
username: poweruser
password: poweruser
- address: 127.0.0.1
port: 5665
username: poweruser
password: poweruser
29 changes: 0 additions & 29 deletions internal/answerfile/answer-file.yml.example

This file was deleted.

32 changes: 0 additions & 32 deletions internal/answerfile/answerfile.go

This file was deleted.

8 changes: 3 additions & 5 deletions internal/collection/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/NETWAYS/support-collector/internal/controls"
"github.com/NETWAYS/support-collector/internal/config"
"github.com/NETWAYS/support-collector/internal/metrics"
"io"
"strings"
Expand All @@ -24,14 +24,12 @@ type Collection struct {
Obfuscators []*obfuscate.Obfuscator
Detailed bool
JournalLoggingInterval string
Control controls.Control
Config config.Config
Metric *metrics.Metrics
}

// New initializes new collection with defaults
func New(w io.Writer) (c *Collection) {
// TODO: add Icinga Control

c = &Collection{
Output: zip.NewWriter(w),
Log: logrus.New(),
Expand All @@ -40,7 +38,7 @@ func New(w io.Writer) (c *Collection) {
Obfuscators: nil,
Detailed: true,
JournalLoggingInterval: "7 days ago",
Control: controls.GetControlDefaults(),
Config: config.GetControlDefaultObject(),
Metric: nil,
}

Expand Down
49 changes: 49 additions & 0 deletions internal/config/answerfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package config

import (
"fmt"
"gopkg.in/yaml.v3"
"io"
"os"
)

const defaultAnswerFileName = "answer-file.yml"

// GenerateDefaultAnswerFile creates a new answer-file with default values in current dir
func GenerateDefaultAnswerFile() error {
file, err := os.Create(defaultAnswerFileName)
if err != nil {
return fmt.Errorf("could not create answer file: %w", err)
}

defer file.Close()

defaults := GetControlDefaultObject()

yamlData, err := yaml.Marshal(defaults)
if err != nil {
return fmt.Errorf("could not marshal yamldata for answer-file: %w", err)
}

_, err = io.WriteString(file, string(yamlData))
if err != nil {
return fmt.Errorf("could not write to answer file: %w", err)
}

return nil
}

// ReadAnswerFile reads given values from answerFile and returns new Config
func ReadAnswerFile(answerFile string, conf *Config) error {
data, err := os.ReadFile(answerFile)
if err != nil {
return fmt.Errorf("could not read answer file: %w", err)
}

err = yaml.Unmarshal(data, &conf)
if err != nil {
return fmt.Errorf("could not unmarshal answer file: %w", err)
}

return nil
}
80 changes: 80 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package config

import (
"fmt"
"github.com/NETWAYS/support-collector/internal/util"
"github.com/NETWAYS/support-collector/modules/icinga2/icingaApi"
"slices"

Check failure on line 7 in internal/config/config.go

View workflow job for this annotation

GitHub Actions / build

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.18.10/x64/src/slices)
"time"
)

var (
ModulesOrder = []string{
"ansible",
"base",
"corosync",
"elastic",
"foreman",
"grafana",
"graphite",
"graylog",
"icinga-director",
"icinga2",
"icingadb",
"icingaweb2",
"influxdb",
"keepalived",
"mongodb",
"mysql",
"postgresql",
"prometheus",
"puppet",
"redis",
"webservers",
}
)

type Config struct {
General General `yaml:"general" json:"general"`
Icinga2 Icinga2 `yaml:"icinga2" json:"icinga2"`
}

type General struct {
OutputFile string `yaml:"outputFile" json:"outputFile"`
AnswerFile string `yaml:"answerFile,omitempty" json:"answerFile,omitempty"`
EnabledModules []string `yaml:"enabledModules" json:"enabledModules"`
DisabledModules []string `yaml:"disabledModules" json:"disabledModules"`
ExtraObfuscators []string `yaml:"extraObfuscators" json:"extraObfuscators"`
DetailedCollection bool `yaml:"detailedCollection" json:"detailedCollection"`
CommandTimeout time.Duration `yaml:"commandTimeout" json:"commandTimeout"`
}

type Icinga2 struct {
Endpoints []icingaApi.Endpoint `yaml:"endpoints" json:"endpoints"`
}

// GetControlDefaultObject returns a new Config object with some pre-defined default values
func GetControlDefaultObject() Config {
return Config{
General: General{
OutputFile: util.BuildFileName(),
AnswerFile: "",
EnabledModules: []string{"all"},
DisabledModules: nil,
ExtraObfuscators: nil,
DetailedCollection: true,
CommandTimeout: 60 * time.Second,
},
Icinga2: Icinga2{},
}
}

// ValidateConfig validates the given config.Config for errors. Returns []error
func ValidateConfig(conf Config) (errors []error) {
for _, name := range conf.General.EnabledModules {
if !slices.Contains(ModulesOrder, name) {
errors = append(errors, fmt.Errorf("invalid module '%s' provided. Cant be enabled", name))
}
}
return errors
}
Loading

0 comments on commit 3edb9c3

Please sign in to comment.