From c14e9d259eb9669f12b0d2cc96adfae743260638 Mon Sep 17 00:00:00 2001 From: Anton Sarukhanov <code@ant.sr> Date: Sat, 23 Mar 2019 14:47:21 -0400 Subject: [PATCH] Add systemd service to zabbix post. --- .../zabbix-sound-level.md | 61 +++++++++++++++---- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/content/System Administration/zabbix-sound-level.md b/content/System Administration/zabbix-sound-level.md index a0201c5..4b966a8 100644 --- a/content/System Administration/zabbix-sound-level.md +++ b/content/System Administration/zabbix-sound-level.md @@ -1,22 +1,23 @@ Title: Measuring sound level with Zabbix Description: Test consumer electronics with enterprise tools! Date: March 22, 2019 -Cover: /media/zabbix/graph-real.png -Featured_Project: true -Here's a way to observe sound level from a microphone using an IT monitoring system. +Here's how I watched the sound level from a microphone with a Raspberry Pi and an IT monitoring system. # Why? -- I wanted to measure how long an old MP3 player could play until the battery died -- ...without having to listen to the music -- ...and I wanted to find out as soon as it stopped playing. +I wanted to... + +- measure how long an old MP3 player could play until the battery died +- ... without having to listen to the music +- ... and I wanted to find out as soon as it stopped playing.  # Zabbix -Zabbix is an open-source monitoring tool. I already use it to keep an eye on my servers. Maybe it can help here? +[Zabbix](https://zabbix.com) is an open-source enterprise IT monitoring tool. +I already use it to keep an eye on my servers. Maybe it can help here? # Measuring volume @@ -55,17 +56,19 @@ First, some housekeeping on the Zabbix server: 2. Create a new **Item** for the `soundmonitor` host. Let's call it `soundlevel`.  3. Put the Item on a **Graph**.  -Now we can test the `zabbix_sender` command. +Let's test the `zabbix_sender` command... ```bash zabbix_sender -z zabbix.example.net -s soundmonitor -k soundlevel -o "0.5" ``` -Take a look at the graph. It should now have our fake datapoint! +...and check the graph. It should have our fake datapoint.  -# All together +There it is! + +# Assembling the pieces We can use [command substitution](https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html) to string together a one-liner. @@ -76,9 +79,9 @@ zabbix_sender -z zabbix.example.net \ -o "$(rec -n stat trim 0 .5 2>&1 | awk '/^Maximum amplitude/ { print $3 }')" ``` -This takes the numeric sound level from the `rec|awk` combination and passes it as the `-o` argument of `zabbix_sender`. +This takes the numeric sound level from `rec | awk` and passes it as an argument to `zabbix_sender`. -Now, we just need to put this in a loop... +Now just put that in a loop... ```bash while true; do @@ -89,8 +92,42 @@ while true; do done ``` +...and we have a working pipeline. + + # Ta-da!  Pausing the iPod produced that valley in the middle. Based on this test, I set a **Trigger** for `soundlevel` falling below `0.5` for 60 seconds or longer. + +# Extra credit + +The Raspberry Pi might lose power and restart. We can address that with a `systemd` service running the `zabbix_sender` loop. + +### `/etc/systemd/system/zs.service` + +```ini +[Unit] +Description=ZabbixSoundMonitor +Wants=network-online.target +After=network.target network-online.target + +[Service] +Environment="AUDIODEV=hw:1,0" +Environment="AUDIODRIVER=alsa" +ExecStart=/usr/local/bin/zs.sh +Restart=always +RestartSec=5 + +[Install] +WantedBy=multi-user.target +``` + +This takes care of a few things: + +1. Waits until a network is available. +2. Sets the `AUDIODEV` and `AUDIODRIVER` environment variables required by `rec`. +3. Runs the script at `/usr/local/bin/zs.sh` (which contains the loop we wrote). + +Enable the new service with `systemctl enable zs.service`, and start it with `service zs start`. It will automatically start on reboot. -- GitLab