Go操作系统系统统计库

用于 Go 的操作系统系统统计库。「OS system statistics library for Go」

Github星跟踪图

OS system statistics library for Go

This is a library to get system metrics like cpu load and memory usage.
The library is created for mackerel-agent.

Example

package main

import (
	"fmt"
	"os"

	"github.com/mackerelio/go-osstat/memory"
)

func main() {
	memory, err := memory.Get()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		return
	}
	fmt.Printf("memory total: %d bytes\n", memory.Total)
	fmt.Printf("memory used: %d bytes\n", memory.Used)
	fmt.Printf("memory cached: %d bytes\n", memory.Cached)
	fmt.Printf("memory free: %d bytes\n", memory.Free)
}

Supported OS, loadavg, uptime, cpu, memory, network, disk i/o, :--:, :--:, :--:, :--:, :--:, :--:, :--:, Linux, o, o, o, o, o, o, Darwin, o, o, △*1, o, o, x, FreeBSD, o, o, x, o, o, x, NetBSD, o, o, x, x, o, x, Windows, x, o, x, o, x, x, *1: unavailable without cgo

Note for counter values

This library returns the counter value for cpu, network and disk I/O statistics by design. To get the cpu usage in percent, network traffic in kB/s or disk IOPS, sleep for a while and calculate the difference.

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/mackerelio/go-osstat/cpu"
)

func main() {
	before, err := cpu.Get()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		return
	}
	time.Sleep(time.Duration(1) * time.Second)
	after, err := cpu.Get()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		return
	}
	total := float64(after.Total - before.Total)
	fmt.Printf("cpu user: %f %%\n", float64(after.User-before.User)/total*100)
	fmt.Printf("cpu system: %f %%\n", float64(after.System-before.System)/total*100)
	fmt.Printf("cpu idle: %f %%\n", float64(after.Idle-before.Idle)/total*100)
}

LICENSE

Copyright 2017-2019 Hatena Co., Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

主要指标

概览
名称与所有者mackerelio/go-osstat
主编程语言Go
编程语言Makefile (语言数: 2)
平台
许可证Apache License 2.0
所有者活动
创建于2017-10-15 01:05:39
推送于2025-06-05 08:32:22
最后一次提交2025-06-05 17:29:35
发布数8
最新版本名称v0.2.6 (发布于 )
第一版名称v0.1.0 (发布于 )
用户参与
星数409
关注者数17
派生数55
提交数265
已启用问题?
问题数10
打开的问题数1
拉请求数41
打开的拉请求数0
关闭的拉请求数13
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?