1.1. Definire nuove funzioni per le expr

1.1.1. Introduzione

In questo tutorial verra’ spiegato come:

  • Creare un nuovo modulo python che definisce nuovi simboli per le expr di Sanet3

  • Configuare Sanet per caricare il modulo

  • Testare una expr che utilizza i nuovi simboli definiti.

Per una spiegazione dettagliata sulle regole da seguire per scrivere correttamente un nuovo modulo si rimanda alla sezione agent-plugin-extend-agent.

1.1.2. Creare la directory con i moduli

Creare una directory che conterra’ i nostri nuovi moduly python per gli agenti di sanet:

export MY_MODULES_DIR=/tmp/my_sanet_modules

mkdir $MY_MODULES_DIR

1.1.3. Aggiungere la directory dei moduly al PYTHONPATH

Per far si che i nuovi moduli python siano importabili da Sanet bisogna aggiornare il PYTHONPATH:

export PYTHONPATH=$PYTHONPATH:$MY_MODULES_DIR

1.1.4. Creare il modulo python

Creare il modulo python:

vim $MY_MODULES_DIR/mymodule.py

Con il seguente contenuto:

from sanet_poller.lib.exceptions import PollException

#-----------------------------------------------------------------------------#

def fun_myfunction(context, val):

        log = context.getLog()

        log.debug("Input value: %s", val)

        if val < 0:
                raise PollException("Invalid input value: %s" % (val))

        return val

#-----------------------------------------------------------------------------#

EXPR_SYMBOLS = {
        'myfunction': fun_myfunction,
}

1.1.5. Configurare Sanet

Aggiungere al file:

{{SANET_INSTALL_DIR}}/conf/settings.py

Aggiungere

LOCAL_POLLER_EXTRA_MODULES += [
        'mymodule',
]

1.1.6. Verificare che il modulo sia utilizzabile nelle expr

Test1

Lanciare:

sanet-manage exec_expr localhost ' myfunction(1) + 1 '

Output:

2017-11-30 09:58:35,793 INFO MainThread > Loading extra symbols from ['mymodule']

VALUE: 2
INFOS:
expression  myfunction(1) + 1  expanded to (1 + 1)

Test 2

Lanciare:

sanet-manage exec_expr localhost ' myfunction(-1) + 1'

Output:

2017-11-30 09:58:54,535 INFO MainThread > Loading extra symbols from ['mymodule']

VALUE: None
INFOS:
ERROR: <class 'sanet_poller.lib.exceptions.PollException'>:Error while evaluating expression: Invalid input value: -1
TRACE : Traceback (most recent call last):
  File "/mnt/labs/sanet3/sanet3-svn/branches/unstable/sanet/core/management/commands/exec_expr.py", line 463, in text_expression
    value = expreval(logger, exec_context, expr_tree)
  File "/mnt/labs/sanet3/sanet3-svn/branches/unstable/sanet-poller/sanet_poller/lib/evaluation/__init__.py", line 1029, in expreval
    raise PollException("Error while evaluating expression: %s " % (ex)) #, traceback.format_exc()) )
PollException: Error while evaluating expression: Invalid input value: -1

1.1.7. Creare un datagroup per il nuovo modulo

Creare un datagroup-template che utilizza la nuova funzione:

cat | sanet-cli -c -T  <<EOF

datagroup-template dg-test

    title "Datagroup di esempio per usare la funzione myfunction()"

    minperiod 60

    condition check

       title "Controllo myfunction()"

       expr   myfunction(1)

       max-tries 1

    exit

exit

node localhost
    datagroup dg-test
    exit
exit

EOF

Testare il nuovo datagroup:

# sanet-cli

execute datagroup path localhost;;dg-test verbose full no-dependson