geodns.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package main
  2. /*
  3. Copyright 2012-2014 Ask Bjørn Hansen
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. import (
  15. "flag"
  16. "fmt"
  17. "log"
  18. "net"
  19. "os"
  20. "os/signal"
  21. "path/filepath"
  22. "runtime"
  23. "runtime/pprof"
  24. "strings"
  25. "time"
  26. )
  27. // VERSION is the current version of GeoDNS
  28. var VERSION string = "2.4.4"
  29. var buildTime string
  30. var gitVersion string
  31. var serverID string
  32. var serverIP string
  33. var serverGroups []string
  34. var timeStarted = time.Now()
  35. var (
  36. flagconfig = flag.String("config", "./dns/", "directory of zone files")
  37. flagcheckconfig = flag.Bool("checkconfig", false, "check configuration and exit")
  38. flagidentifier = flag.String("identifier", "", "identifier (hostname, pop name or similar)")
  39. flaginter = flag.String("interface", "*", "set the listener address")
  40. flagport = flag.String("port", "53", "default port number")
  41. flaghttp = flag.String("http", ":8053", "http listen address (:8053)")
  42. flaglog = flag.Bool("log", false, "be more verbose")
  43. flagcpus = flag.Int("cpus", 1, "Set the maximum number of CPUs to use")
  44. flagShowVersion = flag.Bool("version", false, "Show dnsconfig version")
  45. cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
  46. memprofile = flag.String("memprofile", "", "write memory profile to this file")
  47. )
  48. func init() {
  49. if len(gitVersion) > 0 {
  50. VERSION = VERSION + "/" + gitVersion
  51. }
  52. log.SetPrefix("geodns ")
  53. log.SetFlags(log.Lmicroseconds | log.Lshortfile)
  54. }
  55. func main() {
  56. flag.Parse()
  57. if *memprofile != "" {
  58. runtime.MemProfileRate = 1024
  59. }
  60. if *flagShowVersion {
  61. fmt.Println("geodns", VERSION, buildTime)
  62. os.Exit(0)
  63. }
  64. if len(*flagidentifier) > 0 {
  65. ids := strings.Split(*flagidentifier, ",")
  66. serverID = ids[0]
  67. if len(ids) > 1 {
  68. serverGroups = ids[1:]
  69. }
  70. }
  71. configFileName := filepath.Clean(*flagconfig + "/geodns.conf")
  72. if *flagcheckconfig {
  73. dirName := *flagconfig
  74. err := configReader(configFileName)
  75. if err != nil {
  76. log.Println("Errors reading config", err)
  77. os.Exit(2)
  78. }
  79. Zones := make(Zones)
  80. setupPgeodnsZone(Zones)
  81. err = zonesReadDir(dirName, Zones)
  82. if err != nil {
  83. log.Println("Errors reading zones", err)
  84. os.Exit(2)
  85. }
  86. return
  87. }
  88. if *flagcpus == 0 {
  89. runtime.GOMAXPROCS(runtime.NumCPU())
  90. } else {
  91. runtime.GOMAXPROCS(*flagcpus)
  92. }
  93. log.Printf("Starting geodns %s\n", VERSION)
  94. if *cpuprofile != "" {
  95. prof, err := os.Create(*cpuprofile)
  96. if err != nil {
  97. panic(err.Error())
  98. }
  99. pprof.StartCPUProfile(prof)
  100. defer func() {
  101. log.Println("closing file")
  102. prof.Close()
  103. }()
  104. defer func() {
  105. log.Println("stopping profile")
  106. pprof.StopCPUProfile()
  107. }()
  108. }
  109. go configWatcher(configFileName)
  110. metrics := NewMetrics()
  111. go metrics.Updater()
  112. go statHatPoster()
  113. if *flaginter == "*" {
  114. addrs, _ := net.InterfaceAddrs()
  115. ips := make([]string, 0)
  116. for _, addr := range addrs {
  117. ip, _, err := net.ParseCIDR(addr.String())
  118. if err != nil {
  119. continue
  120. }
  121. if !(ip.IsLoopback() || ip.IsGlobalUnicast()) {
  122. continue
  123. }
  124. ips = append(ips, ip.String())
  125. }
  126. *flaginter = strings.Join(ips, ",")
  127. }
  128. inter := getInterfaces()
  129. Zones := make(Zones)
  130. go monitor(Zones)
  131. go Zones.statHatPoster()
  132. setupPgeodnsZone(Zones)
  133. dirName := *flagconfig
  134. go zonesReader(dirName, Zones)
  135. for _, host := range inter {
  136. go listenAndServe(host)
  137. }
  138. terminate := make(chan os.Signal)
  139. signal.Notify(terminate, os.Interrupt)
  140. <-terminate
  141. log.Printf("geodns: signal received, stopping")
  142. if *memprofile != "" {
  143. f, err := os.Create(*memprofile)
  144. if err != nil {
  145. log.Fatal(err)
  146. }
  147. pprof.WriteHeapProfile(f)
  148. f.Close()
  149. }
  150. }