mackerelio/go-osstat

Name: go-osstat

Owner: Mackerel

Description: OS system statistics library for Go

Created: 2017-10-15 01:05:39.0

Updated: 2018-03-12 13:04:13.0

Pushed: 2018-03-12 13:04:15.0

Homepage:

Size: 99

Language: Go

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

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
age main

rt (
"fmt"
"os"

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


 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.

age main

rt (
"fmt"
"os"
"time"

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


 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
right 2017 Hatena Co., Ltd.

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

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

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

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.