mirror of
https://github.com/munin-monitoring/contrib.git
synced 2025-08-13 17:24:26 +00:00
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 <module> 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.
46 lines
1.3 KiB
Python
Executable file
46 lines
1.3 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
"""
|
|
munin US NOAA weather plugin (http://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
|
|
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 = 'http://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\)')
|
|
|
|
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 Temperature and Dew Point at code %s' % code)
|
|
print('graph_vlabel Temperature and Dew Point in C')
|
|
print('graph_category sensors')
|
|
print('temperature.label Temperature')
|
|
print('dewpoint.label Dew Point')
|
|
print('graph_args --base 1000 -l 0')
|
|
else:
|
|
u = urlopen(url % code)
|
|
txt = str(u.read())
|
|
u.close()
|
|
|
|
C = re_C.findall(txt)[0]
|
|
DewC = re_DewC.findall(txt)[0]
|
|
print('temperature.value %s' % C)
|
|
print('dewpoint.value %s' % DewC)
|