Example #1
0
func init() {
	logcfg, err := config.ParseConfig(configFile)
	if err != nil {
		log.Printf("%s logging config not found")
		return
	}

	// do we want to dump stack traces?
	s, _ := logcfg["stacktrace"]

	if s == "true" {
		stackTrace = true
	}

	// put overrides here
	logLevel := logcfg["loglevel"]
	if logLevel != "" {
		setLogLevel(logLevel)
	}
	return
}
Example #2
0
func main() {

	// handle command line args
	var configFile string
	flag.StringVar(&configFile, "cfg", "daemon.cfg", "full path to daemon config")
	var logConfigPath string
	flag.StringVar(&logConfigPath, "logcfg", "none", "full path to log config")
	flag.Parse()

	if logConfigPath != "none" {
		logr.SetConfig(logConfigPath)
	}

	config, err := config.ParseConfig(configFile)
	if err != nil {
		log.Fatal("couldn't open config file %s", configFile, err)
	}
	nagios_host_file = config["nagios_host_file"]
	nagios_group_file = config["nagios_groups_file"]
	host_list_file = config["host_list_file"]
	watch_root := config["etcd_watch_root_url"]

	s := config["file_rewrite_interval"]
	if s != "" {
		i, err := strconv.Atoi(s)
		if err != nil {
			logr.LogLine(logr.Linfo, ltagsrc, fmt.Sprintf("invalid file rewrite val in config: ", err))
		}

		if i != 0 {
			fileRewriteInterval = i
		}
	}
	logr.LogLine(logr.Linfo, ltagsrc, fmt.Sprintf("file rewrite interval set to: %d", fileRewriteInterval))
	// expect this to be csv or single entry
	etcd_server_list := strings.Split(config["etcd_server_list"], ",")
	kapi, err := GetEtcdKapi(etcd_server_list)
	if err != nil {
		// we die on the inital because it assumes a user is there watching
		logr.LogLine(logr.Lfatal, ltagsrc, fmt.Sprintf("Error getting etcdKAPI", err.Error()))
		os.Exit(2)
	}
	logr.LogLine(logr.Linfo, ltagsrc, "got client")
	etcdWatcher.InitDataMap(kapi, watch_root)
	logr.LogLine(logr.Linfo, ltagsrc, "Dumping map contents for verification")
	etcdWatcher.DumpMap()
	logr.LogLine(logr.Linfo, ltagsrc, "Generating initial config files")
	regenHosts()
	//
	// spin up the web server
	//
	go webservice.StartWebService(config["web_listen_port"])
	watcherOpts := client.WatcherOptions{AfterIndex: 0, Recursive: true}
	w := kapi.Watcher(watch_root, &watcherOpts)
	logr.LogLine(logr.Linfo, ltagsrc, "Waiting for an update...")
	restartCount := 0
	for {
		r, err := w.Next(context.Background())
		if err != nil {
			logr.LogLine(logr.Lerror, ltagsrc, fmt.Sprintf("Error watching etcd", err.Error()))
			// has etcd gone away?
			restartCount++
			switch {
			case restartCount < 10:
				logr.LogLine(logr.Lerror, ltagsrc, "Sleeping for 10 seconds then retrying")
				time.Sleep(10 * time.Second)
			case restartCount < 20:
				time.Sleep(30 * time.Second)
				logr.LogLine(logr.Lerror, ltagsrc, "Sleeping for 30 seconds then retrying.")
			default:
				time.Sleep(60 * time.Second * 5) // default sleep 5 minutes before retry
				logr.LogLine(logr.Lerror, ltagsrc, "Sleeping for 5 minutes then retrying.")
			}
			kapi, err := GetEtcdKapi(etcd_server_list)
			if err != nil {
				logr.LogLine(logr.Lerror, ltagsrc, fmt.Sprintf("Error getting etcdKAPI", err.Error()))
			}
			w = kapi.Watcher(watch_root, &watcherOpts)
			continue
		}
		// do something with it here
		action := r.Action
		k := r.Node.Key
		v := r.Node.Value
		switch action {
		case "delete":
			logr.LogLine(logr.Linfo, ltagsrc, fmt.Sprintf("delete of key: %s", k))
			go removeHost(k)
		case "set":
			logr.LogLine(logr.Linfo, ltagsrc, fmt.Sprintf("update of key: %s, value: %s", k, v))
			go updateHost(k, v)
		}
	}
}