Skip to content

Commit

Permalink
optimize flow and structure
Browse files Browse the repository at this point in the history
  • Loading branch information
danielb42 committed Jul 28, 2020
1 parent 582e84a commit ea6249e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 52 deletions.
Binary file removed artifacts/sensu-webwhois_v1.2.3_linux_amd64.tar.gz
Binary file not shown.
1 change: 0 additions & 1 deletion artifacts/sensu-webwhois_v1.2.3_sha512_checksums.txt

This file was deleted.

Binary file not shown.
1 change: 1 addition & 0 deletions artifacts/sensu-webwhois_v1.2.4_sha512_checksums.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b4906a82feba9c4cb707fb23c2f1fd6bbb53a19afb33bf91a4e059a94616c7bf7fe81c4d96fffc65e6e685a15297e9973e8a05eb8259ce4d8f26188c72f4956d sensu-webwhois_v1.2.4_linux_amd64.tar.gz
Binary file modified bin/sensu-webwhois
Binary file not shown.
96 changes: 47 additions & 49 deletions cmd/sensu-webwhois/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,83 +9,81 @@ import (
"strings"
"time"

whiteflag "github.com/danielb42/whiteflag" // MIT
"github.com/danielb42/whiteflag"
)

var (
stringToLookFor string = "ist bereits registriert."
stringToLookFor = "ist bereits registriert"
timeBegin = time.Now()
httpResp *http.Response
)

