Check_MK - The Official Documentation
Last updated: September 21. 2012
1. The Socket API
The Event Daemon provides access to its current status via a Unix socket and optionally also via TCP. Also commands can be issued that way. This channel is currently being used by:
If your scripts run on the same host as the Event Daemon, then the safest and easiest way is to use the Unix socket. In OMD the path to this socket is tmp/run/mkeventd/status. In a manual installation the path is specified with the option -S to the mkeventd.
The access and protocol is very similar to - but not entirely the same as - MK Livestatus. If you want to access the socket via shell then you can use the small tool unixcat. Details and hints how to do this are documented in the Livestatus documentation. For sake of simplicity only a very basic portion of the protocol is implemented:
With the command GET ... you have access to the following tables:
The following simple example queries all information about the current events:
OMD[mysite]:~$ echo 'GET events' | unixcat tmp/run/mkeventd/status | head -c 2000 [['event_id', 'event_count', 'event_text', 'event_first', 'event_last', 'event_comment', 'event_sl', 'event_host', 'event_contact', 'event_application', 'event_pid', 'event_priority', 'event_facility', 'event_rule_id', 'event_state', 'event_phase', 'event_owner', 'event_match_groups'], [108, 1, u'pam_unix(cron:session): session opened for user root by (uid=0)', 1348153621.0, 1348153621.0, '', 0, u'pam.hirni.de', '', u'CRON', u'10797', 6, 10, 'alles', 0, 'open', '', (u'pam',)], [109, 1, u'(root) CMD (logger hirn`date +)', 1348153621.0, 1348153621.0, '', 0, u'root.hirni.de', '', u'CRON', u'10801', 6, 9, 'alles', 0, 'open', '', (u'root',)], [110, 1, u'(CRON) error (grandchild #10801 failed with exit status 2)', 1348153621.0, 1348153621.0, '', 0, u'CRON.hirni.de', '', u'CRON', u'10797', 6, 9, 'alles', 0, 'open', '', (u'CRON',)], [111, 1, u'8EE75C4398: uid=0 from=<root>', 1348153621.0, 1348153621.0, '', 0, u'8EE75C4398.hirni.de', '', u'postfix/pickup', u'19533', 6, 2, 'alles', 0, 'open', '', (u'8EE75C4398',)], ...
As you can see the result is a list. The first entry of the list is always the list of the columns headers. Each further entry is one row of data (also a Python list) with value corresponding to the column headers.
The following small examply program in Python shows how to connect to the socket and retrieve the results in well formatted Python objects:
#!/usr/bin/python # Example program for accessing status of Event Console import socket, os # Create Unix socket and connect to status socket path = os.getenv("OMD_ROOT") + "/tmp/run/mkeventd/status" sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(path) # Send query (example: only open events) sock.send("GET events\nFilter: event_phase = open") # Read response and convert Python source to Python object response_text = "" while True: chunk = sock.recv(8192) response_text += chunk if not chunk: break response = eval(response_text) # The name of the column headers are the first item of the result list headers = response # Output all results for row in response[1:]: with_headers = zip(headers, row) with_headers.sort() for key, value in with_headers: print "%-20s: %s" % (key, value) print
2. Sending commands
The status socket can also be used for sending commands. Instead of GET you write COMMAND. After a space append the name of the command and its arguments separated by semicola. The following example will delete the event with the id 597:
OMD[mysite]:~$ echo 'COMMAND DELETE;597;omdadmin' | unixcat tmp/run/mkeventd/status None
In case of an error instead of None a text is being returned:
OMD[mysite]:~$ echo 'COMMAND DELETE;5697;omdadmin' | unixcat tmp/run/mkeventd/status ERROR: No event with id 5697
Here is complete table of all available commands: