forked from skotman01/AmbientWx-Zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZabbixWxDist.py
39 lines (32 loc) · 997 Bytes
/
ZabbixWxDist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3
import requests
import json
import sys
import time
import random
import os.path
if len(sys.argv) != 4:
print("Expected 3 arguments", file=sys.stderr)
sys.exit(1)
apikey = sys.argv[2]
appkey = sys.argv[3]
wxDataFile = "/tmp/wxData.json"
url = "https://api.ambientweather.net/v1/devices?applicationKey="+ appkey +"&apiKey=" + apikey
if os.path.exists(wxDataFile) and (time.time() - os.path.getmtime(wxDataFile) < 20):
with open(wxDataFile) as json_file:
wxData = json.load(json_file)
else:
response = requests.request("GET", url)
if response.status_code == 200:
wxData = response.json()
with open(wxDataFile, 'w') as outfile:
json.dump(wxData, outfile)
else:
print(response.text, file=sys.stderr)
sys.exit(1)
try:
OutputWx = float(wxData[0]['lastData'][sys.argv[1]])
except (KeyError,ValueError) as e:
print("Invalid data in response", file=sys.stderr)
sys.exit(1)
print(OutputWx)