add -W option
@@ -24,6 +24,7 @@ type tcpOpts struct {
Warning float64 `short:"w" long:"warning" description:"Response time to result in warning status (seconds)"`
Critical float64 `short:"c" long:"critical" description:"Response time to result in critical status (seconds)"`
Escape bool `short:"E" long:"escape" description:"Can use \\n, \\r, \\t or \\ in send or quit string. Must come before send or quit option. By default, nothing added to send, \\r\\n added to end of quit"`
+ ErrWarning bool `short:"W" long:"err-warning" description:"Set the exit status at error occurrence to WARNING (default is CRITICAL). -c option is not affected by this option."`
}
type exchange struct {
@@ -160,6 +161,7 @@ func (opts *tcpOpts) run() *checkers.Checker {
if err != nil {
return checkers.Unknown(err.Error())
}
+
// prevent changing output of some commands
os.Setenv("LANG", "C")
os.Setenv("LC_ALL", "C")
@@ -178,6 +180,9 @@ func (opts *tcpOpts) run() *checkers.Checker {
conn, err := dial(proto, addr, opts.SSL, opts.NoCheckCertificate, timeout)
if err != nil {
+ if opts.ErrWarning {
+ return checkers.Warning(err.Error())
+ }
return checkers.Critical(err.Error())
}
defer conn.Close()
@@ -185,6 +190,9 @@ func (opts *tcpOpts) run() *checkers.Checker {
if opts.Send != "" {
err := write(conn, []byte(opts.Send), timeout)
if err != nil {
+ if opts.ErrWarning {
+ return checkers.Warning(err.Error())
+ }
return checkers.Critical(err.Error())
}
}
@@ -193,10 +201,16 @@ func (opts *tcpOpts) run() *checkers.Checker {
if opts.expectReg != nil {
buf, err := slurp(conn, opts.MaxBytes, timeout)
if err != nil {
+ if opts.ErrWarning {
+ return checkers.Warning(err.Error())
+ }
return checkers.Critical(err.Error())
}
res = string(buf)
if !opts.expectReg.MatchString(res) {
+ if opts.ErrWarning {
+ return checkers.Warning("Unexpected response from host/socket: " + res)
+ }
return checkers.Critical("Unexpected response from host/socket: " + res)
}
}
@@ -204,6 +218,9 @@ func (opts *tcpOpts) run() *checkers.Checker {
if opts.Quit != "" {
err := write(conn, []byte(opts.Quit), timeout)
if err != nil {
+ if opts.ErrWarning {
+ return checkers.Warning(err.Error())
+ }
return checkers.Critical(err.Error())
}
}
