Jump to content

UserName + NowPlayingItem.Name for InfluxDB and Grafana


plittlefield
 Share

Recommended Posts

plittlefield

Hello Folks,

After the success of getting a count of the current NowPlayingItem entries in the Sessions API, I am now trying to get a list of the Users and what they are currently playing - then send that to my InfluxDB to show in my Grafana server.

I am nearly there with this command line, but cannot quite get it so that JQ only shows those session users who are actually playing something...

wget -q -O - "http://localhost:8096/emby/Sessions?api_key=3469exxxxxxxxxxxxxxx201ef3f" | jq -M '.[].UserName, .[].NowPlayingItem.Name'

will show me the following...

null
"Paul"
"Tracey"
"Sam"
"Tom"
"Katie"
null
"The Secret of the Unicorn (1)"
null
null
null
null

...which is every possible user. In the above example, only user "Paul" is actually playing ""The Secret of the Unicorn (1)".

If I add the JQ options to now show 'nulls'...

wget -q -O - "http://localhost:8096/emby/Sessions?api_key=3469exxxxxxxxxxxxxxx201ef3f" | jq -M '.[].UserName, .[].NowPlayingItem.Name | select(. != null)'

then I can get it to this output...

"Paul"
"Tracey"
"Sam"
"Tom"
"Katie"
"The Secret of the Unicorn (1)"

...which is correct but still shows me users that are not playing anything at that time!

Does anyone have knowledge of JSON and JQ or the API so that I can just get a list of those users that are playing an item now.

I would be happy with this output...

"Paul"
"The Secret of the Unicorn (1)"

...or even better - turn it in to a key + value pair like this...

{ "Paul": "The Secret of the Unicorn (1)" }

...so that I can send that to a bash script and then to my InfluxDB.

Thanks in advance!

Regards,

Paully

Edited by plittlefield
forgot nulls command line
Link to comment
Share on other sites

plittlefield

OK, so with a little more Googling and reading, I now have it down to...

wget -q -O - "http://localhost:8096/emby/Sessions?api_key=3469exxxxxxxxxxxxxxx201ef3f" | jq '.[] | {UserName,NowPlayingItem: .NowPlayingItem.Name} | select(.UserName!=null and .NowPlayingItem!=null)'

{
  "UserName": "Paul",
  "NowPlayingItem": "The Secret of the Unicorn (1)"
}

 

Link to comment
Share on other sites

plittlefield
1 minute ago, Luke said:

Glad you figured it out. Thanks for following up.

No problem... my brain hurts, so tomorrow I'll finish up with the bash script to InfluxDB and Grafana screenshots 🙂

Link to comment
Share on other sites

plittlefield

No, could not resist it... had to crack it tonight before bed...

...and 'jq' has a wonderful csv output filter...

so, the winning command line (so far) is...

wget -q -O - "http://localhost:8096/emby/Sessions?api_key=3469exxxxxxxxxxxxxxx201ef3f" | jq -r -M '.[] | {UserName,NowPlayingItem: .NowPlayingItem.Name} | select(.UserName!=null and .NowPlayingItem!=null) | map(.) | @csv'

"Paul","The Secret of the Unicorn (1)"

🙂

Edited by plittlefield
Link to comment
Share on other sites

plittlefield

OK, so as time allows, I have been working on this.

I now have a script running on my server every minute...

#!/bin/bash

# load variables
CSVFILE=/home/user/embysessions.csv;

# get list of now playing items and send to jq output as csv
wget -q -O - "http://localhost:8096/emby/Sessions?api_key=3469xxxxxxxxxxxxxxxxxxxef3f" | jq -r -M '.[] | {UserName,NowPlayingItem: .NowPlayingItem.Name} | select(.UserName!=null and .NowPlayingItem!=null) | map(.) | @csv' >$CSVFILE;

# show csv file to check output
echo
cat $CSVFILE;
echo

# read csv file to parse values and send to influx via http post
while IFS=, read -r user item; do
  curl -i -XPOST 'http://myinfluxserver.domain.co.uk:8086/write?db=telegraf' --data-binary "emby user=$user,item=$item"
