/
config.go
133 lines (111 loc) · 4.76 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package types
import (
"bytes"
"encoding/json"
"fmt"
"time"
libval "github.com/go-playground/validator/v10"
cfgtps "github.com/nabbar/golib/config/const"
liberr "github.com/nabbar/golib/errors"
logcfg "github.com/nabbar/golib/logger/config"
)
var _defaultConfig = []byte(`{
"name": "",
"check-timeout": "",
"interval-check": "",
"interval-fall": "",
"interval-rise": "",
"fall-count-ko": "",
"fall-count-warn": "",
"rise-count-ko": "",
"rise-count-warn": "",
"logger": ` + string(logcfg.DefaultConfig(cfgtps.JSONIndent+cfgtps.JSONIndent)) + `
}`)
func SetDefaultConfig(cfg []byte) {
_defaultConfig = cfg
}
func DefaultConfig(indent string) []byte {
var res = bytes.NewBuffer(make([]byte, 0))
if err := json.Indent(res, _defaultConfig, indent, cfgtps.JSONIndent); err != nil {
return _defaultConfig
} else {
return res.Bytes()
}
}
type Config struct {
// Name define the name of the monitor
Name string `json:"name" yaml:"name" toml:"name" mapstructure:"name"`
// CheckTimeout define the timeout use for healthcheck. Default is 5 second.
CheckTimeout time.Duration `json:"check-timeout" yaml:"check-timeout" toml:"check-timeout" mapstructure:"check-timeout"`
// IntervalCheck define the time waiting between 2 healthcheck. Default is 5 second.
IntervalCheck time.Duration `json:"interval-check" yaml:"interval-check" toml:"interval-check" mapstructure:"interval-check"`
// IntervalFall define the time waiting between 2 healthcheck when last check is KO. Default is 5 second.
IntervalFall time.Duration `json:"interval-fall" yaml:"interval-fall" toml:"interval-fall" mapstructure:"interval-down"`
// IntervalRise define the time waiting between 2 healthcheck when status is KO or Warn but last check is OK. Default is 5 second.
IntervalRise time.Duration `json:"interval-rise" yaml:"interval-rise" toml:"interval-rise" mapstructure:"interval-rise"`
// FallCountKO define the number of KO before considerate the component as down.
FallCountKO uint8 `json:"fall-count-ko" yaml:"fall-count-ko" toml:"fall-count-ko" mapstructure:"fall-count-ko"`
// FallCountWarn define the number of KO before considerate the component as warn.
FallCountWarn uint8 `json:"fall-count-warn" yaml:"fall-count-warn" toml:"fall-count-warn" mapstructure:"fall-count-warn"`
// RiseCountKO define the number of OK when status is KO before considerate the component as up.
RiseCountKO uint8 `json:"rise-count-ko" yaml:"rise-count-ko" toml:"rise-count-ko" mapstructure:"rise-count-ko"`
// RiseCountWarn define the number of OK when status is Warn before considerate the component as up.
RiseCountWarn uint8 `json:"rise-count-warn" yaml:"rise-count-warn" toml:"rise-count-warn" mapstructure:"rise-count-warn"`
// Logger define the logger options for current monitor log
Logger logcfg.Options `json:"logger" yaml:"logger" toml:"logger" mapstructure:"logger"`
}
func (o Config) Validate() liberr.Error {
var e = ErrorValidatorError.Error(nil)
if err := libval.New().Struct(o); err != nil {
if er, ok := err.(*libval.InvalidValidationError); ok {
e.Add(er)
}
for _, er := range err.(libval.ValidationErrors) {
//nolint #goerr113
e.Add(fmt.Errorf("config field '%s' is not validated by constraint '%s'", er.Namespace(), er.ActualTag()))
}
}
if !e.HasParent() {
e = nil
}
return e
}
func (o Config) Clone() Config {
return Config{
Name: o.Name,
CheckTimeout: o.CheckTimeout,
IntervalCheck: o.IntervalCheck,
IntervalFall: o.IntervalFall,
IntervalRise: o.IntervalRise,
FallCountKO: o.FallCountKO,
FallCountWarn: o.FallCountWarn,
RiseCountKO: o.RiseCountKO,
RiseCountWarn: o.RiseCountWarn,
Logger: o.Logger.Clone(),
}
}