Problem
When you have your own (VMware) Lab you don’t always have time to look at the state of the lab and if ‘all lights are in the green’ so to say. So how cool would it be to be alerted on your phone if a problem would occur on your test environment. But monitoring the environment from inside the environment is always tricky because if its down its not alerting 🙂
Solution
After some searching on the internet I decided to monitor my vSphere environment via a and Raspberry Pi 3 I had laying around with Raspberry Pi OS installed on it. In case of an error there should be a notification popup in a Microsoft Teams channel.
So that’s the theory. Lets see if we can get it to work.
Configuring the alerting
So i have a MSDN subscription for Office 365 and also access to teams in this subscription so what I did wat create a separate team for the alert (you can also get this into a channel in an existing team but for me this was a better separation. I used the web version of teams but you could also use the desktop version.
Installing and configuring the script on the Raspberry Pi
After downloading the script I ran this Powershell oneliner to test if the script was working as expected.
pwsh -ExecutionPolicy Bypass -file Send-vSphereAlarms.ps1 -vcenter vcenter-fqdn -TeamsUri uri |
Where vCenter fdqn is the URL for the vCenter obvious 🙂 and the TeamsURI is the URL you copied earlier from the teams web hook configuration.
CronJob
To make it possible for a Powershell script to run on a Raspberry Pi you need to install Powershell. Microsoft has good documentation regarding this subject here: Installing PowerShell on Raspberry Pi OS – PowerShell | Microsoft Docs. I did it by running these commands in terminal on the raspberry pi.
wget https://github.com/PowerShell/PowerShell/releases/download/v7.2.1/powershell-7.2.1-linux-arm32.tar.gz |
Next step is to create a folder for Powershell to be installed.
mkdir ~/powershell |
Then its time to unpack the downloaded tar file.
tar -xvf ./powershell-7.2.1-linux-arm32.tar.gz -C ~/powershell |
Pro tip: I made my life easier by creating a symlink so I don’t need to browse to the Powershell folder to use pwsh.
sudo ~/powershell/pwsh -command ‘New-Item -ItemType SymbolicLink -Path “/usr/bin/pwsh” -Target “$PSHOME/pwsh” -Force’ |
To make the script work you also need VMware PowerCLI to be installed, this can be done by running this command inside the Raspberry Pi terminal session (inside Powershell).
Install-Module -Name VMware.PowerCLI -Scope CurrentUser |
After installing the PowerCLI addon you can now connect to the vCenter by running this command. For me I added this line as a first line to the Powershell script, there are probably better ways to do this and i’ll update the blog on how to do this but for now it works :-). Do not use cleartext passwords scripts in production environments!!
Connect-VIServer -Server vcenter.ad.wrsolutions.nl -User [email protected] -Password “thisisnotforproductionenvironments” |
So now it’s time to test the script running from inside the Raspberry Pi. you can do this by downloading the .ps1 file to the Raspberry Pi (in my case i downloaded it to the my documents folder for the Pi user. And then running the following command in terminal from inside the Documents folder:
pwsh ./send-vSphereAlarms.ps1 -vcenter <vcenter name> -teamsuri <teams uri> |
Where vCentername is the name of the venter and Teamsuri is the Webhook URL created earlier. in my case this gave this output
After that you need to create a cron job that runs the script on set times and posts the results. To do this type this command in Terminal.
sudo crontab -e |
If it is the first time you open crontab on your Raspberry Pi you will be asked how you want to edit the cronjobs I chose nano (option 1).
Scroll down to the bottom and add the script, what this line does is run the Powershell script every 5 minutes.
*/5 * * * * pwsh -File “/home/pi/Documents/send-vSphereAlarms.ps1 -vcenter <vcenter name> -teamsuri <teams uri> |
So after waiting for a bit I got my first error in my Teams channel
So there are probably a thousand way’s to solve this, and some of them better than mine. But I went for this solution because I had the stuff laying around and I wanted to see what was possible. Hope this helps someone 🙂