Skip to content

Commit 9390dee

Browse files
committed
Fix icinga2 host frontend
1 parent 052e5b3 commit 9390dee

9 files changed

+172
-49
lines changed

Gopkg.lock

+22-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

+4
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@
3232
[[constraint]]
3333
name = "gopkg.in/yaml.v2"
3434
version = "2.2.1"
35+
36+
[[constraint]]
37+
name = "github.com/sirupsen/logrus"
38+
version = "1.0.5"

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build:
2+
go build -i -v -ldflags="-X main.gitversion=$(shell git describe --always --long --dirty)"
3+
strip sendmsg

icinga_host.go

+93-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
package main
22

3-
import "flag"
3+
import (
4+
"flag"
5+
"bytes"
6+
"net/url"
7+
"fmt"
8+
"time"
9+
"github.com/sirupsen/logrus"
10+
"strconv"
11+
)
12+
13+
var icGoodState = map[string]bool {
14+
"UP": true,
15+
}
16+
17+
var icBadState = map[string]bool {
18+
"UNREACHABLE": true,
19+
"DOWN": true,
20+
}
421

522
type icingaHostCmd struct {
623
SimpleCFGLocation *string
@@ -13,6 +30,12 @@ type icingaHostCmd struct {
1330
icHostNotfnUser *string
1431
icHostNotfnComment *string
1532
icURL *string
33+
// Fields below are unused currently but need to be parsed in order to be compliant
34+
icDateTime *string
35+
icHostDisplayname *string
36+
icUserEmail *string
37+
icMailFrom *string
38+
icToSyslog *string
1639
}
1740

1841
func (this *icingaHostCmd) Init(flagSet *flag.FlagSet) {
@@ -25,18 +48,80 @@ func (this *icingaHostCmd) Init(flagSet *flag.FlagSet) {
2548
this.icHostCheckOutput = flagSet.String("o", "", "Check output")
2649
this.icHostNotfnUser = flagSet.String("b", "", "Manual host notification user")
2750
this.icHostNotfnComment = flagSet.String("c", "", "Manual host notification comment")
28-
this.icHostNotfnComment = flagSet.String("i", "", "URL of Webinterface")}
51+
this.icURL = flagSet.String("i", "", "URL of Webinterface")
52+
this.icDateTime = flagSet.String("d", "", "Date/Time of event")
53+
this.icHostDisplayname = flagSet.String("n", "", "Host display name")
54+
this.icUserEmail = flagSet.String("r", "", "User email")
55+
this.icMailFrom = flagSet.String("f", "", "Sender email")
56+
this.icToSyslog = flagSet.String("v", "", "Send to syslog")
57+
}
2958

3059
func (this *icingaHostCmd) Parse() Message {
3160
var msg Message
3261

33-
msg.Fields = []Field {
34-
{
35-
Header: "IPv4",
36-
Text: *this.icHostV4,
37-
},
62+
msg.Body_title = *this.icHostname + " is " + *this.icHostState
63+
64+
addFieldToMessage(&msg, true, "IPv4", this.icHostV4)
65+
addFieldToMessage(&msg, true, "IPv6", this.icHostV6)
66+
addFieldToMessage(&msg, true, "Notification type", this.icHostNotfnType)
67+
addFieldToMessage(&msg, true, "Notification user", this.icHostNotfnUser)
68+
timestamp, err := time.Parse("2006-01-02 15:04:05 -0700", *this.icDateTime)
69+
if err != nil {
70+
logrus.WithFields(logrus.Fields{
71+
"error": err,
72+
}).Warn("Couldn't parse supplied timestamp")
73+
panic("Abort")
74+
} else {
75+
time := convertToSlackDate(timestamp)
76+
addFieldToMessage(&msg, true, "Timestamp", &time)
3877
}
39-
// TODO
78+
79+
var buffer bytes.Buffer
80+
81+
buffer.WriteString("```\n")
82+
buffer.WriteString(*this.icHostCheckOutput)
83+
buffer.WriteString("```\n")
84+
if *this.icHostNotfnComment != "" {
85+
buffer.WriteString("Comment: ")
86+
buffer.WriteString(*this.icHostNotfnComment)
87+
}
88+
msg.Body = buffer.String()
89+
90+
if *this.icURL != "" {
91+
if err != nil {
92+
fmt.Errorf("WARNING: Couldn't parse specified url, skipping link: ", err)
93+
} else {
94+
params := url.Values{}
95+
params.Add("host", *this.icHostname)
96+
msg.Body_link = *this.icURL + "/monitoring/host/show?"+ params.Encode()
97+
}
98+
}
99+
100+
if icBadState[*this.icHostState] {
101+
msg.Color = "#ff5566"
102+
} else if icGoodState[*this.icHostState] {
103+
msg.Color = "#44bb77"
104+
} else {
105+
msg.Color = "#aa44ff"
106+
}
107+
108+
msg.Frontend = "Icinga2 Host Notification"
109+
msg.FrontendIconURL = "https://raw.githubusercontent.com/Icinga/icingaweb2/master/public/img/favicon.png"
40110

41111
return msg
112+
}
113+
114+
func addFieldToMessage(msg *Message, short bool, header string, field *string) {
115+
if *field != "" {
116+
msg.Fields = append(msg.Fields, Field{
117+
Header: header,
118+
Text: *field,
119+
Short: short,
120+
})
121+
}
122+
}
123+
124+
func convertToSlackDate(timestamp time.Time) string {
125+
return "<!date^" + strconv.FormatInt(timestamp.Unix(), 10) + "^{date_num} {time_secs}|" +
126+
timestamp.Format("Mon Jan 2 15:04:05 -0700 MST 2006")+ ">"
42127
}

listarg.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package main
22

33
import (
4-
"strings"
54
"bytes"
5+
"strings"
66
)
77

88
type fieldList []Field
@@ -15,7 +15,7 @@ func (e *FieldArgParseError) Error() string {
1515
return e.err
1616
}
1717

18-
func (s *fieldList) String() string{
18+
func (s *fieldList) String() string {
1919
var buffer bytes.Buffer
2020
for _, item := range *s {
2121
buffer.WriteString("Element Title: ")
@@ -46,4 +46,4 @@ func (s *fieldList) Set(val string) error {
4646
*s = append(*s, field)
4747
}
4848
return nil
49-
}
49+
}

sendmsg.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ package main
22

33
import (
44
"flag"
5-
"io/ioutil"
65
"fmt"
7-
"os"
6+
"github.com/sirupsen/logrus"
87
"gopkg.in/yaml.v2"
8+
"io/ioutil"
9+
"os"
910
)
1011

12+
var gitversion = "undefined"
13+
1114
func main() {
15+
logrus.SetOutput(os.Stdout)
16+
logrus.SetLevel(logrus.WarnLevel)
1217
// 'Simple' frontend - that is specifying each field manually
1318
simpleCommand := flag.NewFlagSet("simple", flag.ExitOnError)
1419
simpleFrontendCmd := simpleCmdData{}
@@ -66,6 +71,6 @@ func main() {
6671

6772
switch cfg.Backend {
6873
case "slack":
69-
send_with_slack(msg, cfg, "simple", "")
74+
send_with_slack(msg, cfg)
7075
}
7176
}

simple.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import "flag"
44

55
type simpleCmdData struct {
66
SimpleCFGLocation *string
7-
head *string
8-
title *string
9-
title_url *string
10-
body *string
11-
color *string
12-
fields fieldList
7+
head *string
8+
title *string
9+
title_url *string
10+
body *string
11+
color *string
12+
fields fieldList
1313
}
1414

1515
func (this *simpleCmdData) Init(flagSet *flag.FlagSet) {
@@ -30,5 +30,8 @@ func (this *simpleCmdData) Parse() Message {
3030
msg.Body_title = *this.title
3131
msg.Body_link = *this.title_url
3232
msg.Fields = this.fields
33+
34+
msg.Frontend = "simple"
35+
3336
return msg
34-
}
37+
}

slack.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
package main
22

33
import (
4-
"net/http"
54
"bytes"
65
"encoding/json"
76
"fmt"
87
"io/ioutil"
8+
"net/http"
99
)
1010

1111
type SlackField struct {
12-
Text string `json:"value,omitempty"`
12+
Text string `json:"value,omitempty"`
1313
Title string `json:"title,omitempty"`
14-
Short bool `json:"short,omitempty"`
14+
Short bool `json:"short,omitempty"`
1515
}
1616

1717
type SlackAttachments struct {
18-
Color string `json:"color,omitempty"`
19-
Text string `json:"text,omitempty"`
20-
Title string `json:"title,omitempty"`
21-
TitleLink string `json:"title_link,omitempty"`
22-
Fields []SlackField `json:"fields,omitempty"`
23-
Footer string `json:"footer,omitempty"`
24-
FooterIcon string `json:"footer_icon,omitempty"`
25-
Head string `json:"author_name,omitempty"`
18+
Color string `json:"color,omitempty"`
19+
Text string `json:"text,omitempty"`
20+
Title string `json:"title,omitempty"`
21+
TitleLink string `json:"title_link,omitempty"`
22+
Fields []SlackField `json:"fields,omitempty"`
23+
Footer string `json:"footer,omitempty"`
24+
FooterIcon string `json:"footer_icon,omitempty"`
25+
Head string `json:"author_name,omitempty"`
2626
}
2727

2828
type SlackMessage struct {
29-
Text string `json:"text,omitempty"`
30-
Attachments []SlackAttachments `json:"attachments,omitempty"`
29+
Text string `json:"text,omitempty"`
30+
Attachments []SlackAttachments `json:"attachments,omitempty"`
3131
}
3232

33-
func send_with_slack(msg Message, cfg Config, frontend string, frontendIcon string) {
33+
func send_with_slack(msg Message, cfg Config) {
3434
var body SlackMessage
3535
var attachement SlackAttachments
3636
attachement.Head = msg.Head
@@ -53,9 +53,9 @@ func send_with_slack(msg Message, cfg Config, frontend string, frontendIcon stri
5353
fields = append(fields, field)
5454
}
5555
attachement.Fields = fields
56-
attachement.Footer = frontend + " (sendmsg)"
57-
if frontendIcon != "" {
58-
attachement.FooterIcon = frontendIcon
56+
attachement.Footer = msg.Frontend + " (sendmsg "+ gitversion+ ")"
57+
if msg.FrontendIconURL != "" {
58+
attachement.FooterIcon = msg.FrontendIconURL
5959
}
6060
body.Attachments = []SlackAttachments{attachement}
6161

@@ -81,4 +81,4 @@ func send_with_slack(msg Message, cfg Config, frontend string, frontendIcon stri
8181
if bodyString != "ok" {
8282
fmt.Errorf("Slack didn't like what we sent, error: ", bodyString)
8383
}
84-
}
84+
}

types.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
package main
22

33
type Field struct {
4-
Text string
4+
Text string
55
Header string
6-
Short bool
6+
Short bool
77
}
88

99
type Message struct {
10-
Head string
11-
Body_title string
12-
Body_link string
13-
Body string
14-
Color string
15-
Fields []Field
10+
Head string
11+
Body_title string
12+
Body_link string
13+
Body string
14+
Color string
15+
Fields []Field
16+
Frontend string
17+
FrontendIconURL string
1618
}
1719

1820
type Config struct {
1921
Backend string `yaml:"backend"`
2022
Webhook string `yaml:"webhook"`
2123
}
2224

23-
type CmdData struct {}
25+
type CmdData struct{}

0 commit comments

Comments
 (0)