Esempio n. 1
0
func Update() (err error) {
	db := database.GetDatabase()
	defer db.Close()

	host, err := hosts.GetHost(db, os.Getenv("HOST_ID"))
	if err != nil {
		return
	}

	if host.Status == "online" {
		cpuUsage.Set(host.CpuUsage)
		memUsage.Set(host.MemUsage)
		serverCount.Set(float64(host.ServerCount))
		deviceCount.Set(float64(host.DeviceCount))
		threadCount.Set(float64(host.ThreadCount))
	} else {
		cpuUsage.Set(0.0)
		memUsage.Set(0.0)
		serverCount.Set(0.0)
		deviceCount.Set(0.0)
		threadCount.Set(0.0)
	}

	return
}
Esempio n. 2
0
func Update() (err error) {
	db := database.GetDatabase()
	defer db.Close()

	data := &series{
		Series: []*metric{},
	}

	host, err := hosts.GetHost(db, os.Getenv("HOST_ID"))
	if err != nil {
		return
	}

	if host.Status != "online" {
		return
	}

	curTime := time.Now().Unix()

	mtric := &metric{
		Metric: "pritunl.cpu_usage",
		Points: [][]interface{}{
			[]interface{}{
				curTime, host.CpuUsage,
			},
		},
		Type: "gauge",
		Host: host.Name,
	}
	data.Series = append(data.Series, mtric)

	mtric = &metric{
		Metric: "pritunl.mem_usage",
		Points: [][]interface{}{
			[]interface{}{
				curTime, host.MemUsage,
			},
		},
		Type: "gauge",
		Host: host.Name,
	}
	data.Series = append(data.Series, mtric)

	mtric = &metric{
		Metric: "pritunl.running_servers",
		Points: [][]interface{}{
			[]interface{}{
				curTime, host.ServerCount,
			},
		},
		Type: "gauge",
		Host: host.Name,
	}
	data.Series = append(data.Series, mtric)

	mtric = &metric{
		Metric: "pritunl.connected_devices",
		Points: [][]interface{}{
			[]interface{}{
				curTime, host.DeviceCount,
			},
		},
		Type: "gauge",
		Host: host.Name,
	}
	data.Series = append(data.Series, mtric)

	mtric = &metric{
		Metric: "pritunl.thread_count",
		Points: [][]interface{}{
			[]interface{}{
				curTime, host.ThreadCount,
			},
		},
		Type: "gauge",
		Host: host.Name,
	}
	data.Series = append(data.Series, mtric)

	jsonData, err := json.Marshal(data)
	if err != nil {
		err = errortypes.ParseError{
			errors.Wrap(err, "datadog: Failed to parse series data"),
		}
		return
	}

	resp, err := http.Post(
		fmt.Sprintf(
			"https://app.datadoghq.com/api/v1/series?api_key=%s",
			os.Getenv("DATADOG_API_KEY")),
		"application/json",
		bytes.NewBuffer(jsonData),
	)

	if resp.StatusCode < 200 || resp.StatusCode > 299 {
		err = errortypes.RequestError{
			errors.New("datadog: Failed to post series"),
		}
		return
	}

	return
}