geodns.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package main
  2. /*
  3. Copyright 2012-2015 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.5.0"
  29. var buildTime string
  30. var gitVersion string
  31. // Set development with the 'devel' build flag to load
  32. // templates from disk instead of from the binary.
  33. var development bool
  34. var serverID string
  35. var serverIP string
  36. var serverGroups []string
  37. var timeStarted = time.Now()
  38. var (
  39. flagconfig = flag.String("config", "./dns/", "directory of zone files")
  40. flagcheckconfig = flag.Bool("checkconfig", false, "check configuration and exit")
  41. flagidentifier = flag.String("identifier", "", "identifier (hostname, pop name or similar)")
  42. flaginter = flag.String("interface", "*", "set the listener address")
  43. flagport = flag.String("port", "53", "default port number")
  44. flaghttp = flag.String("http", ":8053", "http listen address (:8053)")
  45. flaglog = flag.Bool("log", false, "be more verbose")
  46. flagcpus = flag.Int("cpus", 1, "Set the maximum number of CPUs to use")
  47. flagShowVersion = flag.Bool("version", false, "Show dnsconfig version")
  48. cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
  49. memprofile = flag.String("memprofile", "", "write memory profile to this file")
  50. )
  51. func init() {
  52. if len(gitVersion) > 0 {
  53. VERSION = VERSION + "/" + gitVersion
  54. }
  55. log.SetPrefix("geodns ")
  56. log.SetFlags(log.Lmicroseconds | log.Lshortfile)
  57. }
  58. func main() {
  59. flag.Parse()
  60. if *memprofile != "" {
  61. runtime.MemProfileRate = 1024
  62. }
  63. if *flagShowVersion {
  64. fmt.Println("geodns", VERSION, buildTime)
  65. os.Exit(0)
  66. }
  67. if len(*flagidentifier) > 0 {
  68. ids := strings.Split(*flagidentifier, ",")
  69. serverID = ids[0]
  70. if len(ids) > 1 {
  71. serverGroups = ids[1:]
  72. }
  73. }
  74. configFileName := filepath.Clean(*flagconfig + "/geodns.conf")
  75. if *flagcheckconfig {
  76. dirName := *flagconfig
  77. err := configReader(configFileName)
  78. if err != nil {
  79. log.Println("Errors reading config", err)
  80. os.Exit(2)
  81. }
  82. Zones := make(Zones)
  83. setupPgeodnsZone(Zones)
  84. err = zonesReadDir(dirName, Zones)
  85. if err != nil {
  86. log.Println("Errors reading zones", err)
  87. os.Exit(2)
  88. }
  89. return
  90. }
  91. if *flagcpus == 0 {
  92. runtime.GOMAXPROCS(runtime.NumCPU())
  93. } else {
  94. runtime.GOMAXPROCS(*flagcpus)
  95. }
  96. log.Printf("Starting geodns %s\n", VERSION)
  97. if *cpuprofile != "" {
  98. prof, err := os.Create(*cpuprofile)
  99. if err != nil {
  100. panic(err.Error())
  101. }
  102. pprof.StartCPUProfile(prof)
  103. defer func() {
  104. log.Println("closing file")
  105. prof.Close()
  106. }()
  107. defer func() {
  108. log.Println("stopping profile")
  109. pprof.StopCPUProfile()
  110. }()
  111. }
  112. go configWatcher(configFileName)
  113. metrics := NewMetrics()
  114. go metrics.Updater()
  115. go statHatPoster()
  116. if *flaginter == "*" {
  117. addrs, _ := net.InterfaceAddrs()
  118. ips := make([]string, 0)
  119. for _, addr := range addrs {
  120. ip, _, err := net.ParseCIDR(addr.String())
  121. if err != nil {
  122. continue
  123. }
  124. if !(ip.IsLoopback() || ip.IsGlobalUnicast()) {
  125. continue
  126. }
  127. ips = append(ips, ip.String())
  128. }
  129. *flaginter = strings.Join(ips, ",")
  130. }
  131. inter := getInterfaces()
  132. Zones := make(Zones)
  133. go monitor(Zones)
  134. go Zones.statHatPoster()
  135. setupRootZone()
  136. setupPgeodnsZone(Zones)
  137. dirName := *flagconfig
  138. go zonesReader(dirName, Zones)
  139. for _, host := range inter {
  140. go listenAndServe(host)
  141. }
  142. terminate := make(chan os.Signal)
  143. signal.Notify(terminate, os.Interrupt)
  144. <-terminate
  145. log.Printf("geodns: signal received, stopping")
  146. if *memprofile != "" {
  147. f, err := os.Create(*memprofile)
  148. if err != nil {
  149. log.Fatal(err)
  150. }
  151. pprof.WriteHeapProfile(f)
  152. f.Close()
  153. }
  154. }