diff --git a/.gitignore b/.gitignore index d5adb91..749f198 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ *.rej .*.swp /.idea/ +/matrix-host-notification +/matrix-service-notification +dist/ diff --git a/cmd/matrix-host-notification/main.go b/cmd/matrix-host-notification/main.go new file mode 100644 index 0000000..cc92d5c --- /dev/null +++ b/cmd/matrix-host-notification/main.go @@ -0,0 +1,71 @@ +// Copyright Jan Dittberner +// SPDX-License-Identifier: GPL-3.0-or-later +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +// The main package for matrix-host-notification +package main + +import ( + "flag" + "log" + "os" + + "git.ditberner.info/jan/icinga2-matrix-notification/internal/icinga2" + "git.ditberner.info/jan/icinga2-matrix-notification/internal/matrix" +) + +func main() { + config := parseFlags() + + message, err := icinga2.BuildHostNotification(config) + if err != nil { + log.Fatalf("could not build message: %v", err) + } + + if err := matrix.SendMessage(config.MatrixServer.URL, config.MatrixRoom, config.MatrixToken, message); err != nil { + log.Fatalf("could not send message: %v", err) + } +} + +func parseFlags() *icinga2.HostParameters { + config := &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.StringVar(&config.HostOutput, "o", "", "host output ($host.output$)") + flag.StringVar(&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$)") + flag.StringVar(&config.MatrixToken, "y", "", "matrix access token ($notification_matrix_token$)") + + flag.StringVar(&config.HostAddress, "4", "", "host address ($address$)") + flag.StringVar(&config.HostAddress6, "6", "", "host address ($address6$)") + flag.StringVar(&config.NotificationAuthorName, "b", "", "notification author name ($notification.author_name$)") + flag.StringVar(&config.NotificationComment, "c", "", "notification comment ($notification.comment$)") + flag.Var(&config.IcingaWeb2URL, "i", "IcingaWeb 2 URL ($notification_icingaweb2url$)") + + flag.Parse() + + if err := config.ValidateRequired(); err != nil { + flag.Usage() + + os.Exit(2) //nolint:mnd + } + + return config +} diff --git a/cmd/matrix-service-notification/main.go b/cmd/matrix-service-notification/main.go new file mode 100644 index 0000000..dfb7e80 --- /dev/null +++ b/cmd/matrix-service-notification/main.go @@ -0,0 +1,73 @@ +// Copyright Jan Dittberner +// SPDX-License-Identifier: GPL-3.0-or-later +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +package main + +import ( + "flag" + "log" + "os" + + "git.ditberner.info/jan/icinga2-matrix-notification/internal/icinga2" + "git.ditberner.info/jan/icinga2-matrix-notification/internal/matrix" +) + +func main() { + parameters := parseFlags() + + message, err := icinga2.BuildServiceNotification(parameters) + if err != nil { + log.Fatalf("could not build message: %v", err) + } + + if err := matrix.SendMessage( + parameters.MatrixServer.URL, parameters.MatrixRoom, parameters.MatrixToken, message, + ); err != nil { + log.Fatalf("could not send message: %v", err) + } +} + +func parseFlags() *icinga2.ServiceParameters { + parameters := &icinga2.ServiceParameters{} + + flag.StringVar(¶meters.LongDateTime, "d", "", "long date time ($icinga.long_date_time$)") + flag.StringVar(¶meters.Hostname, "l", "", "hostname ($host.name$)") + flag.StringVar(¶meters.HostDisplayName, "n", "", "host display name ($host.display_name$)") + + flag.StringVar(¶meters.ServiceName, "e", "", "service name ($service.name$)") + flag.StringVar(¶meters.ServiceDisplayName, "u", "", "service display name ($service.display_name$)") + flag.StringVar(¶meters.ServiceOutput, "o", "", "service output ($service.output$)") + flag.StringVar(¶meters.ServiceState, "s", "", "service state ($service.state$)") + + flag.StringVar(¶meters.MatrixRoom, "m", "", "matrix room ($notification_matrix_room$)") + flag.Var(¶meters.MatrixServer, "x", "matrix server ($notification_matrix_server$)") + flag.StringVar(¶meters.MatrixToken, "y", "", "matrix access token ($notification_matrix_token$)") + + flag.StringVar(¶meters.HostAddress, "4", "", "host address ($address$)") + flag.StringVar(¶meters.HostAddress6, "6", "", "host address ($address6$)") + flag.StringVar(¶meters.NotificationAuthorName, "b", "", "notification author name ($notification.author_name$)") + flag.StringVar(¶meters.NotificationComment, "c", "", "notification comment ($notification.comment$)") + flag.Var(¶meters.IcingaWeb2URL, "i", "IcingaWeb 2 URL ($notification_icingaweb2url$)") + + flag.Parse() + + if err := parameters.ValidateRequired(); err != nil { + flag.Usage() + + os.Exit(2) //nolint:mnd + } + + return parameters +} diff --git a/internal/icinga2/messages.go b/internal/icinga2/messages.go new file mode 100644 index 0000000..f6edc87 --- /dev/null +++ b/internal/icinga2/messages.go @@ -0,0 +1,149 @@ +// Copyright Jan Dittberner +// SPDX-License-Identifier: GPL-3.0-or-later +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +package icinga2 + +import ( + "fmt" + "strings" +) + +const ( + WarnIcon = "⚠" + ErrorIcon = "❌" + OKIcon = "🆗" + QuestionIcon = "❓" +) + +func getIcon(state string) string { + switch state { + case "UP", "OK": + return OKIcon + case "UNKNOWN": + return QuestionIcon + case "WARNING": + return WarnIcon + default: + return ErrorIcon + } +} + +type CouldNotWriteToBufferErr struct { + Wrapped error +} + +func (e CouldNotWriteToBufferErr) Error() string { + return fmt.Sprintf("could not write to message builder: %s", e.Wrapped) +} + +func (e CouldNotWriteToBufferErr) Unwrap() error { + return e.Wrapped +} + +func addOptionalData(message *strings.Builder, p *BaseParameters) error { + if p.HostAddress != "" { + if _, err := fmt.Fprintf(message, "IPv4: %s
\n", p.HostAddress); err != nil { + return CouldNotWriteToBufferErr{err} + } + } + + if p.HostAddress6 != "" { + if _, err := fmt.Fprintf(message, "IPv6: %s
\n", p.HostAddress6); err != nil { + return CouldNotWriteToBufferErr{err} + } + } + + if p.NotificationAuthorName != "" && p.NotificationComment != "" { + if _, err := fmt.Fprintf( + message, + "Comment by %s: %s
\n", + p.NotificationAuthorName, + p.NotificationComment, + ); err != nil { + return CouldNotWriteToBufferErr{err} + } + } + + return nil +} + +func BuildHostNotification(p *HostParameters) (string, error) { + message := &strings.Builder{} + + if _, err := fmt.Fprintf( + message, + "%s HOST: %s is %s!
\n"+ + "When: %s
\n"+ + "Info: %s
\n", + getIcon(p.HostState), + p.HostDisplayName, p.HostState, + p.LongDateTime, p.HostOutput); err != nil { + return "", CouldNotWriteToBufferErr{err} + } + + if err := addOptionalData(message, &p.BaseParameters); err != nil { + return "", err + } + + if p.IcingaWeb2URL.URL != nil { + icinga2Url := p.IcingaWeb2URL.URL.JoinPath("monitoring/host/show") + + q := icinga2Url.Query() + q.Set("host", p.Hostname) + + icinga2Url.RawQuery = q.Encode() + + if _, err := message.WriteString(icinga2Url.String()); err != nil { + return "", CouldNotWriteToBufferErr{err} + } + } + + return message.String(), nil +} + +func BuildServiceNotification(p *ServiceParameters) (string, error) { + message := &strings.Builder{} + + if _, err := fmt.Fprintf( + message, + "%s Service: %s on %s is %s.
\n"+ + "When: %s
\n"+ + "Info: %s
\n", + getIcon(p.ServiceState), p.ServiceDisplayName, p.HostDisplayName, + p.ServiceState, p.LongDateTime, p.ServiceOutput, + ); err != nil { + return "", CouldNotWriteToBufferErr{err} + } + + if err := addOptionalData(message, &p.BaseParameters); err != nil { + return "", err + } + + if p.IcingaWeb2URL.URL != nil { + icinga2Url := p.IcingaWeb2URL.URL.JoinPath("monitoring/service/show") + + q := icinga2Url.Query() + q.Set("host", p.Hostname) + q.Set("service", p.ServiceName) + + icinga2Url.RawQuery = q.Encode() + + if _, err := message.WriteString(icinga2Url.String()); err != nil { + return "", CouldNotWriteToBufferErr{err} + } + } + + return message.String(), nil +} diff --git a/internal/icinga2/parameters.go b/internal/icinga2/parameters.go new file mode 100644 index 0000000..ef1ac76 --- /dev/null +++ b/internal/icinga2/parameters.go @@ -0,0 +1,104 @@ +// Copyright Jan Dittberner +// SPDX-License-Identifier: GPL-3.0-or-later +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +package icinga2 + +import ( + "errors" + "fmt" + "net/url" +) + +type URLValue struct { + URL *url.URL +} + +func (u *URLValue) Set(value string) error { + var err error + + u.URL, err = url.Parse(value) + if err != nil { + return fmt.Errorf("could not parse URL: %w", err) + } + + return nil +} + +func (u *URLValue) String() string { + if u.URL != nil { + return u.URL.String() + } + + return "" +} + +type MatrixParameters struct { + MatrixRoom string + MatrixServer URLValue + MatrixToken string +} + +func (m *MatrixParameters) HasRequired() bool { + return m.MatrixRoom != "" && m.MatrixServer.URL != nil && m.MatrixToken != "" +} + +type BaseParameters struct { + NotificationAuthorName string + NotificationComment string + IcingaWeb2URL URLValue + LongDateTime string + Hostname string + HostDisplayName string + HostAddress string + HostAddress6 string +} + +func (p BaseParameters) HasRequired() bool { + return p.LongDateTime != "" && p.Hostname != "" && p.HostDisplayName != "" +} + +type HostParameters struct { + HostOutput string + HostState string + BaseParameters + MatrixParameters +} + +func (p *HostParameters) ValidateRequired() error { + if !p.BaseParameters.HasRequired() || !p.MatrixParameters.HasRequired() || + p.HostOutput == "" || p.HostState == "" { + return errors.New("missing required fields") + } + + return nil +} + +type ServiceParameters struct { + ServiceName string + ServiceDisplayName string + ServiceOutput string + ServiceState string + BaseParameters + MatrixParameters +} + +func (p *ServiceParameters) ValidateRequired() error { + if !p.BaseParameters.HasRequired() || !p.MatrixParameters.HasRequired() || + p.ServiceName == "" || p.ServiceDisplayName == "" || p.ServiceState == "" || p.ServiceOutput == "" { + return errors.New("missing required fields") + } + + return nil +} diff --git a/internal/matrix/constants.go b/internal/matrix/constants.go new file mode 100644 index 0000000..7b9aaa2 --- /dev/null +++ b/internal/matrix/constants.go @@ -0,0 +1,21 @@ +// Copyright Jan Dittberner +// SPDX-License-Identifier: GPL-3.0-or-later +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +package matrix + +const ( + TextMessage = "m.text" + HTMLFormat = "org.matrix.custom.html" +) diff --git a/internal/matrix/message.go b/internal/matrix/message.go new file mode 100644 index 0000000..d2b21bf --- /dev/null +++ b/internal/matrix/message.go @@ -0,0 +1,95 @@ +// Copyright Jan Dittberner +// SPDX-License-Identifier: GPL-3.0-or-later +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +package matrix + +import ( + "bytes" + "crypto/rand" + "encoding/base64" + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" +) + +const transactionIDLength = 16 + +type Message struct { + Type string `json:"msgtype"` + Body string `json:"body"` + FormattedBody string `json:"formatted_body"` + Format string `json:"format"` +} + +func SendMessage(server *url.URL, room string, token string, message string) error { + messageBytes, err := json.Marshal(Message{ + Type: TextMessage, + Body: message, + FormattedBody: message, + Format: HTMLFormat, + }) + if err != nil { + return fmt.Errorf("could not build JSON message: %w", err) + } + + transaction, err := generateTransactionID() + if err != nil { + return err + } + + postURL := server.JoinPath("/_matrix/client/r0/rooms/", room, "/send/m.room.message/", transaction) + + request, err := http.NewRequest(http.MethodPut, postURL.String(), bytes.NewBuffer(messageBytes)) + if err != nil { + return fmt.Errorf("could not build request: %w", err) + } + + request.Header.Set("Accept", "application/json") + request.Header.Set("Content-Type", "application/json") + request.Header.Set("Authorization", "Bearer "+token) + + response, err := http.DefaultClient.Do(request) + if err != nil { + return fmt.Errorf("could not send request: %w", err) + } + + defer func() { + _ = response.Body.Close() + }() + + if response.StatusCode > http.StatusBadRequest { + responseBody, err := io.ReadAll(response.Body) + if err != nil { + return fmt.Errorf("received an error: %s, could not read response body: %w", response.Status, err) + } + + return fmt.Errorf("received an error: %s %s", response.Status, string(responseBody)) + } + + return nil +} + +func generateTransactionID() (string, error) { + IDBytes := make([]byte, transactionIDLength) + + _, err := rand.Read(IDBytes) + if err != nil { + return "", fmt.Errorf("could not generate transaction id: %w", err) + } + + return base64.RawURLEncoding.EncodeToString(IDBytes), nil +}