-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.py
executable file
·72 lines (56 loc) · 1.9 KB
/
monitor.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
import smbus2
import bme280
import os
import queue
import socket
import time
import requests
from newrelic_telemetry_sdk import GaugeMetric, CountMetric, SummaryMetric, MetricClient
from dotenv import load_dotenv
load_dotenv()
metric_client = MetricClient(os.getenv('NEWRELIC_INSIGHTS_INSERT_API_KEY'))
hostname = socket.gethostname()
hiveid = (os.getenv('HIVEID'))
port = 1
address = 0x77
bus = smbus2.SMBus(port)
calibration_params = bme280.load_calibration_params(bus, address)
def measure_cpu_temp():
temp = os.popen("vcgencmd measure_temp").readline()
degrees_c = ((temp.replace("temp=","")).replace("'C",""))
degrees_f = convert_centigrade_to_farenheit(float(degrees_c))
return degrees_f
def convert_c_to_f(centigrade):
return centigrade * 9/5 + 32
def measure_bme280_one():
readings = []
data = bme280.sample(bus, address, calibration_params)
print(data)
readings.append(GaugeMetric("temperature", convert_c_to_f(data.temperature), {"units": "Farenheit","host.name": hostname}))
readings.append(GaugeMetric("pressure", data.pressure, {"units": "hPa","host.name": hostname}))
readings.append(GaugeMetric("humidity", data.humidity, {"units": "% rH","host.name": hostname}))
return readings
def is_cnx_active(timeout):
try:
requests.head("http://www.google.com/", timeout=timeout)
return True
except requests.ConnectionError:
return False
data_queue = queue.Queue()
while True:
readings = measure_bme280_one()
data_queue.put(readings)
# with open("temperature_log.txt", "a") as file:
# file.write(f"{time.time()}, {temperature / 100.0}, {humidity / 1024.0}\n")
print(readings)
try:
response = metric_client.send_batch(readings)
response.raise_for_status()
print("Sent metrics successfully!")
while(data_queue.empty()==False):
print(data_queue.queue[0],end=" ")
data_queue.get()
except:
print("problem sending data:",)
time.sleep(60)