Go- CPU/Memory/Network

huangapple go评论99阅读模式
英文:

Go- CPU/Memory/Network

问题

我正在编写一个跨平台的Go应用程序,作为守护进程运行。它基本上监听文件系统上的更改,并将更改复制/下载到/从服务器。我的想法是它不应该减慢机器的速度,而是在机器空闲时排队任务。

为了做到这一点,我想监视CPU、RAM和网络使用情况,如果可能的话还有网络使用情况。我目前的目标是Windows 7+、Mac OS 10+和Linux 3+。

我主要是一个Linux程序员,所以对Windows/BSD的API不太熟悉,但我已经找到了一些关于BSD的好资料,所以我想我可以弄清楚。我在追踪Windows系统调用方面遇到了一些问题(我不知道如何获取Windows的man页面...)。我已经找到了以下资源(适用于Windows):

我更喜欢使用系统调用而不是调用C++ API。显然,我对已经实现这一功能的任何其他Go库也很感兴趣。

最终,我正在寻找类似于这样的东西:http://nodejs.org/api/os.html

我基本上正在寻找系统调用和如何使用它们的示例。

如果我走错了方向,请告诉我。我并不固执于轮询内核以获取系统信息的想法,但在我看来,这似乎是获取该信息最可靠的方法。我更喜欢将应用程序作为非特权用户运行。

英文:

I'm writing a cross-platform Go application that runs as a daemon. It basically listens for changes on the filesystem and copies/downloads changes to/from a server. The idea is that it should never slow down the machine, queueing tasks until the machine is idle.

To do this, I would like to monitor CPU, RAM, and network usage, but network usage (if possible). I am currently targeting Windows 7+, Mac OS 10+, and Linux 3+.

I'm primarily a Linux programmer, so I'm not familiar with Windows/BSD APIs, but I've already found some good information about BSD, so I think I can figure that out. I'm having trouble tracking down the Windows syscalls (I don't know of a way to get a Windows man page...). I've managed to find these resources (for Windows):

I would prefer to use a syscall instead of shelling out/wrapping a C++ API. Obviously, I would be interested in any other Go libraries that already do this.

Ultimately, I'm looking for something like this: http://nodejs.org/api/os.html

I'm pretty much looking for syscalls and examples of how to use them.

If I'm headed in the wrong direction, please let me know. I'm not married to the idea of polling the kernel for system information, but that seemed to me the most reliable way to get that information. I'd prefer to run the application as an underprivileged user.

答案1

得分: 4

我发现了一个项目,https://github.com/AllenDang/w32,它为许多Windows API调用提供了包装器。我在我的分支中添加了一个调用GetSystemTimes(https://github.com/beatgammit/w32),这是我目前正在使用的。我已经打开了一个拉取请求,所以它应该很快合并。

这使我能够获取CPU信息(类似于/proc/stats的空闲、内核和用户时间),看起来效果还不错。以下是一些示例代码:

package main

import (
	"w32"
	"fmt"
	"time"
)

func main() {
	var idle, kernel, user w32.FILETIME

	w32.GetSystemTimes(&idle, &kernel, &user)
	idleFirst   := idle.DwLowDateTime   | (idle.DwHighDateTime << 32)
	kernelFirst := kernel.DwLowDateTime | (kernel.DwHighDateTime << 32)
	userFirst   := user.DwLowDateTime   | (user.DwHighDateTime << 32)

	time.Sleep(time.Second)

	w32.GetSystemTimes(&idle, &kernel, &user)
	idleSecond   := idle.DwLowDateTime   | (idle.DwHighDateTime << 32)
	kernelSecond := kernel.DwLowDateTime | (kernel.DwHighDateTime << 32)
	userSecond   := user.DwLowDateTime   | (user.DwHighDateTime << 32)

	totalIdle   := float64(idleSecond - idleFirst)
	totalKernel := float64(kernelSecond - kernelFirst)
	totalUser   := float64(userSecond - userFirst)
	totalSys    := float64(totalKernel + totalUser)

	fmt.Printf("Idle: %f%%\nKernel: %f%%\nUser: %f%%\n", (totalIdle / totalSys) * 100, (totalKernel / totalSys) * 100, (totalUser / totalSys) * 100)
	fmt.Printf("\nTotal: %f%%\n", (totalSys - totalIdle) * 100 / totalSys)
}

如果我能找到RAM/网络统计信息,我就可以完成了。

英文:

I found https://github.com/AllenDang/w32, a project that provides wrappers for a lot of Windows API calls. I added a call to GetSystemTimes in my fork, and that is what I am currently using. I've opened a pull request, so it should be merged soon.

This allowed me to get CPU information (times for idle, kernel and user, similar to /proc/stats), and it seems to be working pretty well. Here's some example code:

package main

import (
	&quot;w32&quot;
	&quot;fmt&quot;
	&quot;time&quot;
)

func main() {
	var idle, kernel, user w32.FILETIME

	w32.GetSystemTimes(&amp;idle, &amp;kernel, &amp;user)
	idleFirst   := idle.DwLowDateTime   | (idle.DwHighDateTime &lt;&lt; 32)
	kernelFirst := kernel.DwLowDateTime | (kernel.DwHighDateTime &lt;&lt; 32)
	userFirst   := user.DwLowDateTime   | (user.DwHighDateTime &lt;&lt; 32)

	time.Sleep(time.Second)

	w32.GetSystemTimes(&amp;idle, &amp;kernel, &amp;user)
	idleSecond   := idle.DwLowDateTime   | (idle.DwHighDateTime &lt;&lt; 32)
	kernelSecond := kernel.DwLowDateTime | (kernel.DwHighDateTime &lt;&lt; 32)
	userSecond   := user.DwLowDateTime   | (user.DwHighDateTime &lt;&lt; 32)

	totalIdle   := float64(idleSecond - idleFirst)
	totalKernel := float64(kernelSecond - kernelFirst)
	totalUser   := float64(userSecond - userFirst)
	totalSys    := float64(totalKernel + totalUser)

	fmt.Printf(&quot;Idle: %f%%\nKernel: %f%%\nUser: %f%%\n&quot;, (totalIdle / totalSys) * 100, (totalKernel / totalSys) * 100, (totalUser / totalSys) * 100)
	fmt.Printf(&quot;\nTotal: %f%%\n&quot;, (totalSys - totalIdle) * 100 / totalSys)
}

If I can find RAM/Network stats, I should be set.

huangapple
  • 本文由 发表于 2012年7月24日 13:18:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/11624562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定