-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
68 lines (57 loc) · 1.39 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
package main
import (
"fmt"
"github.com/miekg/dns"
"github.com/slack-go/slack"
"log"
"os"
"time"
)
func main() {
token := os.Getenv("SLACK_BOT_TOKEN")
channelId := os.Getenv("CHANNEL_ID")
dnsServer := os.Getenv("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, dnsServer, domain, &count)
for {
select {
case <-ticker.C:
log.Printf("count=%d\n", count)
execLookup(*client, channelId, dnsServer, 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, dnsServer string, domain string, count *int) {
ok := lookupMXRecords(dnsServer, domain)
if ok {
*count++
if *count > 24 {
*count = 0
SendDailyNotification(client, channelId, dnsServer, domain)
}
} else {
*count = 0
SendHourlyNotification(client, channelId, dnsServer, domain)
}
}