mirror of
https://github.com/byReqz/probehost2.git
synced 2024-11-21 14:23:15 +00:00
add basic logging
This commit is contained in:
parent
99ff702d5d
commit
9af6ed7b41
7
go.mod
Normal file
7
go.mod
Normal file
@ -0,0 +1,7 @@
|
||||
module main
|
||||
|
||||
go 1.17
|
||||
|
||||
require github.com/sirupsen/logrus v1.8.1
|
||||
|
||||
require golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
|
12
go.sum
Normal file
12
go.sum
Normal file
@ -0,0 +1,12 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
35
main.go
35
main.go
@ -1,17 +1,44 @@
|
||||
package main
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"log"
|
||||
"strings"
|
||||
"net/http"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func runner(command string, args... string) string{
|
||||
var logstdout = log.New()
|
||||
var logfile = log.New()
|
||||
|
||||
func init() {
|
||||
logstdout.SetOutput(os.Stdout)
|
||||
logstdout.SetLevel(log.WarnLevel)
|
||||
|
||||
logpath, err := os.OpenFile("probehost2.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0660)
|
||||
if err != nil {
|
||||
logstdout.Fatal("Failed to initialize the logfile: ", err.Error())
|
||||
}
|
||||
logfile.SetLevel(log.InfoLevel)
|
||||
logfile.SetOutput(logpath)
|
||||
logfile.Info("probehost2 initialized")
|
||||
}
|
||||
|
||||
func runner(remoteip string, command string, args... string) string{
|
||||
cmd, err := exec.Command(command, args...).Output()
|
||||
if err != nil {
|
||||
if ! strings.Contains(err.Error(), "1") { // dont exit if error code is 1
|
||||
log.Fatal(command, args, "caused an error: ", err)
|
||||
logstdout.WithFields(log.Fields{
|
||||
"remote_ip": remoteip,
|
||||
"command": fmt.Sprint(command, args),
|
||||
"error": err.Error(),
|
||||
}).Warn("the following request failed:")
|
||||
logfile.WithFields(log.Fields{
|
||||
"remote_ip": remoteip,
|
||||
"command": fmt.Sprint(command, args),
|
||||
"error": err.Error(),
|
||||
}).Warn("the following request failed:")
|
||||
}
|
||||
}
|
||||
return string(cmd)
|
||||
@ -24,7 +51,7 @@ func showhelp(w http.ResponseWriter, req *http.Request) {
|
||||
func ping(w http.ResponseWriter, req *http.Request) {
|
||||
geturl := strings.Split(req.URL.String(), "/")
|
||||
target := geturl[2]
|
||||
pingres := runner("ping", "-c5", target)
|
||||
pingres := runner(req.RemoteAddr, "ping", "-c5", target)
|
||||
fmt.Fprintln(w, pingres)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user