Skip to content

Commit 9e6a60b

Browse files
committed
Increase details of Channels documentation
With a release approaching, the Channels documentation got more information about how the Channel system actually work and how to build your own channels. The internal/changes/examples JSON files were inlined to ease the readability and updated, as they contained partially outdated information. For the pkg/plugin part, some API documentation was added as this might get used by external users.
1 parent 13ac3d3 commit 9e6a60b

File tree

1 file changed

+83
-25
lines changed

1 file changed

+83
-25
lines changed

notifications/plugin/plugin.go

Lines changed: 83 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const (
2222
MethodSendNotification = "SendNotification"
2323
)
2424

25-
// ConfigOption describes a config element of the channel form
25+
// ConfigOption describes a config element.
2626
type ConfigOption struct {
2727
// Element name
2828
Name string `json:"name"`
@@ -80,12 +80,23 @@ func (c ConfigOptions) Value() (driver.Value, error) {
8080
return json.Marshal(c)
8181
}
8282

83-
// Info contains plugin information.
83+
// Info contains channel plugin information.
8484
type Info struct {
85-
Type string `db:"type" json:"-"`
86-
Name string `db:"name" json:"name"`
87-
Version string `db:"version" json:"version"`
88-
Author string `db:"author" json:"author"`
85+
// Type of the channel plugin.
86+
//
87+
// Not part of the JSON object. Will be set to the channel plugin file name before database insertion.
88+
Type string `db:"type" json:"-"`
89+
90+
// Name of this channel plugin in a human-readable value.
91+
Name string `db:"name" json:"name"`
92+
93+
// Version of this channel plugin.
94+
Version string `db:"version" json:"version"`
95+
96+
// Author of this channel plugin.
97+
Author string `db:"author" json:"author"`
98+
99+
// ConfigAttributes contains multiple ConfigOption(s) as JSON-encoded list.
89100
ConfigAttributes ConfigOptions `db:"config_attrs" json:"config_attrs"`
90101
}
91102

@@ -94,51 +105,93 @@ func (i *Info) TableName() string {
94105
return "available_channel_type"
95106
}
96107

108+
// Contact to receive notifications for the NotificationRequest.
97109
type Contact struct {
98-
FullName string `json:"full_name"`
110+
// FullName of a Contact as defined in Icinga Notifications.
111+
FullName string `json:"full_name"`
112+
113+
// Addresses of a Contact with a type.
99114
Addresses []*Address `json:"addresses"`
100115
}
101116

117+
// Address to receive this notification. Each Contact might have multiple addresses.
102118
type Address struct {
103-
Type string `json:"type"`
119+
// Type field matches the Info.Type, effectively being the channel plugin file name.
120+
Type string `json:"type"`
121+
122+
// Address is the associated Type-specific address, e.g., an email address for type email.
104123
Address string `json:"address"`
105124
}
106125

126+
// Object which this NotificationRequest is all about, e.g., an Icinga 2 Host or Service object.
107127
type Object struct {
108-
Name string `json:"name"`
109-
Url string `json:"url"`
110-
Tags map[string]string `json:"tags"`
128+
// Name depending on its source, may be "host!service" when from Icinga 2.
129+
Name string `json:"name"`
130+
131+
// Url pointing to this Object, may be to Icinga Web.
132+
Url string `json:"url"`
133+
134+
// Tags defining this Object, may be "host" and "service" when from Icinga 2.
135+
Tags map[string]string `json:"tags"`
136+
137+
// ExtraTags attached, may be a host or service groups when form Icinga 2.
111138
ExtraTags map[string]string `json:"extra_tags"`
112139
}
113140

141+
// Incident of this NotificationRequest, grouping Events for this Object.
114142
type Incident struct {
115-
Id int64 `json:"id"`
116-
Url string `json:"url"`
143+
// Id is the unique identifier for this Icinga Notifications Incident, allows linking related events.
144+
Id int64 `json:"id"`
145+
146+
// Url pointing to the Icinga Notifications Web module's Incident page.
147+
Url string `json:"url"`
148+
149+
// Severity of this Incident.
117150
Severity string `json:"severity"`
118151
}
119152

153+
// Event indicating this NotificationRequest.
120154
type Event struct {
121-
Time time.Time `json:"time"`
122-
Type string `json:"type"`
123-
Username string `json:"username"`
124-
Message string `json:"message"`
155+
// Time when this event occurred, being encoded according to RFC 3339 when passed as JSON.
156+
Time time.Time `json:"time"`
157+
158+
// Type of this event, e.g., a "state" change, "mute" or "unmute". See further ./internal/event/event.go
159+
Type string `json:"type"`
160+
161+
// Username may contain a user triggering this event, depending on the event's source.
162+
Username string `json:"username"`
163+
164+
// Message of this event, might be a check output when the related Object is an Icinga 2 object.
165+
Message string `json:"message"`
125166
}
126167

168+
// NotificationRequest is being sent to a channel plugin via Plugin.SendNotification to request notification dispatching.
127169
type NotificationRequest struct {
128-
Contact *Contact `json:"contact"`
129-
Object *Object `json:"object"`
170+
// Contact to receive this NotificationRequest.
171+
Contact *Contact `json:"contact"`
172+
173+
// Object associated with this NotificationRequest, e.g., an Icinga 2 Service Object.
174+
Object *Object `json:"object"`
175+
176+
// Incident associated with this NotificationRequest.
130177
Incident *Incident `json:"incident"`
131-
Event *Event `json:"event"`
178+
179+
// Event being responsible for creating this NotificationRequest, e.g., a firing Icinga 2 Service Check.
180+
Event *Event `json:"event"`
132181
}
133182

183+
// Plugin defines necessary methods for a channel plugin.
184+
//
185+
// Those methods are being called via the internal JSON-RPC and allow channel interaction. Within the channel's main
186+
// function, the channel should be launched via RunPlugin.
134187
type Plugin interface {
135-
// GetInfo returns the corresponding plugin *Info
188+
// GetInfo returns the corresponding plugin *Info.
136189
GetInfo() *Info
137190

138-
// SetConfig sets the plugin config, returns an error on failure
191+
// SetConfig sets the plugin config, returns an error on failure.
139192
SetConfig(jsonStr json.RawMessage) error
140193

141-
// SendNotification sends the notification, returns an error on failure
194+
// SendNotification sends the notification, returns an error on failure.
142195
SendNotification(req *NotificationRequest) error
143196
}
144197

@@ -161,7 +214,10 @@ func PopulateDefaults(typePtr Plugin) error {
161214
return json.Unmarshal(defaultConf, typePtr)
162215
}
163216

164-
// RunPlugin reads the incoming stdin requests, processes and writes the responses to stdout
217+
// RunPlugin serves the RPC for a Channel Plugin.
218+
//
219+
// This function reads requests from stdin, calls the associated RPC method, and writes the responses to stdout. As this
220+
// function blocks, it should be called last in a channel plugin's main function.
165221
func RunPlugin(plugin Plugin) {
166222
encoder := json.NewEncoder(os.Stdout)
167223
decoder := json.NewDecoder(os.Stdin)
@@ -223,7 +279,9 @@ func RunPlugin(plugin Plugin) {
223279
wg.Wait()
224280
}
225281

226-
// FormatMessage formats a notification message and adds to the given io.Writer
282+
// FormatMessage formats a NotificationRequest message and adds to the given io.Writer.
283+
//
284+
// The created message is a multi-line message as one might expect it in an email.
227285
func FormatMessage(writer io.Writer, req *NotificationRequest) {
228286
if req.Event.Message != "" {
229287
msgTitle := "Comment"

0 commit comments

Comments
 (0)