-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
282 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package email | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/danman113/gobeet/config" | ||
"github.com/danman113/gobeet/site" | ||
"html/template" | ||
"net/smtp" | ||
) | ||
|
||
var ( | ||
// Set from main | ||
Config config.EmailConfig | ||
auth smtp.Auth | ||
EmailPassword *string | ||
|
||
// Temp variables | ||
temp *template.Template | ||
) | ||
|
||
type TemplateData struct { | ||
Config config.EmailConfig | ||
Site *site.Page | ||
Error error | ||
} | ||
|
||
func parseTemplate() { | ||
if temp == nil { | ||
t, tErr := template.ParseFiles(Config.Template) | ||
if tErr != nil { | ||
fmt.Println("Template Parse Error: ") | ||
fmt.Println(tErr) | ||
return | ||
} | ||
temp = t | ||
} | ||
} | ||
|
||
func buildEmailBody(err error, pg *site.Page) ([]byte, error) { | ||
if temp == nil { | ||
parseTemplate() | ||
} | ||
|
||
buf := new(bytes.Buffer) | ||
|
||
eErr := temp.Execute(buf, TemplateData{Config, pg, err}) | ||
|
||
if eErr != nil { | ||
fmt.Println("Template Execute Error: ") | ||
fmt.Println(eErr) | ||
return nil, eErr | ||
} | ||
|
||
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n" | ||
return []byte(fmt.Sprintf("Subject: %s\r\n%s %s", "Gobeet Alert", mime, buf.String())), nil | ||
} | ||
|
||
func SendAlert(err error, pg *site.Page) { | ||
if auth == nil { | ||
auth = smtp.PlainAuth( | ||
"", | ||
Config.Address, | ||
*EmailPassword, | ||
Config.Server, | ||
) | ||
} | ||
|
||
body, bodyErr := buildEmailBody(err, pg) | ||
|
||
if bodyErr != nil { | ||
return | ||
} | ||
|
||
sendErr := smtp.SendMail( | ||
Config.Server+":"+Config.Port, | ||
auth, | ||
Config.Address, | ||
Config.Recipients, | ||
body, | ||
) | ||
if sendErr != nil { | ||
fmt.Println("Email Error!") | ||
fmt.Println(sendErr) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"github.com/danman113/gobeet/config" | ||
"github.com/danman113/gobeet/email" | ||
"github.com/danman113/gobeet/ping" | ||
// . "github.com/danman113/gobeet/re" | ||
// "runtime" | ||
// "time" | ||
) | ||
|
||
func main() { | ||
conf, _ := config.ParseConfigFile("sampleconfig.json") | ||
configPath := flag.String("config", "config.json", "Location for a config file") | ||
emailPassword := flag.String("password", "", "Password for your email account") | ||
flag.Parse() | ||
conf, err := config.ParseConfigFile(*configPath) | ||
if err != nil { | ||
fmt.Println("Could Not Parse Config File") | ||
fmt.Println("Error: ") | ||
fmt.Println(err) | ||
return | ||
} | ||
fmt.Println(conf.Email) | ||
email.Config = conf.Email | ||
email.EmailPassword = emailPassword | ||
// email.SendAlert(err, conf.Sites[0]) | ||
if err != nil { | ||
fmt.Println("Could not parse config file: " + *configPath) | ||
return | ||
} | ||
fmt.Println(*conf) | ||
for _, site := range conf.Sites { | ||
fmt.Println(site) | ||
for _, page := range site.Pages { | ||
fmt.Println(page) | ||
ret := make(chan error) | ||
ping.PingStatus(page, ret) | ||
for val := range ret { | ||
fmt.Println("Error: ") | ||
fomt.Println(val) | ||
} | ||
} | ||
for i, _ := range conf.Sites { | ||
go ping.PingWebsite(&conf.Sites[i]) | ||
} | ||
select {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,85 @@ | ||
{ | ||
"name": "daniel", | ||
"email": { | ||
"address": "[email protected]", | ||
"recipients": ["[email protected]"], | ||
"server": "smtp.gmail.com", | ||
"port": "587", | ||
"template": "sampletemplate.tmpl" | ||
}, | ||
"sites": [ | ||
{ | ||
"url": "www.github.com", | ||
"interval": 6000, | ||
"url": "www.dberezin.com", | ||
"interval": 60000, | ||
"pages": [ | ||
{ | ||
"status": 404, | ||
"status": 200, | ||
"url": "https://dberezin.com", | ||
"timeout": 2000, | ||
"method": "GET", | ||
"duration": 10000 | ||
}, { | ||
"status": 200, | ||
"url": "http://dberezin.com", | ||
"timeout": 200, | ||
"method": "GET" | ||
} | ||
"timeout": 2000, | ||
"method": "GET", | ||
"duration": 10000 | ||
}, { | ||
"status": 200, | ||
"url": "https://dberezin.com/portfolio", | ||
"timeout": 2000, | ||
"method": "GET", | ||
"duration": 10000 | ||
}, { | ||
"status": 200, | ||
"url": "https://dberezin.com/about", | ||
"timeout": 2000, | ||
"method": "GET", | ||
"duration": 10000 | ||
}, { | ||
"status": 200, | ||
"url": "https://dberezin.com/static/pdf/Resume.pdf", | ||
"timeout": 2000, | ||
"method": "GET", | ||
"duration": 10000 | ||
} | ||
] | ||
}, { | ||
"url": "www.viviannghiem.com", | ||
"interval": 60000, | ||
"pages": [ | ||
{ | ||
"status": 200, | ||
"url": "http://viviannghiem.com", | ||
"timeout": 2000, | ||
"method": "GET", | ||
"duration": 10000 | ||
}, { | ||
"status": 200, | ||
"url": "http://viviannghiem.com/pages/about", | ||
"timeout": 2000, | ||
"method": "GET", | ||
"duration": 10000 | ||
}, { | ||
"status": 200, | ||
"url": "http://viviannghiem.com/pages/portfolio", | ||
"timeout": 2000, | ||
"method": "GET", | ||
"duration": 10000 | ||
}, { | ||
"status": 200, | ||
"url": "http://viviannghiem.com/pages/", | ||
"timeout": 2000, | ||
"method": "GET", | ||
"duration": 10000 | ||
}, { | ||
"status": 404, | ||
"url": "http://viviannghiem.com/pages/adsfasfdgsdf", | ||
"timeout": 2000, | ||
"method": "GET", | ||
"duration": 10000 | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>Gobeet Error</title> | ||
<style> | ||
body { | ||
font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif; | ||
font-size: 100%; | ||
margin: 0; | ||
} | ||
.header { | ||
background-color: #e9ecef; | ||
padding: 15px; | ||
} | ||
.content { | ||
padding: 15px; | ||
} | ||
.pre { | ||
font-family: Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; | ||
background-color: aliceblue; | ||
padding: 15px; | ||
} | ||
a { | ||
color: #3097D1; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="header"> | ||
<h1>Your website, <a href="{{.Site.Url}}">{{.Site.Url}}</a> is down!</h1> | ||
</div> | ||
<div class="content"> | ||
<h3>The error is: </h3> | ||
<div class="pre"> | ||
{{.Error}} | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,15 @@ | ||
package site | ||
|
||
import ( | ||
// "time" | ||
) | ||
|
||
type Website struct { | ||
Url string `json: url` | ||
Interval int `json: interval` | ||
Pages []Page `json: pages` | ||
} | ||
|
||
type Page struct { | ||
Url string `json: url` | ||
Status int `json: status` | ||
Method string `json: method` | ||
Timeout int `json: timeout` | ||
Url string `json: url` | ||
Status int `json: status` | ||
Method string `json: method` | ||
Timeout int `json: timeout` | ||
Duration int `json: duration` | ||
} |