Exemplo n.º 1
0
func ListenPorts() []int64 {
	bs, err := systool.CmdOutBytes("ss", "-n", "-l", "-t")
	if err != nil {
		log.Error("ss -n -l exec fail: %s", err)
		return []int64{}
	}

	reader := bufio.NewReader(bytes.NewBuffer(bs))

	// ignore the first line
	var line []byte
	line, _, err = reader.ReadLine()
	if err == io.EOF || err != nil {
		return []int64{}
	}

	ret := []int64{}

	for {
		line, _, err = reader.ReadLine()
		if err == io.EOF || err != nil {
			break
		}

		arr := strings.Fields(string(line))
		arrlen := len(arr)

		if arrlen != 4 && arrlen != 5 {
			log.Error("output of [ss -n -l -t] format error")
			continue
		}

		ci := 2
		if arrlen == 5 {
			ci = 3
		}

		location := strings.LastIndex(arr[ci], ":")
		port := arr[ci][location+1:]

		if p, e := strconv.ParseInt(port, 10, 64); e != nil {
			log.Error("parse port to int64 fail: %s", e)
			continue
		} else {
			ret = append(ret, p)
		}

	}

	ret = slicetool.SliceUniqueInt64(ret)

	log.Info("listening ports: %v", ret)
	return ret
}
Exemplo n.º 2
0
func StartHttp() {

	p, err := strconv.Atoi(global.HttpPort)
	if err != nil {
		logtool.Fetal("port[%s] format error", global.HttpPort)
		os.Exit(1)
	}

	if funcs.PortIsListen(int64(p)) {
		logtool.Fetal("port[%d] is in listen", p)
		os.Exit(1)
	}

	m = martini.Classic()

	m.Use(render.Renderer(render.Options{
		Funcs: []template.FuncMap{{
			"nl2br":      nl2br,
			"htmlquote":  htmlQuote,
			"str2html":   str2html,
			"dateformat": dateFormat,
		}},
	}))

	m.Get("/healthz", func() string {
		return "ok"
	})

	m.Get("/", func(re render.Render) {
		m := make(map[string]string)
		m["version"] = global.Version
		re.HTML(200, "index", m)
	})

	CfgKernelRouter()
	CfgSystemRouter()
	CfgCpuRouter()
	CfgMemRouter()
	CfgDfRouter()
	CfgNetRouter()
	CfgIORouter()

	logtool.Info("use http port: %s", global.HttpPort)
	http.ListenAndServe(":"+global.HttpPort, m)
}
Exemplo n.º 3
0
func CmdRunWithTimeout(cmd *exec.Cmd, timeout time.Duration) (error, bool) {
	done := make(chan error)
	go func() {
		done <- cmd.Wait()
	}()

	var err error
	select {
	case <-time.After(timeout):
		//timeout
		if err = cmd.Process.Kill(); err != nil {
			log.Error("failed to kill: %s, error: %s", cmd.Path, err)
		}
		go func() {
			<-done // allow goroutine to exit
		}()
		log.Info("process:%s killed", cmd.Path)
		return err, true
	case err = <-done:
		return err, false
	}
}