Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Availability logs to ElasticSearch #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion module/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
import sys
import pymongo
import traceback
import hashlib

from elasticsearch import Elasticsearch
from shinken.objects.service import Service
from shinken.modulesctx import modulesctx

Expand Down Expand Up @@ -477,6 +479,7 @@ def record_availability(self, hostname, service, b):
# Cache index ...
query = """%s/%s_%s""" % (hostname, service, day)
q_day = { "hostname": hostname, "service": service, "day": day.strftime('%Y-%m-%d') }
q_day_es = { "hostname": hostname.encode('ascii'), "service": service.encode('ascii'), "day": day.strftime('%Y-%m-%d') }
q_yesterday = { "hostname": hostname, "service": service, "day": yesterday.strftime('%Y-%m-%d') }

# Test if record for current day still exists
Expand Down Expand Up @@ -559,13 +562,43 @@ def record_availability(self, hostname, service, b):
data['last_check_timestamp'] = int(b.data['last_chk'])

self.availability_cache[query] = data

#Encode data for ElasticSearch
data['day'] = data['day'].encode('ascii')
data['@timestamp'] = str(datetime.datetime.utcfromtimestamp(int(data['day_ts'])).isoformat()+'Z').encode('ascii')
data['is_downtime'] = data['is_downtime'].encode('ascii')
data['service'] = data['service'].encode('ascii')
data['hostname'] = data['hostname'].encode('ascii')
data['sla_duration'] = 86400 - data['daily_2']
data['sla_percentage'] = float(data['sla_duration'])/float(86400)
data['sla_percentage_invert'] = 1 - (float(data['sla_duration'])/float(86400))

try:
del data['_id']
except KeyError:
pass

data_es = {key.encode("ascii"): value for (key, value) in data.items()}


# Store cached values ...
try:
logger.debug("[mongo-logs] store for: %s", self.availability_cache[query])
# self.db[self.hav_collection].save(self.availability_cache[query])
self.db[self.hav_collection].replace_one(q_day, self.availability_cache[query], upsert=True)
except AutoReconnect, exp:


#Part to send availability logs to ElasticSearch
es = Elasticsearch()
# Create index name
d = datetime.date.today()
index_name = 'shinken_availability-' + d.strftime('%Y.%m.%d'))
# Create unique id
str1 = str(q_day_es)
encoded = hashlib.md5(str1).hexdigest()
# Send data to ElasticSearch
es.index(index=index_name, doc_type="shinken-logs", id=encoded, body=data_es)
except AutoReconnect, exp:
logger.error("[mongo-logs] Autoreconnect exception when updating availability: %s", str(exp))
self.is_connected = SWITCHING
# Abort update ... no backlog management currently!
Expand Down