Archived
1
0
Fork 0

refactor(monitoring): Improve code and output

- Don't return when a shop is missing
- Make MonitoringResult more generic to handle messages

Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
Julien Riou 2022-09-02 09:11:24 +02:00
parent e7c18d048e
commit 3a8d0825a6
No known key found for this signature in database
GPG key ID: FF42D23B580C89F7

View file

@ -23,29 +23,34 @@ const (
// MonitoringResult to store result of Nagios checks // MonitoringResult to store result of Nagios checks
type MonitoringResult struct { type MonitoringResult struct {
ShopName string ShopName string
UpdatedAt time.Time Message string
ReturnCode int ReturnCode int
} }
// String to print a MonitoringResult nicely // String returns a string to print a MonitoringResult nicely
func (m MonitoringResult) String() string { func (m MonitoringResult) String() string {
diff := time.Now().Sub(m.UpdatedAt) return fmt.Sprintf("%s %s (rc = %d)", m.ShopName, m.Message, m.ReturnCode)
}
var wording string // ReturnCodeString returns a string to print a ReturnCode nicely
if diff.Seconds() > 0 { func ReturnCodeString(rc int) string {
wording = "seconds" switch rc {
} else { case NagiosOk:
wording = "second" return "OK"
case NagiosWarning:
return "WARN"
case NagiosCritical:
return "CRIT"
default:
return "UNK"
} }
return fmt.Sprintf("%s (%d %s ago)", m.ShopName, diff, wording)
} }
// FormatMonitoringResults to print a list of MonitoringResult nicely // FormatMonitoringResults to print a list of MonitoringResult nicely
func FormatMonitoringResults(results []MonitoringResult) string { func FormatMonitoringResults(results []MonitoringResult) string {
var s []string var s []string
for _, result := range results { for _, result := range results {
s = append(s, result.String()) s = append(s, fmt.Sprintf("%s %s", result.ShopName, result.Message))
} }
return strings.Join(s, ", ") return strings.Join(s, ", ")
} }
@ -70,12 +75,20 @@ func Monitor(db *gorm.DB, warningTimeout int, criticalTimeout int) (rc int) {
} }
for _, shop := range shops { for _, shop := range shops {
result := MonitoringResult{
ShopName: shop.Name,
ReturnCode: NagiosOk,
}
// Fetch last execution time // Fetch last execution time
var product Product var product Product
trx := db.Where(Product{ShopID: shop.ID}).Order("updated_at asc").First(&product) trx := db.Where(Product{ShopID: shop.ID}).Order("updated_at asc").First(&product)
if trx.Error == gorm.ErrRecordNotFound { if trx.Error == gorm.ErrRecordNotFound {
fmt.Printf("%s\n", fmt.Errorf("No product found for shop %s", shop.Name)) result.Message = "has not been updated"
return NagiosCritical result.ReturnCode = NagiosCritical
resultMap[NagiosCritical] = append(resultMap[result.ReturnCode], result)
continue
} }
if trx.Error != nil { if trx.Error != nil {
fmt.Printf("%s\n", trx.Error) fmt.Printf("%s\n", trx.Error)
@ -83,36 +96,33 @@ func Monitor(db *gorm.DB, warningTimeout int, criticalTimeout int) (rc int) {
} }
// Compare to thresholds and add to result map // Compare to thresholds and add to result map
result := MonitoringResult{ShopName: shop.Name, UpdatedAt: product.UpdatedAt, ReturnCode: NagiosOk} diff := int(time.Now().Sub(product.UpdatedAt.Local()).Seconds())
result.Message = fmt.Sprintf("updated %d seconds ago", diff)
if product.UpdatedAt.Before(criticalTime) { if product.UpdatedAt.Before(criticalTime) {
log.Infof("%s has been updated at %s (before time of %s) (crit)", shop.Name, product.UpdatedAt, criticalTime)
result.ReturnCode = NagiosCritical result.ReturnCode = NagiosCritical
} else if product.UpdatedAt.Before(warningTime) { } else if product.UpdatedAt.Before(warningTime) {
log.Infof("%s has been updated at %s (before time of %s) (warn)", shop.Name, product.UpdatedAt, warningTime)
result.ReturnCode = NagiosWarning result.ReturnCode = NagiosWarning
} else { } else {
log.Infof("%s has been updated at %s (after %s) (ok)", shop.Name, product.UpdatedAt, warningTime)
} }
log.Info(result)
resultMap[result.ReturnCode] = append(resultMap[result.ReturnCode], result) resultMap[result.ReturnCode] = append(resultMap[result.ReturnCode], result)
} }
var message, prefix string var message string
if len(resultMap[NagiosWarning]) > 0 { if len(resultMap[NagiosWarning]) > 0 {
rc = NagiosWarning rc = NagiosWarning
prefix = "WARN"
message = FormatMonitoringResults(resultMap[NagiosWarning]) message = FormatMonitoringResults(resultMap[NagiosWarning])
} else if len(resultMap[NagiosCritical]) > 0 { } else if len(resultMap[NagiosCritical]) > 0 {
rc = NagiosCritical rc = NagiosCritical
prefix = "CRIT"
message = FormatMonitoringResults(resultMap[NagiosCritical]) message = FormatMonitoringResults(resultMap[NagiosCritical])
} else { } else {
rc = NagiosOk rc = NagiosOk
prefix = "OK"
message = "All shops have been updated recently" message = "All shops have been updated recently"
} }
// Print output // Print output
fmt.Printf("%s - %s\n", prefix, message) fmt.Printf("%s - %s\n", ReturnCodeString(rc), message)
return rc return
} }