Exemple #1
0
func GetSystemInfo() (*lib.SystemInfo, error) {
	var system_info lib.SystemInfo
	var uname_info syscall.Utsname

	if err := syscall.Uname(&uname_info); err != nil {
		log.Fatal(err)
		return nil, err
	}

	system_info.Hostname = string(lib.ConvertToSlice(uname_info.Nodename))
	system_info.Sysname = string(lib.ConvertToSlice(uname_info.Sysname))
	system_info.Release = string(lib.ConvertToSlice(uname_info.Release))
	system_info.Machine = string(lib.ConvertToSlice(uname_info.Machine))

	cpuinfo_file := "/proc/cpuinfo"

	cpuinfo_data, err := lib.ReadFileFullContentByLine(cpuinfo_file)

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

	cpuinfo_model_name_splited := strings.Split(string(cpuinfo_data[4]), ":")
	cpuinfo_frequency_splited := strings.Split(string(cpuinfo_data[6]), ":")

	cpuinfo_model_name := strings.Trim(cpuinfo_model_name_splited[1], " ")
	cpuinfo_frequency := strings.Trim(cpuinfo_frequency_splited[1], " ")

	system_info.CPUModelName = cpuinfo_model_name
	system_info.CPUFrequency = cpuinfo_frequency

	system_info.CPUCores = runtime.NumCPU()

	system_info.Location = lib.GetTimeLocation()
	system_info.GOVersion = runtime.Version()
	system_info.ProcessID = os.Getpid()

	return &system_info, nil
}
Exemple #2
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)
	}
}