diff --git a/cmd/main.go b/cmd/main.go index 6d5d8f4..330254b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -2,22 +2,27 @@ package main import ( "github.com/jasonlvhit/gocron" - dbops "github.com/sooraj-sky/sky-meter/packages/dbops" - skymeter "github.com/sooraj-sky/sky-meter/packages/httpserver" - sentry "github.com/sooraj-sky/sky-meter/packages/logger" - yamlops "github.com/sooraj-sky/sky-meter/packages/yamlops" "gorm.io/driver/postgres" "gorm.io/gorm" "log" "os" + dbops "sky-meter/packages/dbops" + skymeter "sky-meter/packages/httpserver" + sentry "sky-meter/packages/logger" + yamlops "sky-meter/packages/yamlops" ) func main() { log.Println("Launching sky-meter") sentry.SentryInit() dbconnect := os.Getenv("dbconnect") + opsgenieSecret := os.Getenv("opsgeniesecret") if opsgenieSecret == "" { - log.Fatal("Please specify the opsgeniesecret as environment variable, e.g. sooraj@sky:~/go/src/sky-meter$ export dbconnect=host=localhost user=postgres password=postgres dbname=postgres port=5433 sslmode=disable") + log.Fatal("Please specify the opsgeniesecret as environment variable, e.g. export dbconnect=host=localhost user=postgres password=postgres dbname=postgres port=5433 sslmode=disable") + } + + if opsgenieSecret == "" { + log.Fatal("Please specify the opsgeniesecret as environment variable, e.g. export opsgeniesecret=") } db, err := gorm.Open(postgres.New(postgres.Config{ DSN: dbconnect, diff --git a/go.mod b/go.mod index 10739a7..2537951 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/sooraj-sky/sky-meter +module sky-meter go 1.18 diff --git a/models/models.go b/models/models.go index e6c419b..c8e1ec9 100644 --- a/models/models.go +++ b/models/models.go @@ -106,6 +106,7 @@ type UserInput struct { Port int Sender string } + Groups []struct { Name string Emails []string diff --git a/packages/alerts/alert.go b/packages/alerts/alert.go index 31697cb..81daf44 100644 --- a/packages/alerts/alert.go +++ b/packages/alerts/alert.go @@ -3,13 +3,13 @@ package skyalerts import ( "bytes" "encoding/json" - models "github.com/sooraj-sky/sky-meter/models" gomail "gopkg.in/gomail.v2" "html/template" "io/ioutil" "log" "net/http" "os" + models "sky-meter/models" "github.com/opsgenie/opsgenie-go-sdk-v2/alert" "github.com/opsgenie/opsgenie-go-sdk-v2/client" @@ -18,6 +18,8 @@ import ( func SendMail(i models.SmtpErr) { + log.Println(i.Mailto, "mailto") + emailPass := os.Getenv("emailpass") if emailPass == "" { log.Fatal("Please specify the emailpass as environment variable, e.g. env emailpass=your-pass go run http-server.go") diff --git a/packages/dbops/db.go b/packages/dbops/db.go index 16965c4..9283eff 100644 --- a/packages/dbops/db.go +++ b/packages/dbops/db.go @@ -2,10 +2,10 @@ package dbops import ( "encoding/json" - models "github.com/sooraj-sky/sky-meter/models" - skyalerts "github.com/sooraj-sky/sky-meter/packages/alerts" - httpreponser "github.com/sooraj-sky/sky-meter/packages/httpres" "log" + models "sky-meter/models" + skyalerts "sky-meter/packages/alerts" + httpreponser "sky-meter/packages/httpres" "time" "gorm.io/gorm" @@ -37,8 +37,8 @@ func InsertUrlsToDb(db *gorm.DB, endpoints models.UserInput) { for i := range endpoints.Groups { db.Where("NAME=?", endpoints.Groups[i].Name).Find(&Groups) for k := range endpoints.Groups[i].Emails { - if Groups.CreatedAt == 0 && Groups.Email != endpoints.Groups[1].Emails[k] { - db.Create(&models.AlertGroups{Name: endpoints.Groups[i].Name, Email: endpoints.Groups[1].Emails[k]}) + if Groups.CreatedAt == 0 && Groups.Email != endpoints.Groups[i].Emails[k] { + db.Create(&models.AlertGroups{Name: endpoints.Groups[i].Name, Email: endpoints.Groups[i].Emails[k]}) } } @@ -50,8 +50,8 @@ func GetUrlFrequency(db *gorm.DB) { var urlsToCheck []models.AllEndpoints var urlsId []models.AllEndpoints var alertStatus models.OpsgenieAlertData - var GroupsEmailIds models.AlertGroups - var Empty models.AlertGroups + var GroupsEmailIds []models.AlertGroups + var Empty []models.AlertGroups db.Find(&urlsToCheck) for i := 0; i < len(urlsToCheck); i++ { @@ -63,6 +63,12 @@ func GetUrlFrequency(db *gorm.DB) { if err != nil { db.First(&alertStatus, "url = ?", urlsToCheck[i].URL) db.Where("Name=?", urlsToCheck[i].Group).Find(&GroupsEmailIds) + var emailIds []string + for _, group := range GroupsEmailIds { + emailIds = append(emailIds, group.Email) + } + + log.Println(GroupsEmailIds, "email") if alertStatus.URL == urlsToCheck[i].URL { dt := time.Now() @@ -70,7 +76,7 @@ func GetUrlFrequency(db *gorm.DB) { if (AlertStatus == "closed") || (alertStatus.Error != err.Error()) { alertReqId := skyalerts.OpsgenieCreateAlert(urlsToCheck[i].URL, err, urlsToCheck[i].Group) db.Model(&alertStatus).Where("url = ?", urlsToCheck[i].URL).Update("request_id", alertReqId) - d := models.SmtpErr{urlsToCheck[i].URL, "webiste is Down", dt, err.Error(), []string{GroupsEmailIds.Email}} + d := models.SmtpErr{urlsToCheck[i].URL, "webiste is Down", dt, err.Error(), emailIds} skyalerts.SendMail(d) } @@ -79,7 +85,7 @@ func GetUrlFrequency(db *gorm.DB) { dts := time.Now() db.Create(&models.HttpOutput{OutputData: httpOutput, URL: urlsToCheck[i].URL, StatusCode: HttpStatusCode, Error: err.Error()}) - d := models.SmtpErr{urlsToCheck[i].URL, "webiste is Down", dts, err.Error(), []string{GroupsEmailIds.Email}} + d := models.SmtpErr{urlsToCheck[i].URL, "webiste is Down", dts, err.Error(), emailIds} alertReqId := skyalerts.OpsgenieCreateAlert(urlsToCheck[i].URL, err, urlsToCheck[i].Group) db.Create(&models.OpsgenieAlertData{URL: urlsToCheck[i].URL, RequestId: alertReqId, Error: err.Error(), Active: true}) diff --git a/packages/dns/skydns.go b/packages/dns/skydns.go index 3b14292..7f3c695 100644 --- a/packages/dns/skydns.go +++ b/packages/dns/skydns.go @@ -7,7 +7,7 @@ import ( ) // Create a custom Resolver that uses a specific DNS server IP -func customResolver(dnsServer string) *net.Resolver { +func CustomResolver(dnsServer string) *net.Resolver { return &net.Resolver{ PreferGo: true, Dial: func(ctx context.Context, network, address string) (net.Conn, error) { diff --git a/packages/httpres/trace.go b/packages/httpres/trace.go index 17559b2..a80cdbd 100644 --- a/packages/httpres/trace.go +++ b/packages/httpres/trace.go @@ -3,13 +3,15 @@ package httpreponser import ( "crypto/tls" "encoding/json" + "fmt" "log" "net/http" "net/http/httptrace" + "os" "time" - models "github.com/sooraj-sky/sky-meter/models" - skydns "github.com/sooraj-sky/sky-meter/packages/skydns" + models "sky-meter/models" + skydns "sky-meter/packages/dns" ) func GetHttpdata(url string, timeout time.Duration, SkipSsl bool) (httpdata []byte, httpstatuscode int, errs error) { @@ -55,12 +57,13 @@ func tlsConfig() *tls.Config { } func trace() (*httptrace.ClientTrace, *models.Debug) { + //DNS settings + dnsServer := os.Getenv("dnsserver") // Replace with your desired DNS server IP address + resolver := skydns.CustomResolver(dnsServer) + fmt.Sprintln(resolver) d := &models.Debug{} t := &httptrace.ClientTrace{ - //DNS settings - dnsServer := "8.8.8.8" // Replace with your desired DNS server IP address - skydns.resolver := customResolver(dnsServer) DNSStart: func(info httptrace.DNSStartInfo) { t := time.Now().UTC().String() diff --git a/packages/httpserver/server.go b/packages/httpserver/server.go index e6c1ec5..f24087b 100644 --- a/packages/httpserver/server.go +++ b/packages/httpserver/server.go @@ -6,7 +6,7 @@ import ( "os" "github.com/gorilla/mux" - api "github.com/sooraj-sky/sky-meter/packages/api" + api "sky-meter/packages/api" ) func InitServer() { diff --git a/packages/jsonops/jsonops.go b/packages/jsonops/jsonops.go index 3b79603..0bb4d90 100644 --- a/packages/jsonops/jsonops.go +++ b/packages/jsonops/jsonops.go @@ -6,7 +6,7 @@ import ( "io/ioutil" "os" - models "github.com/sooraj-sky/sky-meter/models" + models "sky-meter/models" ) func InputJson() models.JsonInput { diff --git a/packages/yamlops/yamlops.go b/packages/yamlops/yamlops.go index b1a2841..5f05fd1 100644 --- a/packages/yamlops/yamlops.go +++ b/packages/yamlops/yamlops.go @@ -1,12 +1,12 @@ package yamlops import ( - models "github.com/sooraj-sky/sky-meter/models" "gopkg.in/yaml.v2" "io/ioutil" "log" "os" "path/filepath" + models "sky-meter/models" "strconv" ) diff --git a/settings.yml b/settings.yml index 879534e..5412c1d 100644 --- a/settings.yml +++ b/settings.yml @@ -5,6 +5,7 @@ email: server: smtp.gmail.com port: 587 sender: linux.sooraj@gmail.com + groups: - name: prod emails: @@ -14,6 +15,7 @@ groups: emails: - onlyworkofficial@gmail.com - ops@skywalks.in + domains: - name: https://skywalks.in enabled: true