How to Implement Splunk Enterprise On-Premise for a MuleSoft App
How to Implement Splunk Enterprise On-Premise for a MuleSoft App
In this article, see how to implement Splunk Enterprise on-premise for a MuleSoft application using Anypoint Studio and Anypoint Platform Runtime Manager.
Join the DZone community and get the full member experience.Join For Free
What Is Splunk?
Splunk is a tool used for logging, analyzing, reporting, visualizing, monitoring, or searching the machine data in real time.
Machine data is information that is generated by a computer process, application, device, or any other mechanism without any active intervention from humans. Machine data is everywhere, and it can be generated automatically from various sources like computer processes, elevators, cars, smartphones, etc., and generally, such data is generated in forms of events in an unstructured form.
Splunk is capable of searching, reporting, visualizing, logging, or monitoring any type of data including structured and unstructured data.
Splunk Main Components
Splunk Search Heads
Splunk Search Head is a Splunk instance that distributes searches across the indexers.
Indexers are a Splunk component used to index and store incoming data from forwarders. Splunk instances transform the incoming data into events and stores them into indexes to perform search operations efficiently.
Forwarders are used to collect the data from various sources in a secure, reliable way and forward data to Splunk for indexing and analysis.
Splunk can be deployed and administered on-premise.
Splunk Cloud is a SaaS (Software as a Service) platform for operational intelligence and enterprise as a scalable service. No infrastructure is required.
Splunk Light is the solution for small scale IT environments.
You may also like: Java Applications Log Message Analytics Using Splunk
Download and Install Splunk Enterprise On-Premise
For installing the Splunk on-premise or on your machine, you need to navigate to Splunk Free download and create an account by filling out a form. Click here to start downloading for free.
There are 2 options: either you can download an on-premise Splunk Enterprise trial for 60 days or a Splunk Cloud trial for 15 days.
Once you create a Splunk account, it will give you the option to download Splunk Enterprise and Splunk Cloud.
For this article, we will be downloading Splunk 8.0.1 Windows 64-Bit, and it will download splunk-8.0.1-x64-release.msi and simply run the msi. This will start installing Splunk on your machine, and during the installation process, you need to create a password for admin users and you can select local accounts during installation. For windows, it will create windows services “Splunkd Service.”
Once the Splunk is installed, you can browse to http://localhost:8000, and it will navigate you to Splunk Web GUI.
To Login into Splunk, you need to provide username “admin” and password that you have created during installation.
Creating HTTP Event Collector Token
The HTTP Event Collector (HEC) lets you send data and application events to a Splunk deployment over the HTTP and Secure HTTP (HTTPS) protocols. HEC uses a token-based authentication model.
For creating tokens, navigate to Settings → Data inputs → HTTP Event Collector.
Once you select the HTTP Event Collector, it will navigate you to the screen from where you can create a New Token. Click on the New Token button.
Once you click on the New Token button, it will navigate to form and start filling out the details like Name. Click on Next at the top of the web page for filling further details.
On the next screen, you can select SourceType as log4j, as we will be using log4j as a source for sending data to Splunk from the MuleSoft application.
On the same screen, select all index main, summary, and history.
Now click on the Review button on top of the screen. Finally, review the details and click on Submit Button.
Once you click on the Submit button, it will create a token that we will be using for authentication purposes.
You can again navigate to Settings → Data inputs → HTTP Event Collector to see the newly created token.
Also, make sure that token is enabled. In case it is not enabled, you can click on Global Settings and enable the token as shown in the below screenshot.
Also, notice the HTTP Port Number 8088 and it will be required for sending logs from the application to Splunk. We will be using URL http://localhost:8088 and the token that we have created above for sending logs to Splunk from the MuleSoft application.
Edit the Token and select Default Index as main.
Setting Up Mail Server on Splunk
For setting up a mail server, navigate to Settings → Server Settings → Mail Server Settings.
You need to provide Mail host, username, and password. If you are using smtp.gmail.com, use port number 465 for SSL and 587 for TLS.
You can define email format in Mail Server Settings, and we will keep the default for this article. Click Save.
Integrating MuleSoft Application With Splunk Enterprise Using Anypoint Studio
For integrating a MuleSoft application with Splunk, we will be adding HttpAppender in log4j2.xml located at src/main/resources
For connecting to Splunk from a MuleSoft application, you need to provide the URL where we will be sending logs to Splunk. In our case, the URL is “http://localhost:8088/services/collector/raw”, and you need to add an authorization property and value Token we have created above with Prefix Splunk.
We need to define loggers in log4j2.xml.
Also, make sure AppenderRef matches the name of HTTP Appender.
There is a Mulesoft application created that will be logging information, errors, and other information into Splunk.
This MuleSoft application will receive a request and send the request to a world time zone API to fetch timezone details.
Once you have started the Mule application and have sent the request, it will start logging events into Splunk.
Searching Events With Splunk
For viewing the events, you can go to the Splunk home page and select Search & Reporting.
It will navigate you to the search page. On that page, you can use a filter for searching the events and can view a Data Summary. The data summary will show you all the hosts, sources, and source types from where the events are coming from.
These hosts, sources, and source Types can be used to search for events.
Filter 1: sourceType=log4j for last 24 hours
Filter 2: Error in last 24 hours
Filter 3: HTTP:NOT_FOUND in Last 24 hours
Filter 4: HTTP:NOT_FOUND OR HTTP:CONNECTIVITY in Last 24 hours
Integrating MuleSoft Application With Splunk Enterprise Using Anypoint Platform and Anypoint Server Group or Cluster
For integrating Splunk Enterprise on-premise with Anypoint Server Group or Cluster, you need to create a cluster or server group on Anypoint Platform Runtime Manager.
Adding Server on Anypoint Platform Runtime Manager
For adding a server to Anypoint Platform Runtime Manager, navigate to Runtime Manager → Servers → Add Servers.
Once you click on Add Servers, it will give the command that you need to run on your machine to cd into /bin folder of Mule runtime.
After running the command, you will see the server will be added to Anypoint Platform runtime manager, and it will be a deployed agent on your local machine under /conf folder. Agent details can be found in mule-agent.yml. This will establish secure communication between Anypoint Platform Runtime Manager and your local machine Mule runtime.
Creating Server Group
For creating a server group on Anypoint Runtime Manager, navigate to Runtime Manager → Servers → Create Group.
Define the group name and add the servers that you need in that server group. To add a server into a group, it must be in a running state. Click Create Group, and it will create a server group.
You can add more than one server into Server Group.
Deploying MuleSoft Application to Server Group
For deploying an application to the server group, navigate to Runtime Manager → Applications → Deploy application
For deploying the application to a server group, you need to select Deployment Target as your server Group Name (in this case, it is jitendra-group). Click on Deploy Application. This will deploy applications to all the servers within that particular server group.
Enabling Splunk on Server Group Using Anypoint Runtime Manager
For enabling Splunk on Server Group, navigate to Runtime Manager → Servers → Click on your server group → Manage Group
After clicking on Manage Group, click on the Plugins tab and you will see Splunk and ELK plugin.
Click on the Setting icon in front of Splunk to configure it.
Configure Splunk Event Tracking as shown above in the screenshot.
You need to provide the host and port number of Splunk. In our case, the port number is 8088, and the Token is what we have created at the top of article.
Splunk Source is mule-events and it will be useful for searching events coming from Anypoint Runtime Manager. Click Apply.
This tracking will be applicable for all applications deployed within this server group.
We will be executing an application deployed on the server group and it will start logging the data into Splunk, which can be used for generating alerts, reporting, visualizing, troubleshooting, and analytics.
Now you can see the new Source=mule-events and sourceType=mule have been added to the data summary.
Filter sourceType=mule in last 24 hours
Similarly, you can enable Splunk logging for servers under the cluster.
Generating the Reports and Email Alerts With Splunk
There are default reports available in Splunk. To view reports, navigate to Settings → Searches, reports, and alerts.
We will run Errors in the last 24 hours report by clicking on run. This will show all the errors or exceptions that occurred in the last 24 hours.
For scheduling the report, click on Edit → Edit Schedule.
Once you've clicked on Edit Schedule, it will open a pop-up window and start filling the details like schedule, CRON expression, and Triggered Actions (send email) as shown in the below screenshot. Click Save.
Another way of scheduling PDF: Navigate To Splunk Homepage → Search & Reporting → Reports
Errors in the last 24 hours → Add to dashboard.
It will open a pop-up and fill in the Dashboard title. Click Save.
Once saved, it will open a pop-up window. Click on View Dashboard.
Now click on Export → Schedule PDF Delivery.
This will open a pop-up window, and you can schedule a PDF delivery. We have set up a CRON scheduler to run reports every one minute, and this will send an email every one minute with all errors that occurred in the last 24 hours. Click Save.
Sample Email Received
Sample Error PDF
Splunk is a very useful and powerful tool for logging, analyzing, reporting, searching, and visualizing events and data. MuleSoft provides the capability to easily integrate Splunk using Anypoint Studio and Anypoint Platform Runtime Manager.
Published at DZone with permission of Jitendra Bafna , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.