OS system statistics library for Go

Overview

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.

GoDev Build Status

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 yes yes yes yes yes yes
Darwin yes yes *1 yes yes no
FreeBSD yes yes no yes yes no
NetBSD yes yes no no yes no
OpenBSD yes yes no no no no
Windows no yes no yes no no

*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.
Issues
  • use x/sys/unix instead of syscall for linux

    use x/sys/unix instead of syscall for linux

    According to the Go official document (https://golang.org/pkg/syscall), the syscall package is deprecated and recommended to use x/sys packages instead. Also updates the go.sum

    opened by nasa9084 4
  • interface metrics are wrong on docker

    interface metrics are wrong on docker

    Hi ✋ I'm using mackerel-agent(docker), and felt interface metrics are something wrong. https://github.com/mackerelio/go-osstat/blob/master/network/network_linux.go#L17 Does mackerel-agent(docker) reads procfs on the container? But I'd like it to read the value on host machine. My mackerel-agent doesn't mount host's /proc though..

    opened by koooge 3
  • Fix crash on empty line in /proc/stat

    Fix crash on empty line in /proc/stat

    Sometimes /proc/stat contain one or more empty line.

    $ cat /proc/version
    Linux version 2.6.32-042stab092.2 ([email protected]) (gcc version 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC) ) #1 SMP Tue Jul 8 10:35:55 MSK 2014
    $ cat /etc/debian_version
    7.11
    $ cat /proc/stat
    cpu  7470681 0 1586197 13118409351 150947 0 0 0
    cpu0 3293762 0 733398 6560288846 63595 0 0 0
    cpu1 4176919 0 852799 6558120504 87351 0 0 0
    intr 0
    swap 0 0
    
    ctxt 93458773
    btime 1447514956
    processes 275160
    procs_running 0
    procs_blocked 0
    
    opened by hogewest 3
  • Bump actions/setup-go from 2 to 3

    Bump actions/setup-go from 2 to 3

    Bumps actions/setup-go from 2 to 3.

    Release notes

    Sourced from actions/setup-go's releases.

    v3.0.0

    What's Changed

    Breaking Changes

    With the update to Node 16, all scripts will now be run with Node 16 rather than Node 12.

    This new major release removes the stable input, so there is no need to specify additional input to use pre-release versions. This release also corrects the pre-release versions syntax to satisfy the SemVer notation (1.18.0-beta1 -> 1.18.0-beta.1, 1.18.0-rc1 -> 1.18.0-rc.1).

    steps:
      - uses: actions/[email protected]
      - uses: actions/[email protected]
        with:
          go-version: '1.18.0-rc.1' 
      - run: go version
    

    Add check-latest input

    In scope of this release we add the check-latest input. If check-latest is set to true, the action first checks if the cached version is the latest one. If the locally cached version is not the most up-to-date, a Go version will then be downloaded from go-versions repository. By default check-latest is set to false. Example of usage:

    steps:
      - uses: actions/[email protected]
      - uses: actions/[email protected]
        with:
          go-version: '1.16'
          check-latest: true
      - run: go version
    

    Moreover, we updated @actions/core from 1.2.6 to 1.6.0

    v2.1.5

    In scope of this release we updated matchers.json to improve the problem matcher pattern. For more information please refer to this pull request

    v2.1.4

    What's Changed

    New Contributors

    Full Changelog: https://github.com/actions/setup-go/compare/v2.1.3...v2.1.4

    v2.1.3

    • Updated communication with runner to use environment files rather then workflow commands

    v2.1.2

    This release includes vendored licenses for this action's npm dependencies.

    ... (truncated)

    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    dependencies github_actions 
    opened by dependabot[bot] 1
  • [darwin] parse page size in vm_stat output

    [darwin] parse page size in vm_stat output

    The vm_stat command outputs the page size along with the snapshot values, and I think it's better to use this value than using a fixed value as we currently do.

    I looked into some (older and newer) versions of vm_stat and the format seems to be unchanged, so there should be no compatibility issue.

    • https://opensource.apple.com/source/system_cmds/
    • oldest: https://opensource.apple.com/source/system_cmds/system_cmds-175.2/vm_stat.tproj/vm_stat.c.auto.html
    • newest: https://opensource.apple.com/source/system_cmds/system_cmds-880.60.2/vm_stat.tproj/vm_stat.c.auto.html

    Also updated the references (one has been moved, and one is not found).

    opened by susisu 1
  • Missing mention of disk stats unit in reference

    Missing mention of disk stats unit in reference

    I don't see anywhere in the reference about measurement unit of ReadsCompleted and WritesCompleted fields of disk stats type. Are they read/write coumy or read/witten bytes? If they counts, is there anyway to get rKB/s and wKB/s displayed by iostat?

    opened by r9host 1
  • [nit] Update Dependabot config file

    [nit] Update Dependabot config file

    :wave: Dependabot is moving natively into GitHub! This pull request updates your config file to the new syntax. When you merge this pull request, we'll swap out dependabot-preview (me) for a new dependabot app, and you'll be all set!

    With this change, you'll now use the Dependabot page in GitHub, rather than the Dependabot dashboard, to monitor your version updates. Dependabot is now configured exclusively using config files.

    The new version does not yet support private git dependencies. If you use these we recommend recommend leaving Dependabot Preview active.

    If you've got any questions or feedback for us, please let us know by creating an issue in the dependabot/dependabot-core repository.

    Learn more about the relaunch of Dependabot

    Please note that regular @dependabot commands do not work on this pull request.

    :robot::yellow_heart:

    dependencies 
    opened by dependabot-preview[bot] 0
  • collect the zombie process surely

    collect the zombie process surely

    On macOS 10.15.x Catalina (or later?), to execute new process will failed if the executable file of current process was removed. Thus if mackerel-agent is installed by Homebrew, brew upgrade will cause above catastrophe.

    In result, it can occur many zombie processes because the functions that collect metrics such as collectMemoryStats, collectSwapStats will return an error.

    opened by lufia 0
  • consider MemAvailable field in `/proc/meminfo` on linux

    consider MemAvailable field in `/proc/meminfo` on linux

    When there is a MemAvailable field, which is enabled in linux kernel 3.19.1 or later in /proc/meminfo, we use it to calculate accurate memory usage.

    If MemAvailable field is available, MemAvailableEnabled field turn true.

    • Added MemAvailableEnabled field to determine if MemAvaileble can be acquired
    • Adjust the stats.Used calculation way when theMemAvailableEnabled field is true
      • It does not affect mackerel-agent because which calculate memory.used by itself https://github.com/mackerelio/mackerel-agent/blob/9061847409f4f4f7eed550387c58873e86cde1fe/metrics/linux/memory.go#L38

    ref. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773

    opened by Songmu 0
  • memory.Cached undefined

    memory.Cached undefined

    ./emergency-response.go:45:46: memory.Cached undefined (type *memory.Stats has no field or method Cached)
    
    memory, err := memory.Get()
    fmt.Printf("memory cached: %d GB\n", memory.Cached)
    
    opened by Ran-Xing 0
