2024-03-10 11:42:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"git.usbharu.dev/usbharu/unos/watchdog-go/git.usbharu.dev/usbharu/unos/watchdog"
|
|
|
|
"github.com/shirou/gopsutil/v3/mem"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
"gopkg.in/ini.v1"
|
|
|
|
"log"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Url string
|
|
|
|
ClientName string
|
|
|
|
ClientDomain string
|
|
|
|
}
|
|
|
|
|
2024-03-11 06:48:32 +00:00
|
|
|
var regex = regexp.MustCompile(`[^a-zA-Z0-9-_]`)
|
2024-03-10 11:42:27 +00:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
fmt.Println("start gRPC Client.")
|
|
|
|
|
|
|
|
load, err := ini.Load("config.ini")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
config := Config{
|
|
|
|
Url: load.Section("Parent").Key("url").String(),
|
|
|
|
ClientName: load.Section("Watch").Key("name").MustString("Watch Dog Go"),
|
|
|
|
ClientDomain: load.Section("Watch").Key("domain").MustString("watchdog.internal"),
|
|
|
|
}
|
|
|
|
|
|
|
|
dial, err := grpc.Dial(config.Url, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics := make(chan watchdog.Metric)
|
|
|
|
|
|
|
|
client := watchdog.NewPushMetricsServiceClient(dial)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2024-03-11 11:12:34 +00:00
|
|
|
for _, metric := range perMinute(config) {
|
2024-03-10 11:42:27 +00:00
|
|
|
metrics <- metric
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Minute)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-03-11 11:12:34 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
for _, metric := range perDay(config) {
|
|
|
|
metrics <- metric
|
|
|
|
}
|
|
|
|
time.Sleep(24 * time.Hour)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-03-10 11:42:27 +00:00
|
|
|
for metric := range metrics {
|
|
|
|
_, err := client.Push(context.Background(), &metric)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-11 11:12:34 +00:00
|
|
|
func perMinute(config Config) []watchdog.Metric {
|
|
|
|
return []watchdog.Metric{
|
|
|
|
*usedMemory(config),
|
|
|
|
*usedSwap(config),
|
|
|
|
}
|
|
|
|
}
|
2024-03-10 11:42:27 +00:00
|
|
|
|
2024-03-11 11:12:34 +00:00
|
|
|
func perDay(config Config) []watchdog.Metric {
|
|
|
|
return []watchdog.Metric{
|
|
|
|
*totalMem(config),
|
|
|
|
*totalSwap(config),
|
|
|
|
}
|
2024-03-10 11:42:27 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 11:12:34 +00:00
|
|
|
func usedMemory(config Config) *watchdog.Metric {
|
|
|
|
memory, err := mem.VirtualMemory()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
2024-03-10 11:42:27 +00:00
|
|
|
name := config.ClientName + " Used Memory"
|
2024-03-11 11:12:34 +00:00
|
|
|
usedMemory := fmt.Sprintf("%g", memory.UsedPercent)
|
2024-03-10 11:42:27 +00:00
|
|
|
|
2024-03-11 11:12:34 +00:00
|
|
|
log.Printf("Used Mem: %s%%\n", usedMemory)
|
2024-03-10 11:42:27 +00:00
|
|
|
|
2024-03-11 11:12:34 +00:00
|
|
|
return &watchdog.Metric{
|
2024-03-10 11:42:27 +00:00
|
|
|
Name: name,
|
|
|
|
ObjectId: toId(name),
|
|
|
|
Domain: config.ClientDomain,
|
|
|
|
Status: watchdog.Status_OK,
|
|
|
|
Value: usedMemory,
|
|
|
|
Timestamp: timestamppb.Now(),
|
2024-03-11 07:36:08 +00:00
|
|
|
Message: "",
|
2024-03-10 11:42:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-11 11:12:34 +00:00
|
|
|
func usedSwap(config Config) *watchdog.Metric {
|
|
|
|
memory, err := mem.VirtualMemory()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
name := config.ClientName + " Used Swap"
|
|
|
|
usedSwap := fmt.Sprintf("%g", memory.UsedPercent)
|
|
|
|
|
|
|
|
log.Printf("Used Swap: %s%%\n", usedSwap)
|
|
|
|
|
|
|
|
return &watchdog.Metric{
|
|
|
|
Name: name,
|
|
|
|
ObjectId: toId(name),
|
|
|
|
Domain: config.ClientDomain,
|
|
|
|
Status: watchdog.Status_OK,
|
|
|
|
Value: usedSwap,
|
|
|
|
Timestamp: timestamppb.Now(),
|
|
|
|
Message: "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func totalMem(config Config) *watchdog.Metric {
|
|
|
|
memory, err := mem.VirtualMemory()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
name := config.ClientName + " Total Mem"
|
|
|
|
totalMemory := fmt.Sprintf("%d", memory.Total)
|
|
|
|
|
|
|
|
log.Printf("Total Mem: %s%%\n", totalMemory)
|
|
|
|
|
|
|
|
return &watchdog.Metric{
|
|
|
|
Name: name,
|
|
|
|
ObjectId: toId(name),
|
|
|
|
Domain: config.ClientDomain,
|
|
|
|
Status: watchdog.Status_OK,
|
|
|
|
Value: totalMemory,
|
|
|
|
Timestamp: timestamppb.Now(),
|
|
|
|
Message: "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func totalSwap(config Config) *watchdog.Metric {
|
|
|
|
memory, err := mem.SwapMemory()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
name := config.ClientName + " Total Swap"
|
|
|
|
totalMemory := fmt.Sprintf("%d", memory.Total)
|
|
|
|
|
|
|
|
log.Printf("Total Swap: %s%%\n", totalMemory)
|
|
|
|
|
|
|
|
return &watchdog.Metric{
|
|
|
|
Name: name,
|
|
|
|
ObjectId: toId(name),
|
|
|
|
Domain: config.ClientDomain,
|
|
|
|
Status: watchdog.Status_OK,
|
|
|
|
Value: totalMemory,
|
|
|
|
Timestamp: timestamppb.Now(),
|
|
|
|
Message: "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-10 11:42:27 +00:00
|
|
|
func toId(value string) string {
|
|
|
|
return regex.ReplaceAllString(strings.ReplaceAll(value, " ", "-"), "")
|
|
|
|
}
|