diff --git a/inspector/inspector.go b/inspector/inspector.go index 5f7ebde..aae5bf3 100644 --- a/inspector/inspector.go +++ b/inspector/inspector.go @@ -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, diff --git a/inspector/temp.go b/inspector/temp.go index 762e170..32b452b 100644 --- a/inspector/temp.go +++ b/inspector/temp.go @@ -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{ @@ -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, @@ -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{ @@ -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) diff --git a/inspector/temp_test.go b/inspector/temp_test.go new file mode 100644 index 0000000..be8fa27 --- /dev/null +++ b/inspector/temp_test.go @@ -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") + } + } +} diff --git a/integration/integration_test.go b/integration/integration_test.go index 1f4c25f..c7f411a 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -88,6 +88,29 @@ func TestMemInfoonSSH(t *testing.T) { } } +func TestTemponSSH(t *testing.T) { + if SkipNonLinuxOnCI() { + return + } + d := NewSSHForTest() + i, _ := inspector.NewTemp(&d) + i.Execute() + iConcreteLinux, ok := i.(*inspector.TempLinux) + if ok { + if iConcreteLinux.Values == nil { + t.Error("Values did not get set for TempLinux") + } + fmt.Printf(`%#v`, iConcreteLinux.Values) + } + iConcreteDarwin, ok := i.(*inspector.TempDarwin) + if ok { + if iConcreteDarwin.Values == nil { + t.Error("Values did not get set for TempDarwin") + } + fmt.Printf(`%#v`, iConcreteDarwin.Values) + } +} + func TestResponseTimeonWeb(t *testing.T) { d := NewWebForTest() i, _ := inspector.NewResponseTime(&d) diff --git a/integration/integration_unix_test.go b/integration/integration_unix_test.go index c30bb09..e8bfb93 100644 --- a/integration/integration_unix_test.go +++ b/integration/integration_unix_test.go @@ -28,6 +28,27 @@ func TestDFonLocal(t *testing.T) { fmt.Printf(`%#v`, iConcrete.Values) } +func TestTemponLocal(t *testing.T) { + d := NewLocalForTest() + // can either use NewDF() or get the interface and perform type assertion + i, _ := inspector.NewTemp(&d) + i.Execute() + iConcreteLinux, ok := i.(*inspector.TempLinux) + if ok { + if iConcreteLinux.Values == nil { + t.Error("Values did not get set for TempLinux") + } + fmt.Printf(`%#v`, iConcreteLinux.Values) + } + iConcreteDarwin, ok := i.(*inspector.TempDarwin) + if ok { + if iConcreteDarwin.Values == nil { + t.Error("Values did not get set for TempDarwin") + } + fmt.Printf(`%#v`, iConcreteDarwin.Values) + } +} + func TestMemInfoonLocal(t *testing.T) { d := NewLocalForTest() i, _ := inspector.NewMemInfo(&d) diff --git a/integration/integration_windows_test.go b/integration/integration_windows_test.go index 845f295..b257e07 100644 --- a/integration/integration_windows_test.go +++ b/integration/integration_windows_test.go @@ -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)