From deb46a523e11e0c0f37930ad2a65b86f39f7eb8a Mon Sep 17 00:00:00 2001 From: nico Date: Sat, 20 Oct 2018 20:47:03 +0200 Subject: further code optimazation * reduced code clutter - removed for iterations in result and config lists + added list comprehension * updated config --- nextcloud_apps | 38 ++++++++++++++++---------------- nextcloud_dbsize | 38 ++++++++++++++++---------------- nextcloud_multi | 67 +++++++++++++++++++++++++++++--------------------------- nextcloud_shares | 52 +++++++++++++++++++++---------------------- nextcloud_users | 44 +++++++++++++++++++------------------ 5 files changed, 121 insertions(+), 118 deletions(-) diff --git a/nextcloud_apps b/nextcloud_apps index e7a5bbc..14427f8 100755 --- a/nextcloud_apps +++ b/nextcloud_apps @@ -49,7 +49,7 @@ class NextcloudApps: return data def run(self): - # init requests session with specific header and credentials + # init request session with specific header and credentials with requests.Session() as s: # read credentials from env s.auth = (os.environ.get('username'), os.environ.get('password')) @@ -60,29 +60,30 @@ class NextcloudApps: # request the data r = s.get(os.environ.get('url')) - # if status code is successful continue - if r.status_code == 200: - api_response = r.json() - result = self.get_data(api_response) + # if status code is successful continue + if r.status_code == 200: + result = self.get_data(r.json()) - for key in result.keys(): - print('\n'.join(result[key])) + # for key in results print every entry in dict + [print('\n'.join(result[key])) for key in result.keys()] - 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') + 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') def main(self): - # check if first argument is config or autoconf if not fetch data + # check if any argument is given if sys.argv.__len__() >= 2: + # check if first argument is config or autoconf if not fetch data if sys.argv[1] == "config": - for key in self.config().keys(): - print('\n'.join(self.config()[key])) + # for key in config().keys() print every entry in dict + [print('\n'.join(self.config()[key])) for key in self.config().keys()] + # MUNIN_CAP_DIRTYCONFIG capability if os.environ.get('MUNIN_CAP_DIRTYCONFIG') == '1': self.run() elif sys.argv[1] == 'autoconf': @@ -96,4 +97,3 @@ class NextcloudApps: if __name__ == "__main__": NextcloudApps().main() - quit(0) diff --git a/nextcloud_dbsize b/nextcloud_dbsize index 9a15774..5878acb 100755 --- a/nextcloud_dbsize +++ b/nextcloud_dbsize @@ -46,7 +46,7 @@ class NextcloudDB: return data def run(self): - # init requests session with specific header and credentials + # init request session with specific header and credentials with requests.Session() as s: # read credentials from env s.auth = (os.environ.get('username'), os.environ.get('password')) @@ -57,29 +57,30 @@ class NextcloudDB: # request the data r = s.get(os.environ.get('url')) - # if status code is successful continue - if r.status_code == 200: - api_response = r.json() - result = self.get_data(api_response) + # if status code is successful continue + if r.status_code == 200: + result = self.get_data(r.json()) - for key in result.keys(): - print('\n'.join(result[key])) + # for key in results print every entry in dict + [print('\n'.join(result[key])) for key in result.keys()] - 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') + 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') def main(self): - # check if first argument is config or autoconf if not fetch data + # check if any argument is given if sys.argv.__len__() >= 2: + # check if first argument is config or autoconf if not fetch data if sys.argv[1] == "config": - for key in self.config().keys(): - print('\n'.join(self.config()[key])) + # for key in config().keys() print every entry in dict + [print('\n'.join(self.config()[key])) for key in self.config().keys()] + # MUNIN_CAP_DIRTYCONFIG capability if os.environ.get('MUNIN_CAP_DIRTYCONFIG') == '1': self.run() elif sys.argv[1] == 'autoconf': @@ -93,4 +94,3 @@ class NextcloudDB: if __name__ == "__main__": NextcloudDB().main() - quit(0) diff --git a/nextcloud_multi b/nextcloud_multi index 8a53656..c336223 100755 --- a/nextcloud_multi +++ b/nextcloud_multi @@ -19,7 +19,6 @@ import requests import sys import os -import re class NextcloudMultiGraph: @@ -100,43 +99,47 @@ class NextcloudMultiGraph: return config def get_data(self, api_response): - data ={ + data = { 'nextcloud_users': [], 'nextcloud_shares': [], 'nextcloud_dbsize': [], 'nextcloud_available_updates': [] } - users = api_response['ocs']['data']['activeUsers'] - shares = api_response['ocs']['data']['nextcloud']['shares'] - dbsize = api_response['ocs']['data']['server']['database']['size'] + # users + users = api_response['ocs']['data']['activeUsers'] data['nextcloud_users'].append('multigraph nextcloud_users') - for key in users.keys(): - data['nextcloud_users'].append(str(key) + ".value " + str(users[key])) - # use regex to remove permission stats from api response - reg = re.compile("num.*") - share_keys = shares.keys() - sharelist = list(filter(reg.match, share_keys)) + # append for every key in users the key and the value to the results + [data['nextcloud_users'].append(str(key) + ".value " + str(users[key])) + for key in users.keys()] + # shares + shares = api_response['ocs']['data']['nextcloud']['shares'] data['nextcloud_shares'].append('multigraph nextcloud_shares') - for key in sharelist: - data['nextcloud_shares'].append(str(key) + ".value " + str(shares[key])) + # append for every key in shares the key and the value if the key starts with "num" + [data['nextcloud_shares'].append(str(key) + ".value " + str(shares[key])) + for key in shares if key.startswith('num')] + + # dbsize + dbsize = api_response['ocs']['data']['server']['database']['size'] data['nextcloud_dbsize'].append('multigraph nextcloud_dbsize') data['nextcloud_dbsize'].append('db_size.value %s' % dbsize) + # app updates # precaution for Nextcloud versions prior to version 14 version = api_response['ocs']['data']['nextcloud']['system']['version'].split(sep=".") if int(version[0]) >= 14: num_updates_available = api_response['ocs']['data']['nextcloud']['system']['apps']['num_updates_available'] + data['nextcloud_available_updates'].append('multigraph nextcloud_available_updates') data['nextcloud_available_updates'].append('num_updates_available.value %s' % num_updates_available) return data def run(self): - # init requests session with specific header and credentials + # init request session with specific header and credentials with requests.Session() as s: # read credentials from env s.auth = (os.environ.get('username'), os.environ.get('password')) @@ -147,29 +150,30 @@ class NextcloudMultiGraph: # request the data r = s.get(os.environ.get('url')) - # if status code is successful continue - if r.status_code == 200: - api_response = r.json() - result = self.get_data(api_response) + # if status code is successful continue + if r.status_code == 200: + result = self.get_data(r.json()) - for key in result.keys(): - print('\n'.join(result[key])) + # for key in results print every entry in dict + [print('\n'.join(result[key])) for key in result.keys()] - 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') + 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') def main(self): - # check if first argument is config or autoconf if not fetch data + # check if any argument is given if sys.argv.__len__() >= 2: + # check if first argument is config or autoconf if not fetch data if sys.argv[1] == "config": - for key in self.config().keys(): - print('\n'.join(self.config()[key])) + # for key in config().keys() print every entry in dict + [print('\n'.join(self.config()[key])) for key in self.config().keys()] + # MUNIN_CAP_DIRTYCONFIG capability if os.environ.get('MUNIN_CAP_DIRTYCONFIG') == '1': self.run() elif sys.argv[1] == 'autoconf': @@ -183,4 +187,3 @@ class NextcloudMultiGraph: if __name__ == "__main__": NextcloudMultiGraph().main() - quit(0) diff --git a/nextcloud_shares b/nextcloud_shares index 0dc6dbe..98aaccf 100755 --- a/nextcloud_shares +++ b/nextcloud_shares @@ -57,20 +57,19 @@ class NextcloudShares: data ={ 'nextcloud_shares': [], } - 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)) + # shares + shares = api_response['ocs']['data']['nextcloud']['shares'] + data['nextcloud_shares'].append('multigraph nextcloud_shares') - for key in sharelist: - data['nextcloud_shares'].append(str(key) + ".value " + str(shares[key])) + # append for every key in shares the key and the value if the key starts with "num" + [data['nextcloud_shares'].append(str(key) + ".value " + str(shares[key])) + for key in shares if key.startswith('num')] return data def run(self): - # init requests session with specific header and credentials + # init request session with specific header and credentials with requests.Session() as s: # read credentials from env s.auth = (os.environ.get('username'), os.environ.get('password')) @@ -81,29 +80,30 @@ class NextcloudShares: # request the data r = s.get(os.environ.get('url')) - # if status code is successful continue - if r.status_code == 200: - api_response = r.json() - result = self.get_data(api_response) + # if status code is successful continue + if r.status_code == 200: + result = self.get_data(r.json()) - for key in result.keys(): - print('\n'.join(result[key])) + # for key in results print every entry in dict + [print('\n'.join(result[key])) for key in result.keys()] - 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') + 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') def main(self): - # check if first argument is config or autoconf if not fetch data + # check if any argument is given if sys.argv.__len__() >= 2: + # check if first argument is config or autoconf if not fetch data if sys.argv[1] == "config": - for key in self.config().keys(): - print('\n'.join(self.config()[key])) + # for key in config().keys() print every entry in dict + [print('\n'.join(self.config()[key])) for key in self.config().keys()] + # MUNIN_CAP_DIRTYCONFIG capability if os.environ.get('MUNIN_CAP_DIRTYCONFIG') == '1': self.run() elif sys.argv[1] == 'autoconf': @@ -114,7 +114,5 @@ class NextcloudShares: else: self.run() - if __name__ == "__main__": NextcloudShares().main() - quit(0) diff --git a/nextcloud_users b/nextcloud_users index ef638c6..77dbd5f 100755 --- a/nextcloud_users +++ b/nextcloud_users @@ -45,15 +45,17 @@ class NextcloudUsers: data ={ 'nextcloud_users': [], } + # users users = api_response['ocs']['data']['activeUsers'] - for key in users.keys(): - data['nextcloud_users'].append(str(key) + ".value " + str(users[key])) + # append for every key in users the key and the value to the results + [data['nextcloud_users'].append(str(key) + ".value " + str(users[key])) + for key in users.keys()] return data def run(self): - # init requests session with specific header and credentials + # init request session with specific header and credentials with requests.Session() as s: # read credentials from env s.auth = (os.environ.get('username'), os.environ.get('password')) @@ -64,29 +66,30 @@ class NextcloudUsers: # request the data r = s.get(os.environ.get('url')) - # if status code is successful continue - if r.status_code == 200: - api_response = r.json() - result = self.get_data(api_response) + # if status code is successful continue + if r.status_code == 200: + result = self.get_data(r.json()) - for key in result.keys(): - print('\n'.join(result[key])) + # for key in results print every entry in dict + [print('\n'.join(result[key])) for key in result.keys()] - 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') + 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') def main(self): - # check if first argument is config or autoconf if not fetch data + # check if any argument is given if sys.argv.__len__() >= 2: + # check if first argument is config or autoconf if not fetch data if sys.argv[1] == "config": - for key in self.config().keys(): - print('\n'.join(self.config()[key])) + # for key in config().keys() print every entry in dict + [print('\n'.join(self.config()[key])) for key in self.config().keys()] + # MUNIN_CAP_DIRTYCONFIG capability if os.environ.get('MUNIN_CAP_DIRTYCONFIG') == '1': self.run() elif sys.argv[1] == 'autoconf': @@ -100,4 +103,3 @@ class NextcloudUsers: if __name__ == "__main__": NextcloudUsers().main() - quit(0) -- cgit v1.2.3-18-g5258