Skip to content

Commit a0379f1

Browse files
committed
Added healthchecks.io logger
1 parent 3c0cb38 commit a0379f1

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

loggers.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
from urllib.request import urlopen
22

3+
class HealthChecks:
4+
"""Ping a HealthChecks.io URL for each event"""
5+
6+
def __init__(self, url, test_mode=False):
7+
"""
8+
Setup a logger with the specified URL
9+
10+
:param url: the URL to log to on each event
11+
"""
12+
self.url = url
13+
self.test_mode = test_mode
14+
15+
def log(self, event):
16+
"""
17+
Log the event to the specified URL via GET method
18+
19+
:param event: the event
20+
"""
21+
print("Logging to %s:" % self.url)
22+
print("Event: %s" % event)
23+
24+
if not self.test_mode:
25+
urlopen(self.url)
326

427
class ThingSpeak:
528
"""ThingSpeak channel used to log pump on/off events"""

pump_watcher.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,21 +131,28 @@ def gen_mainly_off():
131131
parser.add_argument("gpio_pin", help="GPIO pin connected to the pump", type=int)
132132
parser.add_argument("--thingspeak", dest="thing_speak_api",
133133
help="API key to write new sensor readings to thingspeak.com channel")
134+
parser.add_argument("--healthchecks", dest="healthchecks_url",
135+
help="healthchecks.io URL to ping when pump is activated")
134136
parser.add_argument("--gpio", help="GPIO library to use implementation", type=str, choices=["RPi.GPIO", "wiringpi"],
135137
dest="gpio_lib", default=None)
136138
args = parser.parse_args()
137139
print(args)
138140

139141
# --- Setup where to log the data
140-
logger = None
142+
all_loggers = []
141143

142-
# add a logger to ThingSpeak if defined, otherwise print to console
144+
# add a logger to ThingSpeak if defined
143145
if args.thing_speak_api:
144146
print("Adding ThingSpeak logger (API key %s)" % args.thing_speak_api)
145-
logger = loggers.ThingSpeak(args.thing_speak_api)
147+
all_loggers.append(loggers.ThingSpeak(args.thing_speak_api))
146148
else:
147149
print("Adding console logger")
148-
logger = loggers.ConsoleLogger()
150+
all_loggers.append(loggers.ConsoleLogger())
151+
152+
# add a logger to HealthChecks.io if defined
153+
if args.healthchecks_url:
154+
print("Adding HealthChecks logger (URL %s)" % args.healthchecks_url)
155+
all_loggers.append(loggers.HealthChecks(args.thing_speak_url))
149156

150157
print("Connecting to pin %s" % args.gpio_pin)
151158

@@ -166,7 +173,9 @@ def gen_mainly_off():
166173
print("GPIO disabled")
167174
pump = AbstractPump(args.gpio_pin)
168175

169-
pump.add_listener(logger)
176+
# add all the loggers setup previously
177+
for logger in all_loggers:
178+
pump.add_listener(logger)
170179

171180
try:
172181
print("Waiting for events...")

0 commit comments

Comments
 (0)