aboutsummaryrefslogtreecommitdiff
path: root/resources_controller.go
blob: 0576e0f3f2fe4ec2beb5e3b377bce0a7fa9b45d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * This file is part of caronte (https://github.com/eciavatta/caronte).
 * Copyright (c) 2020 Emiliano Ciavatta.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package main

import (
	"context"
	"github.com/gin-gonic/gin"
	"github.com/shirou/gopsutil/cpu"
	"github.com/shirou/gopsutil/disk"
	"github.com/shirou/gopsutil/mem"
	log "github.com/sirupsen/logrus"
	"sync"
	"time"
)

const (
	averageCPUPercentAlertThreshold = 90.0
	averageCPUPercentAlertMinInterval = 120.0
)

type SystemStats struct {
	VirtualMemory *mem.VirtualMemoryStat `json:"virtual_memory"`
	CPUTimes      []cpu.TimesStat        `json:"cpu_times"`
	CPUPercents   []float64              `json:"cpu_percents"`
	DiskUsage     *disk.UsageStat        `json:"disk_usage"`
}

type ResourcesController struct {
	notificationController *NotificationController
	lastCPUPercent         []float64
	mutex                  sync.Mutex
}

func NewResourcesController(notificationController *NotificationController) *ResourcesController {
	return &ResourcesController{
		notificationController: notificationController,
	}
}

func (csc *ResourcesController) GetProcessStats(c context.Context) interface{} {
	return nil
}

func (csc *ResourcesController) GetSystemStats(c context.Context) SystemStats {
	virtualMemory, err := mem.VirtualMemoryWithContext(c)
	if err != nil {
		log.WithError(err).Panic("failed to retrieve virtual memory")
	}
	cpuTimes, err := cpu.TimesWithContext(c, true)
	if err != nil {
		log.WithError(err).Panic("failed to retrieve cpu times")
	}
	diskUsage, err := disk.UsageWithContext(c, "/")
	if err != nil {
		log.WithError(err).Panic("failed to retrieve disk usage")
	}

	defer csc.mutex.Unlock()
	csc.mutex.Lock()

	return SystemStats{
		VirtualMemory: virtualMemory,
		CPUTimes:      cpuTimes,
		DiskUsage:     diskUsage,
		CPUPercents:   csc.lastCPUPercent,
	}
}

func (csc *ResourcesController) Run() {
	interval, _ := time.ParseDuration("3s")
	var lastAlertTime time.Time

	for {
		cpuPercent, err := cpu.Percent(interval, true)
		if err != nil {
			log.WithError(err).Error("failed to retrieve cpu percent")
			return
		}

		csc.mutex.Lock()
		csc.lastCPUPercent = cpuPercent
		csc.mutex.Unlock()

		avg := Average(cpuPercent)
		if avg > averageCPUPercentAlertThreshold && time.Now().Sub(lastAlertTime).Seconds() > averageCPUPercentAlertMinInterval {
			csc.notificationController.Notify("resources.cpu_alert", gin.H{
				"cpu_percent": cpuPercent,
			})
			log.WithField("cpu_percent", cpuPercent).Warn("cpu percent usage has exceeded the limit threshold")
			lastAlertTime = time.Now()
		}
	}
}