48 lines
1.7 KiB
Go
48 lines
1.7 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"io"
|
||
|
|
"log/slog"
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/prometheus/client_golang/prometheus"
|
||
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||
|
|
"github.com/tidwall/gjson"
|
||
|
|
)
|
||
|
|
|
||
|
|
type scsiCollector struct{}
|
||
|
|
|
||
|
|
func (scsiCollector) Describe(descriptors chan<- *prometheus.Desc) {
|
||
|
|
new(SMARTctlManagerCollector).Describe(descriptors)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (scsiCollector) Collect(metrics chan<- prometheus.Metric) {
|
||
|
|
data := gjson.Parse(`{"scsi_error_counter_log":{"read":{},"write":{},"verify":{"errors_corrected_by_rereads_rewrites":1,"errors_corrected_by_eccfast":2,"errors_corrected_by_eccdelayed":3,"total_uncorrected_errors":4}}}`)
|
||
|
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||
|
|
smart := NewSMARTctl(logger, data, metrics, Device{Label: "synthetic"})
|
||
|
|
smart.mineSCSIErrorCounterLog()
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestSCSIVerifyScrape(t *testing.T) {
|
||
|
|
registry := prometheus.NewPedanticRegistry()
|
||
|
|
registry.MustRegister(scsiCollector{})
|
||
|
|
response := httptest.NewRecorder()
|
||
|
|
request := httptest.NewRequest(http.MethodGet, "/metrics", nil)
|
||
|
|
promhttp.HandlerFor(registry, promhttp.HandlerOpts{}).ServeHTTP(response, request)
|
||
|
|
if response.Code != http.StatusOK {
|
||
|
|
t.Fatalf("metrics returned HTTP %d: %s", response.Code, response.Body.String())
|
||
|
|
}
|
||
|
|
for _, expected := range []string{
|
||
|
|
`smartctl_verify_errors_corrected_by_rereads_rewrites{device="synthetic"} 1`,
|
||
|
|
`smartctl_verify_errors_corrected_by_eccfast{device="synthetic"} 2`,
|
||
|
|
`smartctl_verify_errors_corrected_by_eccdelayed{device="synthetic"} 3`,
|
||
|
|
`smartctl_verify_total_uncorrected_errors{device="synthetic"} 4`,
|
||
|
|
} {
|
||
|
|
if !strings.Contains(response.Body.String(), expected+"\n") {
|
||
|
|
t.Errorf("missing metric: %s", expected)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|