Add Go command implementations

This commit is contained in:
Jan Dittberner 2024-09-26 14:45:04 +02:00
parent d976feef80
commit 59692ae698
7 changed files with 516 additions and 0 deletions

3
.gitignore vendored
View file

@ -2,3 +2,6 @@
*.rej
.*.swp
/.idea/
/matrix-host-notification
/matrix-service-notification
dist/

View file

@ -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 <https://www.gnu.org/licenses/>.
// 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
}

View file

@ -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 <https://www.gnu.org/licenses/>.
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(&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.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.StringVar(&parameters.MatrixRoom, "m", "", "matrix room ($notification_matrix_room$)")
flag.Var(&parameters.MatrixServer, "x", "matrix server ($notification_matrix_server$)")
flag.StringVar(&parameters.MatrixToken, "y", "", "matrix access token ($notification_matrix_token$)")
flag.StringVar(&parameters.HostAddress, "4", "", "host address ($address$)")
flag.StringVar(&parameters.HostAddress6, "6", "", "host address ($address6$)")
flag.StringVar(&parameters.NotificationAuthorName, "b", "", "notification author name ($notification.author_name$)")
flag.StringVar(&parameters.NotificationComment, "c", "", "notification comment ($notification.comment$)")
flag.Var(&parameters.IcingaWeb2URL, "i", "IcingaWeb 2 URL ($notification_icingaweb2url$)")
flag.Parse()
if err := parameters.ValidateRequired(); err != nil {
flag.Usage()
os.Exit(2) //nolint:mnd
}
return parameters
}

View file

@ -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 <https://www.gnu.org/licenses/>.
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, "<strong>IPv4:</strong> %s<br/>\n", p.HostAddress); err != nil {
return CouldNotWriteToBufferErr{err}
}
}
if p.HostAddress6 != "" {
if _, err := fmt.Fprintf(message, "<strong>IPv6:</strong> %s<br/>\n", p.HostAddress6); err != nil {
return CouldNotWriteToBufferErr{err}
}
}
if p.NotificationAuthorName != "" && p.NotificationComment != "" {
if _, err := fmt.Fprintf(
message,
"Comment by <strong>%s:</strong> %s<br/>\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 <strong>HOST:</strong> %s is <strong>%s!</strong><br/>\n"+
"<strong>When:</strong> %s<br/>\n"+
"<strong>Info:</strong> %s<br/>\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 <strong>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,
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
}

View file

@ -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 <https://www.gnu.org/licenses/>.
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
}

View file

@ -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 <https://www.gnu.org/licenses/>.
package matrix
const (
TextMessage = "m.text"
HTMLFormat = "org.matrix.custom.html"
)

View file

@ -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 <https://www.gnu.org/licenses/>.
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
}