Ejemplo n.º 1
0
// Shutdown stops the worker
func (w *Worker) Shutdown() {
	if w.stopping {
		return
	}

	logger.Debug("poller", "shutting down %q polling worker", w.Config.Name)

	// Send shutdown command
	w.cs <- workerCmdShutdown
}
Ejemplo n.º 2
0
func (s *Server) handleStatic(c *gin.Context) {
	var ct string

	// Get static file path
	p, _ := c.Params.Get("path")
	if p == "" {
		p = "index.html"
	}

	// Try to get file data from filesystem
	localPath := path.Join(s.config.StaticDir, p)
	if _, err := os.Stat(localPath); err == nil {
		logger.Debug("server", "serving %q from filesystem", p)
		c.File(localPath)
		return
	}

	// Get file data from built-in assets
	data, err := Asset(strings.TrimPrefix(p, "/"))
	if err != nil {
		c.JSON(http.StatusNotFound, nil)
		return
	}

	switch path.Ext(p) {
	case ".css":
		ct = "text/css"

	case ".js":
		ct = "text/javascript"

	default:
		ct = http.DetectContentType(data)
	}

	logger.Debug("server", "serving %q from built-in assets", p)
	c.Data(http.StatusOK, ct, data)
}
Ejemplo n.º 3
0
func (w *Worker) poll() {
	var (
		l        *livestatus.Livestatus
		q        *livestatus.Query
		resp     *livestatus.Response
		services map[string][]livestatus.Record
		err      error
	)

	// Skip refresh if already in refreshing state
	if w.refreshing {
		logger.Warning("poller", "worker is already refreshing data from %q node", w.Config.Name)
		return
	}

	w.refreshing = true

	logger.Debug("poller", "refreshing data from %q node", w.Config.Name)

	c := NewCatalog(w)

	// Create new LiveStatus instance
	args := strings.SplitN(w.Config.Address, ":", 2)
	if len(args) != 2 {
		logger.Error("poller", "invalid node address %q", w.Config.Address)
		goto end
	}

	l = livestatus.NewLivestatus(args[0], args[1])

	// Query services data
	q = l.Query("services")
	q.Columns(
		"host_name",
		"description",
		"state",
		"last_state_change",
		"comments_with_extra_info",
		"plugin_output",
		"groups",
	)
	q.Filter("state_type > 0")

	resp, err = q.Exec()
	if err != nil {
		logger.Error("poller", "unable to retrieve services from %q node: %s", w.Config.Name, err)
		goto end
	}

	services = make(map[string][]livestatus.Record)
	for _, r := range resp.Records {
		name, err := r.GetString("host_name")
		if err != nil {
			logger.Error("poller", "unable to retrieve \"host_name\" from %q node: %s",
				w.Config.Name, err)
			goto end
		}

		if _, ok := services[name]; !ok {
			services[name] = make([]livestatus.Record, 0)
		}

		services[name] = append(services[name], r)
	}

	// Query hosts data
	q = l.Query("hosts")
	q.Columns(
		"host_name",
		"state",
		"last_state_change",
		"comments_with_extra_info",
		"groups",
	)

	resp, err = q.Exec()
	if err != nil {
		logger.Error("poller", "unable to retrieve hosts from %q node: %s", w.Config.Name, err)
		goto end
	}

	// Fill catalog with retrieved data
	for _, r := range resp.Records {
		h := &Host{
			Catalog: c,
		}

		h.Name, _ = r.GetString("host_name")
		h.State, _ = r.GetInt("state")
		h.StateChanged, _ = r.GetTime("last_state_change")
		h.Comments, _ = getComments(r)
		h.Links, _ = getLinks(h.Name, "", w.Config.Links)
		h.Groups, _ = getStringSlice(r, "groups")
		h.Services = make([]*Service, 0)

		if _, ok := services[h.Name]; ok {
			for _, sr := range services[h.Name] {
				s := &Service{Host: h}

				s.Name, _ = sr.GetString("description")
				s.State, _ = sr.GetInt("state")
				s.StateChanged, _ = sr.GetTime("last_state_change")
				s.Comments, _ = getComments(sr)
				s.Links, _ = getLinks(h.Name, s.Name, w.Config.Links)
				s.Output, _ = sr.GetString("plugin_output")
				s.Groups, _ = getStringSlice(sr, "groups")

				h.Services = append(h.Services, s)
			}
		}

		c.Hosts = append(c.Hosts, h)
	}

	// Replace catalog
	w.Catalog = c

end:
	// Reset refreshing state
	w.refreshing = false
}