Hello all,
I have been attempting to find a way to add the creator of a VM and the date it was created to the attributes of the new VM. I have looked at Bryan McClellan script https://www.markiiisys.com/blog/vm-alarm-script/ and I create the attributes in vCenter first (vm.owner and vm.provisioned) and then I place the script into the VCSA at /root/vm.alarm-attr.py. I make the script executable with chmod +x and then create an alarm to run the script pointing to /root/vm.alarm-attr.py when someone deploys a VM. Although he doesn't specify, I created the attributes type as "virtual machine" and after that did not work I deleted it and created attributes as type "global" also with no luck in adding values after VM deployment.
The alarm runs and the script shows no errors in the events under the newly created VM, but the attributes values to not receive the data. I am new to python so I can't figure out what is not working.
Here is the script:
#!/usr/bin/python
##
## To be ran on the VCSA and called via alarm rule
##
import sys
from getpass import getpass
from datetime import datetime
import ssl
import os
sys.path.extend(os.environ['VMWARE_PYTHON_PATH'].split(';'))
from pyVim import connect
from pyVim.connect import SmartConnect
from pyVmomi import vim
alarm_name = os.getenv('VMWARE_ALARM_NAME', 'debug_VMWARE_ALARM_NAME')
alarm_target_name = os.getenv('VMWARE_ALARM_TARGET_NAME', 'debug_VMWARE_ALARM_TARGET_NAME')
event_decscription = os.getenv('VMWARE_ALARM_EVENTDESCRIPTION', 'debug_VMWARE_ALARM_EVENTDESCRIPTION')
alarm_value = os.getenv('VMWARE_ALARM_ALARMVALUE', 'debug_VMWARE_ALARM_EVENTDESCRIPTION')
alarm_vm = os.getenv('VMWARE_ALARM_EVENT_VM', 'debug_VMWARE_ALARM_EVENT_VM')
alarm_user = os.getenv('VMWARE_ALARM_EVENT_USERNAME', 'debug_VMWARE_ALARM_EVENT_USERNAME')
if alarm_vm != 'debug_VMWARE_ALARM_EVENT_VM':
s=ssl.SSLContext(ssl.PROTOCOL_SSLv23) # For VC 6.5/6.0 s=ssl.SSLContext(ssl.PROTOCOL_TLSv1)
s.verify_mode=ssl.CERT_NONE
# The pwd variable will need to be to a real password.
# Perhaps using a lookup from a vault.
# This is not built into this script
# Of course "vcenter" and "user" will need to be updated as well
si = SmartConnect(host="vCenterNameHere", user="admin@vsphere.local", pwd="AdminPassword", sslContext=s)
content=si.content
def find_vm_obj(content, vimtype, name):
obj = {}
container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
for c in container.view:
if name:
if c.name == name:
obj = c
break
else:
obj = c
break
return obj
vm = find_vm_obj(content, [vim.VirtualMachine], alarm_vm)
## These attributes must exist in vcenter before writing
## Feel free to add, remove or change the ones below
if vm:
vm.setCustomValue('vm.owner', alarm_user)
vm.setCustomValue('vm.provisioned', str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
I don't know what changed but it is now working! Created the attribute under type "global", with script set at root with chmod +x permissions. Alarm for VM deployment triggers an email and script to run! Use as needed, it works great!
Just out of curiosity did you ever figure out how/why this started working? Faced the same issue, 'New VM Created Alarm' on vmtest01 ran script /root/vm.alarm-attr.py
so it appeared to run the script without issue. Only real test I did was edit that script and put in a bogus password for the credential to force it to fail and see what vcenter comes back with, which is 'New VM Created Alarm' on vmtest02 did not complete script, which I would expect.
But the two attributes did not populate, and still don't.
Well just figured out something interesting. It's working as expected on one host only. If a new VM is deployed to that one host, vm.user and vm.provisioned seem to always populate. The plot thickens...