Skip to content

Commit

Permalink
Remove unecessary panics and fix reconnection errors
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Jan 5, 2023
1 parent c3f6aae commit e584caa
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ dev-backend:
air --build.cmd "go build -o ./tmp/$(bin) ." --build.bin "$(build_bin)" --build.exclude_dir "assets,docs,tmp,web,scripts,ssh-key,.github,.git" --build.include_ext "go,yaml,html,js" --build.exclude_file "config.example.yaml" --build.args_bin "--config,config-test.yaml,--verbose" --build.stop_on_error true --misc.clean_on_exit true --log.time true

prod-monolith:
go build -tags prod -o ./tmp/$(bin) . && $(build_bin) --config config-test.yaml -b
go build -tags prod -o ./tmp/$(bin) . && $(build_bin) --config config-test.yaml -b --verbose

app: build-frontend prod-monolith
4 changes: 2 additions & 2 deletions client/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ func (hosts *HostsController) sendMetric(host config.Host, metrics map[string]st
}
for metric, custom := range metrics {
inspectorDriver := hosts.getDriver(host.Address)
initializedMetric, err = inspector.Init(metric, inspectorDriver, custom)
platformDetails, err = (*inspectorDriver).GetDetails()
if err != nil {
log.Error(err)
hosts.handleError(err, metric, host, client)
continue
}
platformDetails, err = (*inspectorDriver).GetDetails()
initializedMetric, err = inspector.Init(metric, inspectorDriver, custom)
if err != nil {
log.Error(err)
hosts.handleError(err, metric, host, client)
Expand Down
7 changes: 7 additions & 0 deletions driver/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ func (d *SSH) RunCommand(command string) (string, error) {
}
out, err := client.Run(command)
if err != nil {
// Connection has to be rebooted cuz EOF
if strings.Contains(fmt.Sprintf("%s", err), "EOF") {
err = &SSHConnectError{
content: err.Error(),
client: d.Host,
}
}
return ``, err
}
return string(out), nil
Expand Down
29 changes: 15 additions & 14 deletions inspector/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,22 @@ func (i *DFWin) Parse(output string) {
available, err := strconv.Atoi(columns[3])
size, err := strconv.Atoi(columns[5])
if err != nil {
panic("Could not parse sizes for DFWin")
}
used := size - available
percentInt := int((float64(used) / float64(size)) * 100)
cols := []string{
columns[1],
fmt.Sprintf("%d", size),
fmt.Sprintf("%d", used),
fmt.Sprintf("%d", available),
columns[6],
}
if strings.HasPrefix(columns[1], i.DeviceStartsWith) {
values = append(values, i.createMetric(cols, percentInt))
log.Errorf("Could not parse sizes for DFWin %e", err)
} else {
values = append(values, i.createMetric(cols, percentInt))
used := size - available
percentInt := int((float64(used) / float64(size)) * 100)
cols := []string{
columns[1],
fmt.Sprintf("%d", size),
fmt.Sprintf("%d", used),
fmt.Sprintf("%d", available),
columns[6],
}
if strings.HasPrefix(columns[1], i.DeviceStartsWith) {
values = append(values, i.createMetric(cols, percentInt))
} else {
values = append(values, i.createMetric(cols, percentInt))
}
}
}
}
Expand Down
47 changes: 25 additions & 22 deletions inspector/meminfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (i *MemInfoLinux) Execute() ([]byte, error) {

func parseIntoNewByteSize(input string, displayBytes string) (int, error) {
if len(input) < 1 {
return 0, nil
return 0, errors.New(fmt.Sprintf("could not parse %s into new byte size", input))
}
unit := string(input[len(input)-1])
modified := strings.TrimSuffix(input, unit)
Expand All @@ -155,34 +155,37 @@ func parseIntoNewByteSize(input string, displayBytes string) (int, error) {
5120.00M 1194.00M
*/
func (i *MemInfoDarwin) Parse(output string) {
var (
err error
memUnusedInt int
memUsedInt int
)
rows := strings.Split(output, "\n")
physMemRaw := rows[0]
swapRaw := rows[1]
physMemCols := strings.Fields(physMemRaw)
swapCols := strings.Fields(swapRaw)
memUsedInt, err := parseIntoNewByteSize(physMemCols[0], i.DisplayByteSize)
if err != nil {
panic("Error parsing memory on MemInfoDarwin")
}
memUnusedInt, err := parseIntoNewByteSize(physMemCols[1], i.DisplayByteSize)
memUsedInt, err = parseIntoNewByteSize(physMemCols[0], i.DisplayByteSize)
memUnusedInt, err = parseIntoNewByteSize(physMemCols[1], i.DisplayByteSize)
if err != nil {
panic("Error parsing memory on MemInfoDarwin")
log.Errorf("Error parsing memory on MemInfoDarwin %e", err)
} else {
memTotal := fmt.Sprintf("%d", memUsedInt+memUnusedInt)
swapTotal := strings.TrimSuffix(swapCols[0], "M")
swapFree := strings.TrimSuffix(swapCols[1], "M")
//TODO: Figure out where to get cached size
i.Values = createMetric(
[]string{
memTotal,
fmt.Sprintf("%d", memUnusedInt),
`0`,
swapTotal,
swapFree,
},
i.RawByteSize,
i.DisplayByteSize,
)
}
memTotal := fmt.Sprintf("%d", memUsedInt+memUnusedInt)
swapTotal := strings.TrimSuffix(swapCols[0], "M")
swapFree := strings.TrimSuffix(swapCols[1], "M")
//TODO: Figure out where to get cached size
i.Values = createMetric(
[]string{
memTotal,
fmt.Sprintf("%d", memUnusedInt),
`0`,
swapTotal,
swapFree,
},
i.RawByteSize,
i.DisplayByteSize,
)
}

func (i *MemInfoDarwin) SetDriver(driver *driver.Driver) {
Expand Down
19 changes: 10 additions & 9 deletions inspector/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,16 @@ func (i *ProcessWin) Parse(output string) {
pidRaw := columns[colLength-5]
pid, err := strconv.Atoi(pidRaw)
if err != nil {
panic("Could not parse pid for row")
}
if i.TrackPID != 0 && i.TrackPID == pid {
value := i.createMetric(columns, pid)
values = append(values, value)
break
} else if i.TrackPID == 0 {
value := i.createMetric(columns, pid)
values = append(values, value)
log.Errorf("Could not parse pid for row: %e", err)
} else {
if i.TrackPID != 0 && i.TrackPID == pid {
value := i.createMetric(columns, pid)
values = append(values, value)
break
} else if i.TrackPID == 0 {
value := i.createMetric(columns, pid)
values = append(values, value)
}
}
}
}
Expand Down
21 changes: 11 additions & 10 deletions inspector/uptime.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ func (i *UptimeDarwin) Parse(output string) {
switchedOn, err := strconv.Atoi(lines[1])
idleTime, err := strconv.ParseFloat(lines[2], 64)
if err != nil {
panic("Could not parse times in UptimeDarwin")
}

i.Values = &UptimeMetrics{
Up: float64(unixTime - switchedOn),
IdlePercent: idleTime,
log.Errorf("Could not parse times in UptimeDarwin: %e", err)
} else {
i.Values = &UptimeMetrics{
Up: float64(unixTime - switchedOn),
IdlePercent: idleTime,
}
}
}

Expand Down Expand Up @@ -150,10 +150,11 @@ func (i *UptimeWindows) Parse(output string) {
upUnformatted := strings.Split(output, "\n")[1]
up, err := strconv.ParseFloat(upUnformatted, 64)
if err != nil {
panic(err)
}
i.Values = &UptimeMetrics{
Up: up,
log.Error("Error parsing windows uptime", err)
} else {
i.Values = &UptimeMetrics{
Up: up,
}
}
}

Expand Down

0 comments on commit e584caa

Please sign in to comment.