Compare commits
4 commits
Author | SHA1 | Date | |
---|---|---|---|
b7f4bec668 | |||
3922222a12 | |||
48a7c17448 | |||
ae6ca5c185 |
13 changed files with 243 additions and 32 deletions
|
@ -12,7 +12,7 @@ on:
|
|||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: [moon, amd64]
|
||||
runs-on: [dev, amd64]
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
|
|
|
@ -10,7 +10,7 @@ on:
|
|||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: [moon, amd64]
|
||||
runs-on: [dev, amd64]
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
|
|
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,3 +1,5 @@
|
|||
tmp/
|
||||
__debug*
|
||||
main
|
||||
bin/
|
||||
nodes.json
|
||||
|
|
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
|
@ -29,7 +29,7 @@
|
|||
"env": {
|
||||
"DUMP_PATH": "../tmp/nodes.json",
|
||||
"CONFIG_PATH": "../tmp/config.json",
|
||||
"NODE_PATH": "../tmp/node"
|
||||
"NODE_PATH": "../tmp/node.json"
|
||||
},
|
||||
"args": [
|
||||
"node"
|
||||
|
|
|
@ -20,3 +20,8 @@ tasks:
|
|||
- go mod tidy
|
||||
- mkdir -p bin
|
||||
- env CGO_ENABLED=0 go build -o bin/lobby2-{{ .VERSION }}-linux-amd64 cli/*.go
|
||||
deploy:
|
||||
cmds:
|
||||
- task: build
|
||||
- scp bin/lobby2-{{ .VERSION }}-linux-amd64 rosti-db:/usr/local/bin/lobby2.tmp
|
||||
- ssh rosti-db mv /usr/local/bin/lobby2.tmp /usr/local/bin/lobby2
|
||||
|
|
29
api/main.go
29
api/main.go
|
@ -3,7 +3,6 @@ package api
|
|||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
_ "gitea.ceperka.net/rosti/lobby2/docs" // This line is necessary for swag to find your docs!
|
||||
"gitea.ceperka.net/rosti/lobby2/nodes"
|
||||
|
@ -117,33 +116,15 @@ func (a *API) refreshHandler(c echo.Context) error {
|
|||
// @Summary Prometheus service discovery
|
||||
// @Description Return one nodes based on given hostname
|
||||
// @Produce application/json
|
||||
// @Success 200 {array} []prometheusDiscovery "Node details"
|
||||
// @Param service path string true "Name of the service like: systemd, pgsql, .."
|
||||
// @Success 200 {array} []nodes.prometheusDiscovery "Node details"
|
||||
// @Failure 401 {object} Message "Forbidden access"
|
||||
// @Security Bearer
|
||||
// @Router /nodes/{hostname} [get]
|
||||
// @Router /prometheus/{service} [get]
|
||||
func (a *API) prometheusHandler(c echo.Context) error {
|
||||
ss := c.QueryParam("service")
|
||||
ss := c.Param("service")
|
||||
|
||||
ns := a.np.List()
|
||||
|
||||
pds := []prometheusDiscovery{
|
||||
{
|
||||
Labels: nodes.Labels{},
|
||||
Targets: []string{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, node := range ns {
|
||||
v, ok := node.KV["prometheus_exporters"]
|
||||
if ok {
|
||||
services := strings.Split(v, ",")
|
||||
for _, service := range services {
|
||||
if ss == service {
|
||||
pds[0].Targets = append(pds[0].Targets, node.HostName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pds := nodes.GetPrometheusSD(a.np, ss)
|
||||
|
||||
return c.JSONPretty(http.StatusOK, pds, " ")
|
||||
}
|
||||
|
|
|
@ -10,8 +10,3 @@ type params struct {
|
|||
Labels nodes.Labels `json:"labels"`
|
||||
KV nodes.KV `json:"kv"`
|
||||
}
|
||||
|
||||
type prometheusDiscovery struct {
|
||||
Labels nodes.Labels `json:"labels"`
|
||||
Targets []string `json:"targets"`
|
||||
}
|
||||
|
|
|
@ -52,3 +52,23 @@ func printAction(c *cli.Context) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func prometheusAction(c *cli.Context) error {
|
||||
cfg := GetConfig()
|
||||
|
||||
np := nodes.NewNodesProcessor(cfg.DumpPath, cfg.DropAfterSeconds)
|
||||
|
||||
pds := nodes.GetPrometheusSD(np, "node")
|
||||
|
||||
for _, pd := range pds {
|
||||
body, err := json.MarshalIndent(pd, "", " ")
|
||||
if err != nil {
|
||||
fmt.Printf("failed to marshal prometheus discovery: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println(string(body))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -27,6 +27,11 @@ func main() {
|
|||
Usage: "Prints all discovered nodes",
|
||||
Action: printAction,
|
||||
},
|
||||
{
|
||||
Name: "prometheus",
|
||||
Usage: "Prints Prometheus Service Discovery",
|
||||
Action: prometheusAction,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
|
60
docs/docs.go
60
docs/docs.go
|
@ -145,6 +145,49 @@ const docTemplate = `{
|
|||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/prometheus/{service}": {
|
||||
"get": {
|
||||
"security": [
|
||||
{
|
||||
"Bearer": []
|
||||
}
|
||||
],
|
||||
"description": "Return one nodes based on given hostname",
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"summary": "Prometheus service discovery",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Name of the service like: systemd, pgsql, ..",
|
||||
"name": "service",
|
||||
"in": "path",
|
||||
"required": true
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Node details",
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/nodes.prometheusDiscovery"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"401": {
|
||||
"description": "Forbidden access",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/api.Message"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
|
@ -181,6 +224,23 @@ const docTemplate = `{
|
|||
"type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"nodes.prometheusDiscovery": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Labels": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"Targets": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"securityDefinitions": {
|
||||
|
|
|
@ -137,6 +137,49 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/prometheus/{service}": {
|
||||
"get": {
|
||||
"security": [
|
||||
{
|
||||
"Bearer": []
|
||||
}
|
||||
],
|
||||
"description": "Return one nodes based on given hostname",
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"summary": "Prometheus service discovery",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Name of the service like: systemd, pgsql, ..",
|
||||
"name": "service",
|
||||
"in": "path",
|
||||
"required": true
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Node details",
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/nodes.prometheusDiscovery"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"401": {
|
||||
"description": "Forbidden access",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/api.Message"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
|
@ -173,6 +216,23 @@
|
|||
"type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"nodes.prometheusDiscovery": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Labels": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"Targets": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"securityDefinitions": {
|
||||
|
|
|
@ -22,6 +22,17 @@ definitions:
|
|||
last_update:
|
||||
type: integer
|
||||
type: object
|
||||
nodes.prometheusDiscovery:
|
||||
properties:
|
||||
Labels:
|
||||
additionalProperties:
|
||||
type: string
|
||||
type: object
|
||||
Targets:
|
||||
items:
|
||||
type: string
|
||||
type: array
|
||||
type: object
|
||||
info:
|
||||
contact: {}
|
||||
description: API of Lobby 2 project that helps to discover and connect to other
|
||||
|
@ -111,6 +122,33 @@ paths:
|
|||
security:
|
||||
- Bearer: []
|
||||
summary: Refresh node
|
||||
/prometheus/{service}:
|
||||
get:
|
||||
description: Return one nodes based on given hostname
|
||||
parameters:
|
||||
- description: 'Name of the service like: systemd, pgsql, ..'
|
||||
in: path
|
||||
name: service
|
||||
required: true
|
||||
type: string
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: Node details
|
||||
schema:
|
||||
items:
|
||||
items:
|
||||
$ref: '#/definitions/nodes.prometheusDiscovery'
|
||||
type: array
|
||||
type: array
|
||||
"401":
|
||||
description: Forbidden access
|
||||
schema:
|
||||
$ref: '#/definitions/api.Message'
|
||||
security:
|
||||
- Bearer: []
|
||||
summary: Prometheus service discovery
|
||||
securityDefinitions:
|
||||
Bearer:
|
||||
in: header
|
||||
|
|
45
nodes/prometheus.go
Normal file
45
nodes/prometheus.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
package nodes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type prometheusDiscovery struct {
|
||||
Labels map[string]string `json:"Labels"`
|
||||
Targets []string `json:"Targets"`
|
||||
}
|
||||
|
||||
func GetPrometheusSD(p *NodesProcessor, ss string) []prometheusDiscovery {
|
||||
ns := p.List()
|
||||
|
||||
pds := []prometheusDiscovery{
|
||||
{
|
||||
Labels: make(map[string]string),
|
||||
Targets: []string{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, node := range ns {
|
||||
port, ok := node.KV["prometheus_port"]
|
||||
if !ok {
|
||||
port = "9999"
|
||||
}
|
||||
host, ok := node.KV["prometheus_host"]
|
||||
if !ok {
|
||||
host = node.HostName
|
||||
}
|
||||
|
||||
v, ok := node.KV["prometheus_exporters"]
|
||||
if ok {
|
||||
services := strings.Split(v, ",")
|
||||
for _, service := range services {
|
||||
if ss == service {
|
||||
pds[0].Targets = append(pds[0].Targets, fmt.Sprintf("%s:%s", host, port))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return pds
|
||||
}
|
Loading…
Reference in a new issue