From 3c06179184d13ec22b463778541fe3b82e842c16 Mon Sep 17 00:00:00 2001
From: kreedom <60944649+kreedom@users.noreply.github.com>
Date: Sun, 16 Feb 2020 20:59:02 +0200
Subject: [PATCH] basic vmalert backbone (#317)

* basic vmalert backbone

* Resolve code review comments for vmalert backbone

* Second review fixes for vmalert backbone
---
 app/vmalert/config/parser.go         | 34 ++++++++++++++++
 app/vmalert/datasource/datasource.go | 14 +++++++
 app/vmalert/main.go                  | 58 ++++++++++++++++++++++++++++
 app/vmalert/provider/alertmanager.go | 11 ++++++
 app/vmalert/provider/common.go       |  8 ++++
 5 files changed, 125 insertions(+)
 create mode 100644 app/vmalert/config/parser.go
 create mode 100644 app/vmalert/datasource/datasource.go
 create mode 100644 app/vmalert/main.go
 create mode 100644 app/vmalert/provider/alertmanager.go
 create mode 100644 app/vmalert/provider/common.go

diff --git a/app/vmalert/config/parser.go b/app/vmalert/config/parser.go
new file mode 100644
index 0000000000..3f1a8081dd
--- /dev/null
+++ b/app/vmalert/config/parser.go
@@ -0,0 +1,34 @@
+package config
+
+import "time"
+
+// Labels basic struct of different labels
+type Labels struct {
+	Severity string
+}
+
+// Annotations basic annotation for alert rule
+type Annotations struct {
+	Summary     string
+	Description string
+}
+
+// Alert basic alert entity rule
+type Alert struct {
+	Name        string
+	Expr        string
+	For         time.Duration
+	Labels      Labels
+	Annotations Annotations
+}
+
+// Group grouping array of alert
+type Group struct {
+	Name  string
+	Rules []Alert
+}
+
+// Parse parses config from given file
+func Parse(filepath string) ([]Group, error) {
+	return []Group{}, nil
+}
diff --git a/app/vmalert/datasource/datasource.go b/app/vmalert/datasource/datasource.go
new file mode 100644
index 0000000000..d68b7a4d4e
--- /dev/null
+++ b/app/vmalert/datasource/datasource.go
@@ -0,0 +1,14 @@
+package datasource
+
+import "context"
+
+// Metrics the data returns from storage
+type Metrics struct{}
+
+// VMStorage represents vmstorage entity with ability to read and write metrics
+type VMStorage struct{}
+
+//Query basic query to the datasource
+func (s *VMStorage) Query(ctx context.Context, query string) ([]Metrics, error) {
+	return nil, nil
+}
diff --git a/app/vmalert/main.go b/app/vmalert/main.go
new file mode 100644
index 0000000000..9b3da62eb4
--- /dev/null
+++ b/app/vmalert/main.go
@@ -0,0 +1,58 @@
+package main
+
+import (
+	"flag"
+	"net/http"
+
+	"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/config"
+	"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/datasource"
+	"github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo"
+	"github.com/VictoriaMetrics/VictoriaMetrics/lib/envflag"
+	"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
+	"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
+	"github.com/VictoriaMetrics/VictoriaMetrics/lib/procutil"
+)
+
+var (
+	configPath     = flag.String("config", "config.yaml", "Path to alert configuration file")
+	httpListenAddr = flag.String("httpListenAddr", ":8880", "Address to listen for http connections")
+)
+
+func main() {
+	envflag.Parse()
+	buildinfo.Init()
+	logger.Init()
+
+	logger.Infof("reading alert rules configuration file from %s", *configPath)
+	alertGroups, err := config.Parse(*configPath)
+	if err != nil {
+		logger.Fatalf("Cannot parse configuration file %s", err)
+	}
+	w := &watchdog{storage: &datasource.VMStorage{}}
+	for id := range alertGroups {
+		go func(group config.Group) {
+			w.run(group)
+		}(alertGroups[id])
+	}
+	go func() {
+		httpserver.Serve(*httpListenAddr, func(w http.ResponseWriter, r *http.Request) bool {
+			panic("not implemented")
+		})
+	}()
+	sig := procutil.WaitForSigterm()
+	logger.Infof("service received signal %s", sig)
+	httpserver.Stop(*httpListenAddr)
+	w.stop()
+}
+
+type watchdog struct {
+	storage *datasource.VMStorage
+}
+
+func (w *watchdog) run(a config.Group) {
+
+}
+
+func (w *watchdog) stop() {
+	panic("not implemented")
+}
diff --git a/app/vmalert/provider/alertmanager.go b/app/vmalert/provider/alertmanager.go
new file mode 100644
index 0000000000..47f229cff6
--- /dev/null
+++ b/app/vmalert/provider/alertmanager.go
@@ -0,0 +1,11 @@
+package provider
+
+import "github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/config"
+
+// AlertManager represents integration provider with Prometheus alert manager
+type AlertManager struct{}
+
+// Send an alert or resolve message
+func (a *AlertManager) Send(rule config.Alert) error {
+	return nil
+}
diff --git a/app/vmalert/provider/common.go b/app/vmalert/provider/common.go
new file mode 100644
index 0000000000..6adfc39eee
--- /dev/null
+++ b/app/vmalert/provider/common.go
@@ -0,0 +1,8 @@
+package provider
+
+import "github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/config"
+
+// AlertProvider is common interface for alert manager provider
+type AlertProvider interface {
+	Send(rule config.Alert) error
+}