Simovits

Simple Kibana app to cancel Elasticsearch tasks

Kibana is becoming a versatile tool for viewing Elasticsearch logs. However, when using Kibana a while it soon becomes clear that you would like to have some features that are missing or yet to be incorporated in the Kibana application. Luckily it is easy to write Kibana plugins for the features that one requires. The documentation for writing Kibana plugins is a bit minimalistic and lacks basic examples.

The simple example shown in this blog post is a working kibana app that can be used to cancel Elasticsearch-tasks from Kibana using the Elasticsearch Tasks API. The example displays the following aspects of kibana plugin writing:

Kibana-plugins are stored in zip-files and installed using the following:

kibana-plugin install file:///kibanaplugin.zip

and removed again with

kibana-plugin remove pluginname

Be aware that the code is required to be stored in a folder within the zip-file called kibana. The code example contains the following files:

The point of using your own API in Kibana instead of calling Elasticsearch directly is so that it is possible to prevent users from calling Elasticsearch directly (security reasons). The code below shows the Kibana routes used in the API of the example code.

 
export default function (server, options) 
{
	const https = 		require('http');
	const strESHost = 	`${options.esHost}` ;
	const esPort = 		`${options.esPort}` ;
	
	function getURL(strHost, strPath, port, strMethod)
	{
		return new Promise((resolve, strURL) => 
		{
			try
			{
				https.get({host:strHost,path:strPath,port:port,method:strMethod}, (resp) => 
				{
					  let data = '';

					  // A chunk of data is received.
					  resp.on('data', (chunk) => 
					  {
							data += chunk;
					  });

					  // The whole response is done.
					  resp.on('end', () => 
					  {
						  //console.log(data);

						  resolve( data);
					  });

				}).on("error", function (httperror)
				{
					console.log("GET request error");
					resolve( "GET request error " + httperror);
				});		
			
			}
			catch(err)
			{
				console.log(err.message);
			}			
		});
	}
	

  server.route(
  {
    path: '/api/yellowpanel/gettasks',
    method: 'GET',
    async handler(req, reply) 
    {
			try
			{
				var strData = await getURL(strESHost, '_tasks', esPort, 'GET');
				
				return strData;
			}
			catch(err)
			{
				console.log(err.message);
				return err.message;
			}
    }
  }
  
  );
  
  
  server.route(
  {
    path: '/api/yellowpanel/stoptask/{name}',
    method: 'GET',
    async handler(req, reply) 
    {
			try
			{		
				console.log(req.params.name);
				
				var strData = await getURL(strESHost, '_tasks/' + escape(req.params.name) + '/_cancel', esPort, 'POST');
				
				return strData;
			
			}
			catch(err)
			{
				console.log(err.message);
				return err.message;
			}	
    }
  }
  
  ); 
  
  server.route(
  {
    path: '/api/yellowpanel/stopalltasks',
    method: 'GET',
    async handler(req, reply) 
    {
			try
			{
				var strData = await getURL(strESHost, '_tasks/_cancel', esPort, 'POST');
				
				return strData;
			}
			catch(err)
			{
				console.log(err.message);
				return err.message;
			}	
    }
  }
  
  );   
}

Download the example code here: https://github.com/jamesrep/yellowpanel/archive/master.zip or:

git clone https://github.com/jamesrep/yellowpanel.git