done < $CSVFILE;

# finish
exit;

...which sends the data to my influx server and gets displayed in Grafana.

The tricky bit in Grafana is configuring the data source query so that it only shows the rows less than 60 seconds from now...

select * from emby where time > now() - 59s order by time desc

...then you have to configure the table panel so that it hides the 'time' column and hey presto you have something like this...

Hope it helps someone!

emby_now_playing.jpg

Edited by plittlefield
Link to comment
Share on other sites

plittlefield

I have edited the above post because my InfluxDB input code was slightly wrong.

I was wrongly inputting a mixture of tags and fields when it should have been just fields with string data.

So, now it handles whitespace in the username and item playing name 🙂

However, my humble script only extracts the .NowPayingItem.Name which is fine for Movies but not so fine for TV Shows.

So, I need to learn how to, or ask a guru here - how to query the JSON data like this...

"Show me the User and what they are watching - if it's a movie just the movie name but if it's a TV show then show me the TV show name and the episode name."

??????

Edited by plittlefield
Link to comment
Share on other sites

plittlefield

OK, a coffee later and more reading we now have...

wget -q -O - "http://localhost:8096/emby/Sessions?api_key=3469xxxxxxxxxxxxxxxxxxxxxx1ef3f" | jq -r -M '.[] | select(.NowPlayingItem != null) | { user: .UserName, item: (.NowPlayingItem.SeriesName + .NowPlayingItem.Name) } | map(.) | @csv'

"Amy","Fatman"
"Paul","Creepshow Bad Wolf Down / The Finger"

...so close but the "Creepshow" needs a colon or dash to make it look like...

"Amy","Fatman"
"Paul","Creepshow: Bad Wolf Down / The Finger"

...nearly there!

🙂

 

  • Like 1
Link to comment
Share on other sites

Spaceboy

just replying to say i'm really interested in any progress you make with this. i use influxdb and grafana for data from my pfsense so i'd be really keen to see what you can get from emby. unfortunately i can't contribute at all, i'm a complete novice and am just re-using other people code.

but thanks for your efforts

Link to comment
Share on other sites

plittlefield

My pleasure.

I've learned so much from the internet that it seems only right to give something back.

Because Emby doesn't have anything like PlexPy or Tautulli I wanted something simple to show just a few live stats without going in to the app or web interface.

I'm about 75% happy with what I have now on my Grafana dashboard... single stat panel with number of now playing items, a time based graph panel of the same numbers over the previous 24 hours and then a table panel listing those same now playing items.

That's it so far!

Edited by plittlefield
Link to comment
Share on other sites

PenkethBoy

What OS are you using?

Curious if you have considered using Powershell as it has a lot of built in features to deal with JSON data you get from emby - have used it a lot with Emby over the last four years to write scripts to pull and push data.

Happy to help if needed.

@Spaceboyuses several of them! 

Link to comment
Share on other sites

plittlefield
2 hours ago, PenkethBoy said:

What OS are you using?

Curious if you have considered using Powershell as it has a lot of built in features to deal with JSON data you get from emby - have used it a lot with Emby over the last four years to write scripts to pull and push data.

Happy to help if needed.

@Spaceboyuses several of them! 

I'm using Ubuntu Linux 18.04 - rock solid.

Sorry, I don't use Windows so PowerShell is not an option 🙂

I'm nearly there to be fair, but if you read this thread you can see where I am up - just a few more tweaks and I am done.

Cheers!

  • Like 1
Link to comment
Share on other sites

CBers
35 minutes ago, plittlefield said:

Sorry, I don't use Windows so PowerShell is not an option

Powershell is available on Linux. 

 

Link to comment
Share on other sites

plittlefield
19 hours ago, plittlefield said:

...nearly there!

Well, here's a classic example of the internet helping out when you ask nicely.

I found a YouTuber called Szymon Stepniak and he gave me the winning 'jq' command for the "if it is a tv show" answer...

{title: ((if .SeriesName then .SeriesName + " / " else "" end) + .Name)}

