• Stars
    star
    384
  • Rank 111,726 (Top 3 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created about 7 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

OS system statistics library for Go

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 Coverage 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.

More Repositories

1

mackerel-agent

mackerel-agent is an agent program to post your hosts' metrics to mackerel.io.
Go
426
star
2

go-check-plugins

Check Plugins for monitoring written in golang
Go
254
star
3

mackerel-agent-plugins

Plugins for mackerel-agent
Go
246
star
4

mkr

Command Line Tool for Mackerel
Go
111
star
5

mackerel-client-go

Mackerel API Client in Go
Go
46
star
6

mackerel-client-ruby

Mackerel client implemented by Ruby
Ruby
41
star
7

fluent-plugin-mackerel

Fluent plugin to send metrics to mackerel.io
Ruby
38
star
8

mackerel-container-agent

Monitoring agent of Mackerel for containers on container orchestration platforms.
Go
35
star
9

ansible-mackerel-agent

An Ansible role for mackerel-agent. (development version)
Jinja
33
star
10

cookbook-mackerel-agent

Chef cookbook for mackerel-agent installation
Ruby
23
star
11

go-mackerel-plugin

Development helpers for mackerel agent plugin
Go
22
star
12

plugin-registry

Perl
16
star
13

go-mackerel-plugin-helper

Development helpers for mackerel agent plugin
Go
13
star
14

documents

Source markdown files of Mackerel documentation hosted on https://mackerel.io
13
star
15

checkers

Go
12
star
16

homebrew-mackerel-agent

homebrew tap to install mackerel-agent
Ruby
10
star
17

golib

Go
8
star
18

itamae-plugin-recipe-mackerel-agent

Ruby
8
star
19

mdg

mdg - Mackerel Dashboard Generator
Ruby
6
star
20

docker-mackerel-agent

Shell
6
star
21

mackerel-plugin-json

Go
5
star
22

hubot-mackerel-notifier

CoffeeScript
5
star
23

mackerel-crawler

Go
4
star
24

mackerel-plugin-aws-batch

Go
3
star
25

fluent-plugin-webhook-mackerel

fluentd input plugin for receiving Mackerel webhook
Ruby
2
star
26

graphite-docker

Graphite image for Docker
Python
1
star
27

check-aws-cloudwatch-logs-insights

yet another Amazon CloudWatch Logs check plugin with CloudWatch Logs Insights
Go
1
star
28

mackerel-plugin-gcp-compute-engine

Go
1
star
29

mackerel-plugin-sample

Sample plugin for mackerel.io agent
Go
1
star
30

mackerel-plugin-aws-ec2

Go
1
star
31

workflows

Reusable workflows for use in the mackerelio organization
1
star
32

mackerel-plugin-nvidia-smi

Go
1
star