Beispiel #1
0
func MachinesMetric() check.ExtensionCheckResult {
	metric := handler.Metric{}
	fleetClient, err := NewFleetClient()
	etcdClient := NewEtcdClient()

	if err != nil {
		log.Println(err.Error())

		return metric.Render()
	}

	machines, err := fleetClient.Machines()

	if err != nil {
		log.Println(err.Error())

		return metric.Render()
	}

	results := make(map[string]uint)

	for _, m := range machines {
		results["machines.all.all"]++
		roles := []string{"all"}

		if r, ok := m.Metadata["role"]; ok {
			results[fmt.Sprintf("machines.%s.all", r)]++
			roles = append(roles, r)
		}

		if r, err := etcdClient.Get(
			fmt.Sprintf("/%s/%s/version", EtcdNamespace(), m.ID),
			false,
			false,
		); err != nil {
			log.Println(err.Error())
		} else {
			for _, role := range roles {
				results[fmt.Sprintf("machines.%s.%s", role, r.Node.Value)]++
			}
		}
	}

	for k, v := range results {
		metric.AddPoint(&handler.Point{k, float64(v)})
	}

	return metric.Render()
}
Beispiel #2
0
func UnitsMetric() check.ExtensionCheckResult {
	metric := handler.Metric{}
	fleetClient, err := NewFleetClient()
	etcdClient := NewEtcdClient()

	if err != nil {
		log.Println(err.Error())

		return metric.Render()
	}

	units, err := fleetClient.UnitStates()

	if err != nil {
		log.Println(err.Error())

		return metric.Render()
	}

	results := make(map[string]uint)

	for _, u := range units {
		results["units.global.total"]++
		results[fmt.Sprintf("units.global.%s", u.SystemdSubState)]++

		if r, err := etcdClient.Get(
			fmt.Sprintf("/%s/%s/hostname", EtcdNamespace(), u.MachineID),
			false,
			false,
		); err != nil {
			log.Println(err.Error())
		} else {
			results[fmt.Sprintf("units.%s.%s", r.Node.Value, u.SystemdSubState)]++
			results[fmt.Sprintf("units.%s.total", r.Node.Value)]++
		}
	}

	for k, v := range results {
		metric.AddPoint(&handler.Point{k, float64(v)})
	}

	return metric.Render()
}
Beispiel #3
0
func (c *Check) Metric() check.ExtensionCheckResult {
	nodes, err := nodesInfo()

	metric := handler.Metric{}

	if err != nil {
		return metric.Render()
	}

	for _, node := range nodes {
		if !node.IsRunning {
			continue
		}

		metric.AddPoint(
			&handler.Point{
				fmt.Sprintf("rabbitmq.%s.%s", node.Name, c.Type),
				float64(c.Method(node)),
			},
		)
	}

	return metric.Render()
}
Beispiel #4
0
func ConnectonMetric() check.ExtensionCheckResult {
	metric := handler.Metric{}
	db, err := sql.Open(
		"postgres",
		fmt.Sprintf("%s?sslmode=disable", os.Getenv("DATABASE_URL")),
	)

	if err != nil {
		return metric.Render()
	}

	defer db.Close()

	rowsDatabases, err := db.Query(
		"SELECT datname FROM pg_database WHERE datistemplate = false",
	)

	if err != nil {
		log.Println(err.Error())
		return metric.Render()
	}

	defer rowsDatabases.Close()

	dbs := []string{}

	for rowsDatabases.Next() {
		var name string
		if err := rowsDatabases.Scan(&name); err != nil {
			log.Println(err.Error())
		}

		dbs = append(dbs, name)
	}

	r := make(map[string]*ConnBreakdown)

	for _, db := range dbs {
		r[db] = &ConnBreakdown{}
	}

	rowsConns, err := db.Query("SELECT datname, state FROM pg_stat_activity")

	if err != nil {
		return metric.Render()
	}

	defer rowsConns.Close()

	for rowsConns.Next() {
		var name string
		var status string

		if err := rowsConns.Scan(&name, &status); err != nil {
			log.Println(err.Error())
		}

		if _, ok := r[name]; !ok {
			continue
		}

		if status == "active" {
			r[name].Active++
		} else if status == "idle" {
			r[name].Idle++
		}
	}

	for db, val := range r {
		metric.AddPoint(
			&handler.Point{
				fmt.Sprintf("postgres.%s.active", db),
				float64(val.Active),
			},
		)

		metric.AddPoint(
			&handler.Point{
				fmt.Sprintf("postgres.%s.idle", db),
				float64(val.Idle),
			},
		)

		metric.AddPoint(
			&handler.Point{
				fmt.Sprintf("postgres.%s.total", db),
				float64(val.Active + val.Idle),
			},
		)
	}

	return metric.Render()
}