So, the whole 'jq' command now looks like this...

jq -c '.[].NowPlayingItem | {SeriesName, Name} | select(.Name != null) | {title: ((if .SeriesName then .SeriesName + " / " else "" end) + .Name)}'

...and the whole bash command (with the additional CSV output) looks like this...

wget -q -O - "http://localhost:8096/emby/Sessions?api_key=3469xxxxxxxxxxxxxxxxxxxxxxxxef3f" | jq -r -M '.[].NowPlayingItem | {SeriesName, Name} | select(.Name != null) | {title: ((if .SeriesName then .SeriesName + " / " else "" end) + .Name)} | map(.) | @csv'

...and if we want to add the 'user' then the command looks like this monster...

wget -q -O - "http://localhost:8096/emby/Sessions?api_key=3469xxxxxxxxxxxxxxxxxxxxxxxxef3f" | jq -r -M '.[] | {UserName, SeriesName: .NowPlayingItem.SeriesName, Name: .NowPlayingItem.Name} | select(.Name != null) | {user: .UserName, item: ((if .SeriesName then .SeriesName
 + " / " else "" end) + .Name)} | map(.) | @csv';

Thanks Szymon - you are a star!

🙂

Edited by plittlefield
  • Like 1
Link to comment
Share on other sites

  • 2 months later...
tinni88
On 11/19/2020 at 4:15 PM, plittlefield said:

OK, so as time allows, I have been working on this.

I now have a script running on my server every minute...


#!/bin/bash

# load variables
CSVFILE=/home/user/embysessions.csv;

# get list of now playing items and send to jq output as csv
wget -q -O - "http://localhost:8096/emby/Sessions?api_key=3469xxxxxxxxxxxxxxxxxxxef3f" | jq -r -M '.[] | {UserName,NowPlayingItem: .NowPlayingItem.Name} | select(.UserName!=null and .NowPlayingItem!=null) | map(.) | @csv' >$CSVFILE;

# show csv file to check output
echo
cat $CSVFILE;
echo

# read csv file to parse values and send to influx via http post
while IFS=, read -r user item; do
  curl -i -XPOST 'http://myinfluxserver.domain.co.uk:8086/write?db=telegraf' --data-binary "emby user=$user,item=$item"
done < $CSVFILE;

# finish
exit;

...which sends the data to my influx server and gets displayed in Grafana.

The tricky bit in Grafana is configuring the data source query so that it only shows the rows less than 60 seconds from now...


select * from emby where time > now() - 59s order by time desc

...then you have to configure the table panel so that it hides the 'time' column and hey presto you have something like this...

Hope it helps someone!

emby_now_playing.jpg

Hey Paully,

Thanks for this post has helped me out a lot but I'm new to Grafana and still learning how to setup the query any chance you could help me with the table setup.

Any help would be much appreciated 

Cheers tinni

Link to comment
Share on other sites

plittlefield
14 hours ago, tinni88 said:

Hey Paully,

Thanks for this post has helped me out a lot but I'm new to Grafana and still learning how to setup the query any chance you could help me with the table setup.

Any help would be much appreciated 

Cheers tinni

How do you mean?

Link to comment
Share on other sites

tinni88
19 minutes ago, plittlefield said:

How do you mean?

hey thanks for getting back to me 

I have the script running and the csv file but i just cant figure out how to make the table on grafana i tried for about 3 hours last night but just couldn't figure it out i just keep getting no data 

cheers tinni 

query.jpg

emby_now_playing.jpg.ef8ce26d5a8ba0a315b3dc2fb37b68fd.jpg

Link to comment
Share on other sites

plittlefield

Yeah, the trick was choosing a table and then hiding certain fields from the database.

Here are 3 screensnots from mine...

 

Screenshot_2021-02-11_12-38-21.png

Screenshot_2021-02-11_12-39-36.png

Screenshot_2021-02-11_12-39-56.png

Link to comment
Share on other sites

tinni88

thanks for the screenshots that will give me something to go on but for some reason I'm still getting no data showing.

