GOT BUSTED!! BUT HEY! THANKS TO PYTHON! :D

So recently I was given task to install kibana on one of the machine. I started with the regular tutorial and ended up installing the ELK stack. The problem started when I was told to install a third party application which was built over by Kibana. The app used to have multiple dashboard(s) which would be fetched in a nice drop down format. Since I was given the task to re-engineer the application more of like a reverse engineering process I got stuck at one point. The simple point being I changed the default URL or the landing page URL to my newly json file which contains all the specific graphs and queries. But whenever I tried to change the other json file(s) the changes were not reflected in the dashboards which were saved at the ‘kibana-int’ index in ElasticSearch.

Hence a simple Python script along with the shell script did the task for me. So basically I created a json file using Kibana. Put in all the queries and graphs/widgets that were required! And a 15 line Python Script did the trick for me.

[
import glob
import json
import urllib

for input_file in glob.glob(‘../dashboards/*.json’):
with open(input_file) as input_:
dashboard = json.load(input_)
title = dashboard[“title”]

wrapper_obj = {
‘user’: ‘guest’,
‘group’: ‘guest’,
‘title’: title,
‘dashboard’: json.dumps(dashboard)
}

output_file = “{}.json”.format(urllib.quote_plus(title))
with open(output_file, “w”) as output_:
json.dump(wrapper_obj, output_)

]

So this script basically fetches the json file from the directory called as dashboards. And pushes it into the generated folder. Where I have already specified the directory structure and Drop_Down navigation menu. So I don’t need to name them again and again. Meaning whatever files are in the generated directory will be automatically shown in the drop down menu of my Kibana Dashboard.

Furthermore, a shell script was later added to create an updated kibana-int index in the elasticsearch database. And voila ! I am good to go.

Here’s my shell script. :

[

File Edit Options Buffers Tools Sh-Script Help
#!/bin/sh

if [ -z “$1” ]; then
_ELASTICHOST=localhost
else
_ELASTICHOST=$1
fi
ELASTICSEARCH=http://$_ELASTICHOST:9200/

for file in generated/*.json
do
name=`basename $file .json`
echo “Loading $name: ”
curl -XPUT $ELASTICSEARCH/kibana-int/dashboard/$name \
-d @$file || exit 1
echo
done ]

And with the click of a button I have all the required files and graphs!!!

Well that’s all. Will be writing about my new adventures ! Till the next time ADIOS! 🙂 python1

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *