Muninで、Switchbot の CO2 センサーをモニタリングするためのプラグイン
#!/bin/bash #%# family=auto #%# capabilities=autoconf available="yes" # # SwitchBot # token="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" secret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" swbot_url="https://api.switch-bot.com" # for v1.1 url_list_v11="${swbot_url}/v1.1/devices" t=$(/bin/date +%s%3N) # time = Epoch time 13 digits nonce=$(/usr/bin/uuidgen) # uuid sign=$(echo -n "$token$t$nonce" | /usr/bin/openssl dgst -sha256 -hmac "$secret" -binary | /usr/bin/base64 -w 0) # SwitchBot meter CO2センサーのデバイスID deviceId="xxxxxxxxxxxx" function status() { url_list_v11_meter="${url_list_v11}/${deviceId}/status" /usr/bin/curl -s --request GET \ -H "Content-Type: application/json" -H "Authorization: ${token}" \ -H "sign: ${sign}" -H "nonce: ${nonce}" -H "t: ${t}" \ "${url_list_v11_meter}" \ | /usr/bin/jq -r '.body | "temperature.value " + (.temperature|tostring) + "\nhumidity.value " + (.humidity\ |tostring) + "\nCO2.value " + (.CO2|tostring)' } case $1 in autoconf ) if [ "$available" = "yes" ]; then echo "yes" exit 0 fi ;; config) echo "graph_title SWBOT Meter CO2 温度/湿度/CO2 (リビング)" echo "graph_category sensors" echo "graph_vlabel 温度[C]/湿度[%]/CO2[ppm]" echo "graph_args -l 0 -u 70" echo "temperature.label 温度[C]" echo "temperature.draw LINE2" echo "temperature.colour 00FF00" echo "temperature.warning 30" echo "humidity.label 湿度[%]" echo "humidity.draw LINE2" echo "humidity.colour 00E0FF" echo "CO2.label CO2[ppm]/100" echo "CO2.draw LINE2" echo "CO2.cdef CO2,0.01,*" #echo "CO2.warning 25" exit 0 ;; esac status
Muninで取得した値を、nagios4 で確認するためのプラグイン。
#!/usr/bin/perl use strict ; use warnings ; use Net::Telnet ; my $telnet = new Net::Telnet( Host => "127.0.0.1" , Port => 4949 , Timeout => 30 ) ; $telnet->open() or die( "Can't connect" ) ; $telnet->waitfor( '/#.*$/' ) ; # munin で読み込む項目を指定 $telnet->print( "fetch switchbot_meter_co2\n" ) ; my $flag = 0 ; my %value = () ; # データを読み込む while( my $line = $telnet->getline() ) { last if ( $line =~ /^\.$/ ) ; if ( $line =~ /^([0-9a-zA-Z_]+)\.value\s+([\.0-9]+)\s*$/ ) { $value{$1} = $2 ; $flag = 1 ; } } # nagios プラグインとして範囲を確認 my $st = 0 ; my $item = "-" ; my %status = ( 0 => "OK" , 1 => "Warning" , 2 => "Critical" , 3 => "Unknown" ) ; if ( exists( $value{'temperature'} ) ) { if ( $value{'temperature'} > $ARGV[1] ) { $st = 2 ; $item = "temperature" ; } elsif ( $value{'temperature'} > $ARGV[0] ) { $st = 1 ; $item = "temperature" ; } } if ( exists( $value{'CO2'} ) ) { if ( $value{'CO2'} > $ARGV[3] ) { $st = 2 ; $item = "CO2" ; } elsif ( $value{'CO2'} > $ARGV[2] ) { $st = 1 ; $item = "CO2" ; } } if ( !$flag ) { $st = 3 ; $item = "Error" ; } # 最終結果の出力 printf( "SBMT_MeterCO2 %s %s %2.1f[C] %2.0f[%%] %4.0f[ppm]\n" , $status{$st} , $item , $value{'temperature'} , $value{'humidity'} , $value{'CO2'} ) ; exit $st ;