Trigger a User event from the VCSA command line

Disclaimer: This is not officially supported by VMware, please use at your own risk.

Recently I faced the challenge with a script running on a VCSA which executes some checks and in case something gets detected it has to trigger a vCenter alarm.

My solution is to log a user event which will be captured by an vCenter alert definition.

1. vCenter alert definition

as trigger manually enter (type): vim.event.GeneralUserEvent

2. The script which will log the user event

#!/usr/bin/python

import sys
sys.path.insert(0, '/usr/lib/vmware/site-packages/')
from pyVim.connect import SmartConnect
from pyVmomi import vim
import ssl
import atexit

s = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
s.verify_mode = ssl.CERT_NONE

si = SmartConnect(host="localhost", user="administrator@vsphere.local", pwd='VMware1!', sslContext=s)

content = si.RetrieveContent()
ds = content.rootFolder

log = si.content.eventManager.LogUserEvent(entity=ds, msg="Issue detected")

Result:

The resulting alert can be catched by vROPs and trigger further events/ tickets.

How to configure VMware monitoring in Check_MK

Check_MK is able to monitor ESXi or vCenter out of the box. The configuration requires 2 steps and preferred a read only user.

1. Login to Check_MK

check_mk_login

2. Create new Host

create_new_host

Hostname: ESXi/vCenter Name or fqdn
IP Address: ESXi/vCenter Management IP

create_new_host_esxi

Save & Go to Services

The error message is normal and we can ignore it.

create_host_error

3. Go to Host & Service Parameters

host_service_parameters

4. Click on Datasource Programs

datasource_programs

5. Click Check state of VMWare ESX via vSphere

Bildschirmfoto 2016-03-03 um 14.53.28

6. Create rule in folder Main directory

Explicit hosts: ESXi-Name
vSphere User name: ReadOnlyUser-Name
vSphere secret: Password of the ReadOnlyUser
Select  
 Host Systems
 Virtual Machines
 Datastores
 Performance Counters
 License Usage
Select
 Display ESX Host power state on 
 Display VM power state on
 Placeholder VMs 

Bildschirmfoto 2016-03-03 um 14.57.08

8. Click on Save

Bildschirmfoto 2016-03-03 um 15.01.20

9. Activate your changes

Bildschirmfoto 2016-03-03 um 15.02.46

Bildschirmfoto 2016-03-03 um 15.04.36

10. Click on Hosts

Bildschirmfoto 2016-03-03 um 15.05.46

11. Click on Hosts

3rd icon – Edit services of host

Bildschirmfoto 2016-03-03 um 15.07.44

12. Activate missing

Bildschirmfoto 2016-03-03 um 15.08.40

13. Activate your changes –> see step 9

14. Check your discovered services.

Go to Views –> All Services

Bildschirmfoto 2016-03-03 um 15.11.56

15. Click on the refresh icon next to Check_MK and Check_MK Discovery

Bildschirmfoto 2016-03-03 um 15.13.38

That’s all. If you have question just leave a comment.

Setup check_mk

Check_MK is a monitoring solution which is originally based on Nagios. It has been extended with a nice web gui including a lot of new features which simplify the setup of a monitoring system. It is available as Enterprise edition and as RAW (Open Source) edition.

There is a limited english documentation available. https://mathias-kettner.de/checkmk.html

Steps to install and configure Check_MK on CentOS7:

Download CentOS7 image (minimal is sufficient) http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso

Install Centos following the standard documention. After you have successfull installed Centos login to the system using SSH.

ssh root@centos7

First install all available updates.

yum update

Second some packages are required to be able to install check_mk successfully.

yum -y install epel-release
yum makecache all
yum -y install php-mcrypt

Download fping (http://pkgs.repoforge.org/fping/?C=M;O=D) and install it:

cd /root
wget http://pkgs.repoforge.org/fping/fping-3.10-1.el7.rf.x86_64.rpm
rpm -i fping*.rpm

Furthermore it is required to change SELINUX.

vi /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

change to

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=permissive
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

Reboot the Centos machine to apply the new SELINUX settings.

init 6

Download check_mk RAW edition for CentOS7 and install it:

https://mathias-kettner.de/support/1.2.6p16/check-mk-raw-1.2.6p16-el7-34.x86_64.rpm
rpm -i check-mk-raw*.rpm

After you have installed Check_MK you have to create your monitoring side. In this example I call it “internal”.

omd create internal
omd start internal

Now you should be able to access check_mk using your browser http://centos7/internal. The last part of the URL is the name of the created side.

check_mk_login

Login using the default credentials

User: omdadmin
Password: omd

check_mk_dashboard

That’s all for the setup in part 2 I will describe how to configure ESXi or vCenter monitoring.