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 "github.com/dustin/go-humanize"
23)
24
25type Sensors struct {
26 Memory float64
27 Uptime string
28 CPU float64
29}
30
31var boottime = time.Now()
32
33func getSensors() Sensors {
34 var usage syscall.Rusage
35 syscall.Getrusage(syscall.RUSAGE_SELF, &usage)
36
37 now := time.Now()
38
39 var sensors Sensors
40 sensors.Memory = float64(usage.Maxrss) / 1024.0
41 sensors.Uptime = humanize.RelTime(boottime, now, "", "")
42 sensors.CPU = time.Duration(usage.Utime.Nano()).Seconds()
43
44 return sensors
45}
46
47func setLimits() error {
48 var limit syscall.Rlimit
49 limit.Cur = 2 * 1024 * 1024 * 1024
50 limit.Max = 2 * 1024 * 1024 * 1024
51 return syscall.Setrlimit(syscall.RLIMIT_DATA, &limit)
52}