jlelse
/
kis3
Archived
1
Fork 0
This repository has been archived on 2021-01-04. You can view files and clone it, but cannot push or open issues or pull requests.
kis3/reports.go

96 lines
2.2 KiB
Go
Raw Permalink Normal View History

2019-05-01 09:46:52 +00:00
package main
import (
"fmt"
"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"`
2020-04-22 19:40:28 +00:00
TGUserId int `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) {
2020-04-22 19:40:28 +00:00
if r.TGUserId == 0 || app.telegram == nil {
2019-05-26 07:12:30 +00:00
fmt.Println("No valid report configuration")
return
}
2020-04-22 19:40:28 +00:00
e := app.telegram.sendMessage(r.TGUserId, r.Name+"\n\n"+string(content))
2019-05-26 07:12:30 +00:00
if e != nil {
fmt.Println("Sending report failed:", e)
return
} else {
fmt.Println("Report sent")
}
}