geodns.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. "github.com/abh/geodns/applog"
  27. "github.com/abh/geodns/monitor"
  28. "github.com/abh/geodns/querylog"
  29. "github.com/abh/geodns/server"
  30. "github.com/abh/geodns/zones"
  31. "github.com/pborman/uuid"
  32. )
  33. // VERSION is the current version of GeoDNS
  34. var VERSION string = "2.7.0"
  35. var buildTime string
  36. var gitVersion string
  37. // Set development with the 'devel' build flag to load
  38. // templates from disk instead of from the binary.
  39. var development bool
  40. var (
  41. serverInfo *monitor.ServerInfo
  42. )
  43. var (
  44. flagconfig = flag.String("config", "./dns/", "directory of zone files")
  45. flagconfigfile = flag.String("configfile", "geodns.conf", "filename of config file (in 'config' directory)")
  46. flagcheckconfig = flag.Bool("checkconfig", false, "check configuration and exit")
  47. flagidentifier = flag.String("identifier", "", "identifier (hostname, pop name or similar)")
  48. flaginter = flag.String("interface", "*", "set the listener address")
  49. flagport = flag.String("port", "53", "default port number")
  50. flaghttp = flag.String("http", ":8053", "http listen address (:8053)")
  51. flaglog = flag.Bool("log", false, "be more verbose")
  52. flagcpus = flag.Int("cpus", 1, "Set the maximum number of CPUs to use")
  53. flagLogFile = flag.String("logfile", "", "log to file")
  54. flagPrivateDebug = flag.Bool("privatedebug", false, "Make debugging queries accepted only on loopback")
  55. flagShowVersion = flag.Bool("version", false, "Show dnsconfig version")
  56. cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
  57. memprofile = flag.String("memprofile", "", "write memory profile to this file")
  58. )
  59. func init() {
  60. if len(gitVersion) > 0 {
  61. VERSION = VERSION + "/" + gitVersion
  62. }
  63. log.SetPrefix("geodns ")
  64. log.SetFlags(log.Lmicroseconds | log.Lshortfile)
  65. serverInfo = &monitor.ServerInfo{}
  66. serverInfo.UUID = uuid.New()
  67. serverInfo.Started = time.Now()
  68. }
  69. func main() {
  70. flag.Parse()
  71. if *memprofile != "" {
  72. runtime.MemProfileRate = 1024
  73. }
  74. if *flagShowVersion {
  75. extra := []string{}
  76. if len(buildTime) > 0 {
  77. extra = append(extra, buildTime)
  78. }
  79. extra = append(extra, runtime.Version())
  80. fmt.Printf("geodns %s (%s)\n", VERSION, strings.Join(extra, ", "))
  81. os.Exit(0)
  82. }
  83. if *flaglog {
  84. applog.Enabled = true
  85. }
  86. if len(*flagLogFile) > 0 {
  87. applog.FileOpen(*flagLogFile)
  88. }
  89. if len(*flagidentifier) > 0 {
  90. ids := strings.Split(*flagidentifier, ",")
  91. serverInfo.ID = ids[0]
  92. if len(ids) > 1 {
  93. serverInfo.Groups = ids[1:]
  94. }
  95. }
  96. var configFileName string
  97. if filepath.IsAbs(*flagconfigfile) {
  98. configFileName = *flagconfigfile
  99. } else {
  100. configFileName = filepath.Clean(filepath.Join(*flagconfig, *flagconfigfile))
  101. }
  102. if *flagcheckconfig {
  103. err := configReader(configFileName)
  104. if err != nil {
  105. log.Println("Errors reading config", err)
  106. os.Exit(2)
  107. }
  108. // dirName := *flagconfig
  109. // Zones := make(zones.Zones)
  110. // srv.setupPgeodnsZone(Zones)
  111. // err = srv.zonesReadDir(dirName, Zones)
  112. if err != nil {
  113. log.Println("Errors reading zones", err)
  114. os.Exit(2)
  115. }
  116. return
  117. }
  118. if *flagcpus == 0 {
  119. runtime.GOMAXPROCS(runtime.NumCPU())
  120. } else {
  121. runtime.GOMAXPROCS(*flagcpus)
  122. }
  123. log.Printf("Starting geodns %s (%s)\n", VERSION, runtime.Version())
  124. if *cpuprofile != "" {
  125. prof, err := os.Create(*cpuprofile)
  126. if err != nil {
  127. panic(err.Error())
  128. }
  129. pprof.StartCPUProfile(prof)
  130. defer func() {
  131. log.Println("closing file")
  132. prof.Close()
  133. }()
  134. defer func() {
  135. log.Println("stopping profile")
  136. pprof.StopCPUProfile()
  137. }()
  138. }
  139. // load geodns.conf config
  140. configReader(configFileName)
  141. // load (and re-load) zone data
  142. go configWatcher(configFileName)
  143. if *flaginter == "*" {
  144. addrs, _ := net.InterfaceAddrs()
  145. ips := make([]string, 0)
  146. for _, addr := range addrs {
  147. ip, _, err := net.ParseCIDR(addr.String())
  148. if err != nil {
  149. continue
  150. }
  151. if !(ip.IsLoopback() || ip.IsGlobalUnicast()) {
  152. continue
  153. }
  154. ips = append(ips, ip.String())
  155. }
  156. *flaginter = strings.Join(ips, ",")
  157. }
  158. inter := getInterfaces()
  159. if Config.HasStatHat() {
  160. log.Println("StatHat integration has been removed in favor of more generic metrics")
  161. }
  162. mon := monitor.NewMonitor(serverInfo)
  163. go mon.Run()
  164. srv := server.NewServer(serverInfo)
  165. if qlc := Config.QueryLog; len(qlc.Path) > 0 {
  166. ql, err := querylog.NewFileLogger(qlc.Path, qlc.MaxSize, qlc.Keep)
  167. if err != nil {
  168. log.Fatalf("Could not start file query logger: %s", err)
  169. }
  170. srv.SetQueryLogger(ql)
  171. }
  172. muxm, err := zones.NewMuxManager(*flagconfig, srv)
  173. if err != nil {
  174. log.Printf("error loading zones: %s", err)
  175. }
  176. go muxm.Run()
  177. for _, host := range inter {
  178. go srv.ListenAndServe(host)
  179. }
  180. go func() {
  181. // setup metrics httpd stuff
  182. }()
  183. terminate := make(chan os.Signal)
  184. signal.Notify(terminate, os.Interrupt)
  185. <-terminate
  186. log.Printf("geodns: signal received, stopping")
  187. if *memprofile != "" {
  188. f, err := os.Create(*memprofile)
  189. if err != nil {
  190. log.Fatal(err)
  191. }
  192. pprof.WriteHeapProfile(f)
  193. f.Close()
  194. }
  195. applog.FileClose()
  196. }