Skip to content

Commit

Permalink
introduce dns-record module (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
ushis authored Mar 1, 2024
1 parent 1c06928 commit 3646cd9
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 159 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ gesundheit -conf /etc/gesundheit/gesundheit.toml
<td>MinAvailable</td>
<td>Min available space considered healthy, e.g. <code>"1G"</code></td>
</tr>
<tr>
<td rowspan="4"><strong>dns-record</strong></td>
<td rowspan="4">Check DNS record</td>
<td>Address</td>
<td>DNS server address, e.g. <code>"127.0.0.1:53"</code></td>
</tr>
<tr>
<td>Type</td>
<td>Record type, e.g. <code>"A"</code></td>
</tr>
<tr>
<td>Name</td>
<td>Name to lookup, e.g. <code>"example.com"</code></td>
</tr>
<tr>
<td>Value</td>
<td>Expected value, e.g. <code>"1.1.1.1"</code></td>
</tr>
<tr>
<td rowspan="2"><strong>exec</strong></td>
<td rowspan="2">Execute check command</td>
Expand Down
108 changes: 108 additions & 0 deletions check/dns-record/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package filemtime

import (
"context"
"errors"
"fmt"
"net"
"time"

"github.com/ushis/gesundheit/check"
"github.com/ushis/gesundheit/result"
)

type Check struct {
Address string
Type string
Name string
Value string
}

func init() {
check.Register("dns-record", New)
}

func New(_ check.Database, configure func(interface{}) error) (check.Check, error) {
check := Check{}

if err := configure(&check); err != nil {
return nil, err
}
if check.Type == "" {
return nil, errors.New("missing Type")
}
if check.Name == "" {
return nil, errors.New("missing Name")
}
return check, nil
}

func (c Check) Exec() result.Result {
records, err := lookup(resolver(c.Address), c.Type, c.Name)

if err != nil {
return result.Fail("failed to lookup %s: %s", c.Name, err)
}
for _, r := range records {
if r == c.Value {
return result.OK("%s %s resolves to %s", c.Type, c.Name, c.Value)
}
}
if len(records) == 0 {
return result.Fail("could not find any records for %s %s", c.Type, c.Name)
}
return result.Fail("%s %s resolves to %s", c.Type, c.Name, records[0])
}

func resolver(addr string) *net.Resolver {
if addr == "" {
return net.DefaultResolver
}
d := &net.Dialer{Timeout: time.Second}

return &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network string, _ string) (net.Conn, error) {
return d.DialContext(ctx, network, addr)
},
}
}

func lookup(r *net.Resolver, typ, name string) (result []string, err error) {
switch typ {
case "A":
records, err := r.LookupIP(context.Background(), "ip4", name)

if err != nil {
return nil, err
}
return mapToStrings(records), nil
case "AAAA":
records, err := r.LookupIP(context.Background(), "ip6", name)

if err != nil {
return nil, err
}
return mapToStrings(records), nil
case "CNAME":
record, err := r.LookupCNAME(context.Background(), name)

if err != nil {
return nil, err
}
return []string{record}, nil
case "TXT":
return r.LookupTXT(context.Background(), name)
default:
return nil, fmt.Errorf("unsupported record type: %s", typ)
}
}

func mapToStrings[T fmt.Stringer](values []T) []string {
s := make([]string, len(values))

for i, val := range values {
s[i] = val.String()
}
return s
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"

_ "github.com/ushis/gesundheit/check/disk-space"
_ "github.com/ushis/gesundheit/check/dns-record"
_ "github.com/ushis/gesundheit/check/exec"
_ "github.com/ushis/gesundheit/check/file-mtime"
_ "github.com/ushis/gesundheit/check/file-presence"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"eslint-plugin-vue": "^8.7.1",
"sass": "^1.71.1",
"typescript": "^5.3.3",
"vite": "^2.9.16",
"vite": "^2.9.17",
"vue-tsc": "^1.8.27"
}
}
Loading

0 comments on commit 3646cd9

Please sign in to comment.