Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error suppression #41749

Closed
w3bservice opened this issue Oct 2, 2020 · 3 comments
Closed

Error suppression #41749

w3bservice opened this issue Oct 2, 2020 · 3 comments

Comments

@w3bservice
Copy link

@w3bservice w3bservice commented Oct 2, 2020

Hello,
how to suppress the error output and output an "exit status 0"?
im new on golang...

What version of Go are you using (go version)?

$ go version
go version go1.15.2 linux/amd64

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/joerg/.cache/go-build"
GOENV="/home/joerg/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/joerg/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/joerg/go"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/usr/lib/golang"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build861249311=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main
import (
	"os"
	"strings"
        "strconv"
	"net/http"
	"bytes"
	"time"
)
func main() {
	var res int
	if strings.EqualFold(os.Getenv("DUPLICATI__PARSED_RESULT"),"Success")  {
	  res = 1
	} else {
	  res = 0
	}
	data := "backup_result " + strconv.Itoa(res) + "\n"
	body := bytes.NewReader([]byte(data))
	req, err := http.NewRequest("POST", "http://pushgateway.tld:9091/metrics/job/"+os.Getenv("DUPLICATI__backup_name"), body)
	req.Header.Set("Content-Type", "application/octet-stream")
	client := &http.Client{}
	resp,err := client.Do(req)
	defer resp.Body.Close()
	if err != nil {
	}
	func() {
    time.Sleep(120 * time.Second)
    }()
    data2 := "backup_result "  + "\n"
	body2 := bytes.NewReader([]byte(data2))
	req2,err2 := http.NewRequest("DELETE", "http://pushgateway.tld:9091/metrics/job/"+os.Getenv("DUPLICATI__backup_name"), body2)
        req2.Header.Set("Content-Type", "application/octet-stream")
	client2 := &http.Client{}
	resp2,err2 := client2.Do(req2)
	defer resp2.Body.Close()
	if err2 != nil {
	}
}

I am working on a solution to monitor our backups, with Duplicati, via Grafana. Duplicate sends a mail to a pushgateway after the backup is done. With the script below it works so far. If the pushgateway is not reachable, the script aborts with an "exit status 2". This will provoke an error message in Duplicati as a warning. The script should also terminate with an "exit status 0" in case of an error.
How can this be realized?
I hope you can help me

@jaswdr
Copy link

@jaswdr jaswdr commented Oct 2, 2020

@w3bservice you can write something like this:

package main

import (
        "fmt"
        "os"
)

func main() {
        fmt.Printf("exit status 0")
        os.Exit(0)
}
@w3bservice
Copy link
Author

@w3bservice w3bservice commented Oct 2, 2020

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x646ff9]

goroutine 1 [running]:
main.main()
/home/joerg/metric5.go:24 +0x359

package main
import (
"os"
"strings"
"strconv"
"net/http"
"bytes"
"time"
"fmt"
)
func main() {
var res int
if strings.EqualFold(os.Getenv("DUPLICATI__PARSED_RESULT"),"Success") {
res = 1
} else {
res = 0
}
data := "backup_result " + strconv.Itoa(res) + "\n"
body := bytes.NewReader([]byte(data))
req, err := http.NewRequest("POST", "http://pushgateway.tld:9091/metrics/job/"+os.Getenv("DUPLICATI__backup_name"), body)
req.Header.Set("Content-Type", "application/octet-stream")
client := &http.Client{}
resp,err := client.Do(req)
defer resp.Body.Close()
if err != nil {
}
fmt.Printf("exit status 0")
os.Exit(0)
func() {
time.Sleep(120 * time.Second)
}()
data2 := "backup_result " + "\n"
body2 := bytes.NewReader([]byte(data2))
req2,err2 := http.NewRequest("DELETE", "http://pushgateway.tld:9091/metrics/job/"+os.Getenv("DUPLICATI__backup_name"), body2)
req2.Header.Set("Content-Type", "application/octet-stream")
client2 := &http.Client{}
resp2,err2 := client2.Do(req2)
defer resp2.Body.Close()
if err2 != nil {
}
}

@ALTree
Copy link
Member

@ALTree ALTree commented Oct 2, 2020

Hi,

the Go project does not use its bug tracker for general discussion or asking questions about the language. The Github bug tracker is only used for tracking bugs and proposals going through the Proposal Process.

Please see the Questions wiki page; it has a list of good places for asking questions. Thanks!

Closing here, since this is not a bug.

@ALTree ALTree closed this Oct 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.