Last active
February 22, 2017 15:44
-
-
Save jfacevedo/950be4a120a2dcf188595387308b3b74 to your computer and use it in GitHub Desktop.
Datadog custom log parser script
This file contains hidden or 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
from datadog import initialize, ThreadStats | |
from datetime import datetime | |
import time | |
import re | |
initialize(api_key='_API_KEY_') | |
stats = ThreadStats() | |
stats.start(flush_interval=60, roll_up_interval=60) | |
def parse_conn(logger, line): | |
current_connections = [] | |
maximun_connection = [] | |
reg = re.compile('.*\*\sUsing (?P<current_connections>\d+) of (?P<maximun_connection>\d+) maximum') | |
try: | |
# Apply regular expression to each line and extract interesting bits. | |
regMatch = reg.match(line) | |
if regMatch: | |
linebits = regMatch.groupdict() | |
current_connections = int(linebits['current_connections']) | |
maximun_connection = int(linebits['maximun_connection']) | |
# Split the line into fields | |
log_args = line.split(' ') | |
# check if there is an space in the day (happens when day is 1 to 9) | |
if len(log_args[2]) == 0: | |
date = log_args[0],log_args[1],log_args[3],log_args[4],log_args[5] | |
else: | |
date = log_args[0],log_args[1],log_args[2],log_args[3],log_args[4] | |
# Convert date into a unix timestamp | |
date = " ".join(date) | |
date = datetime.strptime(date, "%a %b %d %H:%M:%S %Y") | |
date = time.mktime(date.timetuple()) | |
stats.gauge('current_connections', current_connections, date, tags=["type:receiver"]) | |
stats.gauge('maximun_connection', maximun_connection, date, tags=["type:receiver"]) | |
stats.increment('connections_count', 1, date, tags=["type:receiver"]) | |
else: | |
#raise LogsterParsingException("regmatch failed to match") | |
return None | |
except AssertionError: | |
#raise LogsterParsingException("regmatch or contents failed with %s" % e) | |
return None | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment