From c9e2bc743397229a930c2647d5b59034f9be08c0 Mon Sep 17 00:00:00 2001 From: nico Date: Tue, 4 Sep 2018 02:32:57 +0200 Subject: Initial Commit + inital plugins for nextcloud shares and user activity graphs --- .gitignore | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 27 ++++++++++++ nextcloud_shares | 62 +++++++++++++++++++++++++++ nextcloud_users | 52 +++++++++++++++++++++++ 4 files changed, 268 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 nextcloud_shares create mode 100644 nextcloud_users diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..64562bd --- /dev/null +++ b/.gitignore @@ -0,0 +1,127 @@ +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +.idea/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +### Python Patch ### +.venv/ + +### Python.VirtualEnv Stack ### +# Virtualenv +# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/ +[Bb]in +[Ii]nclude +[Ll]ib +[Ll]ib64 +[Ll]ocal +[Ss]cripts +pyvenv.cfg +pip-selfcheck.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..500dd75 --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +# Nextcloud Munin Plugin +Some basic Munin Plugins gathering information from the NextCloud external API. + +## install +To use these Plugins one has to add his specific URL and credential to the script. +``` +URL = 'https://URL.TO.YOUR.NEXTCLOUD.tld/ocs/v2.php/apps/serverinfo/api/v1/info' +auth = ('username', 'password or logintoken') +``` +If these are correct the script needs to be placed in the munin directory eg. `/etc/munin/plugins/` + +The munin-node needs to be restarted to facilitate the new plugins. +`systemctl restart munin-node` + +### everything working? +To check if everything is working as expected check if the plugins actually gather data. +``` +telnet localhost 4949 # localhost or IP the munin-node +list +fetch nextcloud_shares +fetch nextcloud_users +``` + +If everything works as it should, list will return `nextcloud_shares` and `nextcloud_users` within the list of other active plugins. The `fetch` commands will run the script and return the gathered values. As long as none of them are NaN everything works as expected. + +### uninstall +To remove the plugins from munin remove both plugins from the directory and restart the node. diff --git a/nextcloud_shares b/nextcloud_shares new file mode 100644 index 0000000..b2e858e --- /dev/null +++ b/nextcloud_shares @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +import re +import requests +import sys + +URL = 'https://URL.TO.YOUR.NEXTCLOUD.tld/ocs/v2.php/apps/serverinfo/api/v1/info' +auth = ('username', 'password or logintoken') + + +class NextcloudShares: + if (sys.argv.__len__() == 2) and (sys.argv[1] == "config"): + print('graph_title Nextcloud Shares') + print('graph_args --base 1024 -l 0') + print('graph_vlabel number of shares') + print('graph_info graph showing the number of shares') + print('graph_category nextcloud') + + print('num_fed_shares_received.label federated shares recieved') + print('num_fed_shares_sent.label federated shares sent') + print('num_shares.label total number of shares') + print('num_shares_groups.label group shares') + print('num_shares_link.label link shares') + print('num_shares_link_no_password.label link shares without a password') + print('num_shares_user.label user shares') + else: + # init requests session with specific header and credentials + s = requests.Session() + s.auth = auth + s.headers.update({'Accept': 'application/json'}) + + # request data from api + r = s.get(URL) + + # if status code is successful continue + if r.status_code == 200: + api_response = r.json() + shares = api_response['ocs']['data']['nextcloud']['shares'] + + # use regex to remove permission stats from api response + reg = re.compile("num.*") + share_keys = shares.keys() + sharelist = list(filter(reg.match, share_keys)) + + result = list() + for key in sharelist: + result.append(str(key) + ".value " + str(shares[key])) + print("\n".join(result)) + + elif r.status_code == 996: + print('server error') + elif r.status_code == 997: + print('not authorized') + elif r.status_code == 998: + print('not found') + else: + print('unknown error') + + +if __name__ == "__main__": + NextcloudShares() + quit(0) diff --git a/nextcloud_users b/nextcloud_users new file mode 100644 index 0000000..fce9c73 --- /dev/null +++ b/nextcloud_users @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +import requests +import sys + +URL = 'https://URL.TO.YOUR.NEXTCLOUD.tld/ocs/v2.php/apps/serverinfo/api/v1/info' +auth = ('username', 'password or logintoken') + + +class NextcloudUsers: + if (sys.argv.__len__() == 2) and (sys.argv[1] == "config"): + print('graph_title Nextcloud User Activity') + print('graph_args --base 1024 -l 0') + print('graph_vlabel connected users') + print('graph_info graph showing the number of connected user') + print('graph_category nextcloud') + + print('last5minutes.label last 5 minutes') + print('last1hour.label last hour') + print('last24hours.label last 24 hours') + else: + # init requests session with specific header and credentials + s = requests.Session() + s.auth = auth + s.headers.update({'Accept': 'application/json'}) + + # request data from api + r = s.get(URL) + + # if status code is successful continue + if r.status_code == 200: + api_response = r.json() + users = api_response['ocs']['data']['activeUsers'] + + result = list() + for key in users.keys(): + result.append(str(key) + ".value " + str(users[key])) + print("\n".join(result)) + + elif r.status_code == 996: + print('server error') + elif r.status_code == 997: + print('not authorized') + elif r.status_code == 998: + print('not found') + else: + print('unknown error') + + +if __name__ == "__main__": + NextcloudUsers() + quit(0) -- cgit v1.2.3-18-g5258