Script – Get VMs created, deleted or modified on vCenter

powercli

A while back someone asked me if i could tell him who deleted a vm and when.

I was able to get the information but it took me quite some time digging in the events.

After this request I was asked to create a script that would send an email every day with the VMs that were created, deleted or modified.

So this is what I came up with:

Connect-VIServer localhost
$Yesterday = (Get-Date).AddDays(-1).ToString('MM-dd-yyyy')
$Yesterday2 = (Get-Date).AddDays(-2).ToString('MM-dd-yyyy')
Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(-1) |where {$_.Gettype().Name-eq "VmRemovedEvent" -or $_.Gettype().Name-eq "VmCreatedEvent" -or $_.Gettype().Name-eq "VmReconfiguredEvent"} |Sort CreatedTime -Descending |Select CreatedTime, UserName,FullformattedMessage | Export-Csv D:\Scripts\VMReport\$Yesterday.csv -NoTypeInformation
#defining file
$file = "D:\Scripts\VMReport\$Yesterday.csv"
$att = New-Object Net.Mail.Attachment($file)
$smtpServer = "smtpServerName.com"
$smtpFrom = "VMware Report <vmware_report@domain.com>"
$smtpTo = "e-mail address 1 <emailaddress1@domain.com>, e-mail address 2 <emailaddress2@domain.com>"
$messageSubject = "Changes on VMs"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$message.Body = "VMs created, deleted or modified on VCenter"
$message.Attachments.Add($att)
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
Remove-Item D:\Scripts\VMReport\$Yesterday2.csv

Now the explanation

Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(-1) |where {$_.Gettype().Name-eq "VmRemovedEvent" -or $_.Gettype().Name-eq "VmCreatedEvent" -or $_.Gettype().Name-eq "VmReconfiguredEvent"} |Sort CreatedTime -Descending |Select CreatedTime, UserName,FullformattedMessage | Export-Csv D:\Scripts\VMReport\$Yesterday.csv -NoTypeInformation

Get-VIEvent from the vCenter from the day before where the event is equal to removed (VmRemovedEvent), created (VmCreatedEvent) or modified (VmReconfiguredEvent)VMs

The result is sorted by date and it’s exported to a csv file named with the date of a day before.

And this is it from VMware side.

The next part is to send the file by email  (normal powershell scripting)

$file = "D:\Scripts\VMReport\$Yesterday.csv"
$att = New-Object Net.Mail.Attachment($file)
$smtpServer = "smtpServerName.com"
$smtpFrom = "VMware Report <vmware_report@domain.com>"
$smtpTo = "e-mail address 1 <emailaddress1@domain.com>, e-mail address 2 <emailaddress2@domain.com>"
$messageSubject = "Changes on VMs"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$message.Body = "VMs created, deleted or modified on VCenter"
$message.Attachments.Add($att)
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
Remove-Item D:\Scripts\VMReport\$Yesterday2.csv

In the end it removes the file from two days back.

I hope this helps you in some way.

 

AppStack packages migration

appvolumes

App Volumes by VMware provides, in the fly, application delivery to virtualized desktop environments.

This application delivery system allows applications to be delivered to virtualized desktop environments based on profiles using VMDK virtual disks without modifying the Virtual Machine or applications themselves.

App Volumes v. 2.xx has some issues when you try to migrate AppStack packages from one site to another using vSAN storage.

1. Find the AppStack you want to move and Copy the name of the package

1

For you to be able to migrate the AppStack packages from one site to another you need in the original site to create a VM, name it exactly as the AppStack package and attach the VMDK file of the AppStack package you want to migrate as a new disk.

3

After this process is completed you need to clone the machine to the new datacenter and use the same name as the previous one.

4

Now that you have the new VM on the new the VMDK file needs to be copied to VSAN.

To do this you’ll need to run the following command on the destination ESXi that has the VM


vmkfstools -d thin -i /vmfs/volumes/VSAN_FOLDER/NAME_OF_THE_APPSTACK_PACKAGE/NAME_OF_THE_APPSTACK_PACKAGE.vmdk /vmfs/volumes/VSAN_FOLDER/cloudvolumes2/apps/NAME_OF_THE_APPSTACK_PACKAGE.vmdk

You should get an output

Clone: 100% done.

You also need to copy the metadata from the original VSAN storage to the destination VSAN storage

