Brian O'Connor | 6a37ea9 | 2017-08-03 22:45:59 -0700 | [diff] [blame^] | 1 | // Copyright 2016 Open Networking Foundation |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package main |
| 15 | |
| 16 | import ( |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 17 | "flag" |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 18 | "fmt" |
| 19 | "github.com/Sirupsen/logrus" |
| 20 | "github.com/gorilla/mux" |
| 21 | "github.com/kelseyhightower/envconfig" |
| 22 | "net/http" |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 23 | "os" |
David K. Bainbridge | 17911b4 | 2017-01-09 20:53:22 -0800 | [diff] [blame] | 24 | "regexp" |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 25 | "strconv" |
David K. Bainbridge | 17911b4 | 2017-01-09 20:53:22 -0800 | [diff] [blame] | 26 | "strings" |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 27 | "sync" |
| 28 | "text/template" |
| 29 | "time" |
| 30 | ) |
| 31 | |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 32 | const appName = "HARVESTER" |
| 33 | |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 34 | // application application configuration and internal state |
| 35 | type application struct { |
David K. Bainbridge | 8b52a9c | 2017-05-08 16:21:56 -0700 | [diff] [blame] | 36 | Port int `default:"4246" desc:"port on which the service will listen for requests"` |
| 37 | Listen string `default:"0.0.0.0" desc:"IP on which the service will listen for requests"` |
| 38 | LogLevel string `default:"warning" envconfig:"LOG_LEVEL" desc:"log output level"` |
| 39 | LogFormat string `default:"text" envconfig:"LOG_FORMAT" desc:"format of log messages"` |
| 40 | DHCPLeaseFile string `default:"/harvester/dhcpd.leases" envconfig:"DHCP_LEASE_FILE" desc:"lease file to parse for lease information"` |
| 41 | DHCPReservationFile string `default:"/reservations/dhcpd.reservations" envconfig:"DHCP_RESERVATION_FILE" desc:"lease reservation file for IP information"` |
| 42 | OutputFile string `envconfig:"OUTPUT_FILE" desc:"name of file to output discovered lease in bind9 format"` |
| 43 | OutputFormat string `default:"{{.ClientHostname}}\tIN A {{.IPAddress}}\t; {{.HardwareAddress}}" envconfig:"OUTPUT_FORMAT" desc:"specifies the single entry format when outputing to a file"` |
| 44 | VerifyLeases bool `default:"true" envconfig:"VERIFY_LEASES" desc:"verifies leases with a ping"` |
| 45 | VerifyTimeout time.Duration `default:"1s" envconfig:"VERIFY_TIMEOUT" desc:"max timeout (RTT) to wait for verification pings"` |
| 46 | VerifyWithUDP bool `default:"false" envconfig:"VERIFY_WITH_UDP" desc:"use UDP instead of raw sockets for ping verification"` |
| 47 | QueryPeriod time.Duration `default:"30s" envconfig:"QUERY_PERIOD" desc:"period at which the DHCP lease file is processed"` |
| 48 | QuietPeriod time.Duration `default:"2s" envconfing:"QUIET_PERIOD" desc:"period to wait between accepting parse requests"` |
| 49 | RequestTimeout time.Duration `default:"10s" envconfig:"REQUEST_TIMEOUT" desc:"period to wait for processing when requesting a DHCP lease database parsing"` |
| 50 | RNDCUpdate bool `default:"false" envconfig:"RNDC_UPDATE" desc:"determines if the harvester reloads the DNS servers after harvest"` |
| 51 | RNDCAddress string `default:"127.0.0.1" envconfig:"RNDC_ADDRESS" desc:"IP address of the DNS server to contact via RNDC"` |
| 52 | RNDCPort int `default:"954" envconfig:"RNDC_PORT" desc:"port of the DNS server to contact via RNDC"` |
| 53 | RNDCKeyFile string `default:"/key/rndc.conf.maas" envconfig:"RNDC_KEY_FILE" desc:"key file, with default, to contact DNS server"` |
| 54 | RNDCZone string `default:"cord.lab" envconfig:"RNDC_ZONE" desc:"zone to reload"` |
| 55 | BadClientNames []string `default:"localhost" envconfig:"BAD_CLIENT_NAMES" desc:"list of invalid hostnames for clients"` |
| 56 | ClientNameTemplate string `default:"UKN-{{with $x:=.HardwareAddress|print}}{{regex $x \":\" \"\"}}{{end}}" envconfig:"CLIENT_NAME_TEMPLATE" desc:"template for generated host name"` |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 57 | |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 58 | appFlags *flag.FlagSet `ignored:"true"` |
David K. Bainbridge | 17911b4 | 2017-01-09 20:53:22 -0800 | [diff] [blame] | 59 | log *logrus.Logger `ignored:"true"` |
| 60 | interchange sync.RWMutex `ignored:"true"` |
| 61 | leases map[string]*Lease `ignored:"true"` |
| 62 | byHardware map[string]*Lease `ignored:"true"` |
| 63 | byHostname map[string]*Lease `ignored:"true"` |
| 64 | outputTemplate *template.Template `ignored:"true"` |
| 65 | requests chan *chan uint `ignored:"true"` |
| 66 | clientNameTemplate *template.Template `ignored:"true"` |
| 67 | badClientNames map[string]bool `ignored:"true"` |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | func main() { |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 71 | |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 72 | // initialize application state |
| 73 | app := &application{ |
| 74 | log: logrus.New(), |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 75 | appFlags: flag.NewFlagSet("", flag.ContinueOnError), |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 76 | requests: make(chan *chan uint, 100), |
| 77 | } |
| 78 | |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 79 | app.appFlags.Usage = func() { |
| 80 | envconfig.Usage(appName, app) |
| 81 | } |
| 82 | if err := app.appFlags.Parse(os.Args[1:]); err != nil { |
| 83 | if err != flag.ErrHelp { |
| 84 | os.Exit(1) |
| 85 | } else { |
| 86 | return |
| 87 | } |
| 88 | } |
| 89 | |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 90 | // process and validate the application configuration |
| 91 | err := envconfig.Process("HARVESTER", app) |
| 92 | if err != nil { |
| 93 | app.log.Fatalf("unable to parse configuration options : %s", err) |
| 94 | } |
| 95 | switch app.LogFormat { |
| 96 | case "json": |
| 97 | app.log.Formatter = &logrus.JSONFormatter{} |
| 98 | default: |
| 99 | app.log.Formatter = &logrus.TextFormatter{ |
| 100 | FullTimestamp: true, |
| 101 | ForceColors: true, |
| 102 | } |
| 103 | } |
| 104 | level, err := logrus.ParseLevel(app.LogLevel) |
| 105 | if err != nil { |
| 106 | level = logrus.WarnLevel |
| 107 | } |
| 108 | app.log.Level = level |
| 109 | |
| 110 | app.outputTemplate, err = template.New("harvester").Parse(app.OutputFormat) |
| 111 | if err != nil { |
| 112 | app.log.Fatalf("invalid output file format specified : %s", err) |
| 113 | } |
| 114 | |
| 115 | // output the configuration |
| 116 | app.log.Infof(`Configuration: |
David K. Bainbridge | 8b52a9c | 2017-05-08 16:21:56 -0700 | [diff] [blame] | 117 | LISTEN: %s |
| 118 | PORT: %d |
| 119 | LOG_LEVEL: %s |
| 120 | LOG_FORMAT: %s |
| 121 | DHCP_LEASE_FILE: %s |
| 122 | DHCP_RESERVATION_FILE: %s |
| 123 | OUTPUT_FILE: %s |
| 124 | OUTPUT_FORMAT: %s |
| 125 | VERIFY_LEASES: %t |
| 126 | VERIFY_TIMEOUT: %s |
| 127 | VERIFY_WITH_UDP: %t |
| 128 | QUERY_PERIOD: %s |
| 129 | QUIET_PERIOD: %s |
| 130 | REQUEST_TIMEOUT: %s |
| 131 | RNDC_UPDATE: %t |
| 132 | RNDC_ADDRESS: %s |
| 133 | RNDC_PORT: %d |
| 134 | RNDC_KEY_FILE: %s |
| 135 | RNDC_ZONE: %s |
| 136 | BAD_CLIENT_NAMES: %s |
| 137 | CLIENT_NAME_TEMPLATE: %s`, |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 138 | app.Listen, app.Port, |
| 139 | app.LogLevel, app.LogFormat, |
David K. Bainbridge | 8b52a9c | 2017-05-08 16:21:56 -0700 | [diff] [blame] | 140 | app.DHCPLeaseFile, app.DHCPReservationFile, app.OutputFile, strconv.Quote(app.OutputFormat), |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 141 | app.VerifyLeases, app.VerifyTimeout, app.VerifyWithUDP, |
| 142 | app.QueryPeriod, app.QuietPeriod, app.RequestTimeout, |
David K. Bainbridge | 17911b4 | 2017-01-09 20:53:22 -0800 | [diff] [blame] | 143 | app.RNDCUpdate, app.RNDCAddress, app.RNDCPort, app.RNDCKeyFile, app.RNDCZone, |
| 144 | strings.Join(app.BadClientNames[:], ","), app.ClientNameTemplate) |
| 145 | |
| 146 | app.clientNameTemplate, err = template.New("harvester").Funcs(template.FuncMap{ |
| 147 | "regex": func(target, match, replace string) string { |
| 148 | re := regexp.MustCompile(match) |
| 149 | return re.ReplaceAllString(target, replace) |
| 150 | }, |
| 151 | }).Parse(app.ClientNameTemplate) |
| 152 | if err != nil { |
| 153 | app.log.Fatalf("Unable to parse client host name template %s", err) |
| 154 | } |
| 155 | |
| 156 | app.badClientNames = make(map[string]bool) |
| 157 | for _, bad := range app.BadClientNames { |
| 158 | app.badClientNames[bad] = true |
| 159 | } |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 160 | |
| 161 | // establish REST end points |
| 162 | router := mux.NewRouter() |
| 163 | router.HandleFunc("/lease/", app.listLeasesHandler).Methods("GET") |
| 164 | router.HandleFunc("/lease/{ip}", app.getLeaseHandler).Methods("GET") |
| 165 | router.HandleFunc("/lease/hardware/{mac}", app.getLeaseByHardware).Methods("GET") |
| 166 | router.HandleFunc("/lease/hostname/{name}", app.getLeaseByHostname).Methods("GET") |
| 167 | router.HandleFunc("/harvest/", app.doHarvestHandler).Methods("POST") |
| 168 | router.HandleFunc("/harvest", app.doHarvestHandler).Methods("POST") |
| 169 | http.Handle("/", router) |
| 170 | |
| 171 | // start DHCP lease file synchronization handler |
| 172 | go app.syncRequestHandler(app.requests) |
| 173 | |
| 174 | // start loop to periodically synchronize DHCP lease file |
| 175 | go app.syncFromDHCPLeaseFileLoop(app.requests) |
| 176 | |
| 177 | // listen for REST requests |
| 178 | http.ListenAndServe(fmt.Sprintf("%s:%d", app.Listen, app.Port), nil) |
| 179 | } |