1
0
mirror of https://github.com/librenms/librenms-agent.git synced 2024-05-09 09:54:52 +00:00
Files
librenms-librenms-agent/snmp/seafile.py

206 lines
5.7 KiB
Python
Raw Normal View History

2019-10-21 04:03:13 +02:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# https://download.seafile.com/published/web-api/v2.1-admin
# user -> libraries (count)
# user -> trash-libraries (count)
# user -> space consumption (count)
# user -> is activated (bool)
# connected_devices (count)
# groups (count)
# Clients -> plattform (count)
# Clients -> version (count)
import json
import requests
2019-10-21 04:03:13 +02:00
# Configfile content example:
# {"url": "https://seafile.mydomain.org",
# "username": "some_admin_login@mail.address",
# "password": "password",
# "account_identifier": "name",
# "hide_monitoring_account": true
# }
CONFIGFILE = "/etc/snmp/seafile.json"
2019-10-21 04:03:13 +02:00
error = 0
error_string = ""
2019-10-21 04:03:13 +02:00
version = 1
def get_data(url_path, data=None, token=None):
complete_url = "%s/%s" % (url, url_path)
headers = {"Accept": "application/json"}
if token:
headers["Authorization"] = "Token %s" % token
2019-10-21 04:03:13 +02:00
try:
if token:
r = requests.get(complete_url, data=data, headers=headers)
else:
r = requests.post(complete_url, data=data, headers=headers)
2019-10-21 04:03:13 +02:00
try:
return r.json()
except json.decoder.JSONDecodeError:
return "no valid json returned - url correct?"
except requests.exceptions.RequestException as err:
return str(err)
2019-10-21 04:03:13 +02:00
def get_devices():
# get all devices
url_path = "api/v2.1/admin/devices/"
2019-10-21 04:03:13 +02:00
return get_data(url_path, token=token)
def get_groups():
# get all groups
url_path = "api/v2.1/admin/groups/"
2019-10-21 04:03:13 +02:00
return get_data(url_path, token=token)
def get_sysinfo():
# get all groups
url_path = "api/v2.1/admin/sysinfo/"
2019-10-21 04:03:13 +02:00
return get_data(url_path, token=token)
def get_account_information():
# get all accounts withs details
account_list = []
for account in get_data("api2/accounts/", token=token):
2019-10-21 04:03:13 +02:00
# get account details
url_path = "api2/accounts/%s/" % account["email"]
2019-10-21 04:03:13 +02:00
account_data = get_data(url_path, token=token)
# get libraries by owner
url_path = "api/v2.1/admin/libraries/?owner=%s" % account["email"]
account_data["repos"] = get_data(url_path, token=token)["repos"]
2019-10-21 04:03:13 +02:00
# get deleted libraries by owner
url_path = "api/v2.1/admin/trash-libraries/?owner=%s" % account["email"]
account_data["trash_repos"] = get_data(url_path, token=token)["repos"]
2019-10-21 04:03:13 +02:00
account_list.append(account_data)
return account_list
def resort_devices(device_list):
data = {}
platform = {}
client_version = {}
for device in device_list:
# don't list information assigned to monitor account
if hide_monitoring_account:
if device["user"] == configfile["username"]:
2019-10-21 04:03:13 +02:00
continue
if device["platform"] not in platform.keys():
platform[device["platform"]] = 1
2019-10-21 04:03:13 +02:00
else:
platform[device["platform"]] += 1
2019-10-21 04:03:13 +02:00
if device["client_version"] not in client_version.keys():
client_version[device["client_version"]] = 1
2019-10-21 04:03:13 +02:00
else:
client_version[device["client_version"]] += 1
2019-10-21 04:03:13 +02:00
data["platform"] = []
2019-10-21 04:03:13 +02:00
for k, v in platform.items():
data["platform"].append({"os_name": k, "clients": v})
data["client_version"] = []
2019-10-21 04:03:13 +02:00
for k, v in client_version.items():
data["client_version"].append({"client_version": k, "clients": v})
2019-10-21 04:03:13 +02:00
return data
def resort_groups(group_list):
data = {"count": len(group_list)}
2019-10-21 04:03:13 +02:00
return data
def resort_accounts(account_list):
if account_identifier in ["name", "email"]:
2019-10-21 04:03:13 +02:00
identifier = account_identifier
else:
identifier = "name"
2019-10-21 04:03:13 +02:00
accepted_key_list = ["is_active", "usage"]
2019-10-21 04:03:13 +02:00
data = []
for user_account in account_list:
# don't list information assigned to monitor account
if hide_monitoring_account:
if user_account["email"] == configfile["username"]:
2019-10-21 04:03:13 +02:00
continue
new_account = {}
new_account["owner"] = user_account[identifier]
new_account["repos"] = len(user_account["repos"])
new_account["trash_repos"] = len(user_account["trash_repos"])
2019-10-21 04:03:13 +02:00
for k in user_account.keys():
if k not in accepted_key_list:
continue
new_account[k] = user_account[k]
data.append(new_account)
return sorted(data, key=lambda k: k["owner"].lower())
2019-10-21 04:03:13 +02:00
# ------------------------ MAIN --------------------------------------------------------
with open(CONFIGFILE, "r") as json_file:
2019-10-21 04:03:13 +02:00
try:
configfile = json.load(json_file)
except json.decoder.JSONDecodeError as e:
error = 1
error_string = "Configfile Error: '%s'" % e
if not error:
url = configfile["url"]
username = configfile["username"]
password = configfile["password"]
2019-10-21 04:03:13 +02:00
try:
account_identifier = configfile["account_identifier"]
2019-10-21 04:03:13 +02:00
except KeyError:
account_identifier = None
try:
hide_monitoring_account = configfile["hide_monitoring_account"]
2019-10-21 04:03:13 +02:00
except KeyError:
hide_monitoring_account = False
# get token
login_data = {"username": username, "password": password}
ret = get_data("api2/auth-token/", data=login_data)
2019-10-21 04:03:13 +02:00
if type(ret) != str:
if "token" in ret.keys():
token = ret["token"]
2019-10-21 04:03:13 +02:00
else:
error = 1
try:
error_string = json.dumps(ret)
except:
error_string = ret
else:
error = 1
error_string = ret
data = {}
if not error:
ret = get_account_information()
2019-10-21 04:03:13 +02:00
if not error:
data["accounts"] = resort_accounts(ret)
data["devices"] = resort_devices(get_devices()["devices"])
data["groups"] = resort_groups(get_groups()["groups"])
data["sysinfo"] = get_sysinfo()
2019-10-21 04:03:13 +02:00
output = {"error": error, "errorString": error_string, "version": version, "data": data}
2019-10-21 04:03:13 +02:00
print(json.dumps(output))