After this procedure is completed you are able to Import the AppStack package to App Volumes on the new site

5

6

Congratulations you now have replicated an AppStack Package stored in vSAN between two sites.

Now it’s time to delete the VMs you have just created.

On the new site just delete the VM and the disk because you have copied it to a new location.

On the original site detach the disk and delete the VM.

I hope this was clarifying enough.

Get VM name with specific MAC Address

-Recently I got a request to get a virtual machine (vm) name using only the network interface MAC Address.

After search for some scripts I could not find one that really worked for me so I put my brain to work and this is what I came up:

get-vm | get-networkadapter | Where-Object { $_.macaddress -eq "00:50:56:XX:XX:XX"} | select parent, macaddress

A brief description on how it’s working

get-vm -> gets all VMs on the vSphere server.

get-netwrokadapter -> gets the nertwork adapter properties as name, type, macaddress…

$_.macaddress -eq -> for each networkadapter it is going to compare the MACs and if they are equal it is going to be printed out

The output should be something like this:

55a15d7e5c007131

Performance Graphs

In my current job I have to able to provide on demand all kind of performance charts from the virtual machines through VMware vSphere Client.

While doing this some details caught my attention. I’ll try to explain.

Imagine that you want to take a performance log from one week ago lets say 26/08/2016 from 3:00PM till 5:00PM.

The normal procedure is:

  • Find the machine
  • Choose the Performance Tab and click in Advanced

1BC05B31202E961

  • Chart options

1C1BB7D49E2228A

  • Then you choose the Chart Option and in this case is going to be CPU then Custom with the time frame that we want, the counters Usage and click OK

238D4327292C755

Now you have your graph all ready to present but… what is that? We are missing the first 30 minutes…

$17B7E6516FA45AC6

But the time frame is correct!!!

$6BF810019305C2C0

Well it’s not entirely correct as you can see it starts at 3:00:19 and VMware to save log space from performance logs older than a week only saves in 30 minutes intervals this means that you get a reference point from 30 minutes to 30 minutes and as the log starts at 3:00:19 you don’t get the first reference point.

The only way to solve this as we are unable to choose the seconds when we fill in the custom chart option is to put one minute earlier

$469C264F6E26D6C8

Whit this tweak you’ll get your performance graph correct

$536D45BFBF5B0069

I hope that VMware solves this “problem” and don’t assume the seconds in the performance charts or if so they must be zeroed.

 

 

Add host to Icinga2

icinga_logo

Adding a Windows 2008 Server to Icinga2 master Server using Icinga2 agent

Setup the Icinga2 master for node clients and make shure the answer to the first question is n (no).

# icinga2 node wizard

Dowload  the agent from Icinga2 website and run it.

After the installation the setup wizzard will pop up.

icinga1

Remember the Instance Name as you will need it to generate the Setup Ticket in the Icinga2 server.

Add the Icinga2 master

Click the Add button and fill in the box

icinga2

Run this command in Icinga2 server to generate the Setup Ticket

# icinga2 pki ticket -cn infra-veeam.domain.local

Now copy the ticket and past it to the box

icinga3

Firewall

Before you click next check if the port 5665 is open in the Windows server and add a rule in Icinga2 server to permit connections

Check your active zones

# firewall-cmd --get-active-zones

I just had one “public” zone

At this point i added the rule to the firewall by running the command

# firewall-cmd --zone=public --add-port=5665/tcp --permanent

Continue the installation and finish it.

Open a command prompt and type in the command:

command

If all goes well you should get this output

command1

Now update the configured hosts by running and restart icinga2

# icinga2 node update-config
# systemctl reload icinga2

And that’s it! If you have Icinga Web 2 the host should pop up.
This post was based on the Documentation shared by the Icinga project

 

Icinga2 on CentOS7

icinga_logo

A Software to monitor the health of an infrastructure is a vital component to a good system administrator.

After looking arround for open source software i decided to give Icinga2 a try.

Icinga2 is a fork of Nagios (it’s a good start) and looks much cooler 🙂 (IMO The cool factor is very important)

CentOS7 was the choice for the Operating System.

After reading the documentation in Icinga website and having some trouble i decided to add some crucial steps that i had to take to perform a successful installation.

All the steps described are performed as root user

First thing firts, after CentOS installation update your system:

# yum update

Now add the Icinga repository to your package management configuration

