This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
plex.py
executable file
·133 lines (104 loc) · 3.31 KB
/
plex.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
#!/usr/bin/env python
from __future__ import print_function
import sys
import requests
CONFIGS = []
def configure_callback(conf):
host = None
port = None
section = None
sum_leaf = False
instance = None
for node in conf.children:
key = node.key.lower()
val = node.values[0]
if key == 'host':
host = val
elif key == 'port':
port = int(val)
elif key == 'section':
section = int(val)
elif key == 'sumleaf':
sum_leaf = val
elif key == 'instance':
instance = val
else:
collectd.warning('plex plugin: Unknown config key: %s.' % key)
continue
config = {
'host': host,
'port': port,
'section': section,
'sum_leaf': sum_leaf,
'instance': instance
}
collectd.info('plex plugin: Configured with {}'.format(config))
CONFIGS.append(config)
def read_callback():
for conf in CONFIGS:
get_metrics(conf)
def dispatch_value(type_instance, plugin_instance, value):
val = collectd.Values(plugin='plex')
val.type = 'gauge'
val.type_instance = type_instance
val.plugin_instance = plugin_instance
val.values = [value]
val.dispatch()
def get_metrics(conf, callback=None):
url = 'http://{host}:{port}/library/sections/{section}/all'.format(
host=conf['host'],
port=conf['port'],
section=conf['section']
)
data = get_json(url)
count = sum_videos(data, conf['sum_leaf'])
plugin_instance = get_plugin_instance(conf)
type_instance = get_type_instance(data, conf)
if callback is None:
dispatch_value(type_instance, plugin_instance, count)
else:
callback(type_instance, plugin_instance, count)
def get_plugin_instance(conf):
return '{host}-section_{section}'.format(host=conf['host'],
section=conf['section'])
def get_type_instance(data, conf):
instance = conf['instance']
if instance is None:
instance = data['title1'].lower().replace(' ', '_')
return instance
def get_json(url):
headers = {'Accept': 'application/json'}
r = requests.get(url, headers=headers)
return r.json()
def sum_videos(data, sum_leaf=False):
if sum_leaf:
return sum(c['leafCount'] for c in data['_children'])
return len(data['_children'])
def main():
if len(sys.argv) < 6:
print('{} <host> <port> <section> <sum_leaf> <instance>'.format(
sys.argv[0]))
sys.exit(1)
instance = sys.argv[5] if sys.argv[5] != 'auto' else None
conf = {
'host': sys.argv[1],
'port': sys.argv[2],
'section': sys.argv[3],
'sum_leaf': sys.argv[4].lower() == 'true',
'instance': instance
}
def callback(type_instance, plugin_instance, value):
print({
'value': value,
'type_instance': type_instance,
'plugin_instance': plugin_instance,
'full_name': 'plex-{}.gauge-{}.value'.format(plugin_instance,
type_instance)
})
get_metrics(conf, callback)
if __name__ == '__main__':
main()
else:
import collectd
collectd.register_config(configure_callback)
collectd.register_read(read_callback)