func TestPostgresHealthCheckReporting(t *testing.T) { cxn := database.GetPGConnection(conf.DBURI()) healthChecks, _ := healthcheck.ReadHealthCheckYAMLFromFile("healthcheck/healthchecksAll.yml") results, _ := healthChecks.PreformHealthChecks(cxn) var elements []report.Element for _, val := range results { elements = append(elements, val) } metadata := map[string]interface{}{ "name": healthChecks.Name, "schema": "public", "table": "healthchecks", "timestamp": time.Now().Format(time.ANSIC), } rs := report.Set{Elements: elements, Metadata: metadata} prr := report.NewPongo2ReportRunnerFromString(healthcheck.TemplateHealthcheckPostgres) pgr := report.PGHandler{Cxn: cxn} reader, err := prr.ReportReader(rs) err = pgr.HandleReport(reader) if err != nil { t.Fatalf("error writing report to PG database\n%s", err) } }
func healthcheckRunner(config *config.Config, healthcheckPath string, reportPath string, emailListPath string, hcSchema string, hcTable string) { healthChecks, err := healthcheck.ReadHealthCheckYAMLFromFile(healthcheckPath) if err != nil { log.Fatal("Failed to read healthchecks: ", err) } cxn := database.GetPGConnection(config.DBURI()) results, HCerrs := healthChecks.PreformHealthChecks(cxn) numErrors := 0 fatal := false for _, hcerr := range HCerrs { if strings.Contains(strings.ToUpper(hcerr.Err), "FATAL") { fatal = true } if strings.Contains(strings.ToUpper(hcerr.Err), "ERROR") { numErrors = numErrors + 1 } } var elements []report.Element for _, val := range results { elements = append(elements, val) } // Make Templated report metadata := map[string]interface{}{ "name": healthChecks.Name, "db_name": config.PgDatabase, "footer": healthcheck.FooterHealthcheck, "timestamp": time.Now().Format(time.ANSIC), "status": healthcheck.StatusHealthchecks(numErrors, fatal), "schema": hcSchema, "table": hcTable, } rs := report.Set{Elements: elements, Metadata: metadata} // Write report to file if reportPath != "" { prr := report.NewPongo2ReportRunnerFromString(healthcheck.TemplateHealthcheckHTML) reader, _ := prr.ReportReader(rs) fhr := report.FileHandler{Filename: reportPath} err = fhr.HandleReport(reader) if err != nil { log.Error("error writing report to PG database: ", err) } } // Email report if emailListPath != "" { prr := report.NewPongo2ReportRunnerFromString(healthcheck.TemplateHealthcheckHTML) df, err := report.ReadDistributionFormatYAMLFromFile(emailListPath) if err != nil { log.Fatal("Failed to read distribution format: ", err) } for _, level := range report.LogLevelArray { subjectStr := healthcheck.SubjectHealthcheck(healthChecks.Name, config.PgDatabase, config.PgHost, level, numErrors, fatal) logFilteredSet := report.FilterReportSet(rs, level) reader, _ := prr.ReportReader(logFilteredSet) recipients := df.GetEmails(level) if recipients != nil && len(recipients) != 0 && len(logFilteredSet.Elements) != 0 { log.Infof("Send %s to: %v", subjectStr, recipients) ehr := report.EmailHandler{ SMTPHost: config.SMTPHost, SMTPPort: config.SMTPPort, SenderEmail: config.SMTPEmail, SenderName: config.SMTPName, Subject: subjectStr, Recipients: recipients, HTML: true, } err = ehr.HandleReport(reader) if err != nil { log.Error("Failed to email report: ", err) } } } } if hcSchema != "" && hcTable != "" { prr := report.NewPongo2ReportRunnerFromString(healthcheck.TemplateHealthcheckPostgres) pgr := report.PGHandler{Cxn: cxn} reader, err := prr.ReportReader(rs) err = pgr.HandleReport(reader) if err != nil { log.Errorf("Failed to save healthchecks to PG database\n%s", err) } } // Bad Exit if HCerrs != nil { log.Fatal("Healthchecks Failed:\n", spew.Sdump(HCerrs)) } }