From 0325af937b5274b57e865d876d9aee184e6b97b0 Mon Sep 17 00:00:00 2001 From: nico Date: Sun, 16 Sep 2018 02:49:09 +0200 Subject: refactor and optimizations * refactored all plugins to use the same methods * optimized api calls in the multigraph plugin to reduce load * used dict instead of list for performance + added munin dirtyconfig capability to all plugins --- nextcloud_apps | 72 +++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 18 deletions(-) (limited to 'nextcloud_apps') diff --git a/nextcloud_apps b/nextcloud_apps index 574d932..228a197 100755 --- a/nextcloud_apps +++ b/nextcloud_apps @@ -18,23 +18,36 @@ import os class NextcloudApps: - if (sys.argv.__len__() == 2) and (sys.argv[1] == "config"): - print('graph_title Nextcloud pending App updates') - print('graph_args --base 1024 -l 0') - print('graph_vlabel updates available') - print('graph_info graph showing the number of available app updates') - print('graph_category nextcloud') - - print('num_updates_available.label available app updates') - print('num_updates_available.info number of available app updates') - elif (sys.argv.__len__() == 2) and (sys.argv[1] == 'autoconf'): - # check host if env variables are set + def config(self): + config = { + 'apps': [ + 'graph_title Nextcloud available App updates', + 'graph_args --base 1024 -l 0', + 'graph_vlabel updates available', + 'graph_info graph showing the number of available app updates', + 'graph_category nextcloud', + 'num_updates_available.label available app updates', + 'num_updates_available.info number of available app updates' + ] + } + + return config + + def get_data(self, api_response): + data ={ + 'nextcloud_available_updates': [] + } + + # precaution for Nextcloud versions prior to version 14 try: - if None not in {os.environ['url'], os.environ['username'], os.environ['password']}: - print('yes') + num_updates_available = api_response['ocs']['data']['nextcloud']['system']['apps']['num_updates_available'] + data['nextcloud_available_updates'].append('num_updates_available.value %s' % num_updates_available) except KeyError: - print('no env configuration options are missing') - else: + return False + + return data + + def run(self): # read the configuration from munin environment URL = os.environ['url'] auth = (os.environ['username'], os.environ['password']) @@ -52,8 +65,11 @@ class NextcloudApps: if r.status_code == 200: s.close() api_response = r.json() - num_updates_available = api_response['ocs']['data']['nextcloud']['system']['apps']['num_updates_available'] - print('num_updates_available.value %s' % num_updates_available) + + result = self.get_data(api_response) + + for key in result.keys(): + print('\n'.join(result[key])) elif r.status_code == 996: print('server error') @@ -64,7 +80,27 @@ class NextcloudApps: else: print('unknown error') + def main(self): + if (sys.argv.__len__() == 2) and (sys.argv[1] == "config"): + for key in self.config().keys(): + print('\n'.join(self.config()[key])) + try: + if os.environ['MUNIN_CAP_DIRTYCONFIG'] == '1': + self.run() + except KeyError: + pass + + elif (sys.argv.__len__() == 2) and (sys.argv[1] == 'autoconf'): + # check host if env variables are set + try: + if None not in {os.environ['url'], os.environ['username'], os.environ['password']}: + print('yes') + except KeyError: + print('no env configuration options are missing') + else: + self.run() + if __name__ == "__main__": - NextcloudApps() + NextcloudApps().main() quit(0) -- cgit v1.2.3-54-g00ecf