the problem is clearly right in front of me and I'm being thick lol 

with your second screenshot i dont seem to have the data paging options type in any of my options unless im missing something 

678232598_hh(2).thumb.png.49c39154dac33e37510f40dd767274f0.pngnope.thumb.png.30288b50a65f3284fc165baa79c86ad3.png

Link to comment
Share on other sites

plittlefield

That's right, you need to click the pencil icon to enter text edit mode and type in the following:-

select * from yourmeasurementnametable where time > now() - 59s order by time desc

My measurement is called 'emby' which you can see from the posts above in this forum thread, but yours may be called something else.

Link to comment
Share on other sites

tinni88

Yeah I've tried that but still nothing still coming up as no data I'm starting to think that i may have messed up the script some how and it not grabbing the data from the command but the command works in terminal and receives the correct response and the script is connecting to influxdb and creating the csv and the data name is shown under measurements in grafana (emby) but showing no data on the table so maybe its not passing the csv file over or something I'm completely out of ideas 

I'm very grateful for your help on this 

your script I'm using 

"#!/bin/bash

# load variables
CSVFILE=/mnt/user/embysessions/emby.csv;

# get list of now playing items and send to jq output as csv
wget -q -O - "http://Loaclhost:8096/emby/Sessions?api_key=62ab***************************" | jq -r -M '.[] | {UserName, SeriesName: .NowPlayingItem.SeriesName, Name: .NowPlayingItem.Name} | select(.Name != null) | {user: .UserName, item: ((if .SeriesName then .SeriesName
 + " / " else "" end) + .Name)} | map(.) | @csv'

# show csv file to check output
echo
cat $CSVFILE;
echo

# read csv file to parse values and send to influx via http post
while IFS=, read -r user item; do
  curl -i -XPOST 'http://Localhost:8086/write?db=telegraf' --data-binary "emby user=$user,item=$item"
done < $CSVFILE;

# finish
exit;"

 

log output

"Script Starting Feb 11, 2021 13:49.47

Full logs for this script are available at /tmp/user.scripts/tmpScripts/emby-influxdb/log.txt

"Kids","Blippi / Monster Truck Song - Educational videos for preschoolers - Blippi"
"Jade","NCIS / Kill the Messenger"

"Jade","NCIS / Kill the Messenger"
"Kids","Blippi / Monster Truck Song - Educational videos for preschoolers - Blippi"

% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 49 0 0 100 49 0 16333 --:--:-- --:--:-- --:--:-- 24500

% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 98 0 0 100 98 0 32666 --:--:-- --:--:-- --:--:-- 32666

Script Finished Feb 11, 2021 13:49.47

Full logs for this script are available at /tmp/user.scripts/tmpScripts/emby-influxdb/log.txt"

still nope.png

emby csv (2).png

exel.png

Link to comment
Share on other sites

tinni88

So after hours of driving myself up the wall i have finally figured it out i missed part of the script off. (numpty lol)

I now have it up and running in grafana and populating how it should and i do thank you Paully for all your help and also for posting this been a massive help

 

#!/bin/bash

# load variables
CSVFILE=/mnt/user/embysessions/emby.csv;

# get list of now playing items and send to jq output as csv
wget -q -O - "http://Loaclhost:8096/emby/Sessions?api_key=62ab***************************" | jq -r -M '.[] | {UserName, SeriesName: .NowPlayingItem.SeriesName, Name: .NowPlayingItem.Name} | select(.Name != null) | {user: .UserName, item: ((if .SeriesName then .SeriesName
 + " / " else "" end) + .Name)} | map(.) | @csv' >$CSVFILE; <--- Was Missing this 🙈

# show csv file to check output
echo
cat $CSVFILE;
echo

# read csv file to parse values and send to influx via http post
while IFS=, read -r user item; do
  curl -i -XPOST 'http://Localhost:8086/write?db=telegraf' --data-binary "emby user=$user,item=$item"
done < $CSVFILE;

# finish
exit;

Unraid-System-Dashboard-V2-Grafana.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...