Releases(v0.2.2)
Owner
Mackerel
Mackerel
Cloudinsight Agent is a system tool that monitors system processes and services, and sends information back to your Cloudinsight account.

Cloudinsight Agent 中文版 README Cloudinsight Agent is written in Go for collecting metrics from the system it's running on, or from other services, and

cloudinsight-backup 365 Jul 14, 2022
System resource usage profiler tool which regularly takes snapshots of the memory and CPU load of one or more running processes so as to dynamically build up a profile of their usage of system resources.

Vegeta is a system resource usage tracking tool built to regularly take snapshots of the memory and CPU load of one or more running processes, so as to dynamically build up a profile of their usage of system resources.

Kartik 8 Jan 16, 2022
EdgeLog is a lightweight log management system, and Agent is a part of EdgeLog system

EdgeLog is a lightweight log management system, and Agent is a part of EdgeLog system. It is installed on host machine and its main duty is to collect host program log statics.

null 2 Mar 2, 2022
The Prometheus monitoring system and time series database.

Prometheus Visit prometheus.io for the full documentation, examples and guides. Prometheus, a Cloud Native Computing Foundation project, is a systems

Prometheus 43.7k Aug 4, 2022
rtop is an interactive, remote system monitoring tool based on SSH

rtop rtop is a remote system monitor. It connects over SSH to a remote system and displays vital system metrics (CPU, disk, memory, network). No speci

