David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 1 | // Copyright 2016 Open Networking Laboratory |
| 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 ( |
| 17 | "fmt" |
| 18 | "github.com/Sirupsen/logrus" |
| 19 | "github.com/gorilla/mux" |
| 20 | "github.com/kelseyhightower/envconfig" |
| 21 | "net/http" |
David K. Bainbridge | 9bbc6a5 | 2017-01-09 20:53:22 -0800 | [diff] [blame] | 22 | "regexp" |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 23 | "strconv" |
David K. Bainbridge | 9bbc6a5 | 2017-01-09 20:53:22 -0800 | [diff] [blame] | 24 | "strings" |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 25 | "sync" |
| 26 | "text/template" |
| 27 | "time" |
| 28 | ) |
| 29 | |
| 30 | // application application configuration and internal state |
| 31 | type application struct { |
David K. Bainbridge | 9bbc6a5 | 2017-01-09 20:53:22 -0800 | [diff] [blame] | 32 | Port int `default:"4246" desc:"port on which the service will listen for requests"` |
| 33 | Listen string `default:"0.0.0.0" desc:"IP on which the service will listen for requests"` |
| 34 | LogLevel string `default:"warning" envconfig:"LOG_LEVEL" desc:"log output level"` |
| 35 | LogFormat string `default:"text" envconfig:"LOG_FORMAT" desc:"format of log messages"` |
| 36 | DHCPLeaseFile string `default:"/harvester/dhcpd.leases" envconfig:"DHCP_LEASE_FILE" desc:"lease file to parse for lease information"` |
| 37 | OutputFile string `envconfig:"OUTPUT_FILE" desc:"name of file to output discovered lease in bind9 format"` |
| 38 | OutputFormat string `default:"{{.ClientHostname}}\tIN A {{.IPAddress}}\t; {{.HardwareAddress}}" envconfig:"OUTPUT_FORMAT" desc:"specifies the single entry format when outputing to a file"` |
| 39 | VerifyLeases bool `default:"true" envconfig:"VERIFY_LEASES" desc:"verifies leases with a ping"` |
| 40 | VerifyTimeout time.Duration `default:"1s" envconfig:"VERIFY_TIMEOUT" desc:"max timeout (RTT) to wait for verification pings"` |
| 41 | VerifyWithUDP bool `default:"false" envconfig:"VERIFY_WITH_UDP" desc:"use UDP instead of raw sockets for ping verification"` |
| 42 | QueryPeriod time.Duration `default:"30s" envconfig:"QUERY_PERIOD" desc:"period at which the DHCP lease file is processed"` |
| 43 | QuietPeriod time.Duration `default:"2s" envconfing:"QUIET_PERIOD" desc:"period to wait between accepting parse requests"` |
| 44 | RequestTimeout time.Duration `default:"10s" envconfig:"REQUEST_TIMEOUT" desc:"period to wait for processing when requesting a DHCP lease database parsing"` |
| 45 | RNDCUpdate bool `default:"false" envconfig:"RNDC_UPDATE" desc:"determines if the harvester reloads the DNS servers after harvest"` |
| 46 | RNDCAddress string `default:"127.0.0.1" envconfig:"RNDC_ADDRESS" desc:"IP address of the DNS server to contact via RNDC"` |
| 47 | RNDCPort int `default:"954" envconfig:"RNDC_PORT" desc:"port of the DNS server to contact via RNDC"` |
| 48 | RNDCKeyFile string `default:"/key/rndc.conf.maas" envconfig:"RNDC_KEY_FILE" desc:"key file, with default, to contact DNS server"` |
| 49 | RNDCZone string `default:"cord.lab" envconfig:"RNDC_ZONE" desc:"zone to reload"` |
| 50 | BadClientNames []string `default:"localhost" envconfig:"BAD_CLIENT_NAMES" desc:"list of invalid hostnames for clients"` |
| 51 | 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] | 52 | |
David K. Bainbridge | 9bbc6a5 | 2017-01-09 20:53:22 -0800 | [diff] [blame] | 53 | log *logrus.Logger `ignored:"true"` |
| 54 | interchange sync.RWMutex `ignored:"true"` |
| 55 | leases map[string]*Lease `ignored:"true"` |
| 56 | byHardware map[string]*Lease `ignored:"true"` |
| 57 | byHostname map[string]*Lease `ignored:"true"` |
| 58 | outputTemplate *template.Template `ignored:"true"` |
| 59 | requests chan *chan uint `ignored:"true"` |
| 60 | clientNameTemplate *template.Template `ignored:"true"` |
| 61 | badClientNames map[string]bool `ignored:"true"` |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | func main() { |
| 65 | // initialize application state |
| 66 | app := &application{ |
| 67 | log: logrus.New(), |
| 68 | requests: make(chan *chan uint, 100), |
| 69 | } |
| 70 | |
| 71 | // process and validate the application configuration |
| 72 | err := envconfig.Process("HARVESTER", app) |
| 73 | if err != nil { |
| 74 | app.log.Fatalf("unable to parse configuration options : %s", err) |
| 75 | } |
| 76 | switch app.LogFormat { |
| 77 | case "json": |
| 78 | app.log.Formatter = &logrus.JSONFormatter{} |
| 79 | default: |
| 80 | app.log.Formatter = &logrus.TextFormatter{ |
| 81 | FullTimestamp: true, |
| 82 | ForceColors: true, |
| 83 | } |
| 84 | } |
| 85 | level, err := logrus.ParseLevel(app.LogLevel) |
| 86 | if err != nil { |
| 87 | level = logrus.WarnLevel |
| 88 | } |
| 89 | app.log.Level = level |
| 90 | |
| 91 | app.outputTemplate, err = template.New("harvester").Parse(app.OutputFormat) |
| 92 | if err != nil { |
| 93 | app.log.Fatalf("invalid output file format specified : %s", err) |
| 94 | } |
| 95 | |
| 96 | // output the configuration |
| 97 | app.log.Infof(`Configuration: |
David K. Bainbridge | 9bbc6a5 | 2017-01-09 20:53:22 -0800 | [diff] [blame] | 98 | LISTEN: %s |
| 99 | PORT: %d |
| 100 | LOG_LEVEL: %s |
| 101 | LOG_FORMAT: %s |
| 102 | DHCP_LEASE_FILE: %s |
| 103 | OUTPUT_FILE: %s |
| 104 | OUTPUT_FORMAT: %s |
| 105 | VERIFY_LEASES: %t |
| 106 | VERIFY_TIMEOUT: %s |
| 107 | VERIFY_WITH_UDP: %t |
| 108 | QUERY_PERIOD: %s |
| 109 | QUIET_PERIOD: %s |
| 110 | REQUEST_TIMEOUT: %s |
| 111 | RNDC_UPDATE: %t |
| 112 | RNDC_ADDRESS: %s |
| 113 | RNDC_PORT: %d |
| 114 | RNDC_KEY_FILE: %s |
| 115 | RNDC_ZONE: %s |
| 116 | BAD_CLIENT_NAMES: %s |
| 117 | CLIENT_NAME_TEMPLATE: %s`, |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 118 | app.Listen, app.Port, |
| 119 | app.LogLevel, app.LogFormat, |
| 120 | app.DHCPLeaseFile, app.OutputFile, strconv.Quote(app.OutputFormat), |
| 121 | app.VerifyLeases, app.VerifyTimeout, app.VerifyWithUDP, |
| 122 | app.QueryPeriod, app.QuietPeriod, app.RequestTimeout, |
David K. Bainbridge | 9bbc6a5 | 2017-01-09 20:53:22 -0800 | [diff] [blame] | 123 | app.RNDCUpdate, app.RNDCAddress, app.RNDCPort, app.RNDCKeyFile, app.RNDCZone, |
| 124 | strings.Join(app.BadClientNames[:], ","), app.ClientNameTemplate) |
| 125 | |
| 126 | app.clientNameTemplate, err = template.New("harvester").Funcs(template.FuncMap{ |
| 127 | "regex": func(target, match, replace string) string { |
| 128 | re := regexp.MustCompile(match) |
| 129 | return re.ReplaceAllString(target, replace) |
| 130 | }, |
| 131 | }).Parse(app.ClientNameTemplate) |
| 132 | if err != nil { |
| 133 | app.log.Fatalf("Unable to parse client host name template %s", err) |
| 134 | } |
| 135 | |
| 136 | app.badClientNames = make(map[string]bool) |
| 137 | for _, bad := range app.BadClientNames { |
| 138 | app.badClientNames[bad] = true |
| 139 | } |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -0700 | [diff] [blame] | 140 | |
| 141 | // establish REST end points |
| 142 | router := mux.NewRouter() |
| 143 | router.HandleFunc("/lease/", app.listLeasesHandler).Methods("GET") |
| 144 | router.HandleFunc("/lease/{ip}", app.getLeaseHandler).Methods("GET") |
| 145 | router.HandleFunc("/lease/hardware/{mac}", app.getLeaseByHardware).Methods("GET") |
| 146 | router.HandleFunc("/lease/hostname/{name}", app.getLeaseByHostname).Methods("GET") |
| 147 | router.HandleFunc("/harvest/", app.doHarvestHandler).Methods("POST") |
| 148 | router.HandleFunc("/harvest", app.doHarvestHandler).Methods("POST") |
| 149 | http.Handle("/", router) |
| 150 | |
| 151 | // start DHCP lease file synchronization handler |
| 152 | go app.syncRequestHandler(app.requests) |
| 153 | |
| 154 | // start loop to periodically synchronize DHCP lease file |
| 155 | go app.syncFromDHCPLeaseFileLoop(app.requests) |
| 156 | |
| 157 | // listen for REST requests |
| 158 | http.ListenAndServe(fmt.Sprintf("%s:%d", app.Listen, app.Port), nil) |
| 159 | } |