1.简介

使用微创联合M5S空气检测仪、树莓派3b+、prometheus、grafana实现空气质量持续监控告警WEB可视化

grafana dashboard效果:

image-20201031171328947

2.背景

2.1 需求:

1.新办公地址的甲醛(HCHO)、异味(TVOC)做个监测

2.能够把这些空气指标进行WEB展示

3.监控告警关心的空气指标,告警发送到钉钉上

2.2 已有硬件:

树莓派3b+ (长期吃灰中)以下简称树莓派

微创联合M5S空气检测仪(版本:M5S温湿+锂电+TVOC+数据导出 17年485元价格购买 )以下简称空气检测仪

M5S 家用 激光PM2.5检测仪 甲醛 CO2 空气质量 雾霾甲醛 检测仪

【在售价】380.00 元(基础版)

【立即下单】点击链接立即下单:https://s.click.taobao.com/zREU4vu

img

3.设计

把空气检测仪的usb插到树莓派上(树莓派上通过串口读取数据),供电也是通过树莓派供电

相关软件运行在树莓派上

3.1 硬件:

空气检测仪如何导出数据?

空气检测仪是有数据导出版本,自带串口转USB(ch340芯片)

打开空气检测仪并通过检测仪左侧 FUN 按钮,把屏幕切换到第 3 屏,这个时候串口中就会持续输出监控数据

在linux上设备文件符为“/dev/ttyUSB0”

cat /dev/ttyUSB0
#可以看到
24.9 45.2 23 32 35 26 33 35 4419 1301 159 8 3 0 0.006 0.21

