aboutsummaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rwxr-xr-xplugins/check_nextcloud93
1 files changed, 59 insertions, 34 deletions
diff --git a/plugins/check_nextcloud b/plugins/check_nextcloud
index 19439c4..7e638c0 100755
--- a/plugins/check_nextcloud
+++ b/plugins/check_nextcloud
@@ -1,49 +1,74 @@
#!/usr/bin/env python3
import requests
+import argparse
-# Aktive Users | Warning | Critical
-# App Updates
-# opcache vll Prozent mit Max | Warning | Critical
-
-class Monoid:
- def __init__(self, null, typeify, op):
- self.null = null
- self.typeify = typeify
- self.op = op
+status = {'OK': 0, 'WARNING': 1, 'CRITICAL': 2, 'UNKNOWN': 3}
class Check:
- def __init__(self, status: int, description: str, data: str):
+ def __init__(self, status: str, description: str, data: tuple[str,str,str] | None):
self.status = status
self.description = description
self.data = data
-def check_cache_(data_json, warning: float, critical: float) -> Check:
- data_opcache = data_json['ocs']['data']['server']['php']['opcache']
- opcache_full = data_opcache['cache_full'] == 'True'
- cache_max = data_opcache['interned_string_usage']['buffer_size']
- cache_used = data_opcache['interned_string_usage']['used_memory']
- cache_free = data_opcache['interned_string_usage']['free_memory']
-
- cache_free / cache_max >
+ def format(self) -> str:
+ if self.data != None:
+ return f"{self.status} - {self.description} | {';'.join(self.data)}"
+ else:
+ return f"{self.status} - {self.description}"
+ def exit(self):
+ print(self.format())
+ exit(status[self.status])
-def check_cache_full(data_json) -> bool:
- return data_json['ocs']['data']['server']['php']['opcache']['cache_full'] == 'True'
+parser = argparse.ArgumentParser(description='Check nextcloud status via api')
+parser.add_argument('-u', dest='max_conns', action='store', type=int, default=None, nargs=2)
+parser.add_argument('-c', dest='opcache', action='store', type=float, default=None, nargs=2)
+parser.add_argument('-a', dest='updates', action='store_true', help='check for app updates')
+args=parser.parse_args()
-def get_app_updates(data_json) -> list[str]:
- return data_json['ocs']['data']['nextcloud']['system']['apps']['app_updates']
+def check_interned_strings_cache(data_json, warning: float, critical: float) -> Check:
+ data_opcache = data_json['ocs']['data']['server']['php']['opcache']
+ cache_max = data_opcache['interned_strings_usage']['buffer_size']
+ cache_used = data_opcache['interned_strings_usage']['used_memory'] / cache_max
+ data = map('{:.2f}'.format, (cache_used, warning, critical))
+ if cache_used >= critical:
+ return Check('CRITICAL',"The OPcache interned strings buffer is full.", data)
+ if cache_used >= warning:
+ return Check('WARNING', f"OPcache interned strings buffer is to {cache_used:.0%} filled. " +
+ f"This should not be more than {warning:.0%}!", data)
+ return Check('OK', f"OPcache interned strings buffer is to {cache_used:.0%} filled.", data)
-def get_active_users(data_json) -> dict[str,int]:
- '''The keys in the dictionary are "last5minutes", "last1hour" and "last24hours"'''
- return data_json['ocs']['data']['activeUsers']
+def check_activer_users(data_json, warning: int, critical: int) -> Check:
+ active_users = data_json['ocs']['data']['activeUsers']['last5minutes']
+ if active_users >= critical:
+ status = 'CRITICAL'
+ elif active_users >= warning:
+ status = 'WARNING'
+ else:
+ status = 'OK'
+ return Check( status, f'{active_users} {"users were" if active_users != 1 else "user was"} active in the last 5 minutes.'
+ , map(str, (active_users,warning,critical)))
-if __name__ == "__main__":
- # Fail check?
- data_json = requests.get('https://nextcloud.my.cum.re/ocs/v2.php/apps/serverinfo/api/v1/info?format=json', headers ={'NC-Token': ''}).json()
- print(check_cache_full(data_json))
- print(get_app_updates(data_json))
- print(get_active_users(data_json))
+def check_app_updates(data_json) -> Check:
+ updates = data_json['ocs']['data']['nextcloud']['system']['apps']['app_updates']
+ if updates != []:
+ return Check('WARNING', f'The following apps can be updates: {" ".join(updates)}.', None)
+ else:
+ return Check('OK', 'No app updates were found.', None)
-#print(response_raw.json()['ocs']['data']['server']['php']['opcache']['cache_full'])
-#print(response_raw.json()['ocs']['data']['nextcloud']['system']['apps']['app_updates'])
-#print(response_raw.json()['ocs']['data']['activeUsers'])
+try:
+ data_json = requests.get('https://nextcloud.my.cum.re/ocs/v2.php/apps/serverinfo/api/v1/info?format=json'
+ , headers={'NC-Token': ''}).json() #TODO Savely store secret
+ if data_json['ocs']['meta']['status'] == 'failure':
+ raise Exception('API call failed!')
+ if args.updates:
+ check_app_updates(data_json).exit()
+ if args.max_conns != None:
+ warning, critical = args.max_conns
+ check_activer_users(data_json, warning, critical).exit()
+ if args.opcache != None:
+ warning, critical = args.opcache
+ check_interned_strings_cache(data_json, warning, critical).exit()
+except Exception as ex:
+ #TODO Define own execption, we dont want to leak data!
+ Check('UNKNOWN', f'A python runtime execption occured: {ex}', None).exit()