Skip to content

Commit

Permalink
create temperature 🌡️ test 🧪
Browse files Browse the repository at this point in the history
  • Loading branch information
derhnyel committed Jan 4, 2023
1 parent cb51313 commit fd70e50
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 33 deletions.
2 changes: 1 addition & 1 deletion inspector/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var inspectorMap = map[string]NewInspector{
`process`: NewProcess,
`loadavg`: NewLoadAvg,
`tcp`: NewTcp,
`temperature`: NewTemp,
`temp`: NewTemp,
CustomCommand: NewCustom,
// NOTE: Inactive for now
`responsetime`: NewResponseTime,
Expand Down
69 changes: 37 additions & 32 deletions inspector/temp.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,16 @@ func (i TempWin) driverExec() driver.Command {
}

func (i *TempWin) Parse(output string) {
// var values []TempMetrics
log.Debug("Parsing output string in TempWin inspector")
lineChar := "\r"
output = strings.TrimPrefix(output, lineChar)
output = strings.TrimSuffix(output, lineChar)
log.Debug(output)
output = strings.ReplaceAll(output, "\r", "")
lines := strings.Split(output, "\n")
if len(lines) < 1 {
log.Fatalf(`Error Parsing Temperature `)
log.Errorf(`Error Parsing Temperature `)
}
farenValue, err := strconv.ParseFloat(lines[1], 64)
if err != nil {
log.Fatalf(`Error Parsing Temperature: %s `, err)
log.Errorf(`Error Parsing Temperature: %s `, err)
}
celsValue := (farenValue * 10.0) - 27315.0
i.Values = &TempMetrics{
Expand Down Expand Up @@ -96,10 +94,34 @@ func (i TempLinux) driverExec() driver.Command {

func (i *TempLinux) Parse(output string) {
log.Debug("Parsing output string in Temp inspector")
output = strings.ReplaceAll(output, " ", "")
value, err := strconv.ParseFloat(output, 64)
log.Debug(output)
// $ cat /sys/class/thermal/thermal_zone*/temp
// 20000
// 53000
// 50000
// 53000
// 56000
// 68000
// 49000
// 50000
// To see what zones the temperatures are referring to use:
// $ paste <(cat /sys/class/thermal/thermal_zone*/type) <(cat /sys/class/thermal/thermal_zone*/temp) | column -s $'\t' -t | sed 's/\(.\)..$/.\1°C/'
// INT3400 Thermal 20.0°C
// SEN1 45.0°C
// SEN2 51.0°C
// SEN3 57.0°C
// SEN4 59.0°C
// pch_skylake 77.5°C
// B0D4 50.0°C
// x86_pkg_temp 51.0°C
output = strings.ReplaceAll(output, "\r", "")
lines := strings.Split(output, "\n")
if len(lines) < 1 {
log.Errorf(`Error Parsing Temperature `)
}
value, err := strconv.ParseFloat(lines[0], 64)
if err != nil {
log.Fatalf(`Error Parsing Temperature: %s `, err)
log.Errorf(`Error Parsing Temperature: %s `, err)
}
i.Values = &TempMetrics{
CPUTemp: value / 1000.0,
Expand Down Expand Up @@ -130,11 +152,12 @@ func (i TempDarwin) driverExec() driver.Command {

func (i *TempDarwin) Parse(output string) {
log.Debug("Parsing output string in Temp inspector")
tempUnit := "°C"
log.Debug(output)
tempUnit := "°C\n"
output = strings.TrimSuffix(output, tempUnit)
value, err := strconv.ParseFloat(output, 64)
if err != nil {
log.Fatalf(`Error Parsing Temperature: %s `, err)
log.Errorf(`Error Parsing Temperature: %s %s`, err)

}
i.Values = &TempMetrics{
Expand All @@ -158,44 +181,26 @@ func NewTemp(driver *driver.Driver, _ ...string) (Inspector, error) {
if !(details.IsLinux || details.IsDarwin || details.IsWindows) {
return nil, errors.New("Cannot use 'temp' command on drivers outside (linux, darwin, windows)")
}

// $ cat /sys/class/thermal/thermal_zone*/temp
// 20000
// 53000
// 50000
// 53000
// 56000
// 68000
// 49000
// 50000
// To see what zones the temperatures are referring to use:
// $ paste <(cat /sys/class/thermal/thermal_zone*/type) <(cat /sys/class/thermal/thermal_zone*/temp) | column -s $'\t' -t | sed 's/\(.\)..$/.\1°C/'
// INT3400 Thermal 20.0°C
// SEN1 45.0°C
// SEN2 51.0°C
// SEN3 57.0°C
// SEN4 59.0°C
// pch_skylake 77.5°C
// B0D4 50.0°C
// x86_pkg_temp 51.0°C

if details.IsLinux {
temp = &TempLinux{
CPUTempFilePath: `/sys/class/thermal/thermal_zone0/temp`,
RawTempDegree: `°C`,
DisplayTempDegree: `°C`,
Note: `This shows the temperature of thermal zone 0. To see what all the thermal zones are referring to, use 'cat /sys/class/thermal_zone*'`,
}
} else if details.IsWindows {
temp = &TempWin{
CPUTempCommand: `wmic /namespace:\\root\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature`,
RawTempDegree: `°F`,
DisplayTempDegree: `°C`,
Note: `These are current temperatures of all the child objects contained inside the logical thermal zone as implemented by the manufacturer. Query the property InstanceName along with CurrentTemperature to see which temp is for which component using 'wmic /namespace:\\root\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature,InstanceName'`,
}
} else if details.IsDarwin {
temp = &TempDarwin{
CPUTempCommand: `osx-cpu-temp`,
RawTempDegree: `°C`,
DisplayTempDegree: `°C`,
Note: `This works for Intel Chip Darwins and osx-cpu-temp package has to be installed on the darwin machine. To install use 'brew install osx-cpu-temp'`,
}
}
temp.SetDriver(driver)
Expand Down
47 changes: 47 additions & 0 deletions inspector/temp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:build !windows
// +build !windows

package inspector

import (
"testing"
)

func TestTempOnLocal(t *testing.T) {
driver := NewLocalForTest()
d, _ := NewTemp(&driver)
d.Execute()
TempConcreteLinux, ok := d.(*TempLinux)
if ok {
if TempConcreteLinux.Values == nil {
t.Error("Values did not get set for TempLinux")
}
}
TempConcreteDarwin, ok := d.(*TempDarwin)
if ok {
if TempConcreteDarwin.Values == nil {
t.Error("Values did not get set for TempDarwin")
}
}
}

func TestTempOnSSH(t *testing.T) {
if SkipNonLinuxOnCI() {
return
}
driver := NewSSHForTest()
d, _ := NewTemp(&driver)
d.Execute()
TempConcreteLinux, ok := d.(*TempLinux)
if ok {
if TempConcreteLinux.Values == nil {
t.Error("Values did not get set for TempLinux")
}
}
TempConcreteDarwin, ok := d.(*TempDarwin)
if ok {
if TempConcreteDarwin.Values == nil {
t.Error("Values did not get set for TempDarwin")
}
}
}
13 changes: 13 additions & 0 deletions integration/integration_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ func TestDFonLocal(t *testing.T) {
}
}

func TestTemponLocal(t *testing.T) {
d := NewLocalForTest()
i, _ := inspector.Init(`temp`, &d)
i.Execute()
iConcrete, ok := i.(*inspector.TempWin)
if ok {
fmt.Printf("%#v", iConcrete.Values)
if len(iConcrete.Values) < 1 {
t.Error("TempWin not set on Windows")
}
}
}

func TestTcponLocal(t *testing.T) {
d := NewLocalForTest()
i, _ := inspector.Init(`tcp`, &d)
Expand Down

0 comments on commit fd70e50

Please sign in to comment.