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:
Jan Dittberner 2024-09-26 17:18:32 +02:00
parent 902d4cb310
commit 1aeacd0d1b
6 changed files with 81 additions and 10 deletions

View file

@ -45,9 +45,10 @@ func parseFlags() *icinga2.HostParameters {
flag.StringVar(&config.LongDateTime, "d", "", "long date time ($icinga.long_date_time$)")
flag.StringVar(&config.Hostname, "l", "", "hostname ($host.name$)")
flag.StringVar(&config.HostDisplayName, "n", "", "host display name ($host.display_name$)")
flag.Var(&config.NotificationType, "t", "notification type ($notification_type$)")
flag.StringVar(&config.HostOutput, "o", "", "host output ($host.output$)")
flag.StringVar(&config.HostState, "s", "", "host state ($host.state$)")
flag.Var(&config.HostState, "s", "host state ($host.state$)")
flag.StringVar(&config.MatrixRoom, "m", "", "matrix room ($notification_matrix_room$)")
flag.Var(&config.MatrixServer, "x", "matrix server ($notification_matrix_server$)")

View file

@ -13,6 +13,8 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// The main package for matrix-service-notification
package main
import (
@ -45,11 +47,12 @@ func parseFlags() *icinga2.ServiceParameters {
flag.StringVar(&parameters.LongDateTime, "d", "", "long date time ($icinga.long_date_time$)")
flag.StringVar(&parameters.Hostname, "l", "", "hostname ($host.name$)")
flag.StringVar(&parameters.HostDisplayName, "n", "", "host display name ($host.display_name$)")
flag.Var(&parameters.NotificationType, "t", "notification type ($notification_type$)")
flag.StringVar(&parameters.ServiceName, "e", "", "service name ($service.name$)")
flag.StringVar(&parameters.ServiceDisplayName, "u", "", "service display name ($service.display_name$)")
flag.StringVar(&parameters.ServiceOutput, "o", "", "service output ($service.output$)")
flag.StringVar(&parameters.ServiceState, "s", "", "service state ($service.state$)")
flag.Var(&parameters.ServiceState, "s", "service state ($service.state$)")
flag.StringVar(&parameters.MatrixRoom, "m", "", "matrix room ($notification_matrix_room$)")
flag.Var(&parameters.MatrixServer, "x", "matrix server ($notification_matrix_server$)")

View file

@ -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}

View file

@ -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
}

View file

@ -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 matrix
const (

View file

@ -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 matrix
import (