forked from quadseed/dns-mx-watchdog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (59 loc) · 1.61 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/miekg/dns"
"github.com/slack-go/slack"
)
func main() {
token := os.Getenv("SLACK_BOT_TOKEN")
channelId := os.Getenv("CHANNEL_ID")
priDnsServer := os.Getenv("PRI_DNS_SERVER")
secDnsServer := os.Getenv("SEC_DNS_SERVER")
domain := os.Getenv("DOMAIN")
client := slack.New(token)
ticker := time.NewTicker(time.Millisecond * 1000 * 60 * 60)
defer ticker.Stop()
log.Printf("DNS Watchdog task has been started")
count := 24
execLookup(*client, channelId, priDnsServer, secDnsServer, domain, &count)
for {
select {
case <-ticker.C:
log.Printf("count=%d\n", count)
execLookup(*client, channelId, priDnsServer, secDnsServer, domain, &count)
}
}
}
func lookupMXRecords(dnsServer string, domain string) bool {
m := new(dns.Msg)
m.SetQuestion(domain+".", dns.TypeMX)
c := new(dns.Client)
in, _, err := c.Exchange(m, dnsServer)
if err != nil {
fmt.Println("DNS Query Failed:", err)
return false
}
for _, answer := range in.Answer {
if mx, ok := answer.(*dns.MX); ok {
fmt.Printf("Host: %s, Priority: %d\n", mx.Mx, mx.Preference)
}
}
return true
}
func execLookup(client slack.Client, channelId string, priDnsServer string, secDnsServer string, domain string, count *int) {
pri_ok := lookupMXRecords(priDnsServer, domain)
sec_ok := lookupMXRecords(secDnsServer, domain)
if pri_ok && sec_ok {
*count++
if *count > 24 {
*count = 0
SendDailyNotification(client, channelId, priDnsServer, secDnsServer, domain)
}
} else {
*count = 0
SendHourlyNotification(client, channelId, priDnsServer, secDnsServer, domain, pri_ok, sec_ok)
}
}