-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather.py
33 lines (27 loc) · 1.13 KB
/
weather.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
import requests
from prometheus_client import CollectorRegistry, Gauge
from prometheus_client import push_to_gateway
import time
import random
from prometheus_client.exposition import basic_auth_handler
def my_auth_handler(url, method, timeout, headers, data):
username = 'admin'
password = '%changeit%'
return basic_auth_handler(url, method, timeout, headers, data, username, password)
HOURLY_RED_HAT = "https://api.openweathermap.org/data/2.5/onecall?lat=59.4717&lon=24.4580&appid=b6187994e3efa0cf3bab314f7e457dfc"
def get_temperature():
result = requests.get(HOURLY_RED_HAT)
return round(result.json()["current"]["temp"] - 273.15)
def prometheus_temperature(num):
registry = CollectorRegistry()
g = Gauge("red_hat_temp", "Temperature at Red Hat HQ", registry=registry)
g.set(num)
return registry
def push_temperature(url):
#temp = get_temperature()
#registry = prometheus_temperature(temp)
registry = prometheus_temperature(get_temperature())
push_to_gateway(url, "temperature collector", registry, handler=my_auth_handler)
#print(temp)
urls="http://localhost:9091"
push_temperature(urls)