Skip to content

Commit b2871df

Browse files
committed
Complete migration to logrus
1 parent 9390dee commit b2871df

File tree

4 files changed

+20
-29
lines changed

4 files changed

+20
-29
lines changed

Gopkg.lock

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

icinga_host.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"flag"
55
"bytes"
66
"net/url"
7-
"fmt"
87
"time"
98
"github.com/sirupsen/logrus"
109
"strconv"
@@ -70,10 +69,9 @@ func (this *icingaHostCmd) Parse() Message {
7069
logrus.WithFields(logrus.Fields{
7170
"error": err,
7271
}).Warn("Couldn't parse supplied timestamp")
73-
panic("Abort")
7472
} else {
75-
time := convertToSlackDate(timestamp)
76-
addFieldToMessage(&msg, true, "Timestamp", &time)
73+
timeStr := convertToSlackDate(timestamp)
74+
addFieldToMessage(&msg, true, "Timestamp", &timeStr)
7775
}
7876

7977
var buffer bytes.Buffer
@@ -88,13 +86,9 @@ func (this *icingaHostCmd) Parse() Message {
8886
msg.Body = buffer.String()
8987

9088
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-
}
89+
params := url.Values{}
90+
params.Add("host", *this.icHostname)
91+
msg.Body_link = *this.icURL + "/monitoring/host/show?"+ params.Encode()
9892
}
9993

10094
if icBadState[*this.icHostState] {

sendmsg.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"flag"
5-
"fmt"
65
"github.com/sirupsen/logrus"
76
"gopkg.in/yaml.v2"
87
"io/ioutil"
@@ -25,8 +24,7 @@ func main() {
2524
icingaHostCmd.Init(icingaHostCommand)
2625

2726
if len(os.Args) < 2 {
28-
fmt.Print("Please use one of [simple, icingaHost]\n")
29-
return
27+
logrus.Fatal("Please use one of [simple, icingaHost]")
3028
}
3129

3230
var cfglocation *string
@@ -48,12 +46,10 @@ func main() {
4846
if contents, err := ioutil.ReadFile(*cfglocation); err == nil {
4947
err = yaml.UnmarshalStrict(contents, &cfg)
5048
if err != nil {
51-
fmt.Println("ERROR: Couldn't parse the configuration file!", err)
52-
os.Exit(-1)
49+
logrus.WithError(err).Fatal("Couldn't parse the configuration file!")
5350
}
5451
} else {
55-
fmt.Println("ERROR: Couldn't read the configuration file!", err)
56-
os.Exit(-1)
52+
logrus.WithError(err).Fatal("Couldn't read the configuration file!")
5753
}
5854

5955
var msg Message
@@ -65,8 +61,10 @@ func main() {
6561
}
6662

6763
if msg.Body == "" || msg.Body_title == "" {
68-
fmt.Println("ERROR: Either title or body are missing. Aborting now!")
69-
return
64+
logrus.WithFields(logrus.Fields{
65+
"Body": msg.Body,
66+
"Title": msg.Body_title,
67+
}).Fatal("Either title or body are missing!")
7068
}
7169

7270
switch cfg.Backend {

slack.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package main
33
import (
44
"bytes"
55
"encoding/json"
6-
"fmt"
76
"io/ioutil"
87
"net/http"
8+
"github.com/sirupsen/logrus"
99
)
1010

1111
type SlackField struct {
@@ -61,24 +61,23 @@ func send_with_slack(msg Message, cfg Config) {
6161

6262
jsonBody, err := json.Marshal(body)
6363
if err != nil {
64-
fmt.Errorf("Coudln't masrshal message for JSON transport: ", err)
65-
return
64+
logrus.WithError(err).Fatal("Couldn't marshal message for JSON transport!")
6665
}
6766
resp, err := http.Post(cfg.Webhook, "application/reader", bytes.NewBuffer(jsonBody))
6867
if err != nil {
69-
fmt.Errorf("Coudln't POST message to webhook: ", err)
70-
return
68+
logrus.WithError(err).Fatal("Couldn't POST message to webhook!")
7169
}
7270
defer resp.Body.Close()
7371

7472
bodyBytes, err := ioutil.ReadAll(resp.Body)
7573
if err != nil {
76-
fmt.Errorf("Couldn't read body", err)
74+
logrus.WithError(err).Warnf("Couldn't read back response body!")
7775
}
7876
bodyString := string(bodyBytes)
79-
fmt.Print(bodyString)
8077

8178
if bodyString != "ok" {
82-
fmt.Errorf("Slack didn't like what we sent, error: ", bodyString)
79+
logrus.WithFields(logrus.Fields{
80+
"rc": bodyString,
81+
}).Warnf("Slack didn't return 'OK'")
8382
}
8483
}

0 commit comments

Comments
 (0)