Exemplo n.º 1
0
// Loads host settings.
func (host *Host) loadSettings() error {

	var settings *yaml.Yaml

	file := host.DocumentRoot + PS + settingsFile

	_, err := os.Stat(file)

	if err == nil {
		settings, err = yaml.Open(file)
		if err != nil {
			return fmt.Errorf(`Could not parse settings file (%s): %s`, file, err.Error())
		}
	} else {
		return fmt.Errorf(`Error trying to open settings file (%s): %s.`, file, err.Error())
	}

	if host.Watcher != nil {
		host.Watcher.RemoveWatch(file)
		host.Watcher.Watch(file)
	}

	host.Settings = settings

	return nil
}
Exemplo n.º 2
0
func TestSettings(t *testing.T) {
	var err error
	conf, err = yaml.Open(SettingsFile)

	if err != nil {
		panic(err.Error())
	}

}
Exemplo n.º 3
0
// Loads settings
func loadSettings(file string) (*yaml.Yaml, error) {

	var entries map[interface{}]interface{}
	var ok bool

	// Trying to read settings from file.
	y, err := yaml.Open(file)

	if err != nil {
		return nil, err
	}

	// Loading and verifying host entries
	if entries, ok = y.Get("hosts").(map[interface{}]interface{}); ok == false {
		return nil, errors.New("Missing \"hosts\" entry.")
	}

	h := map[string]*host.Host{}

	// Populating host entries.
	for key := range entries {
		name := to.String(key)
		path := to.String(entries[name])

		info, err := os.Stat(path)
		if err != nil {
			return nil, fmt.Errorf("Failed to validate host %s: %q.", name, err)
		}
		if info.IsDir() == false {
			return nil, fmt.Errorf("Host %s does not point to a directory.", name)
		}

		h[name], err = host.New(name, path)

		if err != nil {
			return nil, fmt.Errorf("Failed to initialize host %s: %q.", name, err)
		}
	}

	for name := range hosts {
		hosts[name].Close()
	}

	hosts = h

	if _, ok := hosts["default"]; ok == false {
		log.Printf("Warning: default host was not provided.\n")
	}

	return y, nil
}
Exemplo n.º 4
0
func main() {
	SettingsFile := "../settings.yaml"
	conf, err := yaml.Open(SettingsFile)
	if err != nil {
		panic(err)
	}
	client := twitter.New(&oauth.Credentials{
		to.String(conf.Get("twitter", "app", "key")),
		to.String(conf.Get("twitter", "app", "secret")),
	})
	client.SetAuth(&oauth.Credentials{
		to.String(conf.Get("twitter", "user", "token")),
		to.String(conf.Get("twitter", "user", "secret")),
	})
	_, err = client.VerifyCredentials(nil)

	if err != nil {
		panic(err)
	}
}
Exemplo n.º 5
0
func main() {
	var yml_config_file string
	y_c, e := os.LookupEnv("REDSHIFT_CONFIG")
	if e == false {
		yml_config_file = "config.yml"
	} else {
		yml_config_file = y_c
	}

	settings, err := yaml.Open(yml_config_file)
	s.CheckError(err, true)
	debug := settings.Get("debug").(bool)
	senders := make([]string, 0)

	k := settings.Get("sends")

	for m, _ := range k.(map[interface{}]interface{}) {
		n := fmt.Sprintf("%sGo", strings.Title(m.(string)))
		senders = append(senders, n)
	}

	// There's probably a better way to do this
	redis_is_cluster := settings.Get("redis_is_cluster").(bool)
	redis_list := settings.Get("redis_list").(string)
	redis_watch_interval := settings.Get("redis_watch_interval").(int)
	t := time.Now()
	ts := t.Format("Mon Jan 2 15:04:05 -0700 MST 2006")
	var rClient *redis.Client
	var rCluster *cluster.Cluster
	if redis_is_cluster {
		if debug {
			fmt.Printf("[%s] INFO Starting up in cluster mode\n", ts)
		}
		rCluster = PullCluster(settings)
		defer rCluster.Close()
	} else {
		redis_db := settings.Get("redis_db").(int)
		if debug {
			fmt.Printf("[%s] INFO Starting up in single node mode\n", ts)
		}
		rClient = PullNode(settings)
		defer rClient.Close()
		rClient.Cmd("SELECT", redis_db)
	}
	var r *redis.Reply
	if redis_is_cluster {
		r = rCluster.Cmd("RPOP", redis_list)
	} else {
		r = rClient.Cmd("RPOP", redis_list)
	}
	for {
		if redis_is_cluster {
			r = rCluster.Cmd("RPOP", redis_list)
		} else {
			r = rClient.Cmd("RPOP", redis_list)
		}
		t = time.Now()
		ts = t.Format("Mon Jan 2 15:04:05 -0700 MST 2006")

		// If the response from redis is actual data, process and then pop another response off
		// If not, who cares, go through the old switch case
		for r.Type == redis.BulkReply {
			t = time.Now()
			ts = t.Format("Mon Jan 2 15:04:05 -0700 MST 2006")
			data, err := r.Bytes()
			if debug {
				fmt.Printf("[%s] INFO BulkReply reply received. Data length %d\n", ts, len(data))
			}
			if err != nil {
				fmt.Printf("[%s] ERROR Error received: %s\n", ts, err)
			} else {
				err := Send(ts, data, settings, senders)
				if err != nil {
					fmt.Printf(err.Error())
				}
			}
			if redis_is_cluster {
				r = rCluster.Cmd("RPOP", redis_list)
			} else {
				r = rClient.Cmd("RPOP", redis_list)
			}
		}
		switch r.Type {
		case redis.ErrorReply:
			fmt.Printf("[%s] ERROR ErrorReply received: %s\n", ts, r.Err.Error())
		case redis.NilReply:
			if debug {
				fmt.Printf("[%s] INFO NilReply reply received\n", ts)
			}
		case redis.StatusReply:
			if debug {
				fmt.Printf("[%s] INFO StatusReply reply received: not processing\n", ts)
			}
		case redis.MultiReply:
			if debug {
				fmt.Printf("[%s] INFO MultiReply reply received: not processing\n", ts)
			}
		case redis.IntegerReply:
			if debug {
				fmt.Printf("[%s] INFO IntegerReply reply received: not processing\n", ts)
			}
		default:
			if debug {
				fmt.Printf("[%s] INFO Unknown reply received: not processing\n", ts)
			}
		}

		time.Sleep(time.Duration(redis_watch_interval) * time.Millisecond)
	}

}
Exemplo n.º 6
0
func IngestHandler(w http.ResponseWriter, r *auth.AuthenticatedRequest) {

	var yml_config_file string
	y_c, e := os.LookupEnv("BLUESHIFT_CONFIG")
	if e == false {
		yml_config_file = "config.yml"
	} else {
		yml_config_file = y_c
	}

	settings, err := yaml.Open(yml_config_file)
	if err != nil {
		fmt.Println("TODO: fix errors")
	}

	url_pieces := strings.Split(r.URL.String(), "/")

	group := url_pieces[2]
	source := url_pieces[3]

	t := time.Now()
	ts := t.Format("Mon Jan 2 15:04:05 -0700 MST 2006")
	debug := settings.Get("debug").(bool)

	if r.Method == "GET" {
		http.Error(w, "Method not allowed", 405)
	} else if r.Method == "POST" {
		if debug {
			fmt.Printf("[%s] INFO Message received: %+v\n", ts, r.Body)
		}
		decoder := json.NewDecoder(r.Body)
		var t IngestMessage
		err := decoder.Decode(&t)
		if err != nil {
			fmt.Printf("[%s] ERROR Error received decoding JSON: %s\n", ts, err)
		}
		// You have to have at least magnitude, not necessarily floater
		if isf(t.Floater) == false {
			t.Floater = 0.00
		}
		if nzl(t.Timestamp) && nzl(t.Subject) && nzl(t.Detail) && isn(t.Severity) && isn(t.Magnitude) && isf(t.Floater) {
			// Proceed
			var message Message
			message.Timestamp = t.Timestamp
			message.Group = group
			message.Source = source
			message.Subject = t.Subject
			message.Detail = t.Detail
			message.Magnitude = t.Magnitude
			message.Floater = t.Floater
			message.Severity = t.Severity
			message.Extra = t.Extra

			redis_is_cluster := settings.Get("redis_is_cluster").(bool)
			if redis_is_cluster {
				if debug {
					fmt.Printf("[%s] INFO Starting up in cluster mode\n", ts)
				}
				PushCluster(settings, message)
			} else {
				if debug {
					fmt.Printf("[%s] INFO Starting up in single node mode\n", ts)
				}
				PushNode(settings, message)
			}
		} else {
			// Return 5XX?
			http.Error(w, "Input validation failed.", 500)
		}
	} else {
		// Unknown
		fmt.Printf("[%s] ERROR Unknown HTTP method.\n", ts)
	}
}