How do I create an alert after configuring a hook?

Hi all,

I want to use hooks ( Hooks Docs ) to create alerts based on the different machine states of my VM’s.

I created a hook that successfully reports machines going to the ‘SHUTDOWN’ state:

EXECUTION LOG
   ID       TIMESTAMP    RC EXECUTION
    0     06/02 12:59   255 ERROR

How do I create alerts from this point? For example, I want a message to appear on the Sunstone interface showing my hook, or I want an email to be send out after a hook is activated. Is this possible? Thanks in advance!

1 Like

Hi @anoesheartje,

First, note that you have an error in the hook execution. You can see information about an execution with onehook show <HOOK_ID> -e <EXEC_ID>.

How do I create alerts from this point? For example, I want a message to appear on the Sunstone interface showing my hook, or I want an email to be send out after a hook is activated. Is this possible?

You can do almost everything by programming a script like this. Maybe sending an email is more straightforward than displaying a message in Sunstone, but it could be possible as well.

Cheers

Hello @rdiaz
Adding to the Hook concept.
I want to update the value of User-input as soon as VM is instantiated.
How can I achieve this using CLI and XML-RPC API. Any onevm update commands are there for updating user-inputs of Running VM?

Hi @Rahul_Sharma,

Check the onevm updateconf command.

Cheers.

Hello @rdiaz @ahuertas
Can we create hook for API VM action?
I have created API Hooks

NAME = hook-API
TYPE = api
COMMAND = “vm_action.rb”
ARGUMENTS = $API
CALL = “one.vm.action”
ARGUMENTS_STDIN = yes

But it is not working on creating actions like on VM poweroff or terminate.
Could you please suggest if something is missing in the above commands?

Thanks in advance!!

Hi @Rahul_Sharma ,

Note that the path in COMMAND has to be relative to /var/lib/one/remotes/hooks/ (e.g. COMMAND = "ft/host_error.rb").

Cheers.

Here’s a hook I set up to send a message to a Slack channel when someone starts a VM:

$ onehook show 1
HOOK 1 INFORMATION
ID                : 1
NAME              : hook-vm
TYPE              : state
LOCK              : None

HOOK TEMPLATE
ARGUMENTS="$TEMPLATE"
COMMAND="vm-create-hook.py"
LCM_STATE="PROLOG"
REMOTE="NO"
RESOURCE="VM"
STATE="ACTIVE"


$ cat /var/lib/one/remotes/hooks/vm-create-hook.py
#!/usr/bin/python3

import sys
import base64
import xml.etree.ElementTree as ET
import requests
slackwebhook = "https://hooks.slack.com/services/TBhsgahjdfgahjfdgajhdfaAWYbBy"

decodedt = base64.b64decode(sys.argv[1])
tree = ET.fromstring(decodedt.decode('utf-8'))

def parsexml(toplevel, elem1, elem2):
      elemreturn = []
      for x in tree.iter(toplevel):
            elemreturn.append(x.find('UNAME'))
            elemreturn.append(x.find('NAME'))
            return elemreturn

def main():
      extractelems = parsexml('VM', 'UNAME', 'NAME')
      vmuname = extractelems[0].text
      vmname = extractelems[1].text
      slackmsg = "OpenNebula user " + vmuname + " created VM " + vmname + " on Test system"
      payload = {"text": slackmsg}
      r = requests.post(slackwebhook, json=payload)

if __name__ == '__main__':
      main()
2 Likes