Running an Alarm Script on the vCenter Server Appliance

When alarms on vSphere are triggered by the vCenter Server there is an option to run a script. That script will run on the vCenter Server appliance in Linux. In this article I will explain how to setup the script location and permissions and will show a demonstration script.

First of all let's look at the alarm definition in the vSphere Client. In the image below you see an alarm definition for a virtual machine event. When the VM reaches the powered off state this alarm will be triggered. The script that is configured to run is: /home/vpxd/alarm.sh.

 

The script file must be located in the vCenter Appliance where it will be run. Starting with vSphere 8 the script must run with the privileges for the vpxd user. Information about this, the script and the use of environment variables can be found in the documentation

You could place the script in many different places within the vCenter Appliance but personally I think the best solution is to create a home directory for the vpxd user and place the script file there. The vpxd user already exists in Linux within the appliance but there is no home directory by default for this user.

Create the home directory with the following commands while logged in as root in the Linux Bash shell on the vCenter Appliance: 

mkdir /home/vpxd
chown vpxd /home/vpxd

The result of running these commands will look like this:

Next create a script file to run in the /home/vpxd subdirectory. A sample script is provided below. After creating the file make it executable with the following command:

chmod u+x <script file>

In this example I am just using the env command to dump the environment variables and an example of three environment variables being written into a file with the echo command. These variables are available only at runtime when the script is run after the alarm is triggered. In this sample script I am writing this content into a file. You can use your imagination and inventive skills to do something more interesting. For example executing a REST-API call via curl.

This is the sample script in text so you can copy it and use it as a starting point.

#!/bin/bash
echo $(date) >> /home/vpxd/alarm.log
env | grep VMWARE_ALARM  >> /home/vpxd/alarm.log
echo ================================= >> /home/vpxd/alarm.log
echo $VMWARE_ALARM_EVENTDESCRIPTION >> /home/vpxd/alarm.log
echo $VMWARE_ALARM_NAME >> /home/vpxd/alarm.log
echo $VMWARE_ALARM_TARGET_NAME >> /home/vpxd/alarm.log
echo ================================= >> /home/vpxd/alarm.log

The output from this sample script looks like this for an alarm that was triggered when a virtual machine was powered off.

 

 

Tags:
Follow us on LinkedIn

  

   

   

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer