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_users | 86 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 28 deletions(-) (limited to 'nextcloud_users') diff --git a/nextcloud_users b/nextcloud_users index 2230b84..59fec2c 100755 --- a/nextcloud_users +++ b/nextcloud_users @@ -18,28 +18,39 @@ import os 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_printf %.0lf') - 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('last5minutes.info users connected in the last 5 minutes') - print('last1hour.label last hour') - print('last1hour.info users connected in the last hour') - print('last24hours.label last 24 hours') - print('last24hours.info users connected in the last 24 hours') - 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: + def config(self): + config = { + 'users': [ + 'graph_title Nextcloud User Activity', + 'graph_args --base 1024 -l 0', + 'graph_printf %.0lf', + 'graph_vlabel connected users', + 'graph_info graph showing the number of connected user', + 'graph_category nextcloud', + 'last5minutes.label last 5 minutes', + 'last5minutes.info users connected in the last 5 minutes', + 'last1hour.label last hour', + 'last1hour.info users connected in the last hour', + 'last24hours.label last 24 hours', + 'last24hours.info users connected in the last 24 hours' + ] + } + + return config + + def get_data(self, api_response): + data ={ + 'nextcloud_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])) + + return data + + def run(self): # read the configuration from munin environment URL = os.environ['url'] auth = (os.environ['username'], os.environ['password']) @@ -57,12 +68,11 @@ class NextcloudUsers: if r.status_code == 200: s.close() 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)) + result = self.get_data(api_response) + + for key in result.keys(): + print('\n'.join(result[key])) elif r.status_code == 996: print('server error') @@ -73,7 +83,27 @@ class NextcloudUsers: 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__": - NextcloudUsers() + NextcloudUsers().main() quit(0) -- cgit v1.2.3-54-g00ecf