icinga2-matrix-notification/cmd/matrix-service-notification/main.go
Jan Dittberner 1aeacd0d1b 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
2024-09-26 17:18:32 +02:00

76 lines
3 KiB
Go

// 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-service-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() {
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.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.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$)")
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
}