Database Message Functions

Overview

The db_message_count and db_message_last functions retrieve message records from the database and can be used to check the existence of events or to correlate time series and messages.

Reference

db_message_count

db_message_count(string interval, string type, string source
                          [, string tags | map tags
                                    [, string entity[, string expression]]]) long

Returns the number of message records matching the specified interval, message type, message source, tags, entity, and expression. See matching rules below.

/*
  Returns message count for the same type, source, tags, and entity as in the current message.
  The current message is excluded.
*/
db_message_count('15 minute', type, source, tags, entity)
/*
  Returns message count for the same type, source, entity as in the current command and ANY tags.
  The current message is excluded.
*/
db_message_count('12 hour', type, source, null, entity)

db_message_last

db_message_last(string interval, string type, string source
                          [, string tags | map tags
                                    [, string entity[, string expression]]]) Message

Returns the most recent message record for the specified interval, message type, message source, tags, entity, and expression. See Matching Rules.

/*
  Returns the most recent message for the same type, source as in the current window and ANY entity and tags.
  The current message is excluded.
*/
db_message_last('15 minute', type, source)

The record fields can be accessed using dot notation, for example:

db_message_last('1 hour', 'webhook', 'github').timestamp
Name Type Description
entity string Entity name.
type string Message type.
source string Message source.
tags map Message tags.
severity string Message severity code.
timestamp string Record time as Unix time in milliseconds.
message string Message text.
date string null. Use timestamp field instead.

db_messages

db_messages(string interval, string type, string source
                          [, string tags | map tags
                                    [, string entity[, string expression]]]) [Message]

Returns a list of message records matching the specified interval, message type, message source, tags, entity, and expression. See Matching Rules.

If no message is found, an empty [] list is returned.

The messages in the list are sorted by time in the ascending order, with the most recent message at the start of the list.

  • To access the n-th element in the list, use square brackets [index] or get(index) method.
  • To access the first (most recent) record use index 0, for example db_messages('1 hour', type, source)[0].
  • The first (most recent) record can be also retrieved with the db_message_last function.

Fields of the returned Message objects can be accessed using dot notation, for example db_messages('1 hour', 'webhook', '')[0].timestamp.

Message Date

That date field in the message object is null. The record time is stored in the timestamp field as Unix time in milliseconds.

Matching Rules

Interval

  • Selection interval is specified in count units, for example, 1 hour.
  • End of selection interval is set to the timestamp of the last command in the window. As a result, the current command is excluded.

Type

  • If the message type argument is specified as null or an empty string '', all types are matched.

Source

  • If the message source argument is specified as null or an empty string '', all sources are matched.

Entity

  • If the entity argument is not specified, the current entity in the window is used for matching.
  • If the entity argument is specified as null or empty string '' or * wildcard, all entities are matched.

Tags

  • If tags argument is specified as null or an empty string '', all tags are matched.
  • To match records with empty tags use 'tags.isEmpty() = true' or 'tags.size() = 0' in expression.
  • tags argument matches records that include the specified tags but can also include other tags.
  • tags argument can be specified as follows:
    • String containing one or multiple name=value pairs separated with comma: 'tag1=value1,tag2=value2'.
    • Map: ['tag1':'value1', 'tag2':'value2']
    • The tags field representing the grouping tags of the current window.

Expression

  • The expression field can include the following fields and supports wildcards in field values:
    • message
    • type
    • source
    • severity
    • entity
    • tags and tags.{name}

Examples

db_message_count Examples

/* Checks if the average exceeds 20 and the 'compaction' message was not received
within the last hour for the current entity. */
avg() > 20 && db_message_count('1 hour', 'compaction', '') == 0

/* Checks if the average exceeds 80 and there is an event with 'type=backup-error'
received within the last 15 minutes for entity 'nurswgvml006'. */
avg() > 80 && db_message_count('15 minute', 'backup-error', '', '', 'nurswgvml006') > 0

/* Counts messages within the previous 60 minutes
for 'type=compaction', any source, any tags and all entities. */
db_message_count('1 hour', 'compaction', '',  '', '*')

/* Counts messages with the same text as in the last command, but from different users. */
db_message_count('1 minute', 'webhook', 'slack', 'event.type=' + tags.event.type, entity,
                 'message=' + message + 'AND tags.event.user!=' + tags.event.user)

db_message_last Examples

last_msg = db_message_last('60 minute', 'logger', '')
/* Check that the average exceeds 50 and the severity of the last message with type 'logger'
for the current entity is greater than or equal to 'ERROR'. */
avg() > 50 && last_msg != null && last_msg.severity.toString() >= "6"
/* Retrieves the last message with text beginning 'docker start sftp*'. */
db_message_last('1 minute', 'webhook', 'slack', 'event.channel=D7UKX9NTG,event.type=message',
                'slack', 'message LIKE "docker start sftp*"')

/* Returns the most recent message within 1 day for the current entity,
containing tag 'api_app_id=583' and regardless of type or source. */
db_message_last('1 day', null, null, ["api_app_id":"583"], entity)

/* Returns message with type 'webhook' and empty tags. */
db_message_last('15 second', 'webhook', '',  '', '', "tags.isEmpty()=true")

db_messages Examples

/* Retrieves messages with the text ending '*Selected' and any tags. */
db_messages('30 second', 'webhook', 'axibase-bot', '', 'slack', 'message LIKE "*Selected"')
/* Retrieves messages with severety 'Warning' within 15 second and send values of 'command' tag in notification. */
msgs = db_messages('15 second', 'logger', '', '', '', 'severity="warning"')

@foreach{m : msgs}
@{m.tags.get('command')}
@end{}