121 lines
2.5 KiB
Go

package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"os"
"time"
pb "deevirt.fr/compute/cmd/compute_mgr/proto"
"deevirt.fr/compute/cmd/compute_mgr/server/raft"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
)
var (
myAddr = flag.String("address", "localhost:60051", "TCP host+port for this node")
raftId = flag.String("raft_id", "", "Node id used by Raft")
)
type UP struct {
instance string
status string
}
func main0() {
client, err := api.NewClient(api.Config{
Address: "http://172.16.9.161:9090",
})
if err != nil {
fmt.Printf("Error creating client: %v\n", err)
os.Exit(1)
}
v1api := v1.NewAPI(client)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
r := v1.Range{
Start: time.Now().Add(-time.Minute),
End: time.Now(),
Step: 2 * time.Minute,
}
result, warnings, err := v1api.QueryRange(ctx, "up{job='f242b4bb-b6d0-415f-b3f9-9e9d439532b5'}", r, v1.WithTimeout(5*time.Second))
if err != nil {
fmt.Printf("Error querying Prometheus: %v\n", err)
os.Exit(1)
}
if len(warnings) > 0 {
fmt.Printf("Warnings: %v\n", warnings)
}
//fmt.Printf("Result:\n%v\n", result)
// Convertir le résultat en Matrix
matrix, ok := result.(model.Matrix)
if !ok {
fmt.Println("Erreur : le résultat n'est pas de type Matrix")
return
}
for _, stream := range matrix {
var instance = ""
for key, value := range stream.Metric {
if key == "node_id" {
//test.instance = string(value)
instance = string(value)
}
}
println(instance)
println(stream.Values[0].Value.String())
/*for _, pair := range stream.Values {
println(pair.Value.String())
}*/
}
}
func main() {
flag.Parse()
if *raftId == "" {
log.Fatalf("flag --raft_id is required")
}
ctx := context.Background()
_, port, err := net.SplitHostPort(*myAddr)
if err != nil {
log.Fatalf("failed to parse local address (%q): %v", *myAddr, err)
}
sock, err := net.Listen("tcp", fmt.Sprintf(":%s", port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
r, tm, err := raft.NewRaft(ctx, *raftId, *myAddr)
if err != nil {
log.Fatalf("failed to start raft: %v", err)
}
s := grpc.NewServer()
pb.RegisterDomainServer(s, nil)
tm.Register(s)
//leaderhealth.Setup(r, s, []string{"Example"})
raft.Register(s, r)
reflection.Register(s)
if err := s.Serve(sock); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}