This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoodle_info.py
executable file
·76 lines (62 loc) · 2.21 KB
/
moodle_info.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
#!/usr/bin/env python
# This Module allow zabbix and other solutions to inspect
# moodle api.
import requests
import argparse
import pdb
def get_nested_elements(info, elements):
# Function to traverse dictionaries and print when value is
# not a dict (instead it's a str)
if isinstance(elements, str):
keys = elements.split('.')
if isinstance(info, dict) and len(keys) > 1:
keys.pop(0)
for key in keys:
value = info[key]
if isinstance(value, dict):
keys.pop(0)
if keys:
get_nested_elements(value, keys)
elif value:
print(value)
else:
return('Not Encountered Value')
else:
print(info)
def inspect_ws(**kwargs):
# Get Params and try to pick a result from the server.
server = kwargs.pop('server')
token = kwargs.pop('token')
function = kwargs.pop('function')
data = kwargs.pop('data')
ssl = kwargs.pop('ssl')
if ssl:
prefix = 'https://'
else:
prefix = 'http://'
wsdata = { 'moodlewsrestformat': 'json',
'wsfunction': function.split('.')[0],
'wstoken': token
}
if data:
wsdata += data
result = requests.post(url=prefix+server+'/webservice/rest/server.php',
data=wsdata).json()
if isinstance(result, dict) and 'errorcode' not in result.keys():
get_nested_elements(result, function)
elif isinstance(result, str) or isinstance(result, int):
print(result)
elif isinstance(result,list):
for value in result:
get_nested_elements(value, function)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--server', type=str, required=True)
parser.add_argument('--token', type=str, required=True)
parser.add_argument('--function', type=str, required=True)
parser.add_argument('--data', type=dict, required=False)
parser.add_argument('--ssl', type=bool, required=False, default=False)
args = parser.parse_args()
inspect_ws(server=args.server, token=args.token,
function=args.function, data=args.data,
ssl=args.ssl)