Пример #1
0
// MemoryUsage - XXX
func MemoryUsage() MemoryStruct {
	mem, _ := psmem.VirtualMemory()
	swap, _ := psmem.SwapMemory()

	TotalMB, _ := util.ConvertBytesTo(mem.Total, "mb", 0)
	FreeMB, _ := util.ConvertBytesTo(mem.Free, "mb", 0)
	UsedMB, _ := util.ConvertBytesTo(mem.Used, "mb", 0)
	UsedPercent, _ := util.FloatDecimalPoint(mem.UsedPercent, 0)
	SwapUsedMB, _ := util.ConvertBytesTo(swap.Used, "mb", 0)
	SwapTotalMB, _ := util.ConvertBytesTo(swap.Total, "mb", 0)
	SwapFreeMB, _ := util.ConvertBytesTo(swap.Free, "mb", 0)
	SwapUsedPercent, _ := util.FloatDecimalPoint(swap.UsedPercent, 0)

	m := MemoryStruct{
		UsedMB:          UsedMB,
		TotalMB:         TotalMB,
		FreeMB:          FreeMB,
		UsedPercent:     UsedPercent,
		SwapUsedMB:      SwapUsedMB,
		SwapTotalMB:     SwapTotalMB,
		SwapFreeMB:      SwapFreeMB,
		SwapUsedPercent: SwapUsedPercent,
	}

	return m
}
Пример #2
0
func GetInfo() map[string]interface{} {
	path := "/"
	if runtime.GOOS == "windows" {
		file, _ := exec.LookPath(os.Args[0])
		path = filepath.VolumeName(file)
	}

	diskinfo := disk.UsageStat{}
	if v, err := disk.Usage(path); err == nil {
		var i interface{}
		i = v
		switch inst := i.(type) {
		case disk.UsageStat:
			diskinfo = inst
			break
		case *disk.UsageStat:
			if inst != nil {
				diskinfo = *inst
			}
			break
		}

	}
	diskinfo.Total /= 1024 * 1024
	diskinfo.Used /= 1024 * 1024

	vm := mem.VirtualMemoryStat{}
	sm := mem.SwapMemoryStat{}

	if v, err := mem.VirtualMemory(); err == nil {
		vm = *v
	}
	vm.Total /= 1024 * 1024
	vm.Used /= 1024 * 1024
	if v, err := mem.SwapMemory(); err == nil {
		sm = *v
	}
	sm.Total /= 1024 * 1024
	sm.Used /= 1024 * 1024

	var cpupercent float64

	ps, err := cpu.Percent(time.Millisecond, true)
	if err == nil && len(ps) > 0 {
		for _, v := range ps {
			cpupercent += v
		}
		cpupercent /= float64(len(ps))
	}

	return map[string]interface{}{
		"DiskInfo":      diskinfo,
		"SwapMemory":    sm,
		"VirtualMemory": vm,
		"CpuPercent":    cpupercent * 100,
	}
}
Пример #3
0
func initMemoryInfo(monitorData *monitoringData) error {
	memoryInfo, err := mem.VirtualMemory()
	swapInfo, err := mem.SwapMemory()

	if memoryInfo != nil && swapInfo != nil {
		monitorData.AvailableMemory = memoryInfo.Available
		monitorData.TotalMemory = memoryInfo.Total
		monitorData.UsedMemory = memoryInfo.Used
		monitorData.UsedPercent = memoryInfo.UsedPercent
		monitorData.FreeMemory = memoryInfo.Free
		monitorData.SharedMemory = memoryInfo.Shared
		monitorData.TotalSwapMemory = swapInfo.Total
		monitorData.UsedSwap = swapInfo.Used
		monitorData.FreeSwap = swapInfo.Free
		monitorData.SwapUsedPercent = swapInfo.UsedPercent
	}

	return err
}
Пример #4
0
func (s *systemPS) SwapStat() (*mem.SwapMemoryStat, error) {
	return mem.SwapMemory()
}
Пример #5
0
func (st *Stat) stat(t string) string {
	checkErr := func(err error) string {
		return "系统酱正在食用作死药丸中..."
	}
	switch t {
	case "free":
		m, err := mem.VirtualMemory()
		checkErr(err)
		s, err := mem.SwapMemory()
		checkErr(err)
		mem := new(runtime.MemStats)
		runtime.ReadMemStats(mem)
		return fmt.Sprintf(
			"全局:\n"+
				"Total: %s Free: %s\nUsed: %s %s%%\nCache: %s\n"+
				"Swap:\nTotal: %s Free: %s\n Used: %s %s%%\n"+
				"群组娘:\n"+
				"Allocated: %s\nTotal Allocated: %s\nSystem: %s\n",
			helper.HumanByte(m.Total, m.Free, m.Used, m.UsedPercent, m.Cached,
				s.Total, s.Free, s.Used, s.UsedPercent,
				mem.Alloc, mem.TotalAlloc, mem.Sys)...,
		)
	case "df":
		fs, err := disk.DiskPartitions(false)
		checkErr(err)
		var buf bytes.Buffer
		for k := range fs {
			du, err := disk.DiskUsage(fs[k].Mountpoint)
			switch {
			case err != nil, du.UsedPercent == 0, du.Free == 0:
				continue
			}
			f := fmt.Sprintf("Mountpoint: %s Type: %s \n"+
				"Total: %s Free: %s \nUsed: %s %s%%\n",
				helper.HumanByte(fs[k].Mountpoint, fs[k].Fstype,
					du.Total, du.Free, du.Used, du.UsedPercent)...,
			)
			buf.WriteString(f)
		}
		return buf.String()
	case "os":
		h, err := host.HostInfo()
		checkErr(err)
		uptime := time.Duration(time.Now().Unix()-int64(h.Uptime)) * time.Second
		l, err := load.LoadAvg()
		checkErr(err)
		c, err := cpu.CPUPercent(time.Second*3, false)
		checkErr(err)
		return fmt.Sprintf(
			"OSRelease: %s\nHostName: %s\nUptime: %s\nLoadAvg: %.2f %.2f %.2f\n"+
				"Goroutine: %d\nCPU: %.2f%%",
			h.Platform, h.Hostname, uptime.String(), l.Load1, l.Load5, l.Load15,
			runtime.NumGoroutine(), c[0],
		)
	case "redis":
		info := conf.Redis.Info().Val()
		if info != "" {
			infos := strings.Split(info, "\r\n")
			infoMap := make(map[string]string)
			for k := range infos {
				line := strings.Split(infos[k], ":")
				if len(line) > 1 {
					infoMap[line[0]] = line[1]
				}
			}
			DBSize := conf.Redis.DbSize().Val()
			return fmt.Sprintf("Redis Version: %s\nOS: %s\nUsed Memory: %s\n"+
				"Used Memory Peak: %s\nDB Size: %d\n",
				infoMap["redis_version"], infoMap["os"], infoMap["used_memory_human"],
				infoMap["used_memory_peak_human"], DBSize)
		}
		return ""
	default:
		return "欢迎来到未知领域(ゝ∀・)"
	}
}
Пример #6
0
func json_swapinfo(w http.ResponseWriter, r *http.Request) {
	swapinfo, _ := mem.SwapMemory()
	b, _ := json.Marshal(swapinfo)
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprintf(w, string(b))
}
Пример #7
0
func (o *SwapMetric) Value() (float64, error) {
	m, e := mem.SwapMemory()
	return m.UsedPercent, e
}