From 4e64a6284457bfeabce29928bec9142ba9be3850 Mon Sep 17 00:00:00 2001 From: SourceDoctor Date: Wed, 22 Jan 2020 01:52:20 +0100 Subject: [PATCH] pureftp snmpd extension (#269) --- snmp/pureftpd.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100755 snmp/pureftpd.py diff --git a/snmp/pureftpd.py b/snmp/pureftpd.py new file mode 100755 index 0000000..6f10770 --- /dev/null +++ b/snmp/pureftpd.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 + +import os +import json + +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: + output_data['error'] = 1 + output_data['errorString'] = "Configfile Error: '%s'" % e + + +output = os.popen('sudo ' + 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 + data[acct][state]['bitrate'] += bandwidth_bit + data[acct][state]['connections'] += 1 + +output_data['data'] = data + +print (json.dumps(output_data))