#!/usr/bin/python

import json
import requests

# this script gets statistics for all cluster nodes
# and sends a bulk request to index the data as follows:
# index: 'nodes_stats'
# type: the node name
# id: automatically generated by elasticsearch
# source: statistics per node

# notes:
# - the mapping of 'nodes_stats' is important to ensure correct indexing,
#   namely: timestamp fields as dates and string fields as not-analyzed
# - use fixed/predefined node names, otherwise any restart will generate new names
#   and the data for a single node will be indexed under different types  
 
stats = requests.get('http://localhost:9200/_nodes/stats?all').json()

if (stats is not None):
	nodes_data = stats['nodes']
	if (nodes_data is not None):
		bulk_data = ''
		for node_id in nodes_data:
			node_data = nodes_data[node_id]
			node_name = node_data['name']
			if (node_name is not None and node_data is not None):
				bulk_data += '{"index": {"_index": "nodes_stats", "_type": "' + node_name + '"}}\n'
				bulk_data += json.dumps(node_data) + '\n'
		if (bulk_data != ''):
			response = requests.post('http://localhost:9200/_bulk', data=bulk_data)

