Page 1 of 1

How to: Guide to Setting Up PlexWatchWeb in a Jail -- Robust User Monitoring for Plex

Posted: 16 Dec 2014 22:30
by jay610
Please be advised that the plexpass version of plex is noted as a requirement for the full functionality of plexwatch. I have not tested on the mainline plex version. YMMV

see: https://forums.plex.tv/index.php/topic/ ... d-stopped/

PlexWatch is a handy application that fills a gap for Plex users in terms of user and content monitoring, which informs demand management. Ideally this functionality built in natively, but thankfully someone on the dev team has written it and made it available as a bolt-on over on the Plex forums. It's a recurring script that's written in Perl and can be managed through CLI, but there is also a web-based front-end that really makes this a joy to use and significantly easier to manage.

I think it makes a lot of sense if you have a lot of activity within your home that you would like to keep tabs on, and particularly if you have multiple external users that connect to your Plex server remotely. This allows you to see what IPs/users are connecting and when so that you can alter your network and hardware needs accordingly. It provides some nice data and even organizes it into some decent graphs.

Check Google Images for screenies.

What does it do?

(straight from the developer):
  • notify when a user starts watching a video
    notify when a user stops watching a video
    notify when a user pauses watching a video
    notify when a user resumes watching a video
    notify on recently added content to a PMS server
    notifies via email, prowl, pushover, growl, twitter, boxcar, pushbullet, GNTP and/or a log file
    enable/disable notifications per provider & per notification type (start, stop, paush, resume, recently added)
    backed by a SQLite DB (for state and history)
    CLI to query watched videos, videos being watched and stats on time watched per user
    Limit output per user or exclude users
    ...more to come
It also supports push notifications on these platforms:
With the features accounted for, let's start with a disclaimer: Although I have been running NAS4Free (and before that, FreeNAS 0.7x) for several years, I am still a novice FreeBSD / UNIX user. However, I have successfully built my server box's functionality through a number of extensions that I could never have fathomed when I began Nas4Free. That is solely through the use of how-to guides like this one. For newbs like me, it really helps to have exact instructions that are specific to our platform. I have benefited in this way from the good folks on our forums and so I would like to finally make a contribution myself. If you folks have problems recreating what worked for me on my box, hopefully some wiser souls will be along to help.

Also, please no making fun of my code/commands. I know there are more efficient ways of doing things, stacking commands, etc, but I am taking baby steps here. It's A-B-C's for me. Ok, [/disclaimer]

These two links were helpful in my own efforts to get a working installation going:

https://forums.freenas.org/index.php?threads/plexwatch-installation.19126/

https://github.com/ljunkie/plexWatch/blob/master/README.md

On to the Guide!

I have plexWatch set up in its own jail and have the Plex Logs directory mounted in fstab. You don't really need access to the PMS log directory unless you want to log IP addresses, which I do. You could feasibly set this up within your Plex jail, but I chose to keep things separate so that in case something borks in one it doesn't affect the other.

1) Start with a working jail

I am still on TheBrig because that was the thing to be on when I set my Nas4Free up, but any working jail should work. Note that my jail is based on FreeBSD 9.3 and the new package manager, pkg_tools. What this means when it comes time to add packages to the jail is that my command looks like "pkg install" instead of the old "pkg_add". If you need to upgrade an older jail to the new package manager (as I did and was confused for a while on how to do it), see this thread and then come back:

https://www.xigmanas.com/forums/viewtopic.php?f=79&t=7843

2) enter jail cli

newbs: SSH in, then

Code: Select all

 jls
to see a list of currently running jails, then

Code: Select all

jexec x csh
where "x" is the corresponding integer output by jls.

THE BACK END:

3) Install packages:

Code: Select all

pkg install p5-libwww p5-XML-Simple p5-Time-Duration p5-Time-modules p5-DBD-SQLite p5-JSON nano wget 
4) Get plexWatch!

Code: Select all

mkdir -p /opt/plexWatch/

Code: Select all

 cd /opt/plexWatch/

Code: Select all

wget --no-check-certificate -P /opt/plexWatch/ https://raw.github.com/ljunkie/plexWatch/master/plexWatch.pl

Code: Select all

wget --no-check-certificate -P /opt/plexWatch/ https://raw.github.com/ljunkie/plexWatch/master/config.pl-dist
5) Set correct permissions and create config file from sample

Code: Select all

chmod 777 /opt/plexWatch && chmod 755 /opt/plexWatch/plexWatch.pl

Code: Select all

cp /opt/plexWatch/config.pl-dist /opt/plexWatch/config.pl
6) Correct path in plexWatch.pl and set up config

Code: Select all

nano /opt/plexWatch/plexWatch.pl
We need to input the appropriate path to Perl. This means changing the top line of the script from

#!/usr/bin/perl

to

Code: Select all

#!/usr/local/bin/perl
Alternatively, you can do this in the GUI at Advanced --> File Editor if you prefer.

Once you have saved the corrected this and saved, edit:

Code: Select all

nano /opt/plexWatch/config.pl

The main things to edit at this point are:

$server = 'localhost'; ## IP of your PMS - or localhost
$port = 32400; ## port of your PMS


You could also fill in the lines:

$myPlex_user = '';
$myPlex_pass = '';

You should fill in those lines if you require clients to authenticate on the local network (this is a PMS setting).


7) Test the script and see that it works

Go into your plex server and load up a video. Play, then run the script manually:

Code: Select all

