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/pureftpd.py

78 lines
1.7 KiB
Python
Raw Normal View History

2020-01-22 01:52:20 +01:00
#!/usr/bin/env python3
import json
import os
2020-01-22 01:52:20 +01:00
CONFIGFILE = "/etc/snmp/pureftpd.json"
2020-01-22 01:52:20 +01:00
pureftpwho_cmd = "/usr/sbin/pure-ftpwho"
pureftpwho_args = "-v -s -n"
2020-01-22 01:52:20 +01:00
output_data = {}
output_data["version"] = 1
output_data["errorString"] = ""
output_data["error"] = 0
2020-01-22 01:52:20 +01:00
if os.path.isfile(CONFIGFILE):
with open(CONFIGFILE, "r") as json_file:
2020-01-22 01:52:20 +01:00
try:
configfile = json.load(json_file)
except json.decoder.JSONDecodeError as e:
output_data["error"] = 1
output_data["errorString"] = "Configfile Error: '%s'" % e
2020-01-22 01:52:20 +01:00
else:
configfile = None
if not output_data["error"] and configfile:
2020-01-22 01:52:20 +01:00
try:
if "pureftpwho_cmd" in configfile.keys():
pureftpwho_cmd = configfile["pureftpwho_cmd"]
except KeyError as e:
output_data["error"] = 1
output_data["errorString"] = "Configfile Error: '%s'" % e
2020-01-22 01:52:20 +01:00
output = os.popen(pureftpwho_cmd + " " + pureftpwho_args).read()
2020-01-22 01:52:20 +01:00
data = {}
for line in output.split("\n"):
2020-01-22 01:52:20 +01:00
if not len(line):
continue
(
pid,
acct,
time,
state,
file,
peer,
local,
port,
transfered,
total,
percent,
bandwidth,
) = line.split("|")
2020-01-22 01:52:20 +01:00
if "IDLE" in state:
state = "IDLE"
elif "DL" in state:
state = "DL"
elif "UL" in state:
state = "UL"
if acct not in data.keys():
data[acct] = {}
if state not in data[acct]:
data[acct][state] = {"bitrate": 0, "connections": 0}
2020-03-21 22:13:22 +01:00
bandwidth_bit = int(bandwidth) * 1024 * 8
data[acct][state]["bitrate"] += bandwidth_bit
data[acct][state]["connections"] += 1
2020-01-22 01:52:20 +01:00
output_data["data"] = data
2020-01-22 01:52:20 +01:00
print(json.dumps(output_data))