fix iis collector panic

If the `dst_cache` slice is empty, the program panics when trying to access `dst_cache[0]`

added a `if len(dst_cache) > 0 {}` block around the use of that variable to prevent that panic.
This commit is contained in:
Steve Zook
2018-07-19 13:29:33 -04:00
parent bb67658853
commit 8ea862a3da

View File

@@ -1738,214 +1738,216 @@ func (c *IISCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, e
return nil, err return nil, err
} }
ch <- prometheus.MustNewConstMetric( if len(dst_cache) > 0 {
c.ServiceCache_ActiveFlushedEntries, ch <- prometheus.MustNewConstMetric(
prometheus.GaugeValue, c.ServiceCache_ActiveFlushedEntries,
float64(dst_cache[0].ActiveFlushedEntries), prometheus.GaugeValue,
) float64(dst_cache[0].ActiveFlushedEntries),
)
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FileCacheMemoryUsage, c.ServiceCache_FileCacheMemoryUsage,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst_cache[0].CurrentFileCacheMemoryUsage), float64(dst_cache[0].CurrentFileCacheMemoryUsage),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MaximumFileCacheMemoryUsage, c.ServiceCache_MaximumFileCacheMemoryUsage,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].MaximumFileCacheMemoryUsage), float64(dst_cache[0].MaximumFileCacheMemoryUsage),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FileCacheFlushesTotal, c.ServiceCache_FileCacheFlushesTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].TotalFlushedFiles), float64(dst_cache[0].TotalFlushedFiles),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FileCacheQueriesTotal, c.ServiceCache_FileCacheQueriesTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].FileCacheHits+dst_cache[0].FileCacheMisses), float64(dst_cache[0].FileCacheHits+dst_cache[0].FileCacheMisses),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FileCacheHitsTotal, c.ServiceCache_FileCacheHitsTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].FileCacheHits), float64(dst_cache[0].FileCacheHits),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FilesCached, c.ServiceCache_FilesCached,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst_cache[0].CurrentFilesCached), float64(dst_cache[0].CurrentFilesCached),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FilesCachedTotal, c.ServiceCache_FilesCachedTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].TotalFilesCached), float64(dst_cache[0].TotalFilesCached),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FilesFlushedTotal, c.ServiceCache_FilesFlushedTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].TotalFlushedFiles), float64(dst_cache[0].TotalFlushedFiles),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URICacheFlushesTotal, c.ServiceCache_URICacheFlushesTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].TotalFlushedURIs), float64(dst_cache[0].TotalFlushedURIs),
"user", "user",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URICacheFlushesTotal, c.ServiceCache_URICacheFlushesTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].KernelTotalFlushedURIs), float64(dst_cache[0].KernelTotalFlushedURIs),
"kernel", "kernel",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URICacheQueriesTotal, c.ServiceCache_URICacheQueriesTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].URICacheHits+dst_cache[0].URICacheMisses), float64(dst_cache[0].URICacheHits+dst_cache[0].URICacheMisses),
"user", "user",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URICacheQueriesTotal, c.ServiceCache_URICacheQueriesTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].KernelURICacheHits+dst_cache[0].KernelURICacheMisses), float64(dst_cache[0].KernelURICacheHits+dst_cache[0].KernelURICacheMisses),
"kernel", "kernel",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URICacheHitsTotal, c.ServiceCache_URICacheHitsTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].URICacheHits), float64(dst_cache[0].URICacheHits),
"user", "user",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URICacheHitsTotal, c.ServiceCache_URICacheHitsTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].KernelURICacheHits), float64(dst_cache[0].KernelURICacheHits),
"kernel", "kernel",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URIsCached, c.ServiceCache_URIsCached,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst_cache[0].CurrentURIsCached), float64(dst_cache[0].CurrentURIsCached),
"user", "user",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URIsCached, c.ServiceCache_URIsCached,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst_cache[0].KernelCurrentURIsCached), float64(dst_cache[0].KernelCurrentURIsCached),
"kernel", "kernel",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URIsCachedTotal, c.ServiceCache_URIsCachedTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].TotalURIsCached), float64(dst_cache[0].TotalURIsCached),
"user", "user",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URIsCachedTotal, c.ServiceCache_URIsCachedTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].KernelTotalURIsCached), float64(dst_cache[0].KernelTotalURIsCached),
"kernel", "kernel",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URIsFlushedTotal, c.ServiceCache_URIsFlushedTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].TotalFlushedURIs), float64(dst_cache[0].TotalFlushedURIs),
"user", "user",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URIsFlushedTotal, c.ServiceCache_URIsFlushedTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].KernelTotalFlushedURIs), float64(dst_cache[0].KernelTotalFlushedURIs),
"kernel", "kernel",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MetadataCached, c.ServiceCache_MetadataCached,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst_cache[0].CurrentMetadataCached), float64(dst_cache[0].CurrentMetadataCached),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MetadataCacheFlushes, c.ServiceCache_MetadataCacheFlushes,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].TotalFlushedMetadata), float64(dst_cache[0].TotalFlushedMetadata),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MetadataCacheQueriesTotal, c.ServiceCache_MetadataCacheQueriesTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].MetadataCacheHits+dst_cache[0].MetadataCacheMisses), float64(dst_cache[0].MetadataCacheHits+dst_cache[0].MetadataCacheMisses),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MetadataCacheHitsTotal, c.ServiceCache_MetadataCacheHitsTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].MetadataCacheHits), float64(dst_cache[0].MetadataCacheHits),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MetadataCachedTotal, c.ServiceCache_MetadataCachedTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].TotalMetadataCached), float64(dst_cache[0].TotalMetadataCached),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MetadataFlushedTotal, c.ServiceCache_MetadataFlushedTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].TotalFlushedMetadata), float64(dst_cache[0].TotalFlushedMetadata),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheActiveFlushedItems, c.ServiceCache_OutputCacheActiveFlushedItems,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].OutputCacheCurrentFlushedItems), float64(dst_cache[0].OutputCacheCurrentFlushedItems),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheItems, c.ServiceCache_OutputCacheItems,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].OutputCacheCurrentItems), float64(dst_cache[0].OutputCacheCurrentItems),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheMemoryUsage, c.ServiceCache_OutputCacheMemoryUsage,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].OutputCacheCurrentMemoryUsage), float64(dst_cache[0].OutputCacheCurrentMemoryUsage),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheQueriesTotal, c.ServiceCache_OutputCacheQueriesTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].OutputCacheTotalHits+dst_cache[0].OutputCacheTotalMisses), float64(dst_cache[0].OutputCacheTotalHits+dst_cache[0].OutputCacheTotalMisses),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheHitsTotal, c.ServiceCache_OutputCacheHitsTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].OutputCacheTotalHits), float64(dst_cache[0].OutputCacheTotalHits),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheFlushedItemsTotal, c.ServiceCache_OutputCacheFlushedItemsTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].OutputCacheTotalFlushedItems), float64(dst_cache[0].OutputCacheTotalFlushedItems),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheFlushesTotal, c.ServiceCache_OutputCacheFlushesTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst_cache[0].OutputCacheTotalFlushes), float64(dst_cache[0].OutputCacheTotalFlushes),
) )
}
return nil, nil return nil, nil
} }