How do I add hostname to event titles and messages submitted by my custom agent checks?

When you submit events to your datadog account from a custom agent check, they will get a host tag associated with them by default. But it can sometimes be useful to include the hostname as part of the event title or content. This can be achieved if, in your custom check's code, you import "get_hostname" from "util", and then feed "self.agentConfig" as an argument to "get_hostname", as in this example:

import time
from checks import AgentCheck
from utils import get_hostname

hostname = get_hostname(self.agentConfig)
class TestCheck(AgentCheck): def check(self, instance):
hostname = get_hostname(self.agentConfig)
self.event({
'timestamp': int(time.time()),
'event_type': 'test',
'msg_title': 'a test event occurred on host %s' % (hostname,),
'msg_text': 'test message content for host %s' % (hostname,),
'aggregation_key': "test1234"
})



Such an agent check would create the following event in your event stream.



In this example, the host tag would have been applied even without referencing get_hostname(self.agentConfig), but that reference added the hostname to the event title and content. 

Have more questions? Submit a request

0 Comments

Article is closed for comments.
Powered by Zendesk