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
Jellyfrog 61064dc9fe Cleanup some code (#355)
* Format with isort

* Format with Black

* Fix CRLF

* Format with shellcheck

* Fix some warning

* Fix PHP style

* Dont modifiy check_mk files

* Fixes
2021-03-18 12:24:30 +01:00

78 lines
1.7 KiB
Python
Executable File

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