Skip to content

Commit

Permalink
[database-exporter] added voip last call
Browse files Browse the repository at this point in the history
  • Loading branch information
DictumMortuum committed Feb 15, 2024
1 parent aef0afe commit 96db995
Show file tree
Hide file tree
Showing 9 changed files with 461 additions and 8 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/database-exporter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
on:
push:
paths:
'cmd/database-exporter/**.go'
workflow_dispatch: {}

jobs:
trigger:
runs-on: ubuntu-latest
name: "package database-exporter"
environment: default
steps:
- uses: passeidireto/trigger-external-workflow-action@main
env:
PAYLOAD_REPO: "database-exporter"
PAYLOAD_ARCH: "aarch64"
with:
repository: DictumMortuum/pacman
event: package
github_pat: ${{ secrets.PAT }}
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ BINARIES = \
servus-prices \
servus-scrape \
servus-series \
servus-auth
servus-auth \
database-exporter \

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

Expand Down
96 changes: 96 additions & 0 deletions cmd/database-exporter/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package main

import (
"net/http"

"github.com/DictumMortuum/servus-extapi/pkg/model"
"github.com/DictumMortuum/servus/pkg/models"
"github.com/jmoiron/sqlx"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

type Client struct {
httpClient http.Client
}

func NewClient() *Client {
return &Client{
httpClient: http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
},
}
}

func (c *Client) Metrics() http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
err := c.getStatistics()
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
_, _ = writer.Write([]byte(err.Error()))
return
}

promhttp.Handler().ServeHTTP(writer, request)
}
}

func (c *Client) getStatistics() error {
db, err := sqlx.Connect("mysql", Cfg.Databases["mariadb"])
if err != nil {
return err
}
defer db.Close()

var tmp []models.KeyVal
err = db.Select(&tmp, `select * from tkeyval`)
if err != nil {
return err
}

for _, modem := range tmp {
var stats model.Modem
err = modem.Unmarshal(&stats)
if err != nil {
return err
}

Uptime.WithLabelValues(stats.Host).Set(float64(stats.Uptime))
CurrentUp.WithLabelValues(stats.Host).Set(float64(stats.CurrentUp))
CurrentDown.WithLabelValues(stats.Host).Set(float64(stats.CurrentDown))
CRCUp.WithLabelValues(stats.Host).Set(float64(stats.CRCUp))
CRCDown.WithLabelValues(stats.Host).Set(float64(stats.CRCDown))
MaxUp.WithLabelValues(stats.Host).Set(float64(stats.MaxUp))
MaxDown.WithLabelValues(stats.Host).Set(float64(stats.MaxDown))
DataUp.WithLabelValues(stats.Host).Set(float64(stats.DataUp))
DataDown.WithLabelValues(stats.Host).Set(float64(stats.DataDown))
FECUp.WithLabelValues(stats.Host).Set(float64(stats.FECUp))
FECDown.WithLabelValues(stats.Host).Set(float64(stats.FECDown))
SNRUp.WithLabelValues(stats.Host).Set(float64(stats.SNRUp))
SNRDown.WithLabelValues(stats.Host).Set(float64(stats.SNRDown))

var isEnabled int = 0
if stats.Status {
isEnabled = 1
}

Status.WithLabelValues(stats.Host).Set(float64(isEnabled))

var isVoipEnabled int = 0
if stats.VoipStatus {
isVoipEnabled = 1
}

VoipStatus.WithLabelValues(stats.Host).Set(float64(isVoipEnabled))

var isInVoipCall int = 0
if stats.VoipCallStatus != "idle" {
isInVoipCall = 1
}

VoipCallStatus.WithLabelValues(stats.Host).Set(float64(isInVoipCall))
}

return nil
}
65 changes: 65 additions & 0 deletions cmd/database-exporter/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"

"github.com/heetch/confita"
"github.com/heetch/confita/backend/file"
"github.com/heetch/confita/backend/flags"
)

type Config struct {
Databases map[string]string `config:"databases"`
Exporter struct {
Database struct {
Port string `config:"port"`
}
}
}

var (
Cfg Config
)

func main() {
loader := confita.NewLoader(
file.NewBackend("/etc/conf.d/servusrc.yml"),
flags.NewBackend(),
)

err := loader.Load(context.Background(), &Cfg)
if err != nil {
log.Fatal(err)
}

Init()

serverDead := make(chan struct{})
s := NewServer(NewClient())

go func() {
s.ListenAndServe()
close(serverDead)
}()

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
defer stop()

go func() {
<-ctx.Done()
s.Stop()
}()

select {
case <-ctx.Done():
case <-serverDead:
}

version := "0.0.4"
fmt.Printf("database-exporter v%s HTTP server stopped\n", version)
}
161 changes: 161 additions & 0 deletions cmd/database-exporter/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package main

import (
"log"

"github.com/prometheus/client_golang/prometheus"
)

var (
Uptime = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "uptime",
Namespace: "modem",
},
[]string{"hostname"},
)

Status = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "status",
Namespace: "modem",
},
[]string{"hostname"},
)

MaxUp = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "max_up",
Namespace: "modem",
},
[]string{"hostname"},
)

MaxDown = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "max_down",
Namespace: "modem",
},
[]string{"hostname"},
)

DataUp = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "data_up",
Namespace: "modem",
},
[]string{"hostname"},
)

DataDown = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "data_down",
Namespace: "modem",
},
[]string{"hostname"},
)

CurrentUp = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "current_up",
Namespace: "modem",
},
[]string{"hostname"},
)

CurrentDown = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "current_down",
Namespace: "modem",
},
[]string{"hostname"},
)

FECUp = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fec_up",
Namespace: "modem",
},
[]string{"hostname"},
)

FECDown = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fec_down",
Namespace: "modem",
},
[]string{"hostname"},
)

CRCUp = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "crc_up",
Namespace: "modem",
},
[]string{"hostname"},
)

CRCDown = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "crc_down",
Namespace: "modem",
},
[]string{"hostname"},
)

SNRUp = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "snr_up",
Namespace: "modem",
},
[]string{"hostname"},
)

SNRDown = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "snr_down",
Namespace: "modem",
},
[]string{"hostname"},
)

VoipStatus = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "voip_status",
Namespace: "modem",
},
[]string{"hostname"},
)

VoipCallStatus = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "voip_call_status",
Namespace: "modem",
},
[]string{"hostname"},
)
)

func Init() {
initMetric("modem_uptime", Uptime)
initMetric("modem_status", Status)
initMetric("modem_voip_status", VoipStatus)
initMetric("modem_voip_call_status", VoipCallStatus)
initMetric("modem_current_up", CurrentUp)
initMetric("modem_current_down", CurrentDown)
initMetric("modem_max_up", MaxUp)
initMetric("modem_max_down", MaxDown)
initMetric("modem_data_up", DataUp)
initMetric("modem_data_down", DataDown)
initMetric("modem_fec_up", FECUp)
initMetric("modem_fec_down", FECDown)
initMetric("modem_crc_up", CRCUp)
initMetric("modem_crc_down", CRCDown)
initMetric("modem_snr_up", SNRUp)
initMetric("modem_snr_down", SNRDown)
}

func initMetric(name string, metric *prometheus.GaugeVec) {
prometheus.MustRegister(metric)
log.Printf("New Prometheus metric registered: %s", name)
}
Loading

0 comments on commit 96db995

Please sign in to comment.