diff --git a/zfs_replication_exporter.go b/zfs_replication_exporter.go index 05fed21..bdd4ecf 100644 --- a/zfs_replication_exporter.go +++ b/zfs_replication_exporter.go @@ -5,6 +5,7 @@ import ( "bytes" "flag" "fmt" + "html/template" "io/ioutil" "net/http" _ "net/http/pprof" @@ -12,6 +13,7 @@ import ( "path/filepath" "regexp" "strings" + "sync" "time" "github.com/golang/glog" @@ -38,12 +40,6 @@ var ( Name: "ssh_fetch_duration_seconds", Help: "Time to fetch and parse snapshot age over SSH", }) - snapshotAgesMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{ - Name: "zfs_snapshot_age_seconds", - Help: "Duration in seconds for most recent snapshot for `filesystem`", - }, - []string{"host", "filesystem"}, - ) snapshotTimestampMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{ Name: "zfs_snapshot_timestamp_seconds", Help: "Most recent snapshot timestamp for `filesystem` UNIX epoch seconds", @@ -60,7 +56,6 @@ var ( func init() { prometheus.MustRegister(fetchRequestDurationMetric) - prometheus.MustRegister(snapshotAgesMetric) prometheus.MustRegister(snapshotTimestampMetric) prometheus.MustRegister(snapshotCountsMetrics) } @@ -99,7 +94,19 @@ func newPublicKey() ([]ssh.AuthMethod, error) { return signers, nil } -func updateMetrics(host string, c *ssh.Client) error { +type filesystemName string +type filesystemStat struct { + // Timestamp of most recent snapshot + Timestamp time.Time + // Counts is the number of snapshots + Counts int + // FreenasCounts is the number of snapshots matching freenas' auto-snapshot + // names. + FreenasCounts int +} +type snapshotStats map[filesystemName]*filesystemStat + +func fetchSnapshotStats(host string, c *ssh.Client) (snapshotStats, error) { now := time.Now() defer func() { delta := time.Since(now) @@ -110,19 +117,17 @@ func updateMetrics(host string, c *ssh.Client) error { // represented by a Session. s, err := c.NewSession() if err != nil { - return fmt.Errorf("[%s] error creating new session: %v", host, err) + return nil, fmt.Errorf("[%s] error creating new session: %v", host, err) } defer s.Close() glog.V(2).Infof("[%s] Running %q", host, snapshotListCmd) b, err := s.CombinedOutput(snapshotListCmd) if err != nil { - return fmt.Errorf("[%s] error running %q: %v", host, snapshotListCmd, err) + return nil, fmt.Errorf("[%s] error running %q: %v", host, snapshotListCmd, err) } scanner := bufio.NewScanner(bytes.NewReader(b)) - snapshotAges := make(map[string]time.Time) - snapshotCountsByFilesystem := make(map[string]int) - freenasSnapshotCountsByFilesystem := make(map[string]int) + stats := snapshotStats(make(map[filesystemName]*filesystemStat)) for scanner.Scan() { l := scanner.Text() m := snapshotPattern.FindStringSubmatch(l) @@ -131,35 +136,89 @@ func updateMetrics(host string, c *ssh.Client) error { if idx := strings.Index(l, "@"); idx != -1 { filesystem = l[:idx] } - snapshotCountsByFilesystem[filesystem]++ + name := filesystemName(filesystem) + if _, ok := stats[name]; !ok { + stats[name] = &filesystemStat{} + } + stats[name].Counts++ if len(m) == 2 { t, err := time.Parse(snapshotFormat, m[1]) if err != nil { - glog.Errorf("[%s] Malformed time in snapshot %q: %v", host, m[2], err) + glog.Errorf("[%s] Malformed time in snapshot %q: %v", host, m[1], err) continue } - freenasSnapshotCountsByFilesystem[filesystem]++ - snapshotTime := snapshotAges[filesystem] + glog.V(3).Infof("filesystem: %s timestamp %v", l, t) + stats[name].FreenasCounts++ + snapshotTime := stats[name].Timestamp + glog.V(3).Infof("snapshotTime.Before(t) = %v snapshotTime: %v t: %v", snapshotTime.Before(t), snapshotTime, t) if snapshotTime.Before(t) { - snapshotAges[filesystem] = t + stats[name].Timestamp = t } + } else { + glog.V(3).Infof("[%s] Skipping snapshot with non-conforming timestamp %q", host, l) } } if err := scanner.Err(); err != nil { - return fmt.Errorf("[%s] failed to scan response: %v", host, err) + return nil, fmt.Errorf("[%s] failed to scan response: %v", host, err) } + return stats, nil +} - for filesystem, c := range snapshotCountsByFilesystem { - snapshotCountsMetrics.WithLabelValues(host, filesystem, "all").Set(float64(c)) +func updateMetrics(host string, stats snapshotStats) { + for filesystem, stat := range stats { + snapshotCountsMetrics.WithLabelValues(host, string(filesystem), "all").Set(float64(stat.Counts)) + snapshotCountsMetrics.WithLabelValues(host, string(filesystem), "freenas").Set(float64(stat.FreenasCounts)) + snapshotTimestampMetric.WithLabelValues(host, string(filesystem)).Set(float64(stat.Timestamp.Unix())) } - for filesystem, c := range freenasSnapshotCountsByFilesystem { - snapshotCountsMetrics.WithLabelValues(host, filesystem, "freenas").Set(float64(c)) +} + +type hostsSnapshotStats struct { + sync.Mutex + host2Stats map[string]snapshotStats +} + +var indexTmpl = template.Must(template.New("index").Parse(` + +
+ + + + +| Filesystem | +Counts | +FreeNAS Snapshots | +Most Recent | +
|---|---|---|---|
| {{$name}} | +{{$fsStat.Counts}} | +{{$fsStat.FreenasCounts}} | +{{if $fsStat.FreenasCounts }}{{$fsStat.Timestamp}}{{end}} | +