Example #1
0
func GetLoadInfo(channel chan *lib.LoadInfo, sleep time.Duration) {
	var load_info lib.LoadInfo

	for {
		meminfo_file := "/proc/meminfo"

		meminfo_data, err := lib.ReadFileFullContentByLine(meminfo_file)

		if err != nil {
			log.Fatal(err)
			return
		}

		now_time := lib.GetTimeString()
		load_info.TimeString = now_time

		meminfo_total_splited := bytes.Fields(meminfo_data[0])
		meminfo_free_splited := bytes.Fields(meminfo_data[1])
		meminfo_buffers_splited := bytes.Fields(meminfo_data[2])
		meminfo_cached_splited := bytes.Fields(meminfo_data[3])

		if v, err := strconv.ParseUint(string(meminfo_total_splited[1]), 10, 0); err == nil {
			load_info.MemTotal = v
		}

		if v, err := strconv.ParseUint(string(meminfo_free_splited[1]), 10, 0); err == nil {
			load_info.MemFree = v
		}

		if v, err := strconv.ParseUint(string(meminfo_buffers_splited[1]), 10, 0); err == nil {
			load_info.MemBuffers = v
		}

		if v, err := strconv.ParseUint(string(meminfo_cached_splited[1]), 10, 0); err == nil {
			load_info.MemCached = v
		}

		load_info.MemUsed = load_info.MemTotal - load_info.MemFree

		loadavg_file := "/proc/loadavg"

		loadavg_data, err := lib.ReadFileFullContent(loadavg_file)

		if err != nil {
			log.Fatal(err)
			return
		}

		loadavg_splited := strings.Split(string(loadavg_data), " ")

		if len(loadavg_splited) > 0 {
			// 1分钟内的负载
			load1 := strings.Trim(loadavg_splited[0], " ")
			if v, err := strconv.ParseFloat(load1, 64); err == nil {
				load_info.LoadAVG1 = v
			}

			// 5分钟内的负载
			load5 := strings.Trim(loadavg_splited[1], " ")
			if v, err := strconv.ParseFloat(load5, 64); err == nil {
				load_info.LoadAVG5 = v
			}

			// 15分钟内的负载
			load15 := strings.Trim(loadavg_splited[2], " ")
			if v, err := strconv.ParseFloat(load15, 64); err == nil {
				load_info.LoadAVG15 = v
			}
		}

		channel <- &load_info
		time.Sleep(sleep)
	}
}
Example #2
0
// 获取进程的信息
func GetProcessInfo(channel chan *lib.ProcessInfo, sleep time.Duration) {
	/*
	   Process's memory status in /proc/PID/statm:
	   total program pages |
	   resident pages |
	   shared pages |
	   text(code) |
	   data/stack |
	   library |
	   dirty pages

	   page size is 4K
	*/

	var process_info lib.ProcessInfo

	pagesize := int64(os.Getpagesize())

	for {
		pid := os.Getpid()

		statm_file := fmt.Sprintf("/proc/%d/statm", pid)

		data, err := lib.ReadFileFullContent(statm_file)

		if err != nil {
			log.Fatal(err)
			return
		}

		now_time := lib.GetTimeString()
		process_info.TimeString = now_time

		memory_splited := strings.Split(string(data), " ")

		if len(memory_splited) > 0 {
			// 虚拟内存(VIRT)
			virtual_pages := strings.Trim(memory_splited[0], " ")
			if v, err := strconv.ParseInt(virtual_pages, 10, 0); err == nil {
				process_info.VirtualMemory = v * pagesize
			}

			// 物理内存(RES)
			resisdent_pages := strings.Trim(memory_splited[1], " ")
			if v, err := strconv.ParseInt(resisdent_pages, 10, 0); err == nil {
				process_info.ResisdentMemory = v * pagesize
			}

			// 共享内存(SHR)
			shared_pages := strings.Trim(memory_splited[2], " ")
			if v, err := strconv.ParseInt(shared_pages, 10, 0); err == nil {
				process_info.SharedMemory = v * pagesize
			}
		}

		// 进程运行了多长时间
		process_info.Uptime = int64(time.Now().Sub(lib.StartTime).Seconds())

		channel <- &process_info

		time.Sleep(sleep)
	}
}