doupi6737 2012-07-24 05:18
浏览 76
已采纳

Go- CPU /内存/网络

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条回答 默认 最新

  • dqd2800 2012-07-25 07:57
    关注

    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 (
        "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%%
    Kernel: %f%%
    User: %f%%
    ", (totalIdle / totalSys) * 100, (totalKernel / totalSys) * 100, (totalUser / totalSys) * 100)
        fmt.Printf("
    Total: %f%%
    ", (totalSys - totalIdle) * 100 / totalSys)
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C