Skip to content

Commit

Permalink
Merge pull request #82 from sooraj-sky/dns
Browse files Browse the repository at this point in the history
closes #72; fixed group email sending issue
  • Loading branch information
sooraj-sky authored Feb 26, 2023
2 parents 4136053 + 7f534d7 commit abdbfb9
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 25 deletions.
15 changes: 10 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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=<your-value-here>")
}
db, err := gorm.Open(postgres.New(postgres.Config{
DSN: dbconnect,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/sooraj-sky/sky-meter
module sky-meter

go 1.18

Expand Down
1 change: 1 addition & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ type UserInput struct {
Port int
Sender string
}

Groups []struct {
Name string
Emails []string
Expand Down
4 changes: 3 additions & 1 deletion packages/alerts/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand Down
24 changes: 15 additions & 9 deletions packages/dbops/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]})
}
}

Expand All @@ -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++ {
Expand All @@ -63,14 +63,20 @@ 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()
AlertStatus := skyalerts.CheckAlertStatus(alertStatus.RequestId)
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)
}
Expand All @@ -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})
Expand Down
2 changes: 1 addition & 1 deletion packages/dns/skydns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 8 additions & 5 deletions packages/httpres/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion packages/httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion packages/jsonops/jsonops.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io/ioutil"
"os"

models "github.com/sooraj-sky/sky-meter/models"
models "sky-meter/models"
)

func InputJson() models.JsonInput {
Expand Down
2 changes: 1 addition & 1 deletion packages/yamlops/yamlops.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down
2 changes: 2 additions & 0 deletions settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ email:
server: smtp.gmail.com
port: 587
sender: linux.sooraj@gmail.com

groups:
- name: prod
emails:
Expand All @@ -14,6 +15,7 @@ groups:
emails:
- onlyworkofficial@gmail.com
- ops@skywalks.in

domains:
- name: https://skywalks.in
enabled: true
Expand Down

0 comments on commit abdbfb9

Please sign in to comment.