Here’s a detailed exercise for creating a Data Model in Splunk using the access log data. This exercise will guide you through the process of setting up a Data Model, defining datasets, and working with the data in an accelerated way.
To create a Data Model from access logs (e.g., access_combined sourcetype), which can be used to generate optimized reports and pivots for web traffic analysis, including page requests, user sessions, and response times.
If you haven’t already uploaded the access log data, follow these steps:
1. Go to Settings > Add Data.
2. Choose Upload and select the access log file (access_combined.log or similar format).
3. Assign the data to an index (e.g., index=web) and sourcetype (access_combined).
4. Complete the data ingestion process.
Navigate to the Data Models section:
Define the Data Model:
Click Create to proceed to the next step.
A root dataset is the base dataset from which other datasets can be derived. It forms the foundation of your data model.
access_combined or whichever sourcetype your access log data is associated with. # sourcetype=access_combined
Sree Root Web Events
index=tutorial_data sourcetype=access_combined_wcookie
Description: Add a description for your root dataset (e.g., "This dataset contains all web access events").
Now, define which fields are important to include in your root dataset. These fields will allow you to extract meaningful insights from the data.
Add Fields:
ip: Client IP address.status: HTTP status code (e.g., 200, 404).bytes: Number of bytes returned in the response.response_time: Time taken to respond to the request.url: The requested URL.Add Calculated Fields:
You can also create calculated fields. For example, to convert bytes to kilobytes:
response_size_kb.round(bytes/1024,1)
- **Field Type**: Choose `Number`.Add Aliases (Optional):
ip to client_ip for consistency:
ip.client_ip.Save your root dataset after adding fields and aliases.
Child datasets are derived from the root dataset. You can create specialized child datasets to focus on specific aspects of your access logs, such as error logs or successful page loads.
Add a Child Dataset for Error Logs:
spl
status>=400
Add a Child Dataset for Successful Requests:
spl
status=200
You can create more child datasets based on other criteria, such as response time thresholds or specific IP addresses.
Data models can be accelerated to speed up searches and reports, especially when working with large datasets.
This will pre-compute summaries of the data and significantly improve search performance on the accelerated data.
Once your data model is created, you can use it in Pivots or Searches to generate reports and visualizations.
datamodel command to search within the data model:
| datamodel "Web_Access_Logs_Model"
| from datamodel:"Web_Access_Logs_Model"."Error_Logs"
| from datamodel:"Web_Access_Logs_Model"."Error_Logs" | search status=500
| table _time, error_code, host, uri
| datamodel "Web_Access_Logs_Model" "Error_Logs" search
| stats count by status
| datamodel "Web_Access_Logs_Model" "Error_Logs" search
| stats count by clientip, status
| datamodel "Web_Access_Logs_Model" "Error_Logs" search
| where status >= 500
| stats count by clientip, status
| datamodel "Web_Access_Logs_Model" "Error_Logs" search
| timechart count by status
| datamodel "Web Access Logs Model" "Web Events" search
| datamodel "SreeDataModal" search
|datamodel "SreeDataModal" "Sree_Root_Web_Events" search
|datamodel "SreeDataModal" "Sree_Child_Error_Logs" search
This search retrieves all the data from the Web Events dataset.
status or ip, calculate averages for response_time, etc.).Use the Pivot interface to visualize the data model, or run searches based on the datasets you created to generate insights. For example:
| datamodel "Web Access Logs Model" "Web Events" search
| stats count by status| datamodel "Web Access Logs Model" "Web Events" search
| stats avg(response_time) by status| datamodel "Web Access Logs Model" "Error Logs" search
| stats count by statusOnce you have created visualizations or saved searches using the data model, you can add them to dashboards for real-time monitoring and reporting.
This exercise walks you through creating a data model based on access log data in Splunk. You defined a root dataset and added fields like ip, status, and response_time. You then created child datasets to focus on specific aspects like error logs and successful requests. By enabling acceleration, you improved the performance of searches and reports based on this data model. Finally, you used pivots and searches to analyze the data and visualize it in dashboards.
Data models are a powerful way to structure your data in Splunk, making complex searches and reporting much faster and easier to manage.
You can use child datasets from a Data Model in searches using the datamodel command in Splunk. Child datasets, which are subsets of a parent (root) dataset, allow you to run more specific, focused queries. Below is a step-by-step guide on how to use a child dataset in a search, along with examples.
Identify the Data Model and Child Dataset:
"Web Access Logs Model" and a child dataset named "Error Logs".Use the datamodel Command:
datamodel command is used to search within a data model and its datasets.spl
| datamodel "<Data Model Name>" "<Child Dataset Name>" search
Error Logs):Let’s assume you have a child dataset called "Error Logs" that filters events where status >= 400 (HTTP error codes). Here’s how you would query it:
| datamodel "Web Access Logs Model" "Error Logs" search
| stats count by statusdatamodel "Web Access Logs Model" "Error Logs" search: This command accesses the "Error Logs" child dataset within the "Web Access Logs Model".search keyword tells Splunk to retrieve all events from this dataset.stats count by status command counts how many events exist for each HTTP status code.You can also work with the fields defined in the child dataset. For example, if your "Error Logs" dataset includes fields like ip, status, and response_time, you can access these fields in your search:
| datamodel "Web Access Logs Model" "Error Logs" search
| stats count by ip, statusstatus >= 400) grouped by ip and status.You can add more filtering criteria when working with child datasets. For example, if you want to look for error events that took more than 500ms to respond, you can use additional where or eval filters.
| datamodel "Web Access Logs Model" "Error Logs" search
| where response_time > 500
| stats count by status, ip"Error Logs" dataset and filters them to only include those where response_time is greater than 500ms. The results are grouped by status and ip.You can combine child datasets with other Splunk commands like timechart, chart, eval, etc., to further manipulate the data.
| datamodel "Web Access Logs Model" "Error Logs" search
| timechart count by status"Error Logs" child dataset and creates a time-based chart that shows the count of events over time, grouped by status.To see what fields are available in the child dataset, you can use the table command to list them.
"Error Logs" Dataset| datamodel "Web Access Logs Model" "Error Logs" search
| table _time, ip, status, response_time, bytes_time, ip, status, response_time, bytes) from the "Error Logs" dataset.datamodel Command: This command is used to search within a data model. The structure is | datamodel "<Data Model Name>" "<Dataset Name>" search.stats, timechart, where, eval, etc.) on the child dataset for further analysis.Here’s a complete example using a child dataset (Error Logs), where we:
- Query the dataset.
- Filter events where response_time is high.
- Calculate the average response time by status code.
| datamodel "Web Access Logs Model" "Error Logs" search
| where response_time > 500
| stats avg(response_time) as avg_response_time by statusresponse_time is greater than 500ms and calculates the average response time (avg_response_time) for each HTTP status code.You can use the datamodel command to query and analyze child datasets created within a data model in Splunk. Child datasets allow you to work with filtered and pre-defined event sets, making it easier to focus on specific types of data (e.g., error logs, successful requests, or specific user sessions). You can apply any Splunk command (e.g., stats, timechart, eval) to the child dataset just like you would in a regular search, giving you powerful insights into your data.