1
1
package collector
2
2
3
3
import (
4
+ "fmt"
4
5
"regexp"
5
6
"strconv"
6
7
"strings"
@@ -15,7 +16,7 @@ var uptimeRegex *regexp.Regexp
15
16
var uptimeParts [5 ]time.Duration
16
17
17
18
func init () {
18
- uptimeRegex = regexp .MustCompile (`(?:(\d*)w)?(?:(\d*)d)?(?:(\d*)h)?(?:(\d*)m)?(?:(\d*)s)` )
19
+ uptimeRegex = regexp .MustCompile (`(?:(\d*)w)?(?:(\d*)d)?(?:(\d*)h)?(?:(\d*)m)?(?:(\d*)s)? ` )
19
20
uptimeParts = [5 ]time.Duration {time .Hour * 168 , time .Hour * 24 , time .Hour , time .Minute , time .Second }
20
21
}
21
22
@@ -105,20 +106,26 @@ func (c *resourceCollector) collectMetricForProperty(property string, re *proto.
105
106
func parseUptime (uptime string ) (float64 , error ) {
106
107
var u time.Duration
107
108
108
- for i , match := range uptimeRegex .FindAllStringSubmatch (uptime , - 1 )[0 ] {
109
- if match != "" && i != 0 {
110
- v , err := strconv .Atoi (match )
111
- if err != nil {
112
- log .WithFields (log.Fields {
113
- "uptime" : uptime ,
114
- "value" : match ,
115
- "error" : err ,
116
- }).Error ("error parsing uptime field value" )
117
- return float64 (0 ), err
109
+ reMatch := uptimeRegex .FindAllStringSubmatch (uptime , - 1 )
110
+
111
+ // should get one and only one match back on the regex
112
+ if len (reMatch ) != 1 {
113
+ return 0 , fmt .Errorf ("invalid uptime value sent to regex" )
114
+ } else {
115
+ for i , match := range reMatch [0 ] {
116
+ if match != "" && i != 0 {
117
+ v , err := strconv .Atoi (match )
118
+ if err != nil {
119
+ log .WithFields (log.Fields {
120
+ "uptime" : uptime ,
121
+ "value" : match ,
122
+ "error" : err ,
123
+ }).Error ("error parsing uptime field value" )
124
+ return float64 (0 ), err
125
+ }
126
+ u += time .Duration (v ) * uptimeParts [i - 1 ]
118
127
}
119
- u += time .Duration (v ) * uptimeParts [i - 1 ]
120
128
}
121
129
}
122
-
123
130
return u .Seconds (), nil
124
131
}
0 commit comments