cmd/stdiscosrv: Account IPv4 & IPv6

This commit is contained in:
Jakob Borg 2023-10-26 15:07:52 +02:00
parent 854499382e
commit 58bd931d90

View File

@ -12,6 +12,8 @@ package main
import ( import (
"context" "context"
"log" "log"
"net"
"net/url"
"sort" "sort"
"time" "time"
@ -218,7 +220,7 @@ func (s *levelDBStore) statisticsServe(trigger <-chan struct{}, done chan<- stru
cutoff24h := t0.Add(-24 * time.Hour).UnixNano() cutoff24h := t0.Add(-24 * time.Hour).UnixNano()
cutoff1w := t0.Add(-7 * 24 * time.Hour).UnixNano() cutoff1w := t0.Add(-7 * 24 * time.Hour).UnixNano()
cutoff2Mon := t0.Add(-60 * 24 * time.Hour).UnixNano() cutoff2Mon := t0.Add(-60 * 24 * time.Hour).UnixNano()
current, last24h, last1w, inactive, errors := 0, 0, 0, 0, 0 current, currentIPv4, currentIPv6, last24h, last1w, inactive, errors := 0, 0, 0, 0, 0, 0, 0
iter := s.db.NewIterator(&util.Range{}, nil) iter := s.db.NewIterator(&util.Range{}, nil)
for iter.Next() { for iter.Next() {
@ -233,9 +235,35 @@ func (s *levelDBStore) statisticsServe(trigger <-chan struct{}, done chan<- stru
// If there are addresses that have not expired it's a current // If there are addresses that have not expired it's a current
// record, otherwise account it based on when it was last seen // record, otherwise account it based on when it was last seen
// (last 24 hours or last week) or finally as inactice. // (last 24 hours or last week) or finally as inactice.
addrs := expire(rec.Addresses, nowNanos)
switch { switch {
case len(expire(rec.Addresses, nowNanos)) > 0: case len(addrs) > 0:
current++ current++
seenIPv4, seenIPv6 := false, false
for _, addr := range addrs {
uri, err := url.Parse(addr.Address)
if err != nil {
continue
}
host, _, err := net.SplitHostPort(uri.Host)
if err != nil {
continue
}
if ip := net.ParseIP(host); ip != nil && ip.To4() != nil {
seenIPv4 = true
} else if ip != nil {
seenIPv6 = true
}
if seenIPv4 && seenIPv6 {
break
}
}
if seenIPv4 {
currentIPv4++
}
if seenIPv6 {
currentIPv6++
}
case rec.Seen > cutoff24h: case rec.Seen > cutoff24h:
last24h++ last24h++
case rec.Seen > cutoff1w: case rec.Seen > cutoff1w:
@ -259,6 +287,8 @@ func (s *levelDBStore) statisticsServe(trigger <-chan struct{}, done chan<- stru
iter.Release() iter.Release()
databaseKeys.WithLabelValues("current").Set(float64(current)) databaseKeys.WithLabelValues("current").Set(float64(current))
databaseKeys.WithLabelValues("currentIPv4").Set(float64(currentIPv4))
databaseKeys.WithLabelValues("currentIPv6").Set(float64(currentIPv6))
databaseKeys.WithLabelValues("last24h").Set(float64(last24h)) databaseKeys.WithLabelValues("last24h").Set(float64(last24h))
databaseKeys.WithLabelValues("last1w").Set(float64(last1w)) databaseKeys.WithLabelValues("last1w").Set(float64(last1w))
databaseKeys.WithLabelValues("inactive").Set(float64(inactive)) databaseKeys.WithLabelValues("inactive").Set(float64(inactive))