Skip to content

Commit

Permalink
Cover issues #1, xiaotuanyu120#2 and fix wrong defer
Browse files Browse the repository at this point in the history
  • Loading branch information
tranmh authored Jun 21, 2022
1 parent f09a84a commit d32fbb2
Showing 1 changed file with 39 additions and 25 deletions.
64 changes: 39 additions & 25 deletions watchdog-tomcat.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package main

import (
"bytes"
"errors"
"flag"
"fmt"
"io"
"net/http"
"crypto/tls"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"syscall"
"time"

"github.com/coreos/go-systemd/daemon"
)

func runWatchedApp(application string) (pid int, err error) {
/*Execute Application
1. run app and set group pid for the forked child process
Expand All @@ -32,19 +33,19 @@ func runWatchedApp(application string) (pid int, err error) {
}
pgid := cmd.Process.Pid
cmd.Wait()

/*Get pid of JVM
get pid of child process by filter ps result using group pid*/
grep := exec.Command("grep", strconv.Itoa(pgid))
ps := exec.Command("ps", "axo", "pid,pgid,comm")

var out bytes.Buffer
pr, pw := io.Pipe()

ps.Stdout = pw
grep.Stdin = pr
grep.Stdout = &out

err = ps.Start()
if err != nil {
fmt.Println(err)
Expand All @@ -63,35 +64,48 @@ func runWatchedApp(application string) (pid int, err error) {
}()
defer pr.Close()
grep.Wait()

res := strings.TrimSpace(out.String())
pid, _ = strconv.Atoi(strings.Split(res, " ")[0])
return pid, nil
}

func healthCheck(check_url string, timeout time.Duration) (bool, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
var netClient = &http.Client{
Transport: tr,
Timeout: time.Second * timeout,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := netClient.Get(check_url)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return false, err
}
if resp.StatusCode != 200 {
return false, errors.New("health check status is not 200")
if resp.StatusCode == 200 {
return true, nil
} else if resp.StatusCode >= 300 && resp.StatusCode < 400 {
return true, nil
} else {
return false, errors.New("health check status is not 200 or 3xx")
}
defer resp.Body.Close()
return true, nil
}

func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}

func main() {
// GET ALL FLAGS
app := flag.String("app", "", "path to the app should be run")
Expand All @@ -103,14 +117,14 @@ func main() {
initialcheck_timeout := flag.Duration("initialcheck-timeout", 5, "Timeout for initialcheck when service is boot up")
fail_max := flag.Int("fail-max", 20, "max continued failed time")
flag.Parse()

if !fileExists(*app) {
fmt.Printf("app [%s] should exist and is an executable file", app)
os.Exit(1)
}

check_url := fmt.Sprintf("%s://%s:%s/%s", *scheme, *ip, *port, *path)

// RUN APPLICATION
pid, err := runWatchedApp(*app)
if err != nil {
Expand All @@ -119,7 +133,7 @@ func main() {
} else {
daemon.SdNotify(false, fmt.Sprintf("MAINPID=%d", pid))
}

/* WATCHDOG INITIAL
start watchdog when
1. first health check success*/
Expand All @@ -134,26 +148,26 @@ func main() {
}
time.Sleep(1000 * time.Millisecond)
}

// WATCHDOG START
var wg sync.WaitGroup
wg.Add(1)
go func(check_url string) {
watchdog_usec, _ := strconv.ParseFloat(os.Getenv("WATCHDOG_USEC"), 64)

var wd_fail bool
var wd_usec, check_time_spent float64
var wd_interval, continue_fail int

wd_usec = watchdog_usec / (2 * 1000000)

for {
// send watchdog signal
if wd_fail == false {
daemon.SdNotify(false, daemon.SdNotifyWatchdog)
fmt.Printf("WATCHDOG STATUS: activate; LAST_CHECK_TIME_SPENT: %f; LAST_SLEEP_TIME: %d\n", check_time_spent, wd_interval)
}

// check and change the watchdog failed state
check_start := time.Now()
/* add your alert logic here, send alert to email or IM
Expand All @@ -171,7 +185,7 @@ func main() {
wd_fail = false
}
check_time_spent = time.Since(check_start).Seconds()

if wd_fail == false {
if check_success == false {
fmt.Printf("CHECK STATUS: failed; ERROR: %s\n", err.Error())
Expand All @@ -186,4 +200,4 @@ func main() {
}
}(check_url)
wg.Wait()
}
}

0 comments on commit d32fbb2

Please sign in to comment.