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