watchdog-go #2

Merged
usbharu merged 4 commits from watchdog-go into master 2024-03-13 01:58:54 +00:00
1 changed files with 97 additions and 10 deletions
Showing only changes of commit c9bc100bcd - Show all commits

View File

@ -50,14 +50,22 @@ func main() {
go func() { go func() {
for { for {
metricArray := getMetrics(config) for _, metric := range perMinute(config) {
for _, metric := range metricArray {
metrics <- metric metrics <- metric
} }
time.Sleep(1 * time.Minute) time.Sleep(1 * time.Minute)
} }
}() }()
go func() {
for {
for _, metric := range perDay(config) {
metrics <- metric
}
time.Sleep(24 * time.Hour)
}
}()
for metric := range metrics { for metric := range metrics {
_, err := client.Push(context.Background(), &metric) _, err := client.Push(context.Background(), &metric)
if err != nil { if err != nil {
@ -67,19 +75,32 @@ func main() {
} }
} }
func getMetrics(config Config) []watchdog.Metric { func perMinute(config Config) []watchdog.Metric {
v, _ := mem.VirtualMemory() return []watchdog.Metric{
*usedMemory(config),
return []watchdog.Metric{usedMemory(*v, config)} *usedSwap(config),
}
} }
func usedMemory(stat mem.VirtualMemoryStat, config Config) watchdog.Metric { func perDay(config Config) []watchdog.Metric {
return []watchdog.Metric{
*totalMem(config),
*totalSwap(config),
}
}
func usedMemory(config Config) *watchdog.Metric {
memory, err := mem.VirtualMemory()
if err != nil {
log.Println(err)
return nil
}
name := config.ClientName + " Used Memory" name := config.ClientName + " Used Memory"
usedMemory := fmt.Sprintf("%g", stat.UsedPercent) usedMemory := fmt.Sprintf("%g", memory.UsedPercent)
log.Printf("Mem: %s%%\n", usedMemory) log.Printf("Used Mem: %s%%\n", usedMemory)
return watchdog.Metric{ return &watchdog.Metric{
Name: name, Name: name,
ObjectId: toId(name), ObjectId: toId(name),
Domain: config.ClientDomain, Domain: config.ClientDomain,
@ -90,6 +111,72 @@ func usedMemory(stat mem.VirtualMemoryStat, config Config) watchdog.Metric {
} }
} }
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: "",
}
}
func toId(value string) string { func toId(value string) string {
return regex.ReplaceAllString(strings.ReplaceAll(value, " ", "-"), "") return regex.ReplaceAllString(strings.ReplaceAll(value, " ", "-"), "")
} }