| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | package health
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-10 09:16:25 +01:00
										 |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:40:09 +00:00
										 |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2017-08-27 21:33:38 +01:00
										 |  |  | 	"net"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | 	"github.com/coredns/coredns/plugin/metrics"
 | 
					
						
							| 
									
										
										
										
											2016-09-10 09:16:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							|  |  |  | )
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | func init() {
 | 
					
						
							|  |  |  | 	caddy.RegisterPlugin("health", caddy.Plugin{
 | 
					
						
							|  |  |  | 		ServerType: "dns",
 | 
					
						
							|  |  |  | 		Action:     setup,
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error {
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:40:09 +00:00
										 |  |  | 	addr, lame, err := healthParse(c)
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		return plugin.Error("health", err)
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:40:09 +00:00
										 |  |  | 	h := newHealth(addr)
 | 
					
						
							|  |  |  | 	h.lameduck = lame
 | 
					
						
							| 
									
										
										
										
											2017-08-27 21:33:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	c.OnStartup(func() error {
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:40:30 -05:00
										 |  |  | 		plugins := dnsserver.GetConfig(c).Handlers()
 | 
					
						
							|  |  |  | 		for _, p := range plugins {
 | 
					
						
							|  |  |  | 			if x, ok := p.(Healther); ok {
 | 
					
						
							| 
									
										
										
										
											2017-08-27 21:33:38 +01:00
										 |  |  | 				h.h = append(h.h, x)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	c.OnStartup(func() error {
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | 		// Poll all middleware every second.
 | 
					
						
							| 
									
										
										
										
											2017-08-27 21:33:38 +01:00
										 |  |  | 		h.poll()
 | 
					
						
							|  |  |  | 		go func() {
 | 
					
						
							|  |  |  | 			for {
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:40:09 +00:00
										 |  |  | 				select {
 | 
					
						
							|  |  |  | 				case <-time.After(1 * time.Second):
 | 
					
						
							|  |  |  | 					h.poll()
 | 
					
						
							|  |  |  | 				case <-h.pollstop:
 | 
					
						
							|  |  |  | 					return
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2017-08-27 21:33:38 +01:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		}()
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | 	c.OnStartup(func() error {
 | 
					
						
							| 
									
										
										
										
											2018-09-19 05:11:24 -04:00
										 |  |  | 		metrics.MustRegister(c, HealthDuration)
 | 
					
						
							| 
									
										
										
										
											2018-01-10 11:41:22 +00:00
										 |  |  | 		return nil
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	c.OnStartup(h.OnStartup)
 | 
					
						
							| 
									
										
										
										
											2018-04-21 17:43:02 +01:00
										 |  |  | 	c.OnRestart(h.OnRestart)
 | 
					
						
							|  |  |  | 	c.OnFinalShutdown(h.OnFinalShutdown)
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-16 14:13:28 +01:00
										 |  |  | 	// Don't do AddPlugin, as health is not *really* a plugin just a separate webserver running.
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:40:09 +00:00
										 |  |  | func healthParse(c *caddy.Controller) (string, time.Duration, error) {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	addr := ""
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:40:09 +00:00
										 |  |  | 	dur := time.Duration(0)
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	for c.Next() {
 | 
					
						
							|  |  |  | 		args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		switch len(args) {
 | 
					
						
							|  |  |  | 		case 0:
 | 
					
						
							|  |  |  | 		case 1:
 | 
					
						
							|  |  |  | 			addr = args[0]
 | 
					
						
							| 
									
										
										
										
											2017-08-27 21:33:38 +01:00
										 |  |  | 			if _, _, e := net.SplitHostPort(addr); e != nil {
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:40:09 +00:00
										 |  |  | 				return "", 0, e
 | 
					
						
							| 
									
										
										
										
											2017-08-27 21:33:38 +01:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		default:
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:40:09 +00:00
										 |  |  | 			return "", 0, c.ArgErr()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for c.NextBlock() {
 | 
					
						
							|  |  |  | 			switch c.Val() {
 | 
					
						
							|  |  |  | 			case "lameduck":
 | 
					
						
							|  |  |  | 				args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 				if len(args) != 1 {
 | 
					
						
							|  |  |  | 					return "", 0, c.ArgErr()
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				l, err := time.ParseDuration(args[0])
 | 
					
						
							|  |  |  | 				if err != nil {
 | 
					
						
							|  |  |  | 					return "", 0, fmt.Errorf("unable to parse lameduck duration value: '%v' : %v", args[0], err)
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				dur = l
 | 
					
						
							|  |  |  | 			default:
 | 
					
						
							|  |  |  | 				return "", 0, c.ArgErr()
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:40:09 +00:00
										 |  |  | 	return addr, dur, nil
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | }
 |