func main() {

var err error
log.SetOutput(os.Stderr)

whiteflag.Alias("d", "domain", "use the given domain for check order")
whiteflag.ParseCommandLine()
domainToCheck := whiteflag.GetString("domain")

postString := fmt.Sprintf("lang=de&domain=%s&domainwhois_submit=Abfrage+starten", domainToCheck)
postbody := strings.NewReader(postString)

timeBegin := time.Now()
postBody := strings.NewReader(postString)

os.Setenv("HTTP_PROXY", "")
os.Setenv("HTTPS_PROXY", "")
os.Setenv("http_proxy", "")
os.Setenv("https_proxy", "")

req, err := http.NewRequest("POST", "https://www.denic.de/webwhois/", postbody)
httpReq, err := http.NewRequest("POST", "https://www.denic.de/webwhois/", postBody)
if err != nil {
log.Printf("ERROR: %s\n\n", err.Error())
fmt.Printf("%s %d %d\n", "sensu.webwhois.registered", 0, timeBegin.Unix())
fmt.Printf("%s %d %d\n", "sensu.webwhois.duration", 0, timeBegin.Unix())
fmt.Printf("%s %d %d\n", "sensu.webwhois.responsecode", 0, timeBegin.Unix())
os.Exit(2)
printFailMetricsAndExit(err.Error())
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, err := http.DefaultClient.Do(req)
httpReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")

httpResp, err = http.DefaultClient.Do(httpReq)
if err != nil {
log.Printf("ERROR: %s\n\n", err.Error())
fmt.Printf("%s %d %d\n", "sensu.webwhois.registered", 0, timeBegin.Unix())
fmt.Printf("%s %d %d\n", "sensu.webwhois.duration", 0, timeBegin.Unix())
fmt.Printf("%s %d %d\n", "sensu.webwhois.responsecode", 0, timeBegin.Unix())
os.Exit(2)
printFailMetricsAndExit(err.Error())
}
defer resp.Body.Close()
defer httpResp.Body.Close()

webwhoisResponseTime := time.Since(timeBegin).Milliseconds()

if resp.StatusCode == http.StatusOK {

bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("ERROR: %s\n\n", err.Error())
fmt.Printf("%s %d %d\n", "sensu.webwhois.registered", 0, timeBegin.Unix())
fmt.Printf("%s %d %d\n", "sensu.webwhois.duration", 0, timeBegin.Unix())
fmt.Printf("%s %d %d\n", "sensu.webwhois.responsecode", 200, timeBegin.Unix())
os.Exit(2)
}
bodyString := string(bodyBytes)

if strings.Contains(bodyString, stringToLookFor) {
log.Printf("OK: webwhois output contains '%s'\n\n", stringToLookFor)
fmt.Printf("%s %d %d\n", "sensu.webwhois.registered", 1, timeBegin.Unix())
fmt.Printf("%s %d %d\n", "sensu.webwhois.duration", webwhoisResponseTime, timeBegin.Unix())
fmt.Printf("%s %d %d\n", "sensu.webwhois.responsecode", 200, timeBegin.Unix())
os.Exit(0)
} else {
log.Printf("ERROR: webwhois output did not contain '%s'\n\n", stringToLookFor)
fmt.Printf("%s %d %d\n", "sensu.webwhois.registered", 0, timeBegin.Unix())
fmt.Printf("%s %d %d\n", "sensu.webwhois.duration", webwhoisResponseTime, timeBegin.Unix())
fmt.Printf("%s %d %d\n", "sensu.webwhois.responsecode", 200, timeBegin.Unix())
os.Exit(2)
}
} else {
log.Printf("ERROR: HTTP status code was not 200\n\n")
fmt.Printf("%s %d %d\n", "sensu.webwhois.registered", 0, timeBegin.Unix())
bodyBytes, err := ioutil.ReadAll(httpResp.Body)
if err != nil {
printFailMetricsAndExit(err.Error())
}

if strings.Contains(string(bodyBytes), stringToLookFor) {
log.Printf("OK: webwhois output contains '%s'\n\n", stringToLookFor)
fmt.Printf("%s %d %d\n", "sensu.webwhois.registered", 1, timeBegin.Unix())
fmt.Printf("%s %d %d\n", "sensu.webwhois.duration", webwhoisResponseTime, timeBegin.Unix())
fmt.Printf("%s %d %d\n", "sensu.webwhois.responsecode", resp.StatusCode, timeBegin.Unix())
fmt.Printf("%s %d %d\n", "sensu.webwhois.responsecode", httpResp.StatusCode, timeBegin.Unix())
} else {
printFailMetricsAndExit("webwhois output did not contain", "'"+stringToLookFor+"'")
}
}

func printFailMetricsAndExit(errors ...string) {

var statusCode int

if httpResp != nil {
statusCode = httpResp.StatusCode
httpResp.Body.Close() // nolint:errcheck
}

errStr := "ERROR:"

os.Exit(2)
for _, err := range errors {
errStr += " " + err
}

log.Printf("%s\n\n", errStr)
fmt.Printf("%s %d %d\n", "sensu.webwhois.registered", 0, timeBegin.Unix())
fmt.Printf("%s %d %d\n", "sensu.webwhois.duration", 0, timeBegin.Unix())
fmt.Printf("%s %d %d\n", "sensu.webwhois.responsecode", statusCode, timeBegin.Unix())

os.Exit(2)
}
4 changes: 2 additions & 2 deletions sensu/asset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ api_version: core/v2
metadata:
name: sensu-webwhois
spec:
sha512: bf27b2e19341f1b608dab3fc466c97270c66b9e05c567535b2f4b23a6feb76e012b5a57ed8b89135c9ae2c10361b3d428efb2f144831a4ce607a0495a353bcb7
url: https://github.com/DENICeG/sensu-webwhois/releases/download/v1.2.3/sensu-webwhois_v1.2.3_linux_amd64.tar.gz
sha512: b4906a82feba9c4cb707fb23c2f1fd6bbb53a19afb33bf91a4e059a94616c7bf7fe81c4d96fffc65e6e685a15297e9973e8a05eb8259ce4d8f26188c72f4956d
url: https://github.com/DENICeG/sensu-webwhois/releases/download/v1.2.4/sensu-webwhois_v1.2.4_linux_amd64.tar.gz

0 comments on commit ea6249e

Please sign in to comment.