2020-01-01 16:17:10 +00:00
/*
2020-06-27 10:52:44 +01:00
xsns_75_prometheus . ino - Web based information for Tasmota
2020-01-01 16:17:10 +00:00
Copyright ( C ) 2020 Theo Arends
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# ifdef USE_PROMETHEUS
/*********************************************************************************************\
* Prometheus support
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2020-06-27 10:52:44 +01:00
# define XSNS_75 75
2020-01-01 16:17:10 +00:00
void HandleMetrics ( void )
{
if ( ! HttpCheckPriviledgedAccess ( ) ) { return ; }
AddLog_P ( LOG_LEVEL_DEBUG , S_LOG_HTTP , PSTR ( " Prometheus " ) ) ;
WSContentBegin ( 200 , CT_PLAIN ) ;
2020-01-02 13:17:49 +00:00
2020-01-01 16:17:10 +00:00
char parameter [ FLOATSZ ] ;
2020-06-27 12:21:11 +01:00
// Pseudo-metric providing metadata about the running firmware version.
WSContentSend_P ( PSTR ( " # TYPE tasmota_info gauge \n tasmota_info{version= \" %s \" ,image= \" %s \" ,build_timestamp= \" %s \" } 1 \n " ) ,
my_version , my_image , GetBuildDateAndTime ( ) . c_str ( ) ) ;
2020-10-28 16:32:07 +00:00
WSContentSend_P ( PSTR ( " # TYPE tasmota_uptime_seconds gauge \n tasmota_uptime_seconds %d \n " ) , TasmotaGlobal . uptime ) ;
2020-06-27 03:10:00 +01:00
WSContentSend_P ( PSTR ( " # TYPE tasmota_boot_count counter \n tasmota_boot_count %d \n " ) , Settings . bootcount ) ;
2020-06-29 01:35:27 +01:00
WSContentSend_P ( PSTR ( " # TYPE tasmota_flash_writes_total counter \n tasmota_flash_writes_total %d \n " ) , Settings . save_flag ) ;
2020-06-25 11:06:24 +01:00
2020-06-28 02:43:29 +01:00
// Pseudo-metric providing metadata about the WiFi station.
WSContentSend_P ( PSTR ( " # TYPE tasmota_wifi_station_info gauge \n tasmota_wifi_station_info{bssid= \" %s \" ,ssid= \" %s \" } 1 \n " ) , WiFi . BSSIDstr ( ) . c_str ( ) , WiFi . SSID ( ) . c_str ( ) ) ;
// Wi-Fi Signal strength
WSContentSend_P ( PSTR ( " # TYPE tasmota_wifi_station_signal_dbm gauge \n tasmota_wifi_station_signal_dbm{mac_address= \" %s \" } %d \n " ) , WiFi . BSSIDstr ( ) . c_str ( ) , WiFi . RSSI ( ) ) ;
2020-06-28 11:23:18 +01:00
if ( ! isnan ( global_temperature_celsius ) ) {
dtostrfd ( global_temperature_celsius , Settings . flag2 . temperature_resolution , parameter ) ;
WSContentSend_P ( PSTR ( " # TYPE global_temperature_celsius gauge \n global_temperature_celsius %s \n " ) , parameter ) ;
2020-01-01 16:17:10 +00:00
}
if ( global_humidity ! = 0 ) {
dtostrfd ( global_humidity , Settings . flag2 . humidity_resolution , parameter ) ;
WSContentSend_P ( PSTR ( " # TYPE global_humidity gauge \n global_humidity %s \n " ) , parameter ) ;
}
2020-06-28 11:23:18 +01:00
if ( global_pressure_hpa ! = 0 ) {
dtostrfd ( global_pressure_hpa , Settings . flag2 . pressure_resolution , parameter ) ;
WSContentSend_P ( PSTR ( " # TYPE global_pressure_hpa gauge \n global_pressure_hpa %s \n " ) , parameter ) ;
2020-01-01 16:17:10 +00:00
}
# ifdef USE_ENERGY_SENSOR
dtostrfd ( Energy . voltage [ 0 ] , Settings . flag2 . voltage_resolution , parameter ) ;
2020-08-19 19:57:28 +01:00
WSContentSend_P ( PSTR ( " # TYPE energy_voltage_volts gauge \n energy_voltage_volts %s \n " ) , parameter ) ;
2020-01-01 16:17:10 +00:00
dtostrfd ( Energy . current [ 0 ] , Settings . flag2 . current_resolution , parameter ) ;
2020-08-19 19:57:28 +01:00
WSContentSend_P ( PSTR ( " # TYPE energy_current_amperes gauge \n energy_current_amperes %s \n " ) , parameter ) ;
2020-01-01 16:17:10 +00:00
dtostrfd ( Energy . active_power [ 0 ] , Settings . flag2 . wattage_resolution , parameter ) ;
2020-08-19 19:57:28 +01:00
WSContentSend_P ( PSTR ( " # TYPE energy_power_active_watts gauge \n energy_power_active_watts %s \n " ) , parameter ) ;
2020-01-01 16:17:10 +00:00
dtostrfd ( Energy . daily , Settings . flag2 . energy_resolution , parameter ) ;
2020-08-19 19:57:28 +01:00
WSContentSend_P ( PSTR ( " # TYPE energy_power_kilowatts_daily counter \n energy_power_kilowatts_daily %s \n " ) , parameter ) ;
2020-01-01 16:17:10 +00:00
dtostrfd ( Energy . total , Settings . flag2 . energy_resolution , parameter ) ;
2020-08-19 19:57:28 +01:00
WSContentSend_P ( PSTR ( " # TYPE energy_power_kilowatts_total counter \n energy_power_kilowatts_total %s \n " ) , parameter ) ;
2020-01-01 16:17:10 +00:00
# endif
2020-01-02 13:17:49 +00:00
/*
// Alternative method using the complete sensor JSON data
// For prometheus it may need to be decoded to # TYPE messages
mqtt_data [ 0 ] = ' \0 ' ;
MqttShowSensor ( ) ;
char json [ strlen ( mqtt_data ) + 1 ] ;
snprintf_P ( json , sizeof ( json ) , mqtt_data ) ;
// Do your Prometheus specific processing here.
// Look at function DisplayAnalyzeJson() in file xdrv_13_display.ino as an example how to decode the JSON message
WSContentSend_P ( json ) ;
*/
2020-01-01 16:17:10 +00:00
WSContentEnd ( ) ;
}
/*********************************************************************************************\
* Interface
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2020-06-27 10:52:44 +01:00
bool Xsns75 ( uint8_t function )
2020-01-01 16:17:10 +00:00
{
bool result = false ;
switch ( function ) {
case FUNC_WEB_ADD_HANDLER :
2020-10-20 17:56:18 +01:00
WebServer_on ( PSTR ( " /metrics " ) , HandleMetrics ) ;
2020-01-01 16:17:10 +00:00
break ;
}
return result ;
}
# endif // USE_PROMETHEUS