Esempio n. 1
0
/**
	read node resource usage
**/
func GetNodeResource(w http.ResponseWriter, r *http.Request) {
	// get this node memory
	memory, _ := mem.VirtualMemory()
	// get this node cpu percent usage
	cpu_percent, _ := cpu.CPUPercent(time.Duration(1)*time.Second, false)
	// Disk mount Point
	disk_partitions, _ := disk.DiskPartitions(true)
	// Disk usage
	var disk_usages []*disk.DiskUsageStat
	for _, disk_partition := range disk_partitions {
		if disk_partition.Mountpoint == "/" || disk_partition.Mountpoint == "/home" {
			disk_stat, _ := disk.DiskUsage(disk_partition.Device)
			disk_usages = append(disk_usages, disk_stat)
		}
	}
	// Network
	network, _ := net.NetIOCounters(false)

	// create new node obj with resource usage information
	node_metric := thoth.NodeMetric{
		Cpu:       cpu_percent,
		Memory:    memory,
		DiskUsage: disk_usages,
		Network:   network,
	}

	node_json, err := json.MarshalIndent(node_metric, "", "\t")
	if err != nil {
		fmt.Println("error:", err)
	}
	fmt.Fprint(w, string(node_json))
}
Esempio n. 2
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
}
Esempio n. 3
0
// GetSystem gets statistics about the system
func GetSystem() *api.SystemStats {
	status := new(api.SystemStats)
	if load, err := load.Avg(); err == nil {
		status.Load = &api.SystemStats_Loadstats{
			Load1:  float32(load.Load1),
			Load5:  float32(load.Load5),
			Load15: float32(load.Load15),
		}
	}
	if cpu, err := cpu.Times(false); err == nil && len(cpu) == 1 {
		status.Cpu = &api.SystemStats_CPUStats{
			User:   float32(cpu[0].User),
			System: float32(cpu[0].System),
			Idle:   float32(cpu[0].Idle),
		}
	}
	if mem, err := mem.VirtualMemory(); err == nil {
		status.Memory = &api.SystemStats_MemoryStats{
			Total:     mem.Total,
			Available: mem.Available,
			Used:      mem.Used,
		}
	}
	return status
}
Esempio n. 4
0
func MemInfo() (*Mem, error) {
	info, err := mem.VirtualMemory()
	if err != nil {
		return nil, err
	}
	return &Mem{info.Buffers, info.Cached, info.Total, info.Available, 0, 0, 0}, nil
}
Esempio n. 5
0
func (m Memory) Generate() ([]i3.Output, error) {
	mem, err := mem.VirtualMemory()
	if err != nil {
		return nil, err
	}

	total := mem.Total
	used := total - mem.Buffers - mem.Cached - mem.Free

	percUsed := (100 * used) / total

	var color string
	switch {
	case m.IsCrit(int(percUsed)):
		color = i3.DefaultColors.Crit
	case m.IsWarn(int(percUsed)):
		color = i3.DefaultColors.Warn
	default:
		color = i3.DefaultColors.OK
	}

	out := make([]i3.Output, 1)
	out[0] = i3.Output{
		Name:      m.Name,
		FullText:  fmt.Sprintf(FormatString, percUsed, bytefmt.ByteSize(used), bytefmt.ByteSize(total)),
		Color:     color,
		Separator: true,
	}

	return out, nil
}
Esempio n. 6
0
func GetHardwareProfile() (*pb.HardwareProfile, error) {
	v, err := mem.VirtualMemory()
	if err != nil {
		return &pb.HardwareProfile{}, err
	}
	d, err := disk.Partitions(true)
	if err != nil {
		return &pb.HardwareProfile{}, err
	}
	hw := &pb.HardwareProfile{
		Disks:    make([]*pb.Disk, 0),
		Cpus:     int64(runtime.NumCPU()),
		Memtotal: v.Total,
		Memfree:  v.Free,
	}
	for k := range d {
		usage, err := disk.Usage(d[k].Mountpoint)
		if err != nil {
			continue
		}
		entry := &pb.Disk{
			Path:   d[k].Mountpoint,
			Device: d[k].Device,
			Size_:  usage.Total,
			Used:   usage.Used,
		}
		hw.Disks = append(hw.Disks, entry)
	}
	return hw, nil
}
Esempio n. 7
0
func main() {
	cloud, err := cloud.NewDbusForComDevicehiveCloud()
	if err != nil {
		log.Panic(err)
	}

	h, _ := os.Hostname()

	for {
		time.Sleep(time.Second)
		c, err := cpu.CPUPercent(time.Second, false)
		if err != nil {
			log.Panic(err)
		}

		v, err := mem.VirtualMemory()
		if err != nil {
			log.Panic(err)
		}

		if len(c) > 0 {
			cloud.SendNotification("stats", map[string]interface{}{
				"cpu-usage":    c[0],
				"memory-total": v.Total,
				"memory-free":  v.Free,
				"name":         h,
			}, 1)
		}
	}
}
Esempio n. 8
0
func StartMonitorTask(dir string, filename string) {

	mylog.GetMonitorLogger().Init(dir, filename)

	mylog.GetMonitorLogger().Write("Time  UsedPercent HeapAlloc HeapObjects Alloc NumGC  NumGor\n")
	// alloc, frees
	// 内存

	for {
		logBuffer := new(bytes.Buffer)

		var runMem runtime.MemStats
		runtime.ReadMemStats(&runMem)
		memStat, _ := mem.VirtualMemory()

		fmt.Fprintf(logBuffer, "%v %v %v %v %v %v\n",
			memStat.UsedPercent,
			runMem.HeapAlloc/(M1),
			runMem.HeapObjects,
			runMem.Alloc/(M1),
			runMem.NumGC,
			runtime.NumGoroutine())

		mylog.GetMonitorLogger().Write(logBuffer.String())
		time.Sleep(10 * time.Second)

	}

}
Esempio n. 9
0
//指定时间内扫描cache
func (c *Cache) ExpireBlockStep() int64 {
	c.Lock()
	defer c.Unlock()

	m, err := mem.VirtualMemory()
	if err != nil {
		return int64(0)
	}
	//小于最小触发值,return
	if m.UsedPercent <= c.MinMemUsage {
		return int64(0)
	}
	for file, b := range c.Blocks {
		internal := time.Now().Sub(time.Unix(b.LastAccess, 0))
		if internal >= c.Expire {
			c.ctx.s.logf("expire file: %s, %#v", file, b)
			delete(c.Blocks, file)
			return int64(1)
		}
		if m.UsedPercent >= c.MaxMemUsage {
			if b.lastFiveIdx != 0 && b.lastFiveAccess[b.lastFiveIdx]-b.lastFiveAccess[0] >= 10 {
				c.ctx.s.logf("expire file: %s, %#v", file, b)
				delete(c.Blocks, file)
				return int64(1)
			}
		}
	}
	return int64(0)
}
func main() {
	v, _ := mem.VirtualMemory()

	// almost every return value is a struct
	fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent)

	// convert to JSON. String() is also implemented
	fmt.Println(v)

	h, err := host.HostInfo()
	if err != nil {
		fmt.Println("err:", err)
	} else {

		fmt.Printf("hostname %v", h)
	}

	c, err := cpu.CPUInfo()
	if err != nil {
		fmt.Println("err:", err)
	}
	for _, v := range c {
		fmt.Printf("cpu info %v \n ", v)
	}

}
Esempio n. 11
0
func updateMemory() {
	v, err = mem.VirtualMemory()
	log.Printf("updateMemory(): %v", spew.Sdump(v))
	if err != nil {
		log.Fatal(err)
	}
}
Esempio n. 12
0
func gaugesUpdate(results runner.BenchmarkResults) {
	if s, err := load.Avg(); err == nil {
		gauges["cpu_load1"].Set(s.Load1)
		gauges["cpu_load5"].Set(s.Load5)
		gauges["cpu_load15"].Set(s.Load15)
	}
	if s, err := mem.VirtualMemory(); err == nil {
		gauges["mem_total"].Set(float64(s.Total))
		gauges["mem_used"].Set(float64(s.Used))
	}
	if s, err := disk.Usage("/"); err == nil {
		gauges["disk_total"].Set(float64(s.Total))
		gauges["disk_used"].Set(float64(s.Used))
	}

	if results != nil {
		gauges["io_time"].Set(results["IO"].Time)
		gauges["io_wall_time"].Set(results["IO"].WallTime)
		gauges["io_memory"].Set(float64(results["IO"].Memory))
		gauges["cpu_time"].Set(results["CPU"].Time)
		gauges["cpu_wall_time"].Set(results["CPU"].WallTime)
		gauges["cpu_memory"].Set(float64(results["CPU"].Memory))
		gauges["memory_time"].Set(results["Memory"].Time)
		gauges["memory_wall_time"].Set(results["Memory"].WallTime)
		gauges["memory_memory"].Set(float64(results["Memory"].Memory))
	}
}
Esempio n. 13
0
func memory() string {
	v, err := mem.VirtualMemory()
	if err != nil {
		log.Println(err)
	}
	return fmt.Sprintf("%3d%%", int(v.UsedPercent))
}
Esempio n. 14
0
File: node.go Progetto: bitzend/mgm
func (node mgmNode) collectHostStatistics(out chan mgm.HostStat) {
	for {
		//start calculating network sent
		fInet, err := psnet.NetIOCounters(false)
		if err != nil {
			node.logger.Error("Error reading networking", err)
		}

		s := mgm.HostStat{}
		s.Running = true
		c, err := pscpu.CPUPercent(time.Second, true)
		if err != nil {
			node.logger.Error("Error readin CPU: ", err)
		}
		s.CPUPercent = c

		v, err := psmem.VirtualMemory()
		if err != nil {
			node.logger.Error("Error reading Memory", err)
		}
		s.MEMTotal = v.Total / 1000
		s.MEMUsed = (v.Total - v.Available) / 1000
		s.MEMPercent = v.UsedPercent

		lInet, err := psnet.NetIOCounters(false)
		if err != nil {
			node.logger.Error("Error reading networking", err)
		}
		s.NetSent = (lInet[0].BytesSent - fInet[0].BytesSent)
		s.NetRecv = (lInet[0].BytesRecv - fInet[0].BytesRecv)

		out <- s
	}
}
Esempio n. 15
0
// Collect collects stats related to resource usage of a host
func (h *HostStatsCollector) Collect() (*HostStats, error) {
	hs := &HostStats{Timestamp: time.Now().UTC().UnixNano()}
	if memStats, err := mem.VirtualMemory(); err == nil {
		ms := &MemoryStats{
			Total:     memStats.Total,
			Available: memStats.Available,
			Used:      memStats.Used,
			Free:      memStats.Free,
		}
		hs.Memory = ms
	}

	if cpuStats, err := cpu.Times(true); err == nil {
		cs := make([]*CPUStats, len(cpuStats))
		for idx, cpuStat := range cpuStats {
			cs[idx] = &CPUStats{
				CPU:    cpuStat.CPU,
				User:   cpuStat.User,
				System: cpuStat.System,
				Idle:   cpuStat.Idle,
			}
			percentCalculator, ok := h.statsCalculator[cpuStat.CPU]
			if !ok {
				percentCalculator = NewHostCpuStatsCalculator()
				h.statsCalculator[cpuStat.CPU] = percentCalculator
			}
			idle, user, system, total := percentCalculator.Calculate(cpuStat)
			cs[idx].Idle = idle
			cs[idx].System = system
			cs[idx].User = user
			cs[idx].Total = total
		}
		hs.CPU = cs
	}

	if partitions, err := disk.Partitions(false); err == nil {
		var diskStats []*DiskStats
		for _, partition := range partitions {
			if usage, err := disk.Usage(partition.Mountpoint); err == nil {
				ds := DiskStats{
					Device:            partition.Device,
					Mountpoint:        partition.Mountpoint,
					Size:              usage.Total,
					Used:              usage.Used,
					Available:         usage.Free,
					UsedPercent:       usage.UsedPercent,
					InodesUsedPercent: usage.InodesUsedPercent,
				}
				diskStats = append(diskStats, &ds)
			}
		}
		hs.DiskStats = diskStats
	}

	if uptime, err := host.Uptime(); err == nil {
		hs.Uptime = uptime
	}
	return hs, nil
}
Esempio n. 16
0
func GetRenderContext() *mem.VirtualMemoryStat {
	if avg, err := mem.VirtualMemory(); err == nil {
		return avg
	} else {
		log.Fatal(err)
	}
	return nil
}
Esempio n. 17
0
func GetMemStats() string {
	v, err := mem.VirtualMemory()
	format := "total=%d available=%d used=%d used_percent=%f"
	if err == nil {
		return fmt.Sprintf(format, v.Total, v.Available, v.Used, v.UsedPercent)
	} else {
		return fmt.Sprintf(format, 0, 0, 0, 0)
	}
}
Esempio n. 18
0
File: info.go Progetto: mysll/flynet
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,
	}
}
Esempio n. 19
0
func getMemoryUsage() (float32, float32) {
	v, err := mem.VirtualMemory()
	if err != nil {
		log.Printf("ERROR: Unable to get memory usage for the scout.")
		log.Print(err)
	}

	return float32(v.Total), (float32(v.UsedPercent) / 100.0)
}
Esempio n. 20
0
func main() {
	memSt, err := mem.VirtualMemory()
	if err != nil {
		log.Panicln(err)
	}

	log.Println("Total: ", memSt.Total)
	log.Println("Free: ", memSt.Free)
	log.Println("Used:", memSt.Used)
}
Esempio n. 21
0
func main() {
	v, _ := mem.VirtualMemory()
	c, _ := cpu.CPUInfo()
	// almost every return value is a struct
	fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent)
	for _, v := range c {
		fmt.Printf("Percentage: %f\n", v.Mhz)
	}
	// convert to JSON. String() is also implemented
}
Esempio n. 22
0
func logPoll(n time.Duration, done chan bool) {
	flag := os.O_CREATE | os.O_WRONLY | os.O_APPEND

	fileLog, err := os.OpenFile(FileLog, flag, 0666)
	if err != nil {
		FatalLog.Fatalln("Failed to open log file:", err)
	}
	fileStats, err := os.OpenFile(FileStats, flag, 0666)
	if err != nil {
		FatalLog.Fatalln("Failed to open stats file:", err)
	}
	outLog := io.MultiWriter(fileLog, os.Stdout)

	StatCollect := &statCollector{w: fileStats}
	InfoCounter := NewLogCounter(outLog)
	WarnCounter := NewLogCounter(outLog)
	ErrCounter := NewLogCounter(outLog)
	CritCounter := NewLogCounter(outLog)
	FatalCounter := NewLogCounter(outLog)
	makeLogHandlers(StatCollect, InfoCounter, WarnCounter, ErrCounter, CritCounter, FatalCounter)
	done <- true

	if !DevEnv {
		InfoLog.Println("Now logging stats with", PollCapacity, "records before flushing to disk, every", PollTime, "| Logging every", IntervalPoll)
	}
	var memStats runtime.MemStats
	var stat [PollCapacity]stats
	var pollCount int
	var cpuPercent Percent

	for range time.Tick(n) {
		s := &stat[pollCount]

		runtime.ReadMemStats(&memStats)
		vMem, _ := mem.VirtualMemory()

		if c, _ := cpu.CPUPercent(n, false); len(c) != 0 {
			cpuPercent = Percent(c[0] * 100)
		}
		s.CPUUsed = cpuPercent
		s.MemUsed = memStats.Alloc / KILOBYTE
		s.Memfree = 100 - Percent(vMem.UsedPercent)

		StatsLog.Println("Mem free:", s.Memfree, "% | Mem used:", s.MemUsed, "KB | CPU:", s.CPUUsed, "%")

		if pollCount == PollCapacity-1 {
			//fmt.Println(StatCollect.b.String())
			StatCollect.Flush()
			pollCount = -1
		}
		pollCount++
	}
}
Esempio n. 23
0
// needMemory returns true if we need to free memory
func (gc *GarbageCollector) needMemory() bool {
	// If we have no metrics or minimum memory we remove everything
	if gc.minimumMemory == 0 {
		return true
	}
	stat, err := mem.VirtualMemory()
	if err != nil {
		// TODO: Write a warning to the log
		return true
	}

	return int64(stat.Available) < gc.minimumMemory
}
Esempio n. 24
0
func GetStats(name string) (NodeStats, error) {
	nodeStats := NodeStats{Name: name}

	nodeStats.CPUUsed, _ = getCpuUsage()

	memStat, _ := mem.VirtualMemory()
	nodeStats.MemoryTotal = memStat.Total
	nodeStats.MemoryUsed = memStat.Total - memStat.Available
	nodeStats.MemoryUsedPercent = memStat.UsedPercent

	nodeStats.DiskTotal, nodeStats.DiskUsed, nodeStats.DiskUsedPercent, _ = getDiskUsage()

	return nodeStats, nil
}
Esempio n. 25
0
// MemoryPercent returns how many percent of the total RAM this process uses
func (p *Process) MemoryPercent() (float32, error) {
	machineMemory, err := mem.VirtualMemory()
	if err != nil {
		return 0, err
	}
	total := machineMemory.Total

	processMemory, err := p.MemoryInfo()
	if err != nil {
		return 0, err
	}
	used := processMemory.RSS

	return (100 * float32(used) / float32(total)), nil
}
Esempio n. 26
0
func getMem() bool {
	var flag bool
	v, _ := mem.VirtualMemory()

	// almost every return value is a struct
	fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total/1024, v.Free/1024, v.UsedPercent/1024)

	// convert to JSON. String() is also implemented
	if v.Free/(1024*1024*1024) >= 1 {
		flag = true
	} else {
		flag = false
	}
	return flag
}
Esempio n. 27
0
// 系统状态信息
func handleSystem(resp http.ResponseWriter, req *http.Request) {
	mem, _ := mem.VirtualMemory()
	cpuNum, _ := cpu.CPUCounts(true)
	cpuInfo, _ := cpu.CPUPercent(10*time.Microsecond, true)

	data := make(map[string]interface{})
	data["im.conn"] = len(ClientMaps)
	data["mem.total"] = fmt.Sprintf("%vMB", mem.Total/1024/1024)
	data["mem.free"] = fmt.Sprintf("%vMB", mem.Free/1024/1024)
	data["mem.used_percent"] = fmt.Sprintf("%s%%", strconv.FormatFloat(mem.UsedPercent, 'f', 2, 64))
	data["cpu.num"] = cpuNum
	data["cpu.info"] = cpuInfo

	resp.Write(common.NewIMResponseData(data, "").Encode())
}
Esempio n. 28
0
func gaugesUpdate() {
	if s, err := load.Avg(); err == nil {
		gauges["cpu_load1"].Set(s.Load1)
		gauges["cpu_load5"].Set(s.Load5)
		gauges["cpu_load15"].Set(s.Load15)
	}
	if s, err := mem.VirtualMemory(); err == nil {
		gauges["mem_total"].Set(float64(s.Total))
		gauges["mem_used"].Set(float64(s.Used))
	}
	if s, err := disk.Usage("/"); err == nil {
		gauges["disk_total"].Set(float64(s.Total))
		gauges["disk_used"].Set(float64(s.Used))
	}
}
Esempio n. 29
0
func (s *Server) getLoad() *peerLoad {
	v, _ := mem.VirtualMemory()
	musage := v.UsedPercent

	c, _ := cpu.CPUPercent(time.Second, false)
	cusage := c[0] * 100

	s.RLock()
	clientNum := s.ClientNum
	s.RUnlock()
	return &peerLoad{
		ClientNum: clientNum,
		CpuUsage:  cusage,
		MemUsage:  musage,
	}
}
func virtualMemory(nss []core.Namespace) ([]plugin.MetricType, error) {
	mem, err := mem.VirtualMemory()
	if err != nil {
		return nil, err
	}

	results := make([]plugin.MetricType, len(nss))

	for i, ns := range nss {
		var data interface{}

		switch ns.Element(len(ns) - 1).Value {
		case "total":
			data = mem.Total
		case "available":
			data = mem.Available
		case "used":
			data = mem.Used
		case "used_percent":
			data = mem.UsedPercent
		case "free":
			data = mem.Free
		case "active":
			data = mem.Active
		case "inactive":
			data = mem.Inactive
		case "buffers":
			data = mem.Buffers
		case "cached":
			data = mem.Cached
		case "wired":
			data = mem.Wired
		default:
			return nil, fmt.Errorf("Requested memory statistic %s is not found", ns.String())
		}

		results[i] = plugin.MetricType{
			Namespace_: ns,
			Data_:      data,
			Unit_:      "B",
			Timestamp_: time.Now(),
		}
	}

	return results, nil
}