Example #1
0
func main() {
	debugptr := flag.Int("debug", 0, "Set the debug level, the higher, the more verbose")
	nodaemonptr := flag.Bool("nodaemon", false, "Run in the foreground and not as a daemon")
	listen := flag.String("address", ":8053", "Set the port (+optional address) to listen at")
	nameptr := flag.String("servername", "",
		"Set the server name (and send it to clients)")
	helpptr := flag.Bool("help", false, "Displays usage instructions")
	zoneptr := flag.String("domain", "", "Set the name of the zone we are authoritative for")

	flag.Parse()
	help := *helpptr
	if help {
		fmt.Printf("Usage of %s:\n", os.Args[0])
		flag.PrintDefaults()
		os.Exit(0)
	}
	globalConfig = make(map[string]interface{})
	namemsg := ""
	if *nameptr != "" {
		globalConfig["servername"] = *nameptr
		namemsg = fmt.Sprintf(" %s", *nameptr)
	}
	zonemsg := ""
	if *zoneptr != "" {
		zone = strings.ToLower(*zoneptr)
		globalConfig["zonename"] = zone
		zonemsg = fmt.Sprintf(" on zone %s", zone)
	}
	debug = *debugptr
	globalConfig["debug"] = *debugptr
	globalConfig["daemon"] = !*nodaemonptr
	daemon = !reflect.NewValue(*nodaemonptr).(*reflect.BoolValue).Get()
	udpaddr, error := net.ResolveUDPAddr(*listen)
	checkError(fmt.Sprintf("Cannot parse \"%s\": %s\n", *listen), error)
	tcpaddr, error := net.ResolveTCPAddr(*listen)
	checkError(fmt.Sprintf("Cannot parse \"%s\": %s\n", *listen), error)
	if daemon {
		debuglogger = syslog.NewLogger(syslog.LOG_DEBUG,
			loggerOptions)
		infologger = syslog.NewLogger(syslog.LOG_NOTICE,
			loggerOptions)
		crisislogger = syslog.NewLogger(syslog.LOG_CRIT,
			loggerOptions)
	} else {
		debuglogger = log.New(os.Stderr, nil, "[DEBUG] ",
			loggerOptions)
		infologger = log.New(os.Stderr, nil, "[INFO] ",
			loggerOptions)
		crisislogger = log.New(os.Stderr, nil, "[FATAL] ",
			loggerOptions)
	}
	responder.Init(flag.LastOption())
	infologger.Logf("%s", fmt.Sprintf("Starting%s%s...", namemsg, zonemsg))
	udpchan := make(chan bool)
	go udpListener(udpaddr, udpchan)
	tcpchan := make(chan bool)
	go tcpListener(tcpaddr, tcpchan)

	<-udpchan // Just to wait the listener, otherwise, the Go runtime ends
	// even if there are live goroutines
	<-tcpchan
	infologger.Logf("%s", "Terminating...")
}
Example #2
0
File: ncd.go Project: pjjw/ncd
	flagServicename = flag.String("service", "", "reported servicename")
	flagURL         = flag.String("url", "http://127.0.0.1:8323/ncd/", "url endpoint to post check data to")
	flagSilent      = flag.Bool("silent", false, "suppress output")
	flagPassive     = flag.Bool("passive", true, "submit as passive check")
	flagCmdlist     = flag.Bool("cmdlist", false, "arg is a file containing commands")
	flagAddr        = flag.String("addr", ":8323", "http service address")
	flagEndpoint    = flag.String("endpoint", "/ncd/", "http service endpoint")
	flagUseSSL      = flag.Bool("ssl", false, "use ssl")
	flagSSLCert     = flag.String("cert", "cert.pem", "ssl cert file")
	flagSSLKey      = flag.String("key", "key.pem", "ssl key file")
	flagSpoolDir    = flag.String("spooldir", "/var/nagios/spool/checkresults", "nagios spool directory")
	flagSyslog      = flag.Bool("syslog", true, "log to syslog- if false, log to stdout")
	flagUsername    = flag.String("username", "npd", "basic auth username")
	flagPassword    = flag.String("password", "npd", "basic auth password")

	logger = syslog.NewLogger(syslog.LOG_INFO, log.Flags())
)

var templ = template.MustParse(templateStr, nil)

func root(w http.ResponseWriter, r *http.Request) {
	// check header
	if *flagPassword != "" && *flagUsername != "" {
		auth, ok := r.Header["Authorization"]
		if ok && strings.HasPrefix(auth[0], "Basic ") {
			str := strings.TrimLeft(auth[0], "Basic ")
			decode, err := base64.StdEncoding.DecodeString(str)
			if err != nil {
				log.Print("cannot decode auth string: ", err)
				return
			}