Skip to content

Commit

Permalink
[people] initial
Browse files Browse the repository at this point in the history
  • Loading branch information
DictumMortuum committed Jan 8, 2025
1 parent 0aea160 commit a996ff4
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 104 deletions.
1 change: 1 addition & 0 deletions .github/workflows/servus-utils.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
- 'cmd/servus-scrape/**.go'
- 'cmd/servus-series/**.go'
- 'cmd/servus-deco/**.go'
- 'cmd/servus-people/**.go'
workflow_dispatch: {}

jobs:
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ BINARIES = \
database-exporter \
file-exporter \
servus-deco \
servus-network
servus-network \
servus-people

GOOPTS = -buildmode=pie -trimpath -mod=readonly -modcacherw -ldflags=-s -ldflags=-w

Expand Down
85 changes: 77 additions & 8 deletions cmd/servus-people/db.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,89 @@
package main

import (
"github.com/DictumMortuum/servus-extapi/pkg/db"
"database/sql"

"github.com/DictumMortuum/servus-extapi/pkg/model"
"github.com/jmoiron/sqlx"
)

func getMappings() ([]model.Device, error) {
DB, err := db.DatabaseX()
func Exists(DB *sqlx.DB, mac string) (int64, error) {
q := `
select
id
from
tdevices
where
mac = ?
`

var rs int64
err := DB.Get(&rs, q, mac)
if err == sql.ErrNoRows {
return -1, nil
}
if err != nil && err != sql.ErrNoRows {
return -1, err
}

return rs, nil
}

func Insert(DB *sqlx.DB, payload Val) (int64, error) {
exists, err := Exists(DB, payload.Metric.MAC)
if err != nil {
return nil, err
return -1, err
}

if exists != -1 {
return exists, nil
}
defer DB.Close()

rs := []model.Device{}
q := `select id, mac, alias from tdevices`
err = DB.Select(&rs, q)
q := `
insert into tdevices (
mac,
alias,
online
) values (
:mac,
:nickname,
1
)
`
row, err := DB.NamedExec(q, payload.Metric)
if err != nil {
return -1, err
}

id, err := row.LastInsertId()
if err != nil {
return -1, err
}

return id, nil
}

func Status(DB *sqlx.DB, mac string, online int) error {
_, err := DB.Exec(`update tdevices set online = ? where mac = ?`, online, mac)
if err != nil {
return err
}

return nil
}

func Reset(DB *sqlx.DB) error {
_, err := DB.Exec(`update tdevices set online = 0`)
if err != nil {
return err
}

return nil
}

func Select(DB *sqlx.DB) ([]model.Device, error) {
var rs []model.Device
err := DB.Select(&rs, `select * from tdevices`)
if err != nil {
return nil, err
}
Expand Down
155 changes: 66 additions & 89 deletions cmd/servus-people/main.go
Original file line number Diff line number Diff line change
@@ -1,107 +1,60 @@
package main

import (
"fmt"
"context"
"encoding/json"
"log"
"strings"
"os"
"time"

"github.com/DictumMortuum/servus-extapi/pkg/config"
"github.com/DictumMortuum/servus-extapi/pkg/deco"
"github.com/DictumMortuum/servus-extapi/pkg/db"
"github.com/jmoiron/sqlx"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
)

func printDevices(c *deco.Client) error {
result, err := c.ClientList()
if err != nil {
return err
}
type Val struct {
Metric struct {
Instance string `json:"instance"`
Interface string `json:"interface"`
IP string `json:"ip"`
Nickname string `json:"nickname"`
MAC string `json:"mac"`
} `json:"metric"`
}

mappings, err := getMappings()
func process(DB *sqlx.DB, result model.Value) error {
err := Reset(DB)
if err != nil {
return err
}

for _, device := range result.Result.ClientList {
var status int
if device.Online {
status = 1
} else {
status = 0
vectorVal := result.(model.Vector)
for _, item := range vectorVal {
raw, err := item.MarshalJSON()
if err != nil {
return err
}

nickname := strings.ReplaceAll(device.Name, ",", " ")
for _, mapping := range mappings {
if mapping.Mac == device.MAC {
nickname = mapping.Alias
break
}
var v Val
err = json.Unmarshal(raw, &v)
if err != nil {
return err
}

now := time.Now()
sec := now.Unix()

fmt.Printf(
"client,deco,nickname,%s,ip,%s,mac,%s,type,%s,interface,%s,status,%d=%d\n",
nickname,
device.IP,
device.MAC,
device.ClientType,
device.Interface,
status,
sec)
}

fmt.Printf("client_total,deco=%d\n", len(result.Result.ClientList))

return nil
}

func printDecos(c *deco.Client) error {
result, err := c.DeviceList()
if err != nil {
return err
}

count := 0

for _, device := range result.Result.DeviceList {
var status int
if device.InetStatus == "online" {
status = 1
count++
} else {
status = 0
_, err = Insert(DB, v)
if err != nil {
return err
}

fmt.Printf(
"device,deco,ip,%s,role,%s,inet_error,%s,nickname,%s,group_status,%s=%d\n",
device.DeviceIP,
device.Role,
device.InetErrorMsg,
device.Nickname,
device.GroupStatus,
status)

fmt.Printf(
"signal24,deco,ip,%s,role,%s,inet_error,%s,nickname,%s,group_status,%s=%v\n",
device.DeviceIP,
device.Role,
device.InetErrorMsg,
device.Nickname,
device.GroupStatus,
device.SignalLevel.Band24)

fmt.Printf(
"signal5,deco,ip,%s,role,%s,inet_error,%s,nickname,%s,group_status,%s=%v\n",
device.DeviceIP,
device.Role,
device.InetErrorMsg,
device.Nickname,
device.GroupStatus,
device.SignalLevel.Band5)
err = Status(DB, v.Metric.MAC, 1)
if err != nil {
return err
}
}

fmt.Printf("total,deco=%d\n", count)
return nil
}

Expand All @@ -111,20 +64,44 @@ func main() {
log.Fatal(err)
}

c := deco.New(config.Cfg.Deco.Host)
err = c.Authenticate(config.Cfg.Deco.Pass)
client, err := api.NewClient(api.Config{
Address: "https://prometheus.dictummortuum.com",
})
if err != nil {
log.Fatal(err.Error())
log.Fatalf("Error creating client: %v\n", err)
}

err = printDecos(c)
v1api := v1.NewAPI(client)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

result, _, err := v1api.Query(ctx, "deco_client{type='phone'}", time.Now(), v1.WithTimeout(5*time.Second))
if err != nil {
log.Fatal(err.Error())
log.Fatalf("Error querying Prometheus: %v\n", err)
}

err = printDevices(c)
DB, err := db.DatabaseX()
if err != nil {
log.Fatal(err.Error())
log.Fatal(err)
}
defer DB.Close()

switch result.Type() {
case model.ValVector:
{
err = process(DB, result)
if err != nil {
log.Fatal(err)
}
}
}

devices, err := Select(DB)
if err != nil {
log.Fatal(err)
}

for _, device := range devices {
device.Write(os.Stdout)
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ require (
github.com/heetch/confita v0.10.0
github.com/jmoiron/sqlx v1.4.0
github.com/mrz1836/go-sanitize v1.3.2
github.com/prometheus/client_golang v1.20.4
github.com/prometheus/client_golang v1.20.5
github.com/prometheus/common v0.61.0
github.com/redis/go-redis/v9 v9.7.0
github.com/spf13/cast v1.7.1
github.com/supertokens/supertokens-golang v0.24.1
Expand Down Expand Up @@ -86,7 +87,6 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.60.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/robfig/go-cache v0.0.0-20130306151617-9fc39e0dbf62 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs=
github.com/prometheus/client_golang v1.20.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zIq5+qNN23vI=
github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand All @@ -350,6 +352,8 @@ github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7q
github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/common v0.60.0 h1:+V9PAREWNvJMAuJ1x1BaWl9dewMW4YrHZQbx0sJNllA=
github.com/prometheus/common v0.60.0/go.mod h1:h0LYf1R1deLSKtD4Vdg8gy4RuOvENW2J/h19V5NADQw=
github.com/prometheus/common v0.61.0 h1:3gv/GThfX0cV2lpO7gkTUwZru38mxevy90Bj8YFSRQQ=
github.com/prometheus/common v0.61.0/go.mod h1:zr29OCN/2BsJRaFwG8QOBr41D6kkchKbpeNH7pAjb/s=
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
Expand Down
17 changes: 14 additions & 3 deletions pkg/model/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@ package model

import (
"encoding/json"
"fmt"
"io"

"gorm.io/gorm"
)

type Device struct {
Id int64 `json:"id"`
Mac string `json:"mac"`
Alias string `json:"alias"`
Id int64 `json:"id"`
Mac string `json:"mac"`
Alias string `json:"alias"`
Online bool `json:"online"`
}

func (d Device) Write(w io.Writer) {
status := 0
if d.Online {
status = 1
}
fmt.Fprintf(w, "status,device,mac,%s,alias,%s,online,%d\n", d.Mac, d.Alias, status)
}

func (Device) TableName() string {
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.0.41"
"version": "0.0.42"
}

0 comments on commit a996ff4

Please sign in to comment.