Example #1
0
func processStat(ctx *web.Context) {
	PidStr := strconv.Itoa(os.Getpid())
	cmd := exec.Command("ps", "u", "-p", PidStr)
	var buf bytes.Buffer
	cmd.Stdout = &buf
	err := cmd.Run()
	if err != nil {
		levelog.Error(err)
		ctx.Abort(400, err.Error())
		return
	}

	r := bufio.NewReader(&buf)
	data, _, err := r.ReadLine()
	if err != nil {
		levelog.Error(err)
		ctx.Abort(400, err.Error())
		return
	}

	data, _, err = r.ReadLine()
	if err != nil {
		levelog.Error(err)
		ctx.Abort(400, err.Error())
		return
	}

	levelog.Info("Data is", string(data))
	ds := strings.Split(string(data), " ")
	nds := make([]string, 0, len(ds))
	for i := 0; i < len(ds); i++ {
		if ds[i] != "" {
			nds = append(nds, ds[i])
		}
	}

	bin, err := json.Marshal(processStatInfo{nds[2], nds[3], nds[4], nds[5], runtime.NumGoroutine()})
	if err != nil {
		levelog.Error(err)
		ctx.Abort(400, err.Error())
		return
	}

	ctx.Write(bin)
}
Example #2
0
File: util.go Project: vuleetu/misc
func processCmd(conn net.Conn) {
	defer conn.Close()

	lenBuf := make([]byte, 4)
	_, err := io.ReadFull(conn, lenBuf)
	if err != nil {
		levelog.Error("Read length failed:", err)
		conn.Write([]byte("fail"))
		return
	}

	length := binary.BigEndian.Uint32(lenBuf)

	if length > maximum_cmd_length {
		levelog.Error("Length is greater than maximum cmd length")
		conn.Write([]byte("fail"))
		return
	}

	cmdBuf := make([]byte, length)
	_, err = io.ReadFull(conn, cmdBuf)
	if err != nil {
		levelog.Error("Read body failed:", err)
		conn.Write([]byte("fail"))
		return
	}

	if fun, ok := cmds[strings.ToLower(string(cmdBuf))]; ok {
		err = fun(conn)
		if err != nil {
			levelog.Error("Command execute failed:", err)
			return
		}

		return
	}

	levelog.Warn("No callback for cmd", string(cmdBuf))
	conn.Write([]byte("fail"))
}
Example #3
0
File: util.go Project: vuleetu/misc
func start(addr string) {
	stat, err := os.Stat(addr)
	if err != nil {
		if !os.IsNotExist(err) {
			levelog.Error("Stat", addr, "failed")
			return
		}
	} else {
		if stat.IsDir() {
			levelog.Error("Address can not be a folder")
			return
		}

		err = os.Remove(addr)
		if err != nil {
			levelog.Error("Remove", addr, "failed")
			return
		}
	}

	l, err := net.Listen("unix", addr)
	if err != nil {
		levelog.Error("Listen at unix socket domain with address:", addr, "failed, error is:", err)
		return
	}

	for {
		conn, err := l.Accept()
		if err != nil {
			levelog.Error("Accept new connection from unix socket domain failed")
			continue
		}

		//will use sync block way to handle the conn instead of async
		processCmd(conn)
	}
}