watchdog-go: Swap等を監視するように

This commit is contained in:
usbharu 2024-03-11 20:12:34 +09:00
parent 6314bb6d99
commit c9bc100bcd
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
1 changed files with 97 additions and 10 deletions

View File

@ -50,14 +50,22 @@ func main() {
go func() {
for {
metricArray := getMetrics(config)
for _, metric := range metricArray {
for _, metric := range perMinute(config) {
metrics <- metric
}
time.Sleep(1 * time.Minute)
}
}()
go func() {
for {
for _, metric := range perDay(config) {
metrics <- metric
}
time.Sleep(24 * time.Hour)
}
}()
for metric := range metrics {
_, err := client.Push(context.Background(), &metric)
if err != nil {
@ -67,19 +75,32 @@ func main() {
}
}
func getMetrics(config Config) []watchdog.Metric {
v, _ := mem.VirtualMemory()
return []watchdog.Metric{usedMemory(*v, config)}
func perMinute(config Config) []watchdog.Metric {
return []watchdog.Metric{
*usedMemory(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"
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,
ObjectId: toId(name),
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 {
return regex.ReplaceAllString(strings.ReplaceAll(value, " ", "-"), "")
}