raspberry-pi-anomaly-detection.txt 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Anomaly detection for RPi monitoring
  2. Learn how to use a low-overhead machine learning algorithm alongside Netdata to detect anomalous metrics on a Raspberry Pi.
  3. We love IoT and edge at Netdata, we also love machine learning. Even better if we can combine the two to ease the pain
  4. of monitoring increasingly complex systems.
  5. We recently explored what might be involved in enabling our Python-based [anomalies
  6. collector](/src/collectors/python.d.plugin/anomalies/README.md) on a Raspberry Pi. To our delight, it's actually quite
  7. straightforward!
  8. Read on to learn all the steps and enable unsupervised anomaly detection on your on Raspberry Pi(s).
  9. > Spoiler: It's just a couple of extra commands that will make you feel like a pro.
  10. ## What you need to get started
  11. - A Raspberry Pi running Raspbian, which we'll call a _node_.
  12. - The [open-source Netdata](https://github.com/netdata/netdata) monitoring agent. If you don't have it installed on your
  13. node yet, [get started now](/packaging/installer/README.md).
  14. ## Install dependencies
  15. First make sure Netdata is using Python 3 when it runs Python-based data collectors.
  16. Next, open `netdata.conf` using [`edit-config`](/docs/netdata-agent/configuration/README.md#edit-a-configuration-file-using-edit-config)
  17. from within the [Netdata config directory](/docs/netdata-agent/configuration/README.md#the-netdata-config-directory). Scroll down to the
  18. `[plugin:python.d]` section to pass in the `-ppython3` command option.
  19. ```text
  20. [plugin:python.d]
  21. # update every = 1
  22. command options = -ppython3
  23. ```
  24. Next, install some of the underlying libraries used by the Python packages the collector depends upon.
  25. ```bash
  26. sudo apt install llvm-9 libatlas3-base libgfortran5 libatlas-base-dev
  27. ```
  28. Now you're ready to install the Python packages used by the collector itself. First, become the `netdata` user.
  29. ```bash
  30. sudo su -s /bin/bash netdata
  31. ```
  32. Then pass in the location to find `llvm` as an environment variable for `pip3`.
  33. ```bash
  34. LLVM_CONFIG=llvm-config-9 pip3 install --user llvmlite numpy==1.20.1 netdata-pandas==0.0.38 numba==0.50.1 scikit-learn==0.23.2 pyod==0.8.3
  35. ```
  36. ## Enable the anomalies collector
  37. Now you're ready to enable the collector and restart Netdata.
  38. ```bash
  39. sudo ./edit-config python.d.conf
  40. # restart netdata
  41. sudo systemctl restart netdata
  42. ```
  43. And that should be it! Wait a minute or two, refresh your Netdata dashboard, you should see the default anomalies
  44. charts under the **Anomalies** section in the dashboard's menu.
  45. ![Anomaly detection on the Raspberry
  46. Pi](https://user-images.githubusercontent.com/1153921/110149717-9d749c00-7d9b-11eb-853c-e041a36f0a41.png)
  47. ## Overhead on system
  48. Of course one of the most important considerations when trying to do anomaly detection at the edge (as opposed to in a
  49. centralized cloud somewhere) is the resource utilization impact of running a monitoring tool.
  50. With the default configuration, the anomalies collector uses about 6.5% of CPU at each run. During the retraining step,
  51. CPU utilization jumps to between 20-30% for a few seconds, but you can [configure
  52. retraining](/src/collectors/python.d.plugin/anomalies/README.md#configuration) to happen less often if you wish.
  53. ![CPU utilization of anomaly detection on the Raspberry
  54. Pi](https://user-images.githubusercontent.com/1153921/110149718-9d749c00-7d9b-11eb-9af8-46e2032cd1d0.png)
  55. In terms of the runtime of the collector, it was averaging around 250ms during each prediction step, jumping to about
  56. 8-10 seconds during a retraining step. This jump equates only to a small gap in the anomaly charts for a few seconds.
  57. ![Execution time of anomaly detection on the Raspberry
  58. Pi](https://user-images.githubusercontent.com/1153921/110149715-9cdc0580-7d9b-11eb-826d-faf6f620621a.png)
  59. The last consideration then is the amount of RAM the collector needs to store both the models and some of the data
  60. during training. By default, the anomalies collector, along with all other running Python-based collectors, uses about
  61. 100MB of system memory.
  62. ![RAM utilization of anomaly detection on the Raspberry
  63. Pi](https://user-images.githubusercontent.com/1153921/110149720-9e0d3280-7d9b-11eb-883d-b1d4d9b9b5e1.png)