/opt/plexWatch/plexWatch.pl
Stop video. Run script again:

Code: Select all

/opt/plexWatch/plexWatch.pl

You should note the creation of a SQLite database file in the same path called plexWatch.db.

You can check to see if your activity was logged if you like.

Code: Select all

/opt/plexWatch/plexWatch.pl --watched

8) Set up cronjob

Now we want this script to run on an endless loop so that we don't miss any activity and also so that we don't have to sit in front of a command window constantly executing /opt/plexWatch/plexWatch.pl. That would be a horrible life and the yield would probably not be worth the effort.

Code: Select all

nano /etc/crontab
and add the lines:

Code: Select all

#Plexwatch all the dam time
*	*	*	*	*	root	/opt/plexWatch/plexWatch.pl
That's it. You're all set for the core of the backend.

Bonus Round

You need to be able to send traffic over https in order to enable push notifications, so let's set that up. First, enable your chosen service in

Code: Select all

nano /opt/plexWatch/config.pl
You need to scroll down and add '1' instead of '0' where appropriate as well as some application-specific information.

Now we need to get https working so that our push notes make it to remote servers.

Do:

Code: Select all

cpan


Let this run its course. To my mind, you should now be able to

Code: Select all

install LWP::Protocol::https
and it should Just Work, but I ran into errors in the self-tests.

I then ran

Code: Select all

install Crypt:SSLeay
and

Code: Select all

install IO::Socket::SSL
These completed without errors, and then

Code: Select all

install LWP::Protocol::https
was also able to complete without errors. Based on the output of install Crypt:SSLeay, my guess is that this was unnecessary, but I don't know Perl and I was just trying to see what would stick. At the end of it all, I have a working install and these are the steps I went through to get there.
Bonus Round 2

If you like to log IP addresses (I do -- call me old-fashioned), you need to add an additional package:

Code: Select all

pkg install p5-File-ReadBackwards

You also have to enable debug logging in PMS, map your PMS Log folder (fstab) to your plexWatch jail and edit your config.pl again:

Code: Select all

nano /opt/plexWatch/config.pl
$server_log = '/mnt/Your_plexdata_directory/Plex Media Server/Logs/Plex Media Server.log';
$log_client_ip = 1;
$debug_logging = 1;

Your $server_log directory is in whatever jail you installed PMS to, in /usr/local/plexdata/Plex Media Server/Logs/Plex Media Server.log by default. Again, you need access from this jail, if separate, to these folders in your PMS jail (if applicable) by properly configuring fstab (there is a handy form in the webgui for this in The Brig).

It should look something like this:

Code: Select all

/mnt/plex_jail/usr/local/plexdata/ /mnt/plexWatch_jail/mnt/plexlogs/ nullfs ro 0 0;


Whew! That's all for the back end. If you only want CLI management and push notifications, you're done. You can see all the flags and options you can call over on the github page.


THE FRONT END

It sure is a lot nicer to have a web interface though. Easy on the eyes, easy to manage, access from mobile devices and so on.

1) Add packages:

Code: Select all

pkg install apache24 mod_php5 php5-sqlite3 php5-curl php5-json php5-extensions

2) Adjust your Apache conf to enable handling of php pages on your webserver.

Start by editing your httpd.conf:

Code: Select all

nano /usr/local/etc/apache24/httpd.conf
Look for

Code: Select all

DirectoryIndex index.html index.php
and add these two lines, inside the <IfModule mime_module> block:

Code: Select all

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
3) You need to make sure php correctly reports your time zone, otherwise you'll get wrong timestamps in plexWatchWeb.

Start by initializing php.ini from the default production values supplied with the package:

Code: Select all

cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini
then edit:

Code: Select all

nano /usr/local/etc/php.ini
Look for commented out line,

Code: Select all

";date.timezone="


Remove the ";" and change it to your actual time zone (a list of supported TZs is here:https://php.net/manual/en/timezones.php), in my case:

Code: Select all

date.timezone = Europe/London

4) Download plexWatchWeb to your webroot, unpack and fix its permissions:

Code: Select all

cd /usr/local/www/apache24/data
wget --no-check-certificate https://github.com/ecleese/plexWatchWeb/archive/master.zip
unzip master.zip && rm master.zip
mv plexWatchWeb-master plexWatch
chown -R www:www plexWatch
5) Start the webserver every time the jail starts by editing /etc/rc.conf and appending apache24_enable="YES" to the end:

Code: Select all

echo 'apache24_enable="YES"' >> /etc/rc.conf
6) Restart the service

Code: Select all

service apache24 restart
or, failing that,

Code: Select all

service apache24 onestart
7) BWAHAHAHAHAHA!!!!

Navigate to http://<ip_of_your_jail>/plexWatch and we should now be live!!! You have a bit of initial set up to do. Remember, your plexWatchWeb files live in /usr/local/www/apache24/data so if you need to edit anything, that's where to go. I had to 'factory reset' once by deleting /config/config.php.

On setup you have to point to your plexWatch.db file at /opt/plexWatch/plexWatch.db

If you have problems here, make sure your permissions allow public users to at least read from it (eg, chmod 755). This should already be set from earlier, but heck, check again. I never had this problem, but I noticed out in the forums that some people were 777 the whole directory. If you have problems with the WebGui end, that's where I'd start.


Sorry it was long-winded, but I wanted to be detailed for the beginner as I have been there myself (and am still not far from there now). Let me know if you succeed or fail below, as well as any improvements I can make to the guide, and most of all, enjoy!