Last active
December 11, 2015 21:09
-
-
Save chadselph/4660364 to your computer and use it in GitHub Desktop.
Like `wc -l` but for `tail -f`. Example: tail -f some.log | grep ERROR | ./runningwc.py -i 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python -u | |
from twilio.rest import TwilioRestClient | |
import sys | |
import time | |
import threading | |
import argparse | |
total = 0 | |
class Reporter(threading.Thread): | |
def __init__(self, interval, lower_limit, alert_cb): | |
super(Reporter, self).__init__() | |
self.last_time = time.time() | |
self.last_count = 0 | |
self.interval = interval | |
self.lower_limit = lower_limit | |
self.alert_cb = alert_cb | |
def run(self): | |
while True: | |
time.sleep(self.interval) | |
lines = total - self.last_count | |
print("Last {0} seconds: {1} lines".format(self.interval, lines)) | |
if lines < self.lower_limit: | |
self.alert_cb() | |
self.last_count += lines | |
class Counter(threading.Thread): | |
def run(self): | |
global total | |
for line in sys.stdin: | |
total += 1 | |
class SmsAlertFactory(object): | |
def __init__(self, time_between_messages, to, from_, body): | |
self.max_tbm = time_between_messages | |
self.to = to if isinstance(to, list) else [to] | |
self.from_ = from_ | |
self.body = body | |
self.last_message = 0 | |
def __call__(self): | |
print("ALERT!") | |
if self.max_tbm < (time.time() - self.last_message): | |
self.last_message = time.time() | |
client = TwilioRestClient() | |
for n in self.to: | |
client.sms.messages.create(to=n, from_=self.from_, body=self.body) | |
if __name__ == "__main__": | |
# example using SMS notifier | |
nums = ["+155512341231"] | |
sms_alert = SmsAlertFactory(60 * 15, nums, "+15553421412", "process dead?!") | |
parser = argparse.ArgumentParser(description="Count lines from a streaming stdio") | |
parser.add_argument('-i', '--interval', default=10, type=int) | |
parser.add_argument('-b', '--below', default=-1, type=int) | |
opts = parser.parse_args() | |
Counter().start() | |
Reporter(opts.interval, opts.below, sms_alert).start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment