all repos — honk @ 975ba8e3ac4a485bd13ebdbf0eebb2af968200f1

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	"runtime/debug"
20	"syscall"
21	"time"
22
23	"github.com/dustin/go-humanize"
24)
25
26func init() {
27	if softwareVersion != "develop" {
28		return
29	}
30	bi, ok := debug.ReadBuildInfo()
31	if !ok {
32		return
33	}
34	var vcs, rev, mod string
35	for _, bs := range bi.Settings {
36		if bs.Key == "vcs" {
37			vcs = "/" + bs.Value
38		}
39		if bs.Key == "vcs.revision" {
40			rev = bs.Value
41			if len(rev) > 12 {
42				rev = rev[:12]
43			}
44			rev = "-" + rev
45		}
46		if bs.Key == "vcs.modified" && bs.Value == "true" {
47			mod = "+"
48		}
49	}
50	softwareVersion += vcs + rev + mod
51}
52
53type Sensors struct {
54	Memory float64
55	Uptime string
56	CPU    float64
57}
58
59var boottime = time.Now()
60
61func getSensors() Sensors {
62	var usage syscall.Rusage
63	syscall.Getrusage(syscall.RUSAGE_SELF, &usage)
64
65	now := time.Now()
66
67	var sensors Sensors
68	sensors.Memory = float64(usage.Maxrss) / 1024.0
69	sensors.Uptime = humanize.RelTime(boottime, now, "", "")
70	sensors.CPU = time.Duration(usage.Utime.Nano()).Seconds()
71
72	return sensors
73}
74
75func setLimits() error {
76	var limit syscall.Rlimit
77	limit.Cur = 2 * 1024 * 1024 * 1024
78	limit.Max = 2 * 1024 * 1024 * 1024
79	return syscall.Setrlimit(syscall.RLIMIT_DATA, &limit)
80}