houston을 prometheus target 파일로 출력
This commit is contained in:
@ -2,7 +2,10 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
@ -27,10 +30,12 @@ type ProcessSnapshot struct {
|
||||
}
|
||||
|
||||
type hostWithChan struct {
|
||||
Hostname string
|
||||
Procs []*protos.ProcessDescription `json:"procs"`
|
||||
Deploys map[string][]*protos.VersionAndArgs `json:"deploys"`
|
||||
opChan chan *opdef
|
||||
Hostname string
|
||||
PrivateIp string
|
||||
PublicIp string
|
||||
Procs []*protos.ProcessDescription `json:"procs"`
|
||||
Deploys map[string][]*protos.VersionAndArgs `json:"deploys"`
|
||||
opChan chan *opdef
|
||||
}
|
||||
|
||||
func makeHostWithChan(desc *protos.OperationQueryRequest) *hostWithChan {
|
||||
@ -40,9 +45,11 @@ func makeHostWithChan(desc *protos.OperationQueryRequest) *hostWithChan {
|
||||
}
|
||||
|
||||
return &hostWithChan{
|
||||
Hostname: desc.GetHostname(),
|
||||
Procs: desc.Procs,
|
||||
Deploys: newdeploys,
|
||||
PrivateIp: desc.PrivateIp,
|
||||
PublicIp: desc.PublicIp,
|
||||
Hostname: desc.GetHostname(),
|
||||
Procs: desc.Procs,
|
||||
Deploys: newdeploys,
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,7 +65,8 @@ func (pc *hostWithChan) makeOpChan() *hostWithChan {
|
||||
|
||||
type hostPool struct {
|
||||
sync.Mutex
|
||||
hosts map[string]*hostWithChan
|
||||
hosts map[string]*hostWithChan
|
||||
exportChan chan string
|
||||
}
|
||||
|
||||
type deployingProgress struct {
|
||||
@ -89,6 +97,28 @@ func (sp *hostPool) regist(desc *protos.OperationQueryRequest) (string, chan *op
|
||||
} else {
|
||||
host = makeHostWithChan(desc).withOpChan(host.opChan)
|
||||
}
|
||||
|
||||
logger.Println("houston agent registered :", desc.Hostname, desc.PrivateIp, desc.PublicIp)
|
||||
go func(prvip string, pubip string) {
|
||||
if len(prvip) > 0 {
|
||||
address := net.JoinHostPort(prvip, "9100")
|
||||
if conn, _ := net.DialTimeout("tcp", address, 3*time.Second); conn != nil {
|
||||
conn.Close()
|
||||
sp.exportChan <- "+" + prvip
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if len(pubip) > 0 {
|
||||
address := net.JoinHostPort(pubip, "9100")
|
||||
if conn, _ := net.DialTimeout("tcp", address, 3*time.Second); conn != nil {
|
||||
conn.Close()
|
||||
sp.exportChan <- "+" + pubip
|
||||
return
|
||||
}
|
||||
}
|
||||
}(desc.PrivateIp, desc.PublicIp)
|
||||
|
||||
sp.hosts[desc.Hostname] = host
|
||||
return desc.Hostname, host.opChan
|
||||
}
|
||||
@ -108,6 +138,12 @@ func (sp *hostPool) unregist(key string) {
|
||||
sp.Lock()
|
||||
defer sp.Unlock()
|
||||
|
||||
host := sp.hosts[key]
|
||||
if host != nil {
|
||||
sp.exportChan <- "-" + host.PublicIp
|
||||
sp.exportChan <- "-" + host.PrivateIp
|
||||
}
|
||||
|
||||
delete(sp.hosts, key)
|
||||
}
|
||||
|
||||
@ -452,10 +488,39 @@ func (os *operationServer) DeplyingProgress() []deployingProgress {
|
||||
return os.db.clone()
|
||||
}
|
||||
|
||||
func targetExportLoop(in chan string) {
|
||||
all := make(map[string]bool)
|
||||
for addr := range in {
|
||||
logger.Println("targetExportLoop :", addr)
|
||||
if addr[0] == '+' {
|
||||
all[addr[1:]] = true
|
||||
} else if addr[0] == '-' {
|
||||
delete(all, addr[1:])
|
||||
}
|
||||
|
||||
list := make([]string, 0, len(all))
|
||||
for k := range all {
|
||||
list = append(list, k)
|
||||
}
|
||||
|
||||
output := []map[string]any{{"targets": list}}
|
||||
|
||||
if file, err := os.Create("prometheus_targets.json"); err == nil {
|
||||
enc := json.NewEncoder(file)
|
||||
enc.Encode(output)
|
||||
file.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newOperationServer() *operationServer {
|
||||
exportChan := make(chan string)
|
||||
go targetExportLoop(exportChan)
|
||||
|
||||
return &operationServer{
|
||||
hp: hostPool{
|
||||
hosts: map[string]*hostWithChan{},
|
||||
hosts: map[string]*hostWithChan{},
|
||||
exportChan: exportChan,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user