diff --git a/content/System Administration/zabbix-sound-level.md b/content/System Administration/zabbix-sound-level.md new file mode 100644 index 0000000000000000000000000000000000000000..a0201c5639a787cf5f0ec9208c6303c16cede84b --- /dev/null +++ b/content/System Administration/zabbix-sound-level.md @@ -0,0 +1,96 @@ +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. + +# 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. + + + +# Zabbix + +Zabbix is an open-source monitoring tool. I already use it to keep an eye on my servers. Maybe it can help here? + +# Measuring volume + +We can use the [`rec` tool](https://linux.die.net/man/1/rec) (part of the [SoX](http://sox.sourceforge.net/) audio processor) to grab audio from the microphone and analyze it. + +```bash +rec -n stat trim 0 .5 +``` +- `-n`: record to `null` instead of a file +- `stat`: calculate statistics +- `trim 0 .5`: record `.5` seconds from the start (`0`) + +We can use the `Maximum amplitude` statistic to infer whether the music is playing. + +```bash +rec -n stat trim 0 .5 2>&1 | awk '/^Maximum amplitude/ { print $3 }' +``` + +All that's left is to track this value over time, and alert us when it falls below some threshold. + +# zabbix_sender + +Zabbix can observe metrics in a few different ways (agent, remote network-based checks, etc). +Here, I just want the easiest way to watch a numeric value with minimal configuration overhead. +Seems like a job for [zabbix_sender](https://www.zabbix.com/documentation/current/manpages/zabbix_sender). + +This is the syntax: + +```bash +zabbix_sender -z <zabbix-server> -s <monitored-host> -k <metric> -o <value> +``` + +First, some housekeeping on the Zabbix server: + +1. Create a **Host**. Let's call it `soundmonitor`.  +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. + +```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! + + + +# All together + +We can use [command substitution](https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html) to string together a one-liner. + +```bash +zabbix_sender -z zabbix.example.net \ + -s soundmonitor \ + -k soundlevel \ + -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`. + +Now, we just need to put this in a loop... + +```bash +while true; do + zabbix_sender -z zabbix.example.net \ + -s soundmonitor \ + -k soundlevel \ + -o "$(rec -n stat trim 0 .5 2>&1 | awk '/^Maximum amplitude/ { print $3 }')"; +done +``` + +# 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. diff --git a/content/media/zabbix/graph-0.5.png b/content/media/zabbix/graph-0.5.png new file mode 100644 index 0000000000000000000000000000000000000000..56713ba41ae190fa6b028b7b945d5474c4c8d8b0 Binary files /dev/null and b/content/media/zabbix/graph-0.5.png differ diff --git a/content/media/zabbix/graph-real.png b/content/media/zabbix/graph-real.png new file mode 100644 index 0000000000000000000000000000000000000000..d4d06c8dcc50ff78c62108075ef7d7290aad907a Binary files /dev/null and b/content/media/zabbix/graph-real.png differ diff --git a/content/media/zabbix/graph.png b/content/media/zabbix/graph.png new file mode 100644 index 0000000000000000000000000000000000000000..e7885dc66ceb2e9f65fddcbcb446245fb4d5a495 Binary files /dev/null and b/content/media/zabbix/graph.png differ diff --git a/content/media/zabbix/host.png b/content/media/zabbix/host.png new file mode 100644 index 0000000000000000000000000000000000000000..ace2a155c19f78622900db44edee42194840512e Binary files /dev/null and b/content/media/zabbix/host.png differ diff --git a/content/media/zabbix/item.png b/content/media/zabbix/item.png new file mode 100644 index 0000000000000000000000000000000000000000..4038fdfa68fef51d912eba6e3021248e6f8ad450 Binary files /dev/null and b/content/media/zabbix/item.png differ diff --git a/content/media/zabbix/photo.jpg b/content/media/zabbix/photo.jpg new file mode 100644 index 0000000000000000000000000000000000000000..c57a93e7f5203d822745ee91d7d26452e1b0e0c5 Binary files /dev/null and b/content/media/zabbix/photo.jpg differ diff --git a/content/media/zabbix/soundlevel.png b/content/media/zabbix/soundlevel.png new file mode 100644 index 0000000000000000000000000000000000000000..dc134af86556638cfaab7d9f557331ba68e049d1 Binary files /dev/null and b/content/media/zabbix/soundlevel.png differ diff --git a/content/media/zabbix/trigger.png b/content/media/zabbix/trigger.png new file mode 100644 index 0000000000000000000000000000000000000000..05b4e5c418ad55f94f2a92c3b52aca6894e871d4 Binary files /dev/null and b/content/media/zabbix/trigger.png differ