This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package hostcollect
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// maxUptimeSeconds is the bound internal/host enforces (100 years). Rejecting here
|
||||
// keeps an implausible reading from failing normalization later, where the cause is
|
||||
// harder to see.
|
||||
const maxUptimeSeconds = 100 * 365 * 24 * 60 * 60
|
||||
|
||||
// parseUptime reads the first field of /proc/uptime, the seconds since boot. The
|
||||
// second field (idle time summed over cores) is intentionally unused.
|
||||
func parseUptime(data []byte) (float64, error) {
|
||||
fields := strings.Fields(string(data))
|
||||
if len(fields) == 0 {
|
||||
return 0, errBadUptime
|
||||
}
|
||||
seconds, ok := parseFloat(fields[0])
|
||||
if !ok || seconds < 0 || seconds > maxUptimeSeconds || math.IsNaN(seconds) || math.IsInf(seconds, 0) {
|
||||
return 0, errBadUptime
|
||||
}
|
||||
return seconds, nil
|
||||
}
|
||||
|
||||
func (c *Collector) uptimeSeconds() (float64, error) {
|
||||
data, err := readLimited(c.procPath("uptime"), maxProcessFileBytes)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("hostcollect: read uptime: %w", err)
|
||||
}
|
||||
return parseUptime(data)
|
||||
}
|
||||
Reference in New Issue
Block a user