-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notifications.go
86 lines (76 loc) · 2.33 KB
/
Notifications.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2018 singularitynet foundation.
// All rights reserved.
// <<add licence terms for code reuse>>
// package for monitoring and reporting the daemon metrics
package metrics
import (
"errors"
"github.com/singnet/snet-daemon/config"
log "github.com/sirupsen/logrus"
)
// state of alerts configuration
var isNoAlertsConfig bool
// define heartbeat data model. Service Status JSON object Array marshalled to a string
type Notification struct {
DaemonID string `json:"component_id"`
Timestamp string `json:"timestamp"`
Recipient string `json:"recipient"`
Message string `json:"message"`
Details string `json:"details"`
Component string `json:"component"`
Type string `json:"type"`
Level string `json:"level"`
}
// function for sending an alert to a given endpoint
func (alert *Notification) Send() bool {
if isNoAlertsConfig {
log.Warningf("notifications not configured")
return false
}
serviceURL := config.GetString(config.NotificationServiceEndpoint)
// convert the notification struct to json
jsonAlert, err := ConvertStructToJSON(alert)
log.Infof("notification : %v", string(jsonAlert))
if err != nil {
log.WithError(err).Warningf("json conversion error : %v", err)
return false
} else {
// based on the notification success/failure
status := Publish(jsonAlert, serviceURL)
if !status {
log.Infof("unable to send notifications")
return false
}
}
return true
}
// set the no alerts URL and email State
func SetIsNoAlertsConfig(state bool) {
isNoAlertsConfig = state
}
// validates the heartbeat configurations
func ValidateNotificationConfig() error {
SetIsNoAlertsConfig(false)
// if the URL or email is empty, consider it as not configured and set isInvalidAlertsConfig to true
if config.GetString(config.NotificationServiceEndpoint) == "" || config.GetString(config.AlertsEMail) == "" {
SetIsNoAlertsConfig(true)
} else if !(config.IsValidUrl(config.GetString(config.NotificationServiceEndpoint)) &&
config.ValidateEmail(config.GetString(config.AlertsEMail))) {
return errors.New("service endpoint and alerts email id must be valid")
}
return nil
}
/*
service request
{
"recipient":"raam.comm@gmail.com",
"message":"From the API",
"details":"From the API",
"component":"daemon",
"component_id":"ad",
"type":"INFO",
"level":"10"
}
service response
true/false
*/