While formatters are crucial to contextualizing alerts, it doesn't matter much if the alerts are never sent. This is where messages come in to play.
nagios-herald
provides a base Message class (Message::Base
) from which all Message subclasses inherit. Message::Base
knows that all messages have content and recipients; it's the job of the Message subclasses to define additional behavior (such as how to send a message).
Three message types are defined in nagios-herald
: Email, Pager, and IRC. The IRC class must be subclassed by a custom message class so that the send
function can be implemented. Feel free to cook up any new message types (i.e. CarrierPigeon, SmokeSignal) as you see fit.
Content generated by formatters is assigned to a message in Executor
via the following code:
formatter.generate_message_content
message.content = formatter.content
You may write your own message classes and use them with nagios-herald. These message classes can live in any location. A custom message class should subclass Message::Base.
To use a custom message class, you must add a stanza to commands.cfg
that defineds a command that uses that message class. Commands for custom messages must provide a message-type
that matches your message class name as well as a message-dir
that specifies the directory in which your custom message class is stored.
# notify by carrier pigeon
define command {
command_name notify-host-by-pigeon
command_line /usr/local/nagios-herald/bin/nagios-herald --message-dir=/usr/local/nagios-herald-messages/ --message-type=pigeon --formatter=$_HOSTMESSAGE_FORMATTER_NAME$ --nagios-cgi-url=http://nagios.example.com/nagios/cgi-bin/cmd.cgi [email protected]
}
define command {
command_name notify-service-by-pigeon
command_line /usr/local/nagios-herald/bin/nagios-herald --message-dir=/usr/local/nagios-herald-messages/ --message-type=pigeon --formatter=$_SERVICEMESSAGE_FORMATTER_NAME$ --nagios-cgi-url=http://nagios.example.com/nagios/cgi-bin/cmd.cgi [email protected]
}
To use your custom message class, you can specify the command_name
that uses your message class as a notification command for a host or service. As you can specify more than one notification command per service, you may specify a custom message command along with the command for a provided message to send multiple kinds of messages:
define service {
...
service_notification_commands notify-service-by-email,notify-service-by-carrier-pigeon
_message_formatter_name performance_context
}
The same formatter will be used for each message type.
If your custom message type is different enough from the other message types that use a certain formatter, it may be necessary to use a new key in the content
hash to add content specific to that message type. Content should be added to the content
hash in the formatter; the send
function of the custom message class should then use that content to construct and send the message.
As an example example, the CarrierPigeon
message class may send text contained in content[:handwritten]
. A formatter that is used with the CarrierPigeon
class should implement a method that adds content to handwritten
sections just as the add_html()
method adds content to html
sections. The formatter should add content that should be delivered by carrier pigeon to the handwritten
hash and still add content to be delivered in an HTML email to the html
hash using the add_html()
method.
- File names MUST be lower-cased and underscored.
- Class names MUST be CamelCased.
- A
message-type
specified on the command line (or in a command definition) should match a snake_cased message class name with the exception that a class name that is all upper case should be written as one word. ** Themessage-type
for a class namedCarrierPigeon
iscarrier_pigeon
** Themessage-type
for a class namedIRC
isirc
** Themessage-type
for a class namedInternalIrc
isinternal_irc
** Themessage-type
for a class namedInternalIRC
isinternal_i_r_c