Can you monitor how much you drive your car, even if your car wouldn't have any way to report back to Zabbix? Of course you can! By day, I am a Lead Site Reliability Engineer in a global cyber security company. By night, I monitor my home with Zabbix & Grafana and do some weird experiments with them. Welcome to my blog about the project.
Some forewords: Now that our baby girl is over six months old, she has developed some kind of sleeping pattern. It means she goes to bed very early in the evening, around 6pm. Or, I go to bedroom with her and wait for her to sleep steadily before I get to exit the bedroom without her noticing. It means I have lots of time to think, and also to play around with apps like iPhone Shortcuts. I have previously done few Siri & Zabbix experiments and this will be one more.
I did do this shortcut only two days ago and have not actually driven yet, but I verified that the shortcut itself works when I go into my car and start it up. Also, as I don't want to give out the exact location where we live, so for this blog post I faked our car to be located in Santa Claus Village, Rovaniemi.
Let's get started.
What are you planning?
Even though I already know very well how much I drive -- there's the odometer in our car, a fuel app in my iPhone shows how many litres per month I refuel and so on --, this data is still something that would contribute to my dear single pane of glass, like your company probably wants to have.
My Siri Shortcut is simple: whenever I go to my car and my iPhone connects to car Bluetooth, it's a clear datapoint that I'm probably going somewhere, so the shortcut gets my current location and saves it coordinates to a text file in my iCloud.
Next, just like in my previous Siri examples, a Zabbix Agent on my MacBook keeps an eye on this text file, and very much like in my FlightGear integration example, Zabbix will then populate the coordinates in Zabbix inventory for my car host. This way, I can project the car location to the Geomap widget.
Let's create the shortcut
Here's the shortcut in all its simplicity.
Next up, Zabbix
In Zabbix side of the house, the story is like so many times in my posts: read the text file, and using dependent items create the longitude and latitude items.
It was easier to get macOS Zabbix Agent to get to read /tmp instead of your home directory, as the security is in the way, so a cronjob syncs the file once per minute to /tmp. Not only that, but because in iOS Shortcuts the Append to a text file was the only way I got the shortcut to run without it for asking for a permission to run, my cronjob is actually like this:
* * * * * /usr/bin/tail -n1 /Users/jaba/Desktop/car_location.txt >/tmp/car_location.txt
Beautiful? No, but due Reasons I had to do this, and at least it works.
Anyway, then the longitude/latitude dependent items just use some regular expressions.
Does it work?
Of course it does! See for yourself.
But wait! How does this track your kilometres?
Heh, you got me. It does not. One easy way of doing that would be to use Get distance block in iOS Shortcuts. It actually works -- you get to choose that yes I will be driving, give me the kilometres. Whenever I do that, I would need a text file containing just one line (which would contain the old location), and getting to that point without your iPhone asking anything ever is not so simple, so for now I gave up.
So, the next part of this will be to use some API and make my Zabbix to calculate the distances. That would be cooler anyway, but I'll find time for that next time. Anyway, from now on Zabbix will know the locations where I have started our car, so the data will be collected from today. I know there are limitations in this implementation, such as that if I start to car and just drive to some place and back without ever stopping the engine, that won't really give me any results, but this is better than nothing.
I have been working at Forcepoint since 2014 and as you know by now, I have this never-ending drive for monitoring.