Created
June 22, 2017 07:52
-
-
Save siddontang/21cbc36505e30b58a099fbc32cde8034 to your computer and use it in GitHub Desktop.
Use nmz to inspect process deployed by tidb-ansible.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"os/exec" | |
"strconv" | |
"strings" | |
"syscall" | |
"time" | |
) | |
var ( | |
pidFile = flag.String("pid-file", "", "PID file to get real PID") | |
nmzConfigFile = flag.String("nmz", "", "namazu config file") | |
) | |
func readPID() (int64, error) { | |
data, err := ioutil.ReadFile(*pidFile) | |
if err != nil { | |
return 0, err | |
} | |
pid, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64) | |
if err != nil { | |
return 0, err | |
} | |
return pid, nil | |
} | |
func pexit(msg string) { | |
println(msg) | |
os.Exit(1) | |
} | |
func checkProcExist(pid int64) error { | |
process, err := os.FindProcess(int(pid)) | |
if err != nil { | |
return err | |
} | |
if err = process.Signal(syscall.Signal(0)); err != nil { | |
return err | |
} | |
return nil | |
} | |
func runInspect(pid int64) { | |
ctx, cancel := context.WithCancel(context.Background()) | |
go func() { | |
defer cancel() | |
t := time.NewTicker(time.Second) | |
defer t.Stop() | |
for { | |
<-t.C | |
if err := checkProcExist(pid); err != nil { | |
fmt.Printf("check process %d failed, err %v", pid, err) | |
return | |
} | |
} | |
}() | |
cmd := exec.CommandContext(ctx, "nmz", "inspectors", "proc", "-pid", | |
strconv.FormatInt(pid, 10), "-autopilot", | |
*nmzConfigFile, "-watch-interval", "1s") | |
cmd.Stdout = os.Stdout | |
cmd.Stderr = os.Stderr | |
cmd.Run() | |
} | |
func main() { | |
flag.Parse() | |
if len(*pidFile) == 0 { | |
pexit("must assign the PID file") | |
} | |
if len(*nmzConfigFile) == 0 { | |
pexit("must assign the namazu config file") | |
} | |
for { | |
pid, err := readPID() | |
if err != nil { | |
fmt.Printf("can't read PID from PID file %s, retry later", *pidFile) | |
time.Sleep(5 * time.Second) | |
continue | |
} | |
if err := checkProcExist(pid); err != nil { | |
fmt.Printf("check process %d failed, err %v, retry later", pid, err) | |
time.Sleep(5 * time.Second) | |
continue | |
} | |
runInspect(pid) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment