{"id":12674,"date":"2020-01-03T20:57:24","date_gmt":"2020-01-04T02:57:24","guid":{"rendered":"http:\/\/anthonyreinke.com\/?p=12674"},"modified":"2020-01-03T21:04:21","modified_gmt":"2020-01-04T03:04:21","slug":"connecting-plex-and-splunk","status":"publish","type":"post","link":"https:\/\/anthonyreinke.com\/index.php\/2020\/01\/03\/connecting-plex-and-splunk\/","title":{"rendered":"Connecting Plex and Splunk"},"content":{"rendered":"\n<p>I use Plex (<a href=\"https:\/\/www.plex.tv\/\">https:\/\/www.plex.tv\/<\/a>) to be able to play videos at home.  Different family members have their own accounts on Plex.  I was interested in the viewing habits of the people using my Plex server.  If you put Plex in debug mode you can get a lot of logs but I wanted a better way.<\/p>\n\n\n\n<p>I found PlexWatch (<a href=\"https:\/\/github.com\/ljunkie\/plexWatch\">https:\/\/github.com\/ljunkie\/plexWatch<\/a>) on Github.  PlexWatch is listed as &#8220;Notify and Log watched content on a Plex Media Server&#8221;.  What made me interested in this project is that you could extend it to connect to external providers (Twitter, Boxcar, Prowl, &#8230;).  I was hoping I could use this to connect to Splunk&#8217;s HEC (HTTP Event Collector).<\/p>\n\n\n\n<p>I was able to also find a Splunk HEC library for Perl on Github.  The project is called &#8220;Perl Client for Splunk HTTP Event Collector&#8221; and at <a href=\"https:\/\/github.com\/eforbus\/perl-splunk-hec\">https:\/\/github.com\/eforbus\/perl-splunk-hec<\/a>.<\/p>\n\n\n\n<p>Requirements:<br>1. Command line access to a Plex server<br>2. Splunk instance with HEC enabled<br>3. Perl installed or ability to have it installed<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Below is the step by step I created to connect PlexWatch with Splunk via the HEC.  This was done on a CentOS 7 server.<\/p>\n\n\n\n<p>1. Enable the EPEL Release Repo<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\nsudo yum -y &#8211;enablerepo=extras install epel-release\n<\/p><\/blockquote>\n\n\n\n<p>2. Add the dependancies<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\nsudo yum -y install perl\\(LWP::UserAgent\\) perl\\(XML::Simple\\) perl\\(Pod::Usage\\) perl\\(JSON\\) perl\\(DBI\\) perl-Time-Duration perl-Time-ParseDate  perl-DBD-SQLite perl-LWP-Protocol-https perl-Crypt-SSLeay perl-File-ReadBackwards perl-JSON-XS\n<\/p><\/blockquote>\n\n\n\n<p>3. Create the directory for PlexWatch<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\nsudo mkdir \/opt\/plexWatch\/\n<\/p><\/blockquote>\n\n\n\n<p>4. Download the PlexWatch components<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\nsudo wget -P \/opt\/plexWatch\/ https:\/\/raw.github.com\/ljunkie\/plexWatch\/master\/plexWatch.pl\n<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\nsudo wget -P \/opt\/plexWatch\/ https:\/\/raw.github.com\/ljunkie\/plexWatch\/master\/config.pl-dist\n<\/p><\/blockquote>\n\n\n\n<p>3. Create the directory for PlexWatch<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\nsudo mkdir \/opt\/plexWatch\/\n<\/p><\/blockquote>\n\n\n\n<p>5. Set the permissions for the folder and script<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\nsudo chmod 777 \/opt\/plexWatch &amp;&amp; sudo chmod 755 \/opt\/plexWatch\/plexWatch.pl\n<\/p><\/blockquote>\n\n\n\n<p>6. Copy the configuration file from the default to the one used by the script<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\nsudo cp \/opt\/plexWatch\/config.pl-dist \/opt\/plexWatch\/config.pl\n<\/p><\/blockquote>\n\n\n\n<p>7. Edit the configuration file.  In the examples I show will be using VIM but in the walk through I show VI.  Nano can also be used.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p> sudo vi \/opt\/plexWatch\/config.pl <\/p><\/blockquote>\n\n\n\n<p>7a. Change the $log_client_ip to equal 1 and set the $myPlex_user and $myPlex_pass variables.  The $myPlex_user and $myPlex_pass are the credentials to log in to plex.tv.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/13.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"947\" height=\"225\" src=\"https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/13.png\" alt=\"config.pl section for external ip address and Plex account\" class=\"wp-image-12675\" srcset=\"https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/13.png 947w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/13-300x71.png 300w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/13-768x182.png 768w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/13-810x192.png 810w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/13-700x166.png 700w\" sizes=\"auto, (max-width: 947px) 100vw, 947px\" \/><\/a><\/figure>\n\n\n\n<p>7b. Near the end of the configuration file, find the external section.  It will look like the below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/14.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"799\" height=\"590\" src=\"https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/14.png\" alt=\"config.pl external script area\" class=\"wp-image-12676\" srcset=\"https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/14.png 799w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/14-300x222.png 300w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/14-768x567.png 768w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/14-406x300.png 406w\" sizes=\"auto, (max-width: 799px) 100vw, 799px\" \/><\/a><\/figure>\n\n\n\n<p>7c. Add a new section for the Splunk HEC connector.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/15.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"943\" height=\"916\" src=\"https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/15.png\" alt=\"config.pl script with added section for sending to Splunk\" class=\"wp-image-12677\" srcset=\"https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/15.png 943w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/15-300x291.png 300w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/15-768x746.png 768w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/15-810x787.png 810w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/15-309x300.png 309w\" sizes=\"auto, (max-width: 943px) 100vw, 943px\" \/><\/a><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\n&#8216;Splunk&#8217; =&gt;     {<br>\n     &#8216;enabled&#8217;       =&gt; 1, ## 0 or 1 &#8211; set to 1 to enable Splunk script<br>     &#8216;push_watched&#8217;  =&gt; 1, #stop<br>\n     &#8216;push_watching&#8217; =&gt; 1, #start<br>\n     &#8216;push_paused&#8217;   =&gt; 1, #pause<br>\n     &#8216;push_resumed&#8217;  =&gt; 1, #resume<br>\n<br><br>\n     &#8216;script_format&#8217; =&gt; {<br>\n     &#8216;start&#8217;   =&gt;  &#8216;perl \/opt\/plexWatch\/splunk.pl &#8220;{user}&#8221; &#8220;{state}&#8221; &#8220;{title}&#8221; &#8220;{streamtype}&#8221; &#8220;{year}&#8221; &#8220;{rating}&#8221; &#8220;{platform}&#8221; &#8220;{progress}&#8221; &#8220;{percent_complete}&#8221; &#8220;{ip_address}&#8221; &#8220;{length}&#8221; &#8220;{duration}&#8221; &#8220;{time_left}&#8221;&#8216;,<br>\n     &#8216;paused&#8217;  =&gt;  &#8216;perl \/opt\/plexWatch\/splunk.pl &#8220;{user}&#8221; &#8220;{state}&#8221; &#8220;{title}&#8221; &#8220;{streamtype}&#8221; &#8220;{year}&#8221; &#8220;{rating}&#8221; &#8220;{platform}&#8221; &#8220;{progress}&#8221; &#8220;{percent_complete}&#8221; &#8220;{ip_address}&#8221; &#8220;{length}&#8221; &#8220;{duration}&#8221; &#8220;{time_left}&#8221;&#8216;,<br>\n     &#8216;resumed&#8217; =&gt;  &#8216;perl \/opt\/plexWatch\/splunk.pl &#8220;{user}&#8221; &#8220;{state}&#8221; &#8220;{title}&#8221; &#8220;{streamtype}&#8221; &#8220;{year}&#8221; &#8220;{rating}&#8221; &#8220;{platform}&#8221; &#8220;{progress}&#8221; &#8220;{percent_complete}&#8221; &#8220;{ip_address}&#8221; &#8220;{length}&#8221; &#8220;{duration}&#8221; &#8220;{time_left}&#8221;&#8216;,<br>\n     &#8216;stop&#8217;    =&gt;  &#8216;perl \/opt\/plexWatch\/splunk.pl &#8220;{user}&#8221; &#8220;{state}&#8221; &#8220;{title}&#8221; &#8220;{streamtype}&#8221; &#8220;{year}&#8221; &#8220;{rating}&#8221; &#8220;{platform}&#8221; &#8220;{progress}&#8221; &#8220;{percent_complete}&#8221; &#8220;{ip_address}&#8221; &#8220;{length}&#8221; &#8220;{duration}&#8221; &#8220;{time_left}&#8221;&#8216;,<br>\n     },<br>\n},\n<\/p><\/blockquote>\n\n\n\n<p>8. Download the Splunk HEC connector library for Perl.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p> wget https:\/\/github.com\/eforbus\/perl-splunk-hec\/archive\/master.zip<\/p><\/blockquote>\n\n\n\n<p>9. Unzip the the connector<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p> unzip master.zip<\/p><\/blockquote>\n\n\n\n<p>10. Copy the libraries to the PlexWatch directory<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>sudo cp -R .\/perl-splunk-hec-master\/lib\/Splunk \/opt\/plexWatch\/<\/p><\/blockquote>\n\n\n\n<p>11. Create and edit the HEC script.  This will be what is called from PlexWatch to send the data to the HEC.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p> sudo vi \/opt\/plexWatch\/splunk.pl<\/p><\/blockquote>\n\n\n\n<p>11a. Below is the script.  You will need your Splunk server path and HEC token.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/16.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"944\" height=\"476\" src=\"https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/16.png\" alt=\"splunk.pl Perl script\" class=\"wp-image-12678\" srcset=\"https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/16.png 944w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/16-300x151.png 300w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/16-768x387.png 768w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/16-810x408.png 810w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/16-595x300.png 595w\" sizes=\"auto, (max-width: 944px) 100vw, 944px\" \/><\/a><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p> #!\/usr\/bin\/perl<br> <br> use lib qw(\/opt\/plexWatch\/);<br> <br> use Splunk::HEC;<br> <br> $user=$ARGV[0];<br> $state=$ARGV[1];<br> $title=$ARGV[2];<br> $streamtype=$ARGV[3];<br> $year=$ARGV[4];<br> $rating=$ARGV[5];<br> $platform=$ARGV[6];<br> $progress=$ARGV[7];<br> $percent_complete=$ARGV[8];<br> $ip_address=$ARGV[9];<br> $show_length=$ARGV[10];<br> $duration=$ARGV[11];<br> $time_left=$ARGV[12];<br> <br> my $hec = Splunk::HEC->new(<br>   url => &#8216;https:\/\/SplunkServer:8088\/services\/collector\/event&#8217;,<br>     token => &#8216;6cc8b5ba-48f3-5c2b-8e9e-9e5e81a0ce57&#8217;<br>     );<br> <br>     my $res = $hec->send(event => {user => $user, state => $state, title => $title, streamtype => $streamtype, year => $year, rating => $rating, platform => $platform, progress => $progress, percent_complete => $percent_complete, ip_address => $ip_address, length => $show_length, duration => $duration, time_left => $time_left});<br> <\/p><\/blockquote>\n\n\n\n<p>12. Change the abilities of the script to be executable<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>sudo chmod +x \/opt\/plexWatch\/splunk.pl<\/p><\/blockquote>\n\n\n\n<p>13. Test the script.  This will send sample data to the Splunk HEC.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\/opt\/plexWatch\/splunk.pl user state title streamtype year rating platform progress percent_complete ip_address length duration time_left<\/p><\/blockquote>\n\n\n\n<p>14. Add the PlexWatch script in to the crontab to run on a schedule<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>sudo crontab -e<\/p><\/blockquote>\n\n\n\n<p>14a. Have the script run once per minute<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>* * * * * \/opt\/plexWatch\/plexWatch.pl<\/p><\/blockquote>\n\n\n\n<p>Enjoy the data in Splunk<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/18.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"294\" height=\"314\" src=\"https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/18.png\" alt=\"JSON Data Example\" class=\"wp-image-12684\" srcset=\"https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/18.png 294w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/18-281x300.png 281w\" sizes=\"auto, (max-width: 294px) 100vw, 294px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/17.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"411\" src=\"https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/17-1024x411.png\" alt=\"Dashboard Example\" class=\"wp-image-12683\" srcset=\"https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/17-1024x411.png 1024w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/17-300x120.png 300w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/17-768x308.png 768w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/17-810x325.png 810w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/17-1140x458.png 1140w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/17-700x281.png 700w, https:\/\/anthonyreinke.com\/wp-content\/uploads\/2020\/01\/17.png 1908w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption>Dashboard Example<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>I use Plex (https:\/\/www.plex.tv\/) to be able to play videos at home. Different family members have their own accounts on Plex. I was interested in the viewing habits of the people using my Plex server. If you put Plex in debug mode you can get a lot of logs but I wanted a better way. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[36,34,35,33,31,32,37,27],"class_list":["post-12674","post","type-post","status-publish","format-standard","hentry","category-projects","tag-eforbus","tag-hec","tag-ljunkie","tag-perl","tag-plex","tag-plexwatch","tag-server","tag-splunk"],"_links":{"self":[{"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/posts\/12674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/comments?post=12674"}],"version-history":[{"count":4,"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/posts\/12674\/revisions"}],"predecessor-version":[{"id":12685,"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/posts\/12674\/revisions\/12685"}],"wp:attachment":[{"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/media?parent=12674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/categories?post=12674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anthonyreinke.com\/index.php\/wp-json\/wp\/v2\/tags?post=12674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}