all repos — honk @ 07821ff8e0c3a0c5b907424c404b2993982f80cc

my fork of honk

sensors.go (view raw)

 1//
 2// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
 3//
 4// Permission to use, copy, modify, and distribute this software for any
 5// purpose with or without fee is hereby granted, provided that the above
 6// copyright notice and this permission notice appear in all copies.
 7//
 8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16package main
17
18import (
19	"syscall"
20	"time"
21)
22
23type Sensors struct {
24	Memory float64
25	Uptime float64
26	CPU    float64
27}
28
29var boottime = time.Now()
30
31func getSensors() Sensors {
32	var usage syscall.Rusage
33	syscall.Getrusage(syscall.RUSAGE_SELF, &usage)
34
35	now := time.Now()
36
37	var sensors Sensors
38	sensors.Memory = float64(usage.Maxrss) / 1024.0
39	sensors.Uptime = now.Sub(boottime).Seconds()
40	sensors.CPU = time.Duration(usage.Utime.Nano()).Seconds()
41
42	return sensors
43}