code stolen from project timeserver-monitoring
This commit is contained in:
parent
f2d0d1ca94
commit
3759f0d209
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,3 @@
|
|||||||
build
|
build
|
||||||
.DS_Store
|
.DS_Store
|
||||||
src/tsm/tsm
|
src/ntppm/ntppm
|
||||||
|
@ -4,10 +4,10 @@ steps:
|
|||||||
commands:
|
commands:
|
||||||
- GOPATH=/woodpecker/go
|
- GOPATH=/woodpecker/go
|
||||||
- ls -l
|
- ls -l
|
||||||
- cd src/tsm
|
- cd src/ntppm
|
||||||
- go mod tidy
|
- go mod tidy
|
||||||
- go build -a -installsuffix nocgo -o tsm tsm.go
|
- go build -a -installsuffix nocgo -o ntppm ntppm.go
|
||||||
- cp tsm ../..
|
- cp ntppm ../..
|
||||||
when:
|
when:
|
||||||
- event: [push, tag]
|
- event: [push, tag]
|
||||||
|
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
FROM scratch
|
FROM scratch
|
||||||
|
|
||||||
ENV TSM_MQTT_CONF ""
|
ENV NTPPM_MQTT_CONF ""
|
||||||
|
|
||||||
COPY tsm ./
|
COPY ntppm ./
|
||||||
ENTRYPOINT ["./tsm"]
|
ENTRYPOINT ["./ntppm"]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
74
snippets/test01.go
Normal file
74
snippets/test01.go
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Struktur für die JSON-Daten
|
||||||
|
type NTPResponse struct {
|
||||||
|
History []struct {
|
||||||
|
Timestamp int `json:"ts"`
|
||||||
|
Step int `json:"step"`
|
||||||
|
Score float64 `json:"score"`
|
||||||
|
MonitorID int `json:"monitor_id"`
|
||||||
|
} `json:"history"`
|
||||||
|
Monitors []struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Ts string `json:"ts"`
|
||||||
|
Score float64 `json:"score"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
} `json:"monitors"`
|
||||||
|
Server struct {
|
||||||
|
IP string `json:"ip"`
|
||||||
|
} `json:"server"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// URL für die API-Abfrage
|
||||||
|
url := "https://www.ntppool.org/scores/93.241.86.156/json?limit=1&monitor=24"
|
||||||
|
|
||||||
|
// HTTP-GET-Anfrage senden
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Fehler beim Abrufen der URL:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// Antwort lesen
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Fehler beim Lesen der Antwort:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// JSON-Daten in Struct einlesen
|
||||||
|
var data NTPResponse
|
||||||
|
err = json.Unmarshal(body, &data)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Fehler beim Parsen des JSON:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ausgabe der eingelesenen Daten
|
||||||
|
fmt.Println("NTP Server IP:", data.Server.IP)
|
||||||
|
if len(data.History) > 0 {
|
||||||
|
fmt.Println("Letzte Messung:")
|
||||||
|
fmt.Printf(" Zeitstempel: %d\n", data.History[0].Timestamp)
|
||||||
|
fmt.Printf(" Score: %.2f\n", data.History[0].Score)
|
||||||
|
fmt.Printf(" Monitor ID: %d\n", data.History[0].MonitorID)
|
||||||
|
}
|
||||||
|
if len(data.Monitors) > 0 {
|
||||||
|
fmt.Println("Monitor Info:")
|
||||||
|
fmt.Printf(" Name: %s\n", data.Monitors[0].Name)
|
||||||
|
fmt.Printf(" Score: %.2f\n", data.Monitors[0].Score)
|
||||||
|
fmt.Printf(" Status: %s\n", data.Monitors[0].Status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -28,7 +28,7 @@ type ConfigObject struct {
|
|||||||
var Config ConfigObject
|
var Config ConfigObject
|
||||||
|
|
||||||
func LoadConfiguration() {
|
func LoadConfiguration() {
|
||||||
cfg := os.Getenv("TSM_MQTT_CONF")
|
cfg := os.Getenv("NTPPM_MQTT_CONF")
|
||||||
log.Printf("cfg: %s", cfg)
|
log.Printf("cfg: %s", cfg)
|
||||||
err := json.Unmarshal([]byte(cfg), &Config)
|
err := json.Unmarshal([]byte(cfg), &Config)
|
||||||
if err != nil {
|
if err != nil {
|
@ -1,9 +1,8 @@
|
|||||||
module tsm
|
module ntppm
|
||||||
|
|
||||||
go 1.22.5
|
go 1.22.5
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/beevik/ntp v1.4.3
|
|
||||||
github.com/eclipse/paho.mqtt.golang v1.4.3
|
github.com/eclipse/paho.mqtt.golang v1.4.3
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
)
|
)
|
@ -5,14 +5,14 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
|
||||||
"tsm/config"
|
"ntppm/config"
|
||||||
"tsm/mqtt"
|
"ntppm/mqtt"
|
||||||
"tsm/tsmq"
|
"ntppm/ntppq"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.SetPrefix("TSM: ")
|
log.SetPrefix("NTPPM: ")
|
||||||
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
||||||
|
|
||||||
log.Println("starting")
|
log.Println("starting")
|
||||||
@ -22,8 +22,8 @@ func main() {
|
|||||||
mqtt.Start()
|
mqtt.Start()
|
||||||
defer mqtt.Stop()
|
defer mqtt.Stop()
|
||||||
|
|
||||||
tsmq.Start()
|
ntppq.Start()
|
||||||
defer tsmq.Stop()
|
defer ntppq.Stop()
|
||||||
|
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
signal.Notify(c, os.Interrupt, os.Kill)
|
signal.Notify(c, os.Interrupt, os.Kill)
|
@ -1,4 +1,4 @@
|
|||||||
package tsmq
|
package ntppq
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
@ -6,10 +6,8 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"tsm/config"
|
"ntppm/config"
|
||||||
"tsm/mqtt"
|
"ntppm/mqtt"
|
||||||
|
|
||||||
"github.com/beevik/ntp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type variable_t struct {
|
type variable_t struct {
|
Loading…
x
Reference in New Issue
Block a user