# rpm --import http://packages.icinga.org/icinga.key
# curl -o /etc/yum.repos.d/ICINGA-release.repo http://packages.icinga.org/epel/ICINGA-release.repo
# yum makecache

Install Icinga2

# yum install icinga2
# systemctl enable icinga2
# systemctl start icinga2

Congratulations Icinga2 is installed.

CheckPlugins

It’s time to install the checkplugins.

As Icinga2 is a Nagios fork we are going ot install the same plugins for it to work properly by installing the Monitoring Plugins.

# wget https://www.monitoring-plugins.org/download/monitoring-plugins-2.1.2.tar.gz
# yum install gcc
# gzip -dc monitoring-plugins-2.x.tar.gz | tar -xf -
# cd monitoring-plugins-2.x
# ./configure
# make
# make install

Vim editor

Icinga2 comes with some color schemes for vim that you can install by

# PREFIX=~/.vim
# mkdir -p $PREFIX/{syntax,ftdetect}
# cd /usr/share/doc/icinga2-common-2.4.1/syntax/
# cp vim/syntax/icinga2.vim $PREFIX/syntax/
# cp vim/ftdetect/icinga2.vim $PREFIX/ftdetect/

Lets change the installation directory of the plugins from /urs/lib64/* to /urs/local/libexec in the constants.conf file of icinga2. The file is in /etc/icinga2

# cd /etc/icinga2
# vim constants.conf

Creating a systemctl for Icinga2

# systemctl enable icinga2
# systemctl start icinga2

Icinga Web 2 interface

At this point Icinga2 is istalled and configured.
Icinga 2 can be used with Icinga Web 2 and a number of other web interfaces.
For now i am going to install Icinga Web 2.
Firts we are going to install MySQL

# yum install mariadb-server mariadb
# systemctl enable mariadb
# systemctl start mariadb
# mysql_secure_installation

Now install install the icinga2-ido-mysql package

# yum install icinga2-ido-mysql

MySQL setup and configuration

# mysql -u root -p
mysql>  CREATE DATABASE icinga;
        GRANT SELECT, INSERT, UPDATE, DELETE, DROP, CREATE VIEW, INDEX, EXECUTE ON icinga.* TO 'icinga'@'localhost' IDENTIFIED BY 'icinga';
# mysql -u root -p icinga < /usr/share/icinga2-ido-mysql/schema/mysql.sql

Enable the IDO MySQL module

# icinga2 feature enable ido-mysql
# systemctl restart icinga2

WebServer install

If you did not install the CenOS web server you need to install httpd

# yum install httpd
# systemctl enable httpd
# systemctl start httpd

Firewall

Add some rules to permit access to the web server

# firewall-cmd --add-service=http
# firewall-cmd --permanent --add-service=http

Command Pipe

For the Icinga2 to receive commands from the Icinga Web 2 you need to enable the command pipe

# icinga2 feature enable command
# systemctl restart icinga2

Icinga Web 2 interface installation

Setting up package repository

# rpm --import http://packages.icinga.org/icinga.key
# curl -o /etc/yum.repos.d/ICINGA-release.repo http://packages.icinga.org/epel/ICINGA-release.repo
# yum makecache

EPEL install

# yum install epel-release

PHP install

# yum install php php-mysql

Don’t forget to change the date.timezone in /etc/php.ini

Let’s install the web interface

# yum install icingaweb2 icingacli

And now lets prepare for the web setup

# icingacli setup token create

Run this command to mitigate an error at the web setup

chcon -R -t httpd_sys_rw_content_t /etc/icingaweb2/

Copy the token and access the site:
http://<server-ip>/icingaweb2/setup
past the token and continue the setup
If the website does not open restart the httpd by running:

# systemctl restart http

For the web interface to work (send commands to your icinga server) you need to disable SELinx in your system.
To do that just edit the file /etc/selinux/confing and change the enforcing to disabled.

VMware ESXi server hardware

I have a VMware infrastructure in my company and it’s time to make a hardware upgrade.

All my server are up and running and i can’t shut them down, how can i check how many slots of physical memory RAM are free? It’s as simple as running on the ESXi Shell the command:

smbiosDump

This command will list all the hardware on the server and all you need to find is the
Memory Device section an in Size you’ll see the size of the memory that is installed or it says Size: no memory installed all you have to do is count the ones that says that.