读取过程中,按一下检测仪上的 RST 键,可以显示每列数据对应的字段定义,(按完后需要再按 FUN 键切换到第 3 屏

*--------------------------------------- Data Output Format Definition ----------------------------------------*
TEMP HUMI CH_PM1.0 CH_PM2.5 CH_PM10 US_PM1.0 US_PM2.5 US_PM10 >0.3um >0.5um >1.0um >2.5um >5.0um >10um HCHO TVOC
TEMP HUMI CH_PM1.0 CH_PM2.5 CH_PM10 US_PM1.0 US_PM2.5 US_PM10 >0.3um >0.5um >1.0um >2.5um >5.0um >10um HCHO TVOC
25.1 43.3 21 29 31 22 29 31 3843 1136 170 6 2 0 0.003 0.2
25.1 43.2 22 30 31 23 30 31 3876 1156 170 5 2 0 0.005 0.16
25.1 43.3 22 30 31 23 30 31 3963 1174 164 6 2 0 0.003 0.21
25.2 43.3 22 30 31 23 30 31 3942 1163 167 6 2 0 0.007 0.21

带二氧化碳检测的版本在HCHO字段前多一个CO2字段

更多资料看空气检测仪配套的资料

3.2 软件:

软件监控采用prometheus + grafana的方案

本文为流水理鱼wwek原创 www.iamle.com

现在只需要实现一个串口(/dev/ttyUSB0) exporter即可把空气监测仪的数据打通给prometheus使用

最后配置prometheus取空气检测仪的exporter数据,并配置grafana面板

监控告警直接使用grafana带的监控告警

4.实现

*安装配置wclh_air_detector_exporter 获得空气检测仪数据

wclh_air_detector_exporter读取串口数据并把数据进行结构化,然后输出metrics

M5S Temperature and Humidity+lithium battery+CO2+TVOC PM2.5 CO2(S8)TEMP&HUMI Detector Haze PM2.5 sensors Laser PM2.5 detector

M5S 家用 激光PM2.5检测仪 甲醛 CO2 空气质量 雾霾甲醛 检测仪

WCLH_AIR_DETECTOR_EXPORTER_VERSION=0.1.3
wget https://github.com/wwek/wclh_air_detector_exporter/releases/download/v${WCLH_AIR_DETECTOR_EXPORTER_VERSION}/wclh_air_detector_exporter_${WCLH_AIR_DETECTOR_EXPORTER_VERSION}_linux_armv7.tar.gz
tar zxvf wclh_air_detector_exporter_${WCLH_AIR_DETECTOR_EXPORTER_VERSION}_linux_armv7.tar.gz
mkdir -p /data/soft/wclh_air_detector_exporter
mv wclh_air_detector_exporter /data/soft/wclh_air_detector_exporter
cd /data/soft/wclh_air_detector_exporter

#./wclh_air_detector_exporter -serial_port /dev/ttyUSB0

#自动启动&进程守护
sudo bash -c 'cat > /etc/systemd/system/wclh_air_detector_exporter.service << EOF [Unit] Description=https://github.com/wwek/wclh_air_detector_exporter Wants=network-online.target After=network-online.target [Service] Restart=on-failure #User=root ExecStart=/data/soft/wclh_air_detector_exporter/wclh_air_detector_exporter [Install] WantedBy=default.target EOF' sudo systemctl daemon-reload sudo systemctl status wclh_air_detector_exporter sudo systemctl start wclh_air_detector_exporter sudo systemctl enable wclh_air_detector_exporter sudo systemctl status wclh_air_detector_exporter curl http://localhost:9166/metrics ``` ## 安装配置prometheus ``` PROMETHEUS_VERSION=2.22.0 wget https://github.com/prometheus/prometheus/releases/download/v${PROMETHEUS_VERSION}/prometheus-${PROMETHEUS_VERSION}.linux-armv7.tar.gz tar zxvf prometheus-${PROMETHEUS_VERSION}.linux-armv7.tar.gz mkdir -p /data/soft/ mv prometheus-${PROMETHEUS_VERSION}.linux-armv7 prometheus &amp;&amp; mv prometheus /data/soft/ #自动启动&amp;进程守护 sudo bash -c 'cat &gt; /etc/systemd/system/prometheus.service &lt;&lt; EOF
[Unit]
Description=https://prometheus.io
Wants=network-online.target
After=network-online.target

[Service]
Restart=on-failure
#User=root
ExecStart=/data/soft/prometheus/prometheus --config.file="/data/soft/prometheus/prometheus.yml"

[Install]
WantedBy=default.target
EOF'

sudo systemctl daemon-reload
sudo systemctl status prometheus
sudo systemctl start prometheus
sudo systemctl enable prometheus
sudo systemctl status prometheus

curl http://localhost:9090
#<a href="/graph">Found</a>.

安装配置grafana

sudo apt-get install -y apt-transport-https
sudo apt-get install -y software-properties-common wget
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
sudo apt-get update
sudo apt-get install grafana
sudo systemctl daemon-reload
sudo systemctl start grafana-server
sudo systemctl enable grafana-server
sudo systemctl status grafana-server

curl http://localhost:3000
#<a href="/login">Found</a>.

grafana 中先配置 prometheus(http://localhost:9090)数据源,然后导入 “grafana-dashboard.json”空气检测仪的dashboard

监控告警

直接使用grafana自带的告警功能,将关心的指标进行监控并告警

简介

Prometheus

Prometheus 是一个开源的服务监控系统和时间序列数据库。作为容器领域的监控比较热门。
Prometheus 采用 PULL 拉的方式进行监控,如果不在一个网络中可以采用 Push gateway 作为中间件来被 PULL 拉。
架构图为

Prometheus 官方提供常用的 exporters 。
blackbox_exporter 是 Prometheus 官方提供的 exporter之一,可以提供 http、dns、tcp、icmp(ping)的监控数据采集。

Grafana

Grafana 是开源的,功能齐全的度量仪表盘和图形编辑器,支持 Graphite,OpenTSDB,Elasticsearch,Cloudwatch,Prometheus,InfluxDB,小米监控
等。

安装

分别安装grafana、prometheus、blackbox_exporter,安装文档参考官网。

配置

prometheus 配置和运行

在prometheus的配置文件 prometheus.yml 中增加一个新的job

  - job_name: 'ping_all'
    scrape_interval: 5s
    metrics_path: /probe
    params:
      module: [icmp]  #ping
    static_configs:
      - targets: ['219.150.32.132', '219.148.204.66', '183.59.4.178', '202.96.209.5', '202.100.4.15', '219.146.0.253', '222.173.113.3', '61.139.2.69', '61.128.114.133', '202.98.234.35', '202.100.64.68', '202.100.100.1', '221.232.129.35', '222.88.19.1', '222.190.127.1', '202.96.96.68', '202.101.103.54', '61.140.99.11', '202.96.161.1', '202.103.224.74']
        labels:
          group: '一线城市-电信网络监控'
      - targets: ['218.8.251.163', '218.107.51.1', '221.7.34.1', '112.65.244.1', '218.25.9.38', '124.89.76.214', '202.102.152.3', '202.102.128.68', '124.161.132.1', '221.7.1.20', '27.98.234.1', '218.104.111.122', '218.28.199.1', '221.6.4.66', '221.12.31.58', '218.104.136.149', '210.21.4.130', '210.21.196.6', '221.7.128.68', '221.199.15.5']
        labels:
          group: '一线城市-联通网络监控'
      - targets: ['211.137.241.34', '211.136.192.1', '218.203.160.194', '117.131.0.22', '211.137.32.178', '117.136.25.1', '211.137.191.26', '211.137.180.1', '211.137.106.1', '218.202.152.130', '211.139.73.34', '223.77.0.1', '211.138.16.1', '221.131.143.1', '211.140.10.2', '211.143.195.255', '183.238.55.1', '218.204.216.65', '211.138.245.180', '211.138.56.1']
        labels:
          group: '一线城市-移动网络监控'
      - targets: ['61.233.168.1', '61.234.113.1', '211.98.224.87', '222.44.44.1', '222.33.8.1', '61.232.201.76', '211.98.19.1', '61.232.49.1', '211.98.208.1', '61.233.199.129', '222.34.16.1', '61.232.206.1', '61.233.79.1', '211.98.43.1', '61.232.78.1', '61.232.93.193', '211.98.161.1', '61.235.99.1', '222.52.72.66', '211.138.56.1']
        labels:
          group: '一线城市-铁通网络监控'
      - targets: ['219.150.32.132', '219.153.32.1', '219.149.6.99', '220.189.218.45', '123.172.77.1', '202.103.124.34', '222.172.200.68', '202.100.64.68', '222.93.201.1', '222.191.255.1', '202.101.224.68', '222.85.179.1', '220.178.79.2', '59.49.12.97', '222.223.255.1', '219.148.172.97', '219.132.199.49', '14.120.0.1', '222.223.29.5', '222.173.222.1', '202.101.107.55', '222.74.58.29']
        labels:
          group: '二线城市-电信网络监控'
      - targets: ['60.24.4.1', '221.5.203.86', '202.96.86.18', '221.12.33.227', '202.99.96.68', '58.20.127.238', '221.3.131.11', '124.152.0.1', '221.3.131.11', '221.6.96.1', '220.248.192.13', '221.13.28.234', '211.91.88.129', '202.99.192.66', '202.99.160.68', '202.99.224.68', '58.252.65.1', '221.4.141.254', '60.2.17.49', '202.102.154.3', '36.249.127.25', '202.99.224.8']
        labels:
          group: '二线城市-联通网络监控'
      - targets: ['211.137.160.5', '218.201.17.2', '211.140.192.1', '211.140.35.1', '211.141.66.209', '211.138.225.2', '111.17.191.25', '218.203.160.195', '211.103.55.51', '211.103.114.1', '183.217.255.254', '211.139.2.18', '211.138.180.2', '211.142.12.2', '111.11.85.1', '211.138.91.2', '218.204.173.197', '183.233.223.25', '211.143.60.1', '120.192.167.2', '218.207.133.3', '120.193.138.1']
        labels:
          group: '二线城市-移动网络监控'
      - targets: ['61.234.70.2', '211.98.114.1', '61.232.157.1', '61.234.186.1', '61.235.225.1', '211.98.71.1', '211.98.72.7', '123.81.2.9', '211.98.44.65', '222.45.86.1', '222.49.82.1', '211.98.108.1', '222.48.116.17', '211.98.149.117', '61.235.145.1', '222.39.142.1', '110.211.253.9', '222.50.104.1', '61.234.76.1', '61.232.53.113', '61.234.196.185', '222.39.156.1']
        labels:
          group: '二线城市-铁通网络监控'
      - targets: ['166.111.8.28', '202.112.26.34', '202.114.240.6', '202.116.160.33', '211.65.207.170', '218.26.226.100', '222.221.252.162', '202.38.64.1', '202.201.48.1', '202.118.176.1', '121.193.138.1', '222.23.245.1', '58.195.136.3', '202.118.1.28', '59.77.229.1', '121.48.177.1', '121.250.214.1', '59.75.177.1', '59.75.112.254']
        labels:
          group: '教育网络监控'
    relabel_configs:
      - source_labels: [__address__]
        regex: (.*)(:80)?
        target_label: __param_target
        replacement: ${1}
      - source_labels: [__param_target]
        regex: (.*)
        target_label: ping
        replacement: ${1}
      - source_labels: []
        regex: .*
        target_label: __address__
        replacement: 127.0.0.1:9115  # Blackbox exporter.

顺序运行 blackbox_exporter,prometheus

grafana 配置和运行

运行grafana
在 grafana中增加 Data Sources 选 prometheus
然后按照grafana的文档新定制一个面板
ROW中指标选probe_duration_seconds{job=”ping_all”}

grafana prometheus ping监控