Merge branch 'temps'

This commit is contained in:
Caleb Bassi 2018-05-23 21:43:29 -07:00
commit bed05dc425
14 changed files with 136 additions and 52 deletions

2
Gopkg.lock generated
View File

@ -58,7 +58,7 @@
"net",
"process"
]
revision = "cd915bdc31582b0a56405ede7fa2f4ab043f851b"
revision = "57f370e13068146efe1cb7129f79e5d51da8a242"
[[projects]]
branch = "master"

View File

@ -6,11 +6,9 @@ package widgets
import (
"fmt"
"sort"
"strings"
"time"
ui "github.com/cjbassi/termui"
psHost "github.com/shirou/gopsutil/host"
)
type Temp struct {
@ -43,18 +41,6 @@ func NewTemp() *Temp {
return self
}
func (self *Temp) update() {
sensors, _ := psHost.SensorsTemperatures()
for _, sensor := range sensors {
// only sensors with input in their name are giving us live temp info
if strings.Contains(sensor.SensorKey, "input") {
// removes '_input' from the end of the sensor name
label := sensor.SensorKey[:strings.Index(sensor.SensorKey, "_input")]
self.Data[label] = int(sensor.Temperature)
}
}
}
// Buffer implements ui.Bufferer interface and renders the widget.
func (self *Temp) Buffer() *ui.Buffer {
buf := self.Block.Buffer()

View File

@ -1,18 +1,17 @@
// +build darwin
// +build cgo
// TODO do we need to add '+build cgo'?
package host
package widgets
// #cgo LDFLAGS: -framework IOKit
// #include "include/smc.c"
import "C"
import "context"
func SensorsTemperatures() ([]TemperatureStat, error) {
return SensorsTemperaturesWithContext(context.Background())
type TemperatureStat struct {
SensorKey string `json:"sensorKey"`
Temperature float64 `json:"sensorTemperature"`
}
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
func SensorsTemperatures() ([]TemperatureStat, error) {
temperatureKeys := []string{
C.AMBIENT_AIR_0,
C.AMBIENT_AIR_1,
@ -49,3 +48,10 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err
}
return temperatures, nil
}
func (self *Temp) update() {
sensors, _ := SensorsTemperatures()
for _, sensor := range sensors {
self.Data[sensor.SensorKey] = int(sensor.Temperature)
}
}

19
src/widgets/temp_linux.go Normal file
View File

@ -0,0 +1,19 @@
package widgets
import (
"strings"
psHost "github.com/shirou/gopsutil/host"
)
func (self *Temp) update() {
sensors, _ := psHost.SensorsTemperatures()
for _, sensor := range sensors {
// only sensors with input in their name are giving us live temp info
if strings.Contains(sensor.SensorKey, "input") {
// removes '_input' from the end of the sensor name
label := sensor.SensorKey[:strings.Index(sensor.SensorKey, "_input")]
self.Data[label] = int(sensor.Temperature)
}
}
}

View File

@ -0,0 +1,12 @@
package widgets
import (
psHost "github.com/shirou/gopsutil/host"
)
func (self *Temp) update() {
sensors, _ := psHost.SensorsTemperatures()
for _, sensor := range sensors {
self.Data[sensor.SensorKey] = int(sensor.Temperature)
}
}

View File

@ -117,6 +117,10 @@ Several methods have been added which are not present in psutil, but will provid
- VirtualizationSystem (ex: "LXC")
- VirtualizationRole (ex: "guest"/"host")
- IOCounters
- Label (linux only) The registered [device mapper name](https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-block-dm)
- cpu/CPUInfo() (linux, freebsd)
- CPU (ex: 0, 1, ...)

View File

@ -42,6 +42,7 @@ type IOCountersStat struct {
WeightedIO uint64 `json:"weightedIO"`
Name string `json:"name"`
SerialNumber string `json:"serialNumber"`
Label string `json:"label"`
}
func (d UsageStat) String() string {

View File

@ -5,6 +5,7 @@ package disk
import (
"context"
"fmt"
"io/ioutil"
"os/exec"
"path/filepath"
"strconv"
@ -370,6 +371,8 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC
d.Name = name
d.SerialNumber = GetDiskSerialNumber(name)
d.Label = GetLabel(name)
ret[name] = d
}
return ret, nil
@ -406,6 +409,26 @@ func GetDiskSerialNumberWithContext(ctx context.Context, name string) string {
return ""
}
// GetLabel returns label of given device or empty string on error.
// Name of device is expected, eg. /dev/sda
// Supports label based on devicemapper name
// See https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-block-dm
func GetLabel(name string) string {
// Try label based on devicemapper name
dmname_filename := common.HostSys(fmt.Sprintf("block/%s/dm/name", name))
if !common.PathExists(dmname_filename) {
return ""
}
dmname, err := ioutil.ReadFile(dmname_filename)
if err != nil {
return ""
} else {
return string(dmname)
}
}
func getFsType(stat unix.Statfs_t) string {
t := int64(stat.Type)
ret, ok := fsTypeMap[t]

View File

@ -217,3 +217,11 @@ func KernelVersionWithContext(ctx context.Context) (string, error) {
_, _, version, err := PlatformInformation()
return version, err
}
func SensorsTemperatures() ([]TemperatureStat, error) {
return SensorsTemperaturesWithContext(context.Background())
}
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
return []TemperatureStat{}, common.ErrNotImplementedError
}

View File

@ -1,18 +0,0 @@
// +build darwin
// +build !cgo
package host
import (
"context"
"github.com/shirou/gopsutil/internal/common"
)
func SensorsTemperatures() ([]TemperatureStat, error) {
return SensorsTemperaturesWithContext(context.Background())
}
func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
return []TemperatureStat{}, common.ErrNotImplementedError
}

View File

@ -0,0 +1,43 @@
// Created by cgo -godefs - DO NOT EDIT
// cgo -godefs types_linux.go
package host
const (
sizeofPtr = 0x4
sizeofShort = 0x2
sizeofInt = 0x4
sizeofLong = 0x4
sizeofLongLong = 0x8
sizeOfUtmp = 0x180
)
type (
_C_short int16
_C_int int32
_C_long int32
_C_long_long int64
)
type utmp struct {
Type int16
Pad_cgo_0 [2]byte
Pid int32
Line [32]int8
Id [4]int8
User [32]int8
Host [256]int8
Exit exit_status
Session int32
Tv timeval
Addr_v6 [4]int32
X__unused [20]int8
}
type exit_status struct {
Termination int16
Exit int16
}
type timeval struct {
Sec int32
Usec int32
}

View File

@ -39,7 +39,7 @@ func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
if err != nil {
return nil, err
}
buffers, err := unix.SysctlUint32("vfs.bufspace")
buffers, err := unix.SysctlUint64("vfs.bufspace")
if err != nil {
return nil, err
}