OP5 Monitor

Nacoma save hooks

Introduction

This topic explains how to create Nacoma save hooks. It is for a technical audience with a good knowledge of scripting.

OP5 Monitor is delivered with a default set of Nacoma save hooks in folder /opt/monitor/op5/nacoma/hooks/save, which are triggered every time a save occurs in OP5 Monitor. You can create your own custom save hooks in the same folder, allowing you to perform environment-specific operations whenever users save data in OP5 Monitor.

How do save hooks work?

OP5 Monitor executes save hooks every time configuration files are flushed from the database and (Undefined variable: OP5-Monitor/components.naemon) has verified them. Hook arguments are as follows:

  • The first argument is the user name of the person clicking the Save button.
  • The second argument is the full name of the person, retrieved by matching the user name to an OP5 Monitor contact_name. If no matching contact is found, the second argument is a null string.

The hooks receive as input the changelog entries accumulated since the user last imported the configuration files (undo) or flushed them from the database to files (save).

Note that the hooks are run:

  • By the web server when a Save command is issued from the user interface.
  • As the console user when a Save command is issued by the cli tools.

Create a save hook

To create your own save hook, place a script in folder /opt/monitor/op5/nacoma/hooks/save. You can use any scripting language. Any commands in your script will be executed on save, as long as your script is executable.

Library and functions

You can use any of the scripts in the same folder as a basis for creating your own. The existing scripts use library /usr/lib/python2.7/site-packages/nacoma/hooks.py.

Useful functions and attributes you can use in your scripts are summarised in the following tables. See Example script for more details.

Function Description
is_new() True if the object was created.
is_deleted() True if the object was deleted.
is_renamed() True if the object was renamed.

 

Attribute Description
type Object type.
id ID in the object database before the logged action was applied (a deleted object still has an object ID).
oldname (Undefined variable: OP5-Monitor/components.naemon) name of the object before the action (null for a created object).
newname (Undefined variable: OP5-Monitor/components.naemon) name of the object after the action (null for a deleted object).
username Username of the user who made the change.
time Time of the change.
pre PHP serialised representation of the object before the change (null for a created object).
post PHP serialised representation of the object after the change (null for a deleted object).

Example script

The following example script prints to the Nachos log.

#!/usr/bin/env python2
        
import sys
        
from nacoma.hooks import Change
        
# Using print here will send the information to the logs
        
def main():
    for line in sys.stdin:
        change = Change(line)
        # Useful functions
        if change.is_deleted():
            print("Deleted")
        if change.is_new():
            print("New")
        if change.is_renamed():
            print("Renamed")
     	 # Useful attributes
        print(change.type)
        print(change.id)
        print(change.oldname)
        print(change.newname)
        print(change.username)
        print(change.time)
        print(change.pre)
        print(change.post)

if __name__ == '__main__':
    main()

Note: Although the example provided is written in Python, other scripting languages work just as well.