Compare commits
4 commits
Author | SHA1 | Date | |
---|---|---|---|
1c2da0dbb8 | |||
baadf876dc | |||
9f3326d611 | |||
1aeacd0d1b |
9 changed files with 130 additions and 10 deletions
37
README.md
37
README.md
|
@ -9,6 +9,43 @@ implementation](https://github.com/linuxmail/icinga2-matrix_notification) which
|
|||
has been built on the [E-Mail notifications by
|
||||
sysadmama](https://github.com/sysadmama).
|
||||
|
||||
## Building the binaries
|
||||
|
||||
You can build binaries via `go build` or using [Goreleaser](https://goreleaser.com/intro/).
|
||||
|
||||
```shell
|
||||
go build ./cmd/matrix-host-notification
|
||||
go build ./cmd/matrix-service-notification
|
||||
```
|
||||
|
||||
```shell
|
||||
goreleaser build
|
||||
```
|
||||
|
||||
You may have to add the `--snapshot` and `--clean` command line flags to `goreleaser` if
|
||||
you are not building a tagged version
|
||||
or have local modifications.
|
||||
|
||||
## Code quality checks
|
||||
|
||||
The repository contains a configuration for [golangci-lint](https://golangci-lint.run/). You can
|
||||
run the linting via
|
||||
|
||||
```shell
|
||||
golangci-lint run
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Copy the notification command binaries to the Icinga 2 systems you want to
|
||||
send notifications from.
|
||||
|
||||
Use the configuration examples as a base for your own NotificationCommand and
|
||||
Notification object definitions. See
|
||||
[Notification](https://icinga.com/docs/icinga-2/latest/doc/09-object-types/#notification) and
|
||||
[NotificationCommand](https://icinga.com/docs/icinga-2/latest/doc/09-object-types/#notificationcommand)
|
||||
in the Icinga 2 documentation for reference.
|
||||
|
||||
## License
|
||||
|
||||
Icinga2 Matrix notifications
|
||||
|
|
|
@ -99,6 +99,10 @@ object NotificationCommand "matrix-service-notification" {
|
|||
required = true
|
||||
value = "$notification_servicestate$"
|
||||
}
|
||||
"-t" = {
|
||||
required = true
|
||||
value = "$notification_type$"
|
||||
}
|
||||
"-u" = {
|
||||
required = true
|
||||
value = "$notification_servicedisplayname$"
|
||||
|
|
|
@ -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$)")
|
||||
|
|
|
@ -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(¶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.Var(¶meters.NotificationType, "t", "notification type ($notification_type$)")
|
||||
|
||||
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.Var(¶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$)")
|
||||
|
|
8
debian/changelog
vendored
8
debian/changelog
vendored
|
@ -1,3 +1,11 @@
|
|||
icinga2-matrix-notification (0.2.0-1) unstable; urgency=medium
|
||||
|
||||
* New release
|
||||
* improve documentation
|
||||
* add support for notification types
|
||||
|
||||
-- Jan Dittberner <jandd@debian.org> Thu, 26 Sep 2024 17:33:07 +0200
|
||||
|
||||
icinga2-matrix-notification (0.1.0-1) unstable; urgency=medium
|
||||
|
||||
* Initial release
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 (
|
||||
|
|
|
@ -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 (
|
||||
|
|
Loading…
Reference in a new issue