2019-05-01 09:46:52 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-05-26 07:12:30 +00:00
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
2019-05-01 09:46:52 +00:00
|
|
|
"github.com/jordan-wright/email"
|
|
|
|
"github.com/whiteshtef/clockwork"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/smtp"
|
|
|
|
)
|
|
|
|
|
2019-05-26 07:12:30 +00:00
|
|
|
type report struct {
|
2019-05-27 13:03:30 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Time string `json:"time"`
|
|
|
|
Query string `json:"query"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
To string `json:"to"`
|
|
|
|
TGUserId int64 `json:"tgUserId"`
|
2019-05-26 07:12:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-26 20:32:02 +00:00
|
|
|
func startReports() {
|
2019-05-01 09:46:52 +00:00
|
|
|
scheduler := clockwork.NewScheduler()
|
|
|
|
for _, r := range appConfig.Reports {
|
2019-05-01 10:50:09 +00:00
|
|
|
scheduledReport := r
|
|
|
|
scheduler.Schedule().Every().Day().At(scheduledReport.Time).Do(func() {
|
|
|
|
executeReport(&scheduledReport)
|
2019-05-01 09:46:52 +00:00
|
|
|
})
|
|
|
|
}
|
2019-05-26 20:32:02 +00:00
|
|
|
scheduler.Run()
|
2019-05-01 09:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func executeReport(r *report) {
|
|
|
|
fmt.Println("Execute report:", r.Name)
|
|
|
|
req, e := http.NewRequest("GET", "http://localhost:"+appConfig.Port+"/stats?"+r.Query, nil)
|
|
|
|
if e != nil {
|
|
|
|
fmt.Println("Executing report failed:", e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.SetBasicAuth(appConfig.StatsUsername, appConfig.StatsPassword)
|
|
|
|
res, e := http.DefaultClient.Do(req)
|
|
|
|
if e != nil {
|
|
|
|
fmt.Println("Executing report failed:", e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
body, e := ioutil.ReadAll(res.Body)
|
|
|
|
if e != nil {
|
|
|
|
fmt.Println("Executing report failed:", e)
|
|
|
|
return
|
|
|
|
}
|
2019-05-26 07:12:30 +00:00
|
|
|
sendReport(r, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendReport(r *report, content []byte) {
|
|
|
|
switch r.Type {
|
|
|
|
case "telegram":
|
|
|
|
sendTelegram(r, content)
|
|
|
|
default:
|
|
|
|
sendMail(r, content)
|
|
|
|
}
|
2019-05-01 09:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func sendMail(r *report, content []byte) {
|
2019-05-27 13:03:30 +00:00
|
|
|
if r.To == "" || appConfig.SmtpFrom == "" || appConfig.SmtpUser == "" || appConfig.SmtpHost == "" {
|
2019-05-26 07:12:30 +00:00
|
|
|
fmt.Println("No valid report configuration")
|
|
|
|
return
|
|
|
|
}
|
2019-05-27 13:03:30 +00:00
|
|
|
smtpHostNoPort, _, _ := net.SplitHostPort(appConfig.SmtpHost)
|
2019-05-01 09:46:52 +00:00
|
|
|
mail := email.NewEmail()
|
2019-05-27 13:03:30 +00:00
|
|
|
mail.From = appConfig.SmtpFrom
|
2019-05-01 09:46:52 +00:00
|
|
|
mail.To = []string{r.To}
|
|
|
|
mail.Subject = "KISSS report: " + r.Name
|
|
|
|
mail.Text = content
|
2019-05-27 13:03:30 +00:00
|
|
|
e := mail.Send(appConfig.SmtpHost, smtp.PlainAuth("", appConfig.SmtpUser, appConfig.SmtpPassword, smtpHostNoPort))
|
2019-05-01 09:46:52 +00:00
|
|
|
if e != nil {
|
|
|
|
fmt.Println("Sending report failed:", e)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
fmt.Println("Report sent")
|
|
|
|
}
|
|
|
|
}
|
2019-05-26 07:12:30 +00:00
|
|
|
|
|
|
|
func sendTelegram(r *report, content []byte) {
|
2019-05-27 13:03:30 +00:00
|
|
|
if r.TGUserId == 0 || app.tgBot == nil {
|
2019-05-26 07:12:30 +00:00
|
|
|
fmt.Println("No valid report configuration")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
msg := tgbotapi.NewMessage(r.TGUserId, r.Name+"\n\n"+string(content))
|
2019-05-27 13:03:30 +00:00
|
|
|
_, e := app.tgBot.Send(msg)
|
2019-05-26 07:12:30 +00:00
|
|
|
if e != nil {
|
|
|
|
fmt.Println("Sending report failed:", e)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
fmt.Println("Report sent")
|
|
|
|
}
|
|
|
|
}
|