Created
October 31, 2016 10:41
-
-
Save dimaqq/5de22c14679197587b8d295c201dd8b5 to your computer and use it in GitHub Desktop.
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
import os | |
import time | |
import json | |
import logging | |
import platform | |
import datetime | |
import requests | |
import threading | |
import subprocess | |
TOKEN = "YOUR-LOGGLY-TOKEN-HERE" | |
queue = [] | |
quit = False | |
class Sender(threading.Thread): | |
def run(self): | |
while not quit: | |
try: | |
if not queue: | |
time.sleep(1) | |
continue | |
data = queue[:] | |
# Loggly bulk API wants one event per line | |
r = requests.post(f"https://logs-01.loggly.com/bulk/{TOKEN}/tag/vmstat", data="\n".join(map(json.dumps, data))) | |
r.raise_for_status() | |
del queue[:len(data)] | |
logging.info("Send %s records", len(data)) | |
except Exception: | |
logging.exception("ouch") | |
time.sleep(1) | |
def loop(): | |
header = dict() | |
p = subprocess.Popen(["vmstat", "1"], stdout=subprocess.PIPE, universal_newlines=True) | |
for line in p.stdout: | |
bits = line.split() | |
try: | |
data = tuple(map(int, bits)) | |
except ValueError: | |
header[len(bits)] = bits | |
continue | |
event = dict(zip(header[len(bits)], data)) | |
event["timestamp"] = datetime.datetime.utcnow().isoformat() | |
event["hostname"] = platform.node() | |
queue.append(event) | |
if __name__ == "__main__": | |
logging.basicConfig(level=logging.DEBUG if "DEBUG" in os.environ else logging.WARN) | |
Sender().start() | |
try: | |
loop() | |
finally: | |
quit = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment