diff options
| author | Fuwn <[email protected]> | 2026-01-17 23:17:49 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-01-17 23:17:49 -0800 |
| commit | 4bc6165258cd7b5b76ccb01aa75c7cefdc35d143 (patch) | |
| tree | e7c3bb335a1efd48f82d365169e8b4a66b7abe1d /internal/monitor/monitor.go | |
| download | kaze-4bc6165258cd7b5b76ccb01aa75c7cefdc35d143.tar.xz kaze-4bc6165258cd7b5b76ccb01aa75c7cefdc35d143.zip | |
feat: Initial commit
Diffstat (limited to 'internal/monitor/monitor.go')
| -rw-r--r-- | internal/monitor/monitor.go | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/internal/monitor/monitor.go b/internal/monitor/monitor.go new file mode 100644 index 0000000..4f4ab0f --- /dev/null +++ b/internal/monitor/monitor.go @@ -0,0 +1,86 @@ +package monitor + +import ( + "context" + "time" + + "github.com/Fuwn/kaze/internal/config" + "github.com/Fuwn/kaze/internal/storage" +) + +// Result represents the outcome of a monitor check +type Result struct { + MonitorName string + Timestamp time.Time + Status Status + ResponseTime time.Duration + StatusCode int // HTTP status code (0 for non-HTTP) + Error error + SSLExpiry *time.Time + SSLDaysLeft int +} + +// Status represents the status of a monitor +type Status string + +const ( + StatusUp Status = "up" + StatusDown Status = "down" + StatusDegraded Status = "degraded" +) + +// Monitor is the interface that all monitor types must implement +type Monitor interface { + // Name returns the monitor's name + Name() string + + // Type returns the monitor type (http, https, tcp) + Type() string + + // Target returns the monitor target (URL or host:port) + Target() string + + // Interval returns the check interval + Interval() time.Duration + + // Check performs the monitoring check and returns the result + Check(ctx context.Context) *Result +} + +// New creates a new monitor based on the configuration +func New(cfg config.MonitorConfig) (Monitor, error) { + switch cfg.Type { + case "http", "https": + return NewHTTPMonitor(cfg) + case "tcp": + return NewTCPMonitor(cfg) + default: + return nil, &UnsupportedTypeError{Type: cfg.Type} + } +} + +// UnsupportedTypeError is returned when an unknown monitor type is specified +type UnsupportedTypeError struct { + Type string +} + +func (e *UnsupportedTypeError) Error() string { + return "unsupported monitor type: " + e.Type +} + +// ToCheckResult converts a monitor Result to a storage CheckResult +func (r *Result) ToCheckResult() *storage.CheckResult { + cr := &storage.CheckResult{ + MonitorName: r.MonitorName, + Timestamp: r.Timestamp, + Status: string(r.Status), + ResponseTime: r.ResponseTime.Milliseconds(), + StatusCode: r.StatusCode, + SSLExpiry: r.SSLExpiry, + SSLDaysLeft: r.SSLDaysLeft, + } + if r.Error != nil { + cr.Error = r.Error.Error() + } + return cr +} |