go.d whoisquery: try requesting extended data if no expiration date (#17913)
Ilya Mashchenko committed
Jun 16, 2024 at 21:58 UTC
e2be6a875ab52093d3d517e318631807e9dd0949
2 files changed
+42
-14
src/go/collectors/go.d.plugin/modules/whoisquery/provider.go
+41
-13
@@ -4,6 +4,7 @@ package whoisquery
4
5
import (
6
"errors"
7
+ "fmt"
8
"strings"
9
"time"
10
@@ -16,7 +17,7 @@ type provider interface {
17
remainingTime() (float64, error)
18
}
19
19
-type fromNet struct {
20
+type whoisClient struct {
21
domainAddress string
22
client *whois.Client
23
}
@@ -26,35 +27,62 @@ func newProvider(config Config) (provider, error) {
27
client := whois.NewClient()
28
client.SetTimeout(config.Timeout.Duration())
29
29
- return &fromNet{
30
+ return &whoisClient{
31
domainAddress: domain,
32
client: client,
33
}, nil
34
}
35
35
-func (f *fromNet) remainingTime() (float64, error) {
36
- raw, err := f.client.Whois(f.domainAddress)
36
+func (c *whoisClient) remainingTime() (float64, error) {
37
+ info, err := c.queryWhoisInfo()
38
if err != nil {
39
return 0, err
40
}
41
41
- result, err := whoisparser.Parse(raw)
42
+ if info.Domain.ExpirationDate == "" {
43
+ if !strings.HasPrefix(c.domainAddress, "=") {
44
+ // some servers support requesting extended data
45
+ // https://github.com/netdata/netdata/issues/17907#issuecomment-2171758380
46
+ c.domainAddress = fmt.Sprintf("= %s", c.domainAddress)
47
+ return c.remainingTime()
48
+ }
49
+ }
50
+
51
+ return parseWhoisInfoExpirationDate(info)
52
+}
53
+
54
+func (c *whoisClient) queryWhoisInfo() (*whoisparser.WhoisInfo, error) {
55
+ resp, err := c.client.Whois(c.domainAddress)
56
if err != nil {
43
- return 0, err
57
+ return nil, err
58
}
59
46
- // https://community.netdata.cloud/t/whois-query-monitor-cannot-parse-expiration-time/3485
47
- if strings.Contains(result.Domain.ExpirationDate, " ") {
48
- if v, err := time.Parse("2006.01.02 15:04:05", result.Domain.ExpirationDate); err == nil {
49
- return time.Until(v).Seconds(), nil
50
- }
60
+ info, err := whoisparser.Parse(resp)
61
+ if err != nil {
62
+ return nil, err
63
}
64
53
- if result.Domain.ExpirationDate == "" {
65
+ return &info, nil
66
+}
67
+
68
+func parseWhoisInfoExpirationDate(info *whoisparser.WhoisInfo) (float64, error) {
69
+ if info == nil {
70
+ return 0, errors.New("nil Whois Info")
71
+ }
72
+
73
+ date := info.Domain.ExpirationDate
74
+ if date == "" {
75
return 0, errors.New("no expiration date")
76
}
77
57
- expire, err := dateparse.ParseAny(result.Domain.ExpirationDate)
78
+ if strings.Contains(date, " ") {
79
+ // https://community.netdata.cloud/t/whois-query-monitor-cannot-parse-expiration-time/3485
80
+ if v, err := time.Parse("2006.01.02 15:04:05", date); err == nil {
81
+ return time.Until(v).Seconds(), nil
82
+ }
83
+ }
84
+
85
+ expire, err := dateparse.ParseAny(date)
86
if err != nil {
87
return 0, err
88
}
src/go/collectors/go.d.plugin/modules/whoisquery/whoisquery_test.go
+1
-1
@@ -72,7 +72,7 @@ func TestWhoisQuery_Init(t *testing.T) {
72
73
var typeOK bool
74
if test.providerType == net {
75
- _, typeOK = whoisquery.prov.(*fromNet)
75
+ _, typeOK = whoisquery.prov.(*whoisClient)
76
}
77
78
assert.True(t, typeOK)