-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_kodi_playback.py
executable file
·153 lines (127 loc) · 5.61 KB
/
check_kodi_playback.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/python
"""
check_kodi_playback.py
Created by: David Angelovich <[email protected]>
Website: http://maxpowerindustries.com
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
This script gets the playback status from Kodi and reports the currently
playing video or audio item. It also reports current file playback
percentage as performance data to allow basic graphing to indicate usage.
"""
import json, requests, pprint, argparse, urllib, sys, re
def debugprint(debugobject, debugstring):
"""
Print debug information if running in debug mode
"""
if CMDLINEARGS.debug:
print "===== " + debugstring + " ====="
pprint.pprint(debugobject)
print "===== " + debugstring + " ====="
print ""
def querykodi(jsonquery):
""""
Query the kodi server from the given URL
"""
try:
jsonresponse = requests.get(jsonquery, headers=HTTPHEADERS)
except requests.exceptions.RequestException as reqexception:
print 'Error!', reqexception
sys.exit(RETURNCODE)
if jsonresponse.status_code != 200:
print 'Error!', URLPARAMETERS, 'returned HTTP:', \
jsonresponse.status_code
sys.exit(RETURNCODE)
#jsonresponse.text will look like this if something is playing
#{"id":1,"jsonrpc":"2.0","result":[{"playerid":1,"type":"video"}]}
#and if nothing is playing:
#{"id":1,"jsonrpc":"2.0","result":[]}
jsondata = json.loads(jsonresponse.text)
debugprint(jsondata, "jsondata")
return jsondata
# Nagios return codes
NAGIOSOK = 0
NAGIOSWARNING = 1
NAGIOSCRITICAL = 2
NAGIOSUNKNOWN = 3
# Process command line arguments
ARGPARSER = argparse.ArgumentParser(description='Check Kodi playback status.')
ARGPARSER.add_argument('-H',
'--host',
action='store',
nargs=1,
help="Specify the host to query")
ARGPARSER.add_argument('-c',
'--critical',
action='store_true',
help="If data retrieval fails, return critical")
ARGPARSER.add_argument('-w',
'--warning',
action='store_true',
help="If data retrieval fails, return warning")
ARGPARSER.add_argument('-d',
'--debug',
action='store_true',
help="Enable debug mode")
CMDLINEARGS = ARGPARSER.parse_args()
# Set the global default return code in case something goes wrong
if CMDLINEARGS.critical or (CMDLINEARGS.critical and CMDLINEARGS.warning):
RETURNCODE = NAGIOSCRITICAL
else:
RETURNCODE = NAGIOSWARNING
# Specifying the host is mandatory
if not CMDLINEARGS.host:
print "-H HOST must be specified.\n"
ARGPARSER.print_help()
sys.exit(RETURNCODE)
KODIURL = 'http://' + CMDLINEARGS.host[0] + '/jsonrpc?'
debugprint(KODIURL, "KODIURL")
#Required header for XBMC JSON-RPC calls, otherwise you'll get a
#415 HTTP response code - Unsupported media type
HTTPHEADERS = {'content-type': 'application/json'}
# Query to get the currently playing / paused video or audio
RAWJSONQUERY = {"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 1}
URLPARAMETERS = urllib.urlencode({'request': json.dumps(RAWJSONQUERY)})
debugprint(URLPARAMETERS, "URLPARAMETERS")
QUERYRESULTS = querykodi(KODIURL + URLPARAMETERS)
#result is an empty list if nothing is playing or paused.
if QUERYRESULTS['result']:
#We need the specific "playerid" of the currently playing file in order
#to pause it
PLAYERID = QUERYRESULTS['result'][0]["playerid"]
# Get the currently playing item's title
RAWJSONQUERY = {"jsonrpc": "2.0", "method": "Player.GetItem",
"params": {"playerid": PLAYERID,
"properties" : ["file"]},
"id": 1}
URLPARAMETERS = urllib.urlencode({'request': json.dumps(RAWJSONQUERY)})
debugprint(URLPARAMETERS, "URLPARAMETERS")
QUERYRESULTS = querykodi(KODIURL + URLPARAMETERS)
if QUERYRESULTS['result']['item']['file']:
PLAYBACKFILE = QUERYRESULTS['result']['item']['file']
# Clean up the filename for the output
PLAYBACKFILE = re.sub(r"^.*\/(.*)$", r"\1", PLAYBACKFILE, 0, re.M|re.S)
# Get the currently playing item's playback percentage
RAWJSONQUERY = {"jsonrpc": "2.0", "method": "Player.GetProperties",
"params": {"playerid": PLAYERID,
"properties" : ["percentage"]},
"id": 1}
URLPARAMETERS = urllib.urlencode({'request': json.dumps(RAWJSONQUERY)})
debugprint(URLPARAMETERS, "URLPARAMETERS")
QUERYRESULTS = querykodi(KODIURL + URLPARAMETERS)
if QUERYRESULTS["result"]["percentage"]:
print "Now playing: " + PLAYBACKFILE + \
"|playstatus=1 playbackpercent=" + \
"%.2f" % QUERYRESULTS['result']['percentage'] + "%"
else:
# Kodi isn't playing anything
print "Kodi is not playing any media.|playstatus=0 playbackpercent=0"
exit(0)