RapidLoop 2k Aug 7, 2022
distributed monitoring system

OWL OWL 是由国内领先的第三方数据智能服务商 TalkingData 开源的一款企业级分布式监控告警系统,目前由 Tech Operation Team 持续开发更新维护。 OWL 后台组件全部使用 Go 语言开发,Go 语言是 Google 开发的一种静态强类型、编译型、并发型,并具有垃圾回

null 821 Jul 30, 2022
An Open Source video surveillance management system for people making this world a safer place.

Kerberos Open Source Docker Hub | Documentation | Website Kerberos Open source (v3) is a cutting edge video surveillance management system made availa

Kerberos.io 302 Aug 8, 2022
A system and resource monitoring tool written in Golang!

Grofer A clean and modern system and resource monitor written purely in golang using termui and gopsutil! Currently compatible with Linux only. Curren

PES Open Source Community 214 Aug 6, 2022
A tool to list and diagnose Go processes currently running on your system

gops gops is a command to list and diagnose Go processes currently running on your system. $ gops 983 980 uplink-soecks go1.9 /usr/local/bin/u

Google 5.7k Aug 10, 2022
An open-source and enterprise-level monitoring system.

Falcon+ Documentations Usage Open-Falcon API Prerequisite Git >= 1.7.5 Go >= 1.6 Getting Started Docker Please refer to ./docker/README.md. Build from

Open-Falcon 6.9k Aug 5, 2022
Distributed simple and robust release management and monitoring system.

Agente Distributed simple and robust release management and monitoring system. **This project on going work. Road map Core system First worker agent M

StreetByters Community 31 Mar 3, 2022
checkah is an agentless SSH system monitoring and alerting tool.

CHECKAH checkah is an agentless SSH system monitoring and alerting tool. Features: agentless check over SSH (password, keyfile, agent) config file bas

deadc0de 7 Aug 5, 2022
An example logging system using Prometheus, Loki, and Grafana.

Logging Example Structure Collector Export numerical data for Prometheus and log data for Promtail. Exporter uses port 8080 Log files are saved to ./c

YoungHwan Joo 3 May 11, 2022
Cloudprober is a monitoring software that makes it super-easy to monitor availability and performance of various components of your system.

Cloudprober is a monitoring software that makes it super-easy to monitor availability and performance of various components of your system. Cloudprobe

null 170 Aug 9, 2022
Gomon - Go language based system monitor

Copyright © 2021 The Gomon Project. Welcome to Gomon, the Go language based system monitor Welcome to Gomon, the Go language based system monitor Over

zosmac 2 May 17, 2022
System information collector

Gohai Gohai is a tool which collects an inventory of system information. It aims to implement some parts of features from facter and ohai. It's forked

null 0 Mar 19, 2020
BTFS - The First Scalable Decentralized Storage System - A Foundational Platform for Decentralized Applications

go-btfs What is BTFS? BitTorrent File System (BTFS) is a protocol forked from IPFS that utilizes the TRON network and the BitTorrent Ecosystem for int

BitTorrent Inc. 83 Jul 26, 2022
Wlog: Logging System Desgned For Web

Logging System Desgned For Web Clean This is very clean logging system and easy

AoXiang Li 0 May 14, 2022
Clogger: a WIP version of my idealized logging system

Clogger Clogger is a WIP version of my idealized logging system. In many ways, i

Colin Douch 10 Jan 24, 2022