Monitoring Microsoft 365 using Raspberry Pi and M365 CLI
By Anatoly Mironov
I would like to show you my recent hobby project with a raspberry pi, a unicorn phat and the powerful cli-microsoft365: A simple monitoring solution of Microsoft 365 Services.
Status of some important services in Microsoft 365
In essence, I put the unicorn phat onto the raspberry pi zero w and wrote this python script:
The python script checks the service status every five minutes and shows it with colors on the unicorn phat.
Color coding
Since the unicorn phat is just a grid of 8x4 rgb leds, I needed to color code the different service statuses (more on the statuses later in this post). I came up with these color combinations. It doesn’t matter what combinations they are as long as they mean something to you (or as long as you can decode them).
- π© π© π© π© ServiceOperational
- π© π© π© π¨ ServiceRestored
- πͺ πͺ πͺ πͺ Investigating
- π© π© π© πͺ FalsePositive
- β¬οΈ β¬οΈ β¬οΈ β¬οΈ InformationUnavailable
- π₯ π₯ π₯ π₯ ServiceInterruption
- π₯ π₯ π₯ π¨ ExtendedRecovery
- π₯ π₯ π¨ π© ServiceDegradation
- π© π© π© π¦ PIRPublished
- π₯ π¨ π¨ π© RestoringService
ServiceStatus
There is a list of all possible statuses you can get for Microsoft 365 Services, and it is here:
cli-microsoft365
Install the cli-microsoft365 npm package globally.
sudo npm i -g @pnp/cli-microsoft365
You have to log in, admin consent (if you run this for the first time) and then you can get the status of the Microsoft 365 Services by running:
m365 tenant status list
Services
There are many services in Microsoft 365. I choose the 8 most important ones (from my point of view), because there are only 8 rows on the unicorn phat, you can choose your services and order them as you prefer of course. Beware the spelling and the casing:
- SharePoint
- microsoftteams
- Exchange
- OneDriveForBusiness
- yammer
- Viva (it was Forms)
- PowerBIcom
- Intune
Assembling the hardware
I had my raspberry pi zero w, with raspberry pi os already installed. I attached the unicorn phat using solderless pogo pins. I found a little white cardboard box, cut out a rectangular hole for the unicorn phat and glued the raspberry pi with unicorn inside the box. On the front side I put a sticker with the actual service names for every led row. I connected it to the power, ran the script.
Only three pins are needed.
I tested it first without a box.
I glued the hardware on the inside of the cardboard with a glue gun.
Exploring the @pnp/cli-microsoft365.
Other tips and tricks
The pogo pins were to loose and the leds did not work. I had to shorten the plastic holders a little to tighten the the pins.
Login to cli-microsoft365 as sudo
When I explored the m365 commands, it worked perfectly. My login was cached. Then I needed to run my scripts as sudo, since it requires communication with GPIO pins and the unicorn phat. It didn’t work. The login cache is in different place if you run as sudo. Obvious, when I look at it afterwards, but it took some time to realize that. So, if you are going to do the same, just make sure you log in to m365 as sudo as well, before running the script:
sudo m365 login
Pinout
This web resource is gold, it shows the pinout and connections to many hats etc:
You only need three pins:
- 5V Power (Pin 2)
- Ground (Pin 6)
- GPIO 18, Data (Pin 12)
subprocess in python
m365 is a command that you can run in terminal, from a python script I use subprocess to call it and get the results.
Running the script even when you log out
start the script with nohup:
sudo nohup python3 unicorn03_m365.py &
Installing node and npm
Installing node and npm on raspberry pi zero can be tricky. Follow this guide to avoid hassle.
Edit 2022-01-23
The json result has change a little, now the data is an array rather than an array in an object called value. So I changed the code accordingly and verified that it works.
{ x['Id']: x for x in data }
Another change in this solution is introduction of Viva. Instead of Forms I am showing the service status of Viva!
The latest code is in the same place on github
Edit 2022-04-18
I re-installed the OS on my raspberry pi zero. m365 has changed slightly, now the status names, ids etc are in camel case. I updated the script. Also the actual call has changed from
m365 tenant status list --output json
to
m365 tenant serviceannouncement health list --output json
It works great again. The script is in the same place, and its git history can reveal my changes: unicorn03_m365.py.
I also printed out a case for the raspberry pi zero and the unicorn phat using a model I found on Thingiverse. I will try to come up with something for service labels, though.
Comments from Wordpress.com
Sinan Abutaleb - Apr 2, 2021
Awsome project as always Anatoly. Love that you are using a raspberry pi zero for it :)
Anatoly Mironov - Apr 3, 2021
Thank you Sinan!