Skip to content

Commit

Permalink
slack sdk change
Browse files Browse the repository at this point in the history
Signed-off-by: manisha kumari <manisha.kumari@calsoftinc.com>
  • Loading branch information
ManishaKumari295 committed May 30, 2024
1 parent eb437c9 commit 753e018
Show file tree
Hide file tree
Showing 7 changed files with 448 additions and 12 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/sensu/sensu-slack-handler
go 1.14

require (
github.com/bluele/slack v0.0.0-20180528010058-b4b4d354a079
github.com/coreos/etcd v3.3.25+incompatible // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
Expand All @@ -18,6 +17,7 @@ require (
github.com/sensu/sensu-go/api/core/v2 v2.6.0
github.com/sensu/sensu-go/types v0.5.0 // indirect
github.com/sirupsen/logrus v1.7.0 // indirect
github.com/slack-go/slack v0.13.0 // indirect
github.com/spf13/afero v1.5.1 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v1.1.1 // indirect
Expand All @@ -31,4 +31,4 @@ require (
google.golang.org/grpc v1.35.0 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/gogo/protobuf v1.1.1 h1:72R+M5VuhED/KujmZVcIquuo8mBgX4oVda//DQb3PXo=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
Expand Down Expand Up @@ -107,6 +108,7 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand All @@ -122,6 +124,7 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
Expand Down Expand Up @@ -257,6 +260,8 @@ github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/slack-go/slack v0.13.0 h1:7my/pR2ubZJ9912p9FtvALYpbt0cQPAqkRy2jaSI1PQ=
github.com/slack-go/slack v0.13.0/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
Expand Down
35 changes: 25 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"os"
"strings"

"github.com/bluele/slack"
//"github.com/bluele/slack"
"github.com/sensu-community/sensu-plugin-sdk/sensu"
"github.com/sensu-community/sensu-plugin-sdk/templates"
corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/slack-go/slack"
)

// HandlerConfig contains the Slack handler configuration
Expand Down Expand Up @@ -186,19 +187,20 @@ func messageStatus(event *corev2.Event) string {
}
}

func messageAttachment(event *corev2.Event) *slack.Attachment {
// Manisha found
func messageAttachment(event *corev2.Event) slack.Attachment {
description, err := templates.EvalTemplate("description", config.slackDescriptionTemplate, event)
if err != nil {
fmt.Printf("%s: Error processing template: %s", config.PluginConfig.Name, err)
}

description = strings.Replace(description, `\n`, "\n", -1)
attachment := &slack.Attachment{
attachment := slack.Attachment{
Title: "Description",
Text: description,
Fallback: formattedMessage(event),
Color: messageColor(event),
Fields: []*slack.AttachmentField{
Fields: []slack.AttachmentField{
{
Title: "Status",
Value: messageStatus(event),
Expand All @@ -219,16 +221,29 @@ func messageAttachment(event *corev2.Event) *slack.Attachment {
return attachment
}

// Manisha found
func sendMessage(event *corev2.Event) error {
hook := slack.NewWebHook(config.slackwebHookURL)
err := hook.PostMessage(&slack.WebHookPostPayload{
Attachments: []*slack.Attachment{messageAttachment(event)},
hookmsg := &slack.WebhookMessage{
Attachments: []slack.Attachment{messageAttachment(event)},
Channel: config.slackChannel,
IconUrl: config.slackIconURL,
IconURL: config.slackIconURL,
Username: config.slackUsername,
})
}

err := slack.PostWebhook(config.slackwebHookURL, hookmsg)

//
//hook := slack.NewWebHook(config.slackwebHookURL)
//
//
//err := hook.PostMessage(&slack.WebHookPostPayload{
// Attachments: []*slack.Attachment{messageAttachment(event)},
// Channel: config.slackChannel,
// IconUrl: config.slackIconURL,
// Username: config.slackUsername,
//})
if err != nil {
return fmt.Errorf("Failed to send Slack message: %v", err)
return fmt.Errorf("Failed to send Slack message: %v ", err)
}

// FUTURE: send to AH
Expand Down
207 changes: 207 additions & 0 deletions test/Hello_old.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
{
"check": {
"command": "echo \"Hello {{ .annotations.helloworld | default `Sensu Integration` }} world!\"",
"handlers": [
"pagerduty"
],
"high_flap_threshold": 0,
"interval": 30,
"low_flap_threshold": 0,
"publish": true,
"runtime_assets": null,
"subscriptions": [
"example",
"entity:ip-172-31-27-229"
],
"proxy_entity_name": "",
"check_hooks": null,
"stdin": false,
"subdue": null,
"ttl": 0,
"timeout": 10,
"round_robin": false,
"duration": 0.008208882,
"executed": 1710240247,
"history": [
{
"status": 0,
"executed": 1710239677
},
{
"status": 0,
"executed": 1710239707
},
{
"status": 0,
"executed": 1710239737
},
{
"status": 0,
"executed": 1710239767
},
{
"status": 0,
"executed": 1710239797
},
{
"status": 0,
"executed": 1710239827
},
{
"status": 0,
"executed": 1710239857
},
{
"status": 0,
"executed": 1710239887
},
{
"status": 0,
"executed": 1710239917
},
{
"status": 0,
"executed": 1710239947
},
{
"status": 0,
"executed": 1710239977
},
{
"status": 0,
"executed": 1710240007
},
{
"status": 0,
"executed": 1710240037
},
{
"status": 0,
"executed": 1710240067
},
{
"status": 0,
"executed": 1710240097
},
{
"status": 0,
"executed": 1710240127
},
{
"status": 0,
"executed": 1710240137
},
{
"status": 0,
"executed": 1710240157
},
{
"status": 0,
"executed": 1710240187
},
{
"status": 0,
"executed": 1710240217
},
{
"status": 0,
"executed": 1710240247
}
],
"issued": 1710240247,
"output": "Hello Sensu Integration world!\n",
"state": "passing",
"status": 0,
"total_state_change": 0,
"last_ok": 1710240247,
"occurrences": 3317,
"occurrences_watermark": 3317,
"output_metric_format": "",
"output_metric_handlers": null,
"env_vars": null,
"metadata": {
"name": "helloworld",
"namespace": "default",
"labels": {
"sensu.io/managed_by": "sensuctl"
}
},
"secrets": null,
"is_silenced": false,
"scheduler": "memory",
"processed_by": "ip-172-31-27-229",
"pipelines": []
},
"entity": {
"entity_class": "agent",
"system": {
"hostname": "ip-172-31-27-229",
"os": "linux",
"platform": "ubuntu",
"platform_family": "debian",
"platform_version": "22.04",
"network": {
"interfaces": [
{
"name": "lo",
"addresses": [
"127.0.0.1/8",
"::1/128"
]
},
{
"name": "eth0",
"mac": "02:03:a3:e8:8e:fb",
"addresses": [
"172.31.27.229/20",
"fe80::3:a3ff:fee8:8efb/64"
]
}
]
},
"arch": "amd64",
"libc_type": "glibc",
"vm_system": "xen",
"vm_role": "guest",
"cloud_provider": "",
"processes": null
},
"subscriptions": [
"entity:ip-172-31-27-229",
"linux",
"ssl"
],
"last_seen": 1710240247,
"deregister": false,
"deregistration": {},
"user": "agent",
"redact": [
"password",
"passwd",
"pass",
"api_key",
"api_token",
"access_key",
"secret_key",
"private_key",
"secret"
],
"metadata": {
"name": "ip-172-31-27-229",
"namespace": "default",
"labels": {
"disk_critical": "97",
"disk_warning": "91"
},
"created_by": "sensu"
},
"sensu_agent_version": "6.11.0"
},
"id": "5f165346-09eb-4827-b05b-fc19cbc94bcd",
"metadata": {
"namespace": "default"
},
"pipelines": null,
"sequence": 92438,
"timestamp": 1710240247
}
1 change: 1 addition & 0 deletions test/event.json

Large diffs are not rendered by default.

Loading

0 comments on commit 753e018

Please sign in to comment.