From 05d820334ac4c8ecc90ce740af10b7578c1ad254 Mon Sep 17 00:00:00 2001 From: HaseHarald Date: Sat, 15 Apr 2023 16:20:29 +0200 Subject: [PATCH 1/4] fix(weather_press_/temp_): Cast url-read object to string The read function on an urllib urlopen object returns an object as a response. Regular expressions using re can't be used on such objects. This causes the following error: ``` Traceback (most recent call last): File "/tmp/weather/./weather_press_LOWW", line 43, in hpa = re_hpa.findall(txt)[0] TypeError: cannot use a string pattern on a bytes-like object ``` This can be easily fixed, because said object can simply be cast to string. Which is, what this patch does for both the US NOAA based plugins. --- plugins/weather/weather_press_ | 2 +- plugins/weather/weather_temp_ | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/weather/weather_press_ b/plugins/weather/weather_press_ index 12cc9936..7ce091d9 100755 --- a/plugins/weather/weather_press_ +++ b/plugins/weather/weather_press_ @@ -37,7 +37,7 @@ elif len(sys.argv) == 2 and sys.argv[1] == "config": print('graph_scale no') else: u = urlopen(url % code) - txt = u.read() + txt = str(u.read()) u.close() hpa = re_hpa.findall(txt)[0] diff --git a/plugins/weather/weather_temp_ b/plugins/weather/weather_temp_ index 2d28b436..d0e9c606 100755 --- a/plugins/weather/weather_temp_ +++ b/plugins/weather/weather_temp_ @@ -37,7 +37,7 @@ elif len(sys.argv) == 2 and sys.argv[1] == "config": print('graph_args --base 1000 -l 0') else: u = urlopen(url % code) - txt = u.read() + txt = str(u.read()) u.close() C = re_C.findall(txt)[0] From d8b4732c4a6758e0b45c0e2d047800f6d4bb3a12 Mon Sep 17 00:00:00 2001 From: HaseHarald Date: Sat, 15 Apr 2023 16:39:01 +0200 Subject: [PATCH 2/4] fix(weather_press_/temp_): Use https In 2023, it should be the default to use an encrypted connection, which totally is supported by the data-source. --- plugins/weather/weather_press_ | 6 +++--- plugins/weather/weather_temp_ | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/plugins/weather/weather_press_ b/plugins/weather/weather_press_ index 7ce091d9..b10e3d02 100755 --- a/plugins/weather/weather_press_ +++ b/plugins/weather/weather_press_ @@ -1,11 +1,11 @@ #!/usr/bin/env python3 """ -munin US NOAA weather plugin (http://tgftp.nws.noaa.gov) +munin US NOAA weather plugin (https://tgftp.nws.noaa.gov) Draws pressure in hPa. Copy/link file as 'weather_pressure_CODE', like: weather_pressure_LOWW for Austria, Vienna. -Get the code by going to http://tgftp.nws.noaa.gov, selecting your +Get the code by going to https://tgftp.nws.noaa.gov, selecting your location, and copying the code from the address bar of your browser; should be something like CODE.html. @@ -16,7 +16,7 @@ import sys from urllib.request import urlopen import re -url = 'http://tgftp.nws.noaa.gov/data/observations/metar/decoded/%s.TXT' +url = 'https://tgftp.nws.noaa.gov/data/observations/metar/decoded/%s.TXT' re_hpa = re.compile(r'Pressure.*\((\d+) hPa\)') diff --git a/plugins/weather/weather_temp_ b/plugins/weather/weather_temp_ index d0e9c606..dd54e53f 100755 --- a/plugins/weather/weather_temp_ +++ b/plugins/weather/weather_temp_ @@ -1,11 +1,11 @@ #!/usr/bin/env python3 """ -munin US NOAA weather plugin (http://tgftp.nws.noaa.gov) +munin US NOAA weather plugin (https://tgftp.nws.noaa.gov) Draws temperature/dew point in C. Copy/link file as 'weather_temp_CODE', like: weather_temp_LOWW for Austria, Vienna. -Get the code by going to http://tgftp.nws.noaa.gov, selecting your +Get the code by going to https://tgftp.nws.noaa.gov, selecting your location, and copying the code from the address bar of your browser; should be something like CODE.html. @@ -16,7 +16,7 @@ import sys from urllib.request import urlopen import re -url = 'http://tgftp.nws.noaa.gov/data/observations/metar/decoded/%s.TXT' +url = 'https://tgftp.nws.noaa.gov/data/observations/metar/decoded/%s.TXT' re_C = re.compile(r'Temperature:.*\((-?\d+\.?\d?) C\)') re_DewC = re.compile(r'Dew.*\((-?\d+\.?\d?) C\)') From 0e4dd7d1fbe572bfdc22eb7230e7807dd866907c Mon Sep 17 00:00:00 2001 From: HaseHarald Date: Sat, 15 Apr 2023 17:01:37 +0200 Subject: [PATCH 3/4] feat(weather_hum_): Add a humidity plugin for US NOAA weather This adds a plugin for relative humidity, based on the weather_press_ and weather_temp_ plugins. It's basically a copy of these two plugins with only minor adjustments. --- plugins/weather/weather_hum_ | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 plugins/weather/weather_hum_ diff --git a/plugins/weather/weather_hum_ b/plugins/weather/weather_hum_ new file mode 100755 index 00000000..ed443e26 --- /dev/null +++ b/plugins/weather/weather_hum_ @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +""" +munin US NOAA weather plugin (http://tgftp.nws.noaa.gov) + +Draws relative humidity in %. +Copy/link file as 'weather_humidity_CODE', like: weather_humidity_LOWW for Austria, Vienna. + +Get the code by going to http://tgftp.nws.noaa.gov, selecting your +location, and copying the code from the address bar of your browser; should +be something like CODE.html. + +Linux users might need to adjust the shebang. +""" + +import sys +from urllib.request import urlopen +import re + +url = 'https://tgftp.nws.noaa.gov/data/observations/metar/decoded/%s.TXT' + +re_hum = re.compile(r'Relative Humidity:\s*(\d+)%') + + +code = sys.argv[0][(sys.argv[0].rfind('_') + 1):] +if not code: + sys.exit(1) +elif len(sys.argv) == 2 and sys.argv[1] == "autoconf": + print("yes") +elif len(sys.argv) == 2 and sys.argv[1] == "config": + print('graph_title Relative humidity at code %s' % code) + print('graph_vlabel humidity in %') + print('graph_category sensors') + + print('humidity.label Humidity') + print('graph_args --base 1000 -l 0') +else: + u = urlopen(url % code) + txt = str(u.read()) + u.close() + + hum = re_hum.findall(txt)[0] + print('humidity.value %s' % hum) From b76f6304409cd682a1e82cae805454b9e9ba0575 Mon Sep 17 00:00:00 2001 From: HaseHarald Date: Sat, 22 Jul 2023 20:28:28 +0200 Subject: [PATCH 4/4] fix(weather_temp_): Better match temperature and dew point For whatever reason, sometimes linebreaks are only denoted as \n in the querry-result. This lead to the temperature RegEx matching the dew point. This caused the temperature to be reported the same as the dew point. These changes should make the RegEx more specific and work consistently, no matter if linebreaks are actual linebreaks or just \n. --- plugins/weather/weather_temp_ | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/weather/weather_temp_ b/plugins/weather/weather_temp_ index dd54e53f..fe4f64fd 100755 --- a/plugins/weather/weather_temp_ +++ b/plugins/weather/weather_temp_ @@ -18,8 +18,8 @@ import re url = 'https://tgftp.nws.noaa.gov/data/observations/metar/decoded/%s.TXT' -re_C = re.compile(r'Temperature:.*\((-?\d+\.?\d?) C\)') -re_DewC = re.compile(r'Dew.*\((-?\d+\.?\d?) C\)') +re_C = re.compile(r'Temperature:\s*[0-9]*\s*[F]*\s*\((-?\d+\.?\d?) C\)') +re_DewC = re.compile(r'Dew.*:\s*[0-9]*\s*[F]*\s*\((-?\d+\.?\d?) C\)') code = sys.argv[0][(sys.argv[0].rfind('_') + 1):]