Implement support for notification types
- add new flag "-t" for notification types to matrix-host-notification and matrix-service-notification - implement value validation for HostState and ServiceState
This commit is contained in:
parent
902d4cb310
commit
1aeacd0d1b
6 changed files with 81 additions and 10 deletions
|
@ -13,6 +13,7 @@
|
|||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package icinga2
|
||||
|
||||
import (
|
||||
|
@ -84,10 +85,11 @@ func BuildHostNotification(p *HostParameters) (string, error) {
|
|||
|
||||
if _, err := fmt.Fprintf(
|
||||
message,
|
||||
"%s <strong>HOST:</strong> %s is <strong>%s!</strong><br/>\n"+
|
||||
"%s <strong>[%s] HOST:</strong> %s is <strong>%s!</strong><br/>\n"+
|
||||
"<strong>When:</strong> %s<br/>\n"+
|
||||
"<strong>Info:</strong> %s<br/>\n",
|
||||
getIcon(p.HostState),
|
||||
getIcon(string(p.HostState)),
|
||||
p.NotificationType,
|
||||
p.HostDisplayName, p.HostState,
|
||||
p.LongDateTime, p.HostOutput); err != nil {
|
||||
return "", CouldNotWriteToBufferErr{err}
|
||||
|
@ -118,10 +120,11 @@ func BuildServiceNotification(p *ServiceParameters) (string, error) {
|
|||
|
||||
if _, err := fmt.Fprintf(
|
||||
message,
|
||||
"%s <strong>Service:</strong> %s on %s is <strong>%s</strong>.<br/>\n"+
|
||||
"%s <strong>[%s] Service:</strong> %s on %s is <strong>%s</strong>.<br/>\n"+
|
||||
"<strong>When:</strong> %s<br/>\n"+
|
||||
"<strong>Info:</strong> %s<br/>\n",
|
||||
getIcon(p.ServiceState), p.ServiceDisplayName, p.HostDisplayName,
|
||||
getIcon(string(p.ServiceState)), p.NotificationType,
|
||||
p.ServiceDisplayName, p.HostDisplayName,
|
||||
p.ServiceState, p.LongDateTime, p.ServiceOutput,
|
||||
); err != nil {
|
||||
return "", CouldNotWriteToBufferErr{err}
|
||||
|
|
|
@ -13,12 +13,14 @@
|
|||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package icinga2
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type URLValue struct {
|
||||
|
@ -44,6 +46,65 @@ func (u *URLValue) String() string {
|
|||
return ""
|
||||
}
|
||||
|
||||
type HostStateValue string
|
||||
|
||||
func (v *HostStateValue) Set(value string) error {
|
||||
if strings.EqualFold(value, "Up") || strings.EqualFold(value, "Down") {
|
||||
*v = HostStateValue(strings.ToUpper(value))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("invalid host state value: %s", value)
|
||||
}
|
||||
|
||||
func (v *HostStateValue) String() string {
|
||||
return string(*v)
|
||||
}
|
||||
|
||||
type ServiceStateValue string
|
||||
|
||||
func (v *ServiceStateValue) Set(value string) error {
|
||||
allowedValues := []string{"OK", "Warning", "Critical", "Unknown"}
|
||||
|
||||
for _, allowed := range allowedValues {
|
||||
if strings.EqualFold(value, allowed) {
|
||||
*v = ServiceStateValue(strings.ToUpper(value))
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("invalid service state value: %s", value)
|
||||
}
|
||||
|
||||
func (v *ServiceStateValue) String() string {
|
||||
return string(*v)
|
||||
}
|
||||
|
||||
type NotificationTypeValue string
|
||||
|
||||
func (v *NotificationTypeValue) Set(value string) error {
|
||||
allowedValues := []string{
|
||||
"DowntimeStart", "DowntimeEnd", "DownTimeRemoved", "Custom", "Acknowledgement", "Problem", "Recovery",
|
||||
"FlappingStart", "FlappingEnd",
|
||||
}
|
||||
|
||||
for _, allowed := range allowedValues {
|
||||
if strings.EqualFold(value, allowed) {
|
||||
*v = NotificationTypeValue(strings.ToUpper(value))
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("invalid notification type value: %s", value)
|
||||
}
|
||||
|
||||
func (v *NotificationTypeValue) String() string {
|
||||
return string(*v)
|
||||
}
|
||||
|
||||
type MatrixParameters struct {
|
||||
MatrixRoom string
|
||||
MatrixServer URLValue
|
||||
|
@ -51,12 +112,13 @@ type MatrixParameters struct {
|
|||
}
|
||||
|
||||
func (m *MatrixParameters) HasRequired() bool {
|
||||
return m.MatrixRoom != "" && m.MatrixServer.URL != nil && m.MatrixToken != ""
|
||||
return !(m.MatrixRoom == "" || m.MatrixServer.URL == nil || m.MatrixToken == "")
|
||||
}
|
||||
|
||||
type BaseParameters struct {
|
||||
NotificationAuthorName string
|
||||
NotificationComment string
|
||||
NotificationType NotificationTypeValue
|
||||
IcingaWeb2URL URLValue
|
||||
LongDateTime string
|
||||
Hostname string
|
||||
|
@ -66,12 +128,12 @@ type BaseParameters struct {
|
|||
}
|
||||
|
||||
func (p BaseParameters) HasRequired() bool {
|
||||
return p.LongDateTime != "" && p.Hostname != "" && p.HostDisplayName != ""
|
||||
return !(p.LongDateTime == "" || p.Hostname == "" || p.HostDisplayName == "" || p.NotificationType == "")
|
||||
}
|
||||
|
||||
type HostParameters struct {
|
||||
HostOutput string
|
||||
HostState string
|
||||
HostState HostStateValue
|
||||
BaseParameters
|
||||
MatrixParameters
|
||||
}
|
||||
|
@ -89,7 +151,7 @@ type ServiceParameters struct {
|
|||
ServiceName string
|
||||
ServiceDisplayName string
|
||||
ServiceOutput string
|
||||
ServiceState string
|
||||
ServiceState ServiceStateValue
|
||||
BaseParameters
|
||||
MatrixParameters
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue