Fix validate that string is not empty before strconv.ParseFloat (#59)

Thanks. I could have sworn I went through already and done this at one point in time, but I guess not.
This commit is contained in:
Jesus Rafael Carrillo 2019-12-02 19:01:36 -08:00 committed by Steve Brunton
parent 5cccba9e17
commit 9ceb56fdbf
8 changed files with 30 additions and 10 deletions

View File

@ -73,7 +73,9 @@ func (c *dhcpCollector) colllectForDHCPServer(ctx *collectorContext, dhcpServer
}).Error("error fetching DHCP lease counts") }).Error("error fetching DHCP lease counts")
return err return err
} }
if reply.Done.Map["ret"] == "" {
return nil
}
v, err := strconv.ParseFloat(reply.Done.Map["ret"], 32) v, err := strconv.ParseFloat(reply.Done.Map["ret"], 32)
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{

View File

@ -32,7 +32,9 @@ func description(prefix, name, helpText string, labelNames []string) *prometheus
func splitStringToFloats(metric string) (float64, float64, error) { func splitStringToFloats(metric string) (float64, float64, error) {
strs := strings.Split(metric, ",") strs := strings.Split(metric, ",")
if len(strs) == 0 {
return 0, 0, nil
}
m1, err := strconv.ParseFloat(strs[0], 64) m1, err := strconv.ParseFloat(strs[0], 64)
if err != nil { if err != nil {
return math.NaN(), math.NaN(), err return math.NaN(), math.NaN(), err

View File

@ -88,7 +88,9 @@ func (c *poeCollector) collectMetricsForInterface(name string, se *proto.Sentenc
if !ok { if !ok {
continue continue
} }
if v == "" {
continue
}
value, err := strconv.ParseFloat(v, 64) value, err := strconv.ParseFloat(v, 64)
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{

View File

@ -88,7 +88,9 @@ func (c *poolCollector) collectForPool(ipVersion, topic, pool string, ctx *colle
}).Error("error fetching pool counts") }).Error("error fetching pool counts")
return err return err
} }
if reply.Done.Map["ret"] == "" {
return nil
}
v, err := strconv.ParseFloat(reply.Done.Map["ret"], 32) v, err := strconv.ParseFloat(reply.Done.Map["ret"], 32)
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{

View File

@ -54,7 +54,7 @@ func (c *resourceCollector) collect(ctx *collectorContext) error {
} }
for _, re := range stats { for _, re := range stats {
c.collectForStat(re, ctx) c.collectForStat(re, ctx)
} }
return nil return nil
@ -82,8 +82,8 @@ func (c *resourceCollector) collectForStat(re *proto.Sentence, ctx *collectorCon
func (c *resourceCollector) collectMetricForProperty(property string, re *proto.Sentence, ctx *collectorContext) { func (c *resourceCollector) collectMetricForProperty(property string, re *proto.Sentence, ctx *collectorContext) {
var v float64 var v float64
var err error var err error
// const boardname = "BOARD" // const boardname = "BOARD"
// const version = "3.33.3" // const version = "3.33.3"
boardname := re.Map["board-name"] boardname := re.Map["board-name"]
version := re.Map["version"] version := re.Map["version"]
@ -91,6 +91,9 @@ func (c *resourceCollector) collectMetricForProperty(property string, re *proto.
if property == "uptime" { if property == "uptime" {
v, err = parseUptime(re.Map[property]) v, err = parseUptime(re.Map[property])
} else { } else {
if re.Map[property] == "" {
return
}
v, err = strconv.ParseFloat(re.Map[property], 64) v, err = strconv.ParseFloat(re.Map[property], 64)
} }

View File

@ -74,7 +74,9 @@ func (c *routesCollector) colllectCount(ipVersion, topic string, ctx *collectorC
}).Error("error fetching routes metrics") }).Error("error fetching routes metrics")
return err return err
} }
if reply.Done.Map["ret"] == "" {
return nil
}
v, err := strconv.ParseFloat(reply.Done.Map["ret"], 32) v, err := strconv.ParseFloat(reply.Done.Map["ret"], 32)
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
@ -100,7 +102,9 @@ func (c *routesCollector) colllectCountProtcol(ipVersion, topic, protocol string
}).Error("error fetching routes metrics") }).Error("error fetching routes metrics")
return err return err
} }
if reply.Done.Map["ret"] == "" {
return nil
}
v, err := strconv.ParseFloat(reply.Done.Map["ret"], 32) v, err := strconv.ParseFloat(reply.Done.Map["ret"], 32)
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{

View File

@ -93,7 +93,9 @@ func (c *wlanIFCollector) collectForInterface(iface string, ctx *collectorContex
func (c *wlanIFCollector) collectMetricForProperty(property, iface string, re *proto.Sentence, ctx *collectorContext) { func (c *wlanIFCollector) collectMetricForProperty(property, iface string, re *proto.Sentence, ctx *collectorContext) {
desc := c.descriptions[property] desc := c.descriptions[property]
channel := re.Map["channel"] channel := re.Map["channel"]
if re.Map[property] == "" {
return
}
v, err := strconv.ParseFloat(re.Map[property], 64) v, err := strconv.ParseFloat(re.Map[property], 64)
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{

View File

@ -78,6 +78,9 @@ func (c *wlanSTACollector) collectForStat(re *proto.Sentence, ctx *collectorCont
} }
func (c *wlanSTACollector) collectMetricForProperty(property, iface, mac string, re *proto.Sentence, ctx *collectorContext) { func (c *wlanSTACollector) collectMetricForProperty(property, iface, mac string, re *proto.Sentence, ctx *collectorContext) {
if re.Map[property] == "" {
return
}
v, err := strconv.ParseFloat(re.Map[property], 64) v, err := strconv.ParseFloat(re.Map[property], 64)
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{