{"id":3680,"date":"2023-02-12T19:47:47","date_gmt":"2023-02-13T00:47:47","guid":{"rendered":"https:\/\/osric.com\/chris\/accidental-developer\/?p=3680"},"modified":"2023-02-12T19:47:47","modified_gmt":"2023-02-13T00:47:47","slug":"running-splunk-in-aws","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2023\/02\/running-splunk-in-aws\/","title":{"rendered":"Running Splunk in AWS"},"content":{"rendered":"<p>I don&#8217;t like using Google Analytics. The data is useful and well-presented, but I really just want basic web stats without sending all my web stats (along with data from my users) to Google. I&#8217;ve considered a number of other options, including <a href=\"https:\/\/matomo.org\/\">Matomo<\/a>. But I already use Splunk at work, why not run Splunk at home too?<\/p>\n<p>Splunk Enterprise offers a 60-day trial license. After that, there&#8217;s a <a href=\"https:\/\/docs.splunk.com\/Documentation\/Splunk\/9.0.3\/Admin\/MoreaboutSplunkFree\">free license<\/a>. It&#8217;s not really clear that the free license covers what I&#8217;m trying to do here. The free license info includes:<\/p>\n<blockquote><p>If you want to run Splunk Enterprise to practice searches, data ingestion, and other tasks without worrying about a license, Splunk Free is the tool for you.<\/p><\/blockquote>\n<p>I think this scenario qualifies. This is a hobby server. I use Splunk at my day job, so this is in some sense Splunk practice. I&#8217;ll give it more thought over the next 60 days. Your situation may vary!<\/p>\n<p>I launched an EC2 instance in AWS (Amazon Web Services). I picked a t2.micro instance. That instance size might be too small, but I&#8217;m not planning to send much data there. I picked Amazon Linux, which uses <code>yum<\/code> and RPMs for package management, familiar from the RHEL, CentOS, and now Rocky Linux servers I use frequently. (One thing to note, the default user for Amazon Linux is <code>ec2-user<\/code>. I always have to look that up.)<\/p>\n<p>For purposes of this post, I&#8217;ll use <code>203.0.113.18<\/code> as the EC2 instance&#8217;s public IP address. (<code>203.0.113.0\/24<\/code> is an address block reserved for documentation, see <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc5737\">RFC 5737<\/a>.)<\/p>\n<p>I transferred the RPM to the new server. I&#8217;m using Splunk 9.0.3, the current version as of this writing. I installed it:<\/p>\n<pre><code>sudo yum install splunk-9.0.3-dd0128b1f8cd-linux-2.6-x86_64.rpm<\/code><\/pre>\n<p>Yum reported the installed size as 1.4 GB. Important to note, since I used an 8 GB HD, the default volume size when I launched the EC2 instance.<\/p>\n<p>I added an inbound rule the security group associated with the EC2 instance to allow 8000\/tcp traffic from my home IPv4 address.<\/p>\n<p>The installation works! I was able to connect to <code>203.0.113.18:8000<\/code> in a web browser. My connection to <code>203.0.113.18:8000<\/code> was not encrypted, but one thing at a time, right?<\/p>\n<p>Disk space, as I suspected, might be an issue. This warning appeared in Splunk&#8217;s health status:<\/p>\n<pre><code>MinFreeSpace=5000. The diskspace remaining=3962 is less than 1 x minFreeSpace on \/opt\/splunk\/var\/lib\/splunk\/audit\/db<\/code><\/pre>\n<p>Next question: how do I get data into Splunk? The Splunk Enterprise download page helpfully includes a link to a &#8220;Getting Data In \u2014 Linux&#8221; video, although the video focused on ingesting local logs. I&#8217;m more interested in setting up the Splunk Universal Forwarder on a different server and ingesting logs from the osric.com web server. I installed the Splunk forwarder on the target web server.<\/p>\n<p>I enabled a receiver via Splunk web (see <a href=\"https:\/\/docs.splunk.com\/Documentation\/Forwarder\/9.0.3\/Forwarder\/Enableareceiver\">Enable a receiver for Splunk Enterprise<\/a> for information). I used the suggested port, 9997\/tcp.<\/p>\n<p>I also allowed this traffic from the web server&#8217;s IPv4 address via the AWS security group associated with the EC2 instance.<\/p>\n<p>I configured the forwarder on the target web server (see <a href=\"https:\/\/docs.splunk.com\/Documentation\/Forwarder\/9.0.3\/Forwarder\/Configuretheuniversalforwarder\">Configure the universal forwarder using configuration files<\/a> for more details):<\/p>\n<pre><code>$ .\/bin\/splunk add forward-server 203.0.113.18:9997\r\nWarning: Attempting to revert the SPLUNK_HOME ownership\r\nWarning: Executing \"chown -R splunk \/opt\/splunkforwarder\"\r\nWARNING: Server Certificate Hostname Validation is disabled. Please see server.conf\/[sslConfig]\/cliVerifyServerName for details.\r\nSplunk username: admin\r\nPassword:\r\nAdded forwarding to: 203.0.113.18:9997.<\/code><\/pre>\n<p>I tried running a search, but the disk space limitations finally became apparent:<\/p>\n<pre><code>Search not executed: The minimum free disk space (5000MB) reached for \/opt\/splunk\/var\/run\/splunk\/dispatch. user=admin., concurrency_category=\"historical\", concurrency_context=\"user_instance-wide\", current_concurrency=0, concurrency_limit=5000<\/code><\/pre>\n<p>I increased disk to 16 GB. (I&#8217;d never done that before for an EC2 instance, but it was surprisingly easy.)<\/p>\n<p>I needed to add something to monitor. On the target web server host I ran the following:<\/p>\n<pre><code>$ sudo -u splunk \/opt\/splunkforwarder\/bin\/splunk add monitor \/var\/www\/chris\/data\/logs<\/code><\/pre>\n<p>The resulting output included the following message:<\/p>\n<pre><code>Checking: \/opt\/splunkforwarder\/etc\/system\/default\/alert_actions.conf\r\n                Invalid key in stanza [webhook] in \/opt\/splunkforwarder\/etc\/system\/default\/alert_actions.conf, line 229: enable_allowlist (value: false).<\/code><\/pre>\n<p>It&#8217;s not clear if that&#8217;s actually a problem, and a few search results suggested it wasn&#8217;t worth worrying about.<\/p>\n<p>Everything was configured to forward data from the web server to Splunk. How could I find the data? I tried running a simple Splunk search:<\/p>\n<pre><code>index=main<\/code><\/pre>\n<p>0 events returned. I also checked the indices at http:\/\/203.0.113.18:8000\/en-US\/manager\/search\/data\/indexes, which showed there were 0 events in the <code>main<\/code> index.<\/p>\n<p>I ran <code>tcpdump<\/code> on the target web server and confirmed there were successful connections to <code>203.0.113.18<\/code> on port 9997\/tcp:<\/p>\n<pre><code>sudo tcpdump -i eth0 -nn port 9997<\/code><\/pre>\n<p>I tried another search on the Splunk web interface, this time querying some of Splunk&#8217;s internal indexes:<\/p>\n<pre><code>index=_* osric<\/code><\/pre>\n<p>Several results were present. Clearly communication was happening. But where were the web logs?<\/p>\n<p>The <code>splunk<\/code> user on the target web server doesn&#8217;t have permissions to read the web logs! I ran the following:<\/p>\n<pre><code>chown apache:splunk \/var\/www\/chris\/data\/logs\/osric*<\/code><\/pre>\n<p>After that change, the Indexes page in the Splunk web interface still showed 0 events in the main index.<\/p>\n<p>I followed the advice on <a href=\"https:\/\/community.splunk.com\/t5\/Getting-Data-In\/What-are-the-basic-troubleshooting-steps-in-case-of-universal\/m-p\/456364\">What are the basic troubleshooting steps in case of universal forwarder and heavy forwarder not forwarding data to Splunk?<\/a>, but still wasn&#8217;t seeing any issues. I took a close look again at the advice to check permissions. Tailing a specific log file worked fine, but getting a directory listing as the <code>splunk<\/code> user failed:<\/p>\n<pre><code>$ sudo -u splunk ls logs\r\nls: cannot open directory logs: Permission denied<\/code><\/pre>\n<p>Of course! The <code>splunk<\/code> user had access to the logs themselves, but not to the directory containing them. It couldn&#8217;t enumerate the log files. I ran the following:<\/p>\n<pre><code>$ sudo chgrp splunk logs<\/code><\/pre>\n<p>That did it! Logs were flowing! Search queries like the following produced results on the Splunk web interface:<\/p>\n<pre><code>index=main<\/code><\/pre>\n<p>The search was slow, and there were warnings present when searching:<\/p>\n<pre><code>Configuration initialization for \/opt\/splunk\/etc took longer than expected (1964ms) when dispatching a search with search ID 1676220274.309. This usually indicates problems with underlying storage performance.<\/code><\/pre>\n<p>I looks like t2.micro is much too small and under-powered for Splunk, even an instance with very little data (only 3 MB of data and 20,000 log events in the main index).<\/p>\n<p>Despite these drawbacks, the data was searchable. How did Splunk compare as a solution?<\/p>\n<p><strong>Dashboards<\/strong><br \/>\nI&#8217;ll need to create dashboards from scratch. I&#8217;ll want to know top pages, top URIs resulting in 404 errors, top user agents, etc. All of those will need to be built. It&#8217;s possible there&#8217;s a good Splunk app available that includes a lot of common dashboards for the Apache web server, but I haven&#8217;t really explored that.<\/p>\n<p>Google Analytics can&#8217;t report on 404 errors, but otherwise it provides a lot of comprehensive dashboards and data visualizations. Even if all you want are basic web stats, an application tailored to web analytics will include a lot of ready-made functionality.<\/p>\n<p><strong>Robots, Spiders, and Crawlers (and More)<\/strong><br \/>\nIt turns out, a large percentage of requests to my web server are not from human beings. Many of the requests are coming from robots. At least 31% of requests in the past day were coming from these 9 bots:<\/p>\n<ul>\n<li>8LEGS<\/li>\n<li>Sogou<\/li>\n<li>PetalBot<\/li>\n<li>AhrefsBot<\/li>\n<li>SEOkicks<\/li>\n<li>zoominfobot<\/li>\n<li>SemrushBot<\/li>\n<li>BingBot<\/li>\n<li>DotBot<\/li>\n<\/ul>\n<p>Google Analytics (and presumably other web analytics tools) do a great job of filtering these out. It&#8217;s good to know which bots are visiting, but it&#8217;s not really telling me anything about which content is most popular with users.<\/p>\n<p><strong>Security Insights<\/strong><br \/>\nRelated to the above, the stats from the web logs do a <em>much<\/em> better job of showing suspicious activity than Google Analytics does. It&#8217;s much easier to see which IP addresses are requesting files that don&#8217;t exist, or are repeatedly trying and failing to log in to WordPress (19% of all requests are for <code>wp-login.php<\/code>). This is useful information that I can use to help protect the server: I&#8217;ve previously written about <a href=\"https:\/\/osric.com\/chris\/accidental-developer\/2019\/07\/block-wordpress-scanners-fail2ban\/\">how to block WordPress scanners using fail2ban<\/a>. A tool dedicated to web analytics likely won&#8217;t provide this kind of detail, and may in fact hide it from site administrators if they aren&#8217;t also reviewing their logs.<\/p>\n<p><strong>Costs<\/strong><br \/>\nThe t2.micro instance will cost me approximately 8 USD per month. The t2.micro instance clearly isn&#8217;t powerful enough to run Splunk at any reasonable level of performance, even for a single-user system with a fairly small number of log events.<\/p>\n<p>What is the right size instance? I don&#8217;t have enough experience running Splunk as an administrator to make a guess, or even to determine if the bottleneck is CPU (likely) or RAM. But I decided to at least try upgrading the instance to t2.medium to see if that made a difference, since that includes 2 virtual CPUs (twice that of the t2.micro) and 4 GB RAM (four times that of t2.micro).<\/p>\n<p>It did make a difference! The Splunk web interface is much faster now, but will cost roughly 33 USD per month. That&#8217;s getting close to the amount I pay to run the web server itself. I think setting up Splunk to collect web stats was a useful exercise, but I&#8217;m going to look at some other alternatives as Google Analytics replacements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I don&#8217;t like using Google Analytics. The data is useful and well-presented, but I really just want basic web stats without sending all my web stats (along with data from my users) to Google. I&#8217;ve considered a number of other options, including Matomo. But I already use Splunk at work, why not run Splunk at &hellip; <a href=\"https:\/\/osric.com\/chris\/accidental-developer\/2023\/02\/running-splunk-in-aws\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Running Splunk in AWS<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[571],"tags":[556],"class_list":["post-3680","post","type-post","status-publish","format-standard","hentry","category-splunk","tag-splunk"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/3680","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=3680"}],"version-history":[{"count":10,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/3680\/revisions"}],"predecessor-version":[{"id":3691,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/3680\/revisions\/3691"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=3680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=3680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=3680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}