Ejemplo n.º 1
0
# os.system('echo 0 > /var/run/upsmon.pid')

# os.system('chmod -R 777 /var/run')

# os.system('upsdrvctl -u root start')
# os.system('upsd')
# os.system('upsmon -D &')

# time.sleep(10)

while 1:
    client = PyNUTClient()

    fields = {}
    for var in client.list_vars(ups_name):
        value = client.get_var(ups_name, var)
        try:
            fields[var] = float(value)
        except:
            fields[var] = value

    measurement = [{'measurement': 'ups', 'fields': fields}]

    print(measurement)

    influx_client = InfluxDBClient(influx_host,
                                   influx_port,
                                   database=influx_db)
    influx_client.create_database(influx_db)

    influx_client.write_points(measurement)
Ejemplo n.º 2
0
class TestClient(unittest.TestCase):

    def setUp(self):
        self.client = PyNUTClient(connect=False, debug=True)
        self.client._srv_handler = MockServer(broken=False)
        self.broken_client = PyNUTClient(connect=False, debug=True)
        self.broken_client._srv_handler = MockServer(broken=True)
        self.not_ok_client = PyNUTClient(connect=False, debug=True)
        self.not_ok_client._srv_handler = MockServer(ok=False,
                broken=False)
        self.valid = "test"
        self.invalid = "does_not_exist"
        self.valid_ups_name = "Test UPS 1"
        self.valid_desc = self.valid_ups_name
        self.valid_value = '100'
        self.valid_command_desc = self.valid_desc
        telnetlib.Telnet = Mock()

    def test_init_with_args(self):
        PyNUTClient(connect=False, login='******', password='******',
                host='test', port=1)

    def test_supports_context_manager(self):
        try:
            with PyNUTClient(connect=False) as client:
                pass
        except AttributeError:
            assert(False)

    def test_connect(self):
        try:
            PyNUTClient()
        except Exception:
            assert(False)

    def test_connect_debug(self):
        try:
            PyNUTClient(debug=True)
        except Exception:
            assert(False)

    def test_connect_broken(self):
        telnetlib.Telnet = MockServer
        client = PyNUTClient(login=self.valid, password=self.valid,
                connect=False)
        self.assertRaises(PyNUTError, client._connect)

    def test_connect_credentials(self):
        try:
            PyNUTClient(login=self.valid, password=self.valid,
                    debug=True)
        except TypeError:
            pass
        except PyNUTError:
            pass
        except Exception:
            assert(False)

    def test_connect_credentials_username_ok(self):
        try:
            telnetlib.Telnet = MockServer
            PyNUTClient(login=self.valid, password=self.valid,
                    debug=True)
        except TypeError:
            pass
        except PyNUTError:
            pass
        except Exception:
            assert(False)

    def test_get_ups_list(self):
        ups_list = self.client.list_ups()
        self.assertEquals(type(ups_list), dict)
        self.assertEquals(len(ups_list), 2)
        self.assertEquals(ups_list[self.valid], self.valid_ups_name)

    def test_get_ups_list_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.list_ups)

    def test_get_ups_vars_valid_ups(self):
        vars = self.client.list_vars(self.valid)
        self.assertEquals(type(vars), dict)
        self.assertEquals(len(vars), 2)
        self.assertEquals(vars['battery.charge'], '100')

    def test_get_ups_vars_invalid_ups(self):
        self.assertRaises(PyNUTError, self.client.list_vars, self.invalid)

    def test_get_ups_vars_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.list_vars, self.valid)

    def test_get_ups_commands_valid_ups(self):
        commands = self.client.list_commands(self.valid)
        self.assertEquals(type(commands), dict)
        self.assertEquals(len(commands), 1)
        self.assertEquals(commands[self.valid], self.valid_command_desc)

    def test_get_ups_commands_invalid_ups(self):
        self.assertRaises(PyNUTError, self.client.list_commands, self.invalid)

    def test_get_ups_commands_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.list_commands,
                self.valid)

    def test_get_rw_vars_valid_ups(self):
        vars = self.client.list_rw_vars(self.valid)
        self.assertEquals(type(vars), dict)
        self.assertEquals(vars[self.valid], self.valid)

    def test_get_rw_vars_invalid_ups(self):
        self.assertRaises(PyNUTError, self.client.list_rw_vars, self.invalid)

    def test_set_rw_var_valid(self):
        try:
            self.client.set_var(self.valid, self.valid, self.valid)
        except PyNUTError:
            assert(False)

    def test_set_rw_var_invalid(self):
        self.assertRaises(PyNUTError, self.client.set_var, self.invalid,
                self.invalid, self.invalid)

    def test_set_rw_var_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.set_var, self.valid,
                self.valid, self.valid)

    def test_run_ups_command_valid(self):
        try:
            self.client.run_command(self.valid, self.valid)
        except PyNUTError:
            assert(False)

    def test_run_ups_command_invalid(self):
        self.assertRaises(PyNUTError, self.client.run_command, self.invalid,
                    self.invalid)

    def test_run_ups_command_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.run_command, self.valid,
                    self.valid)

    def test_fsd_valid_ups(self):
        try:
            self.client.fsd(self.valid)
        except PyNUTError:
            assert(False)

    def test_fsd_invalid_ups(self):
        self.assertRaises(PyNUTError, self.client.fsd, self.invalid)

    def test_fsd_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.fsd, self.valid)

    def test_fsd_not_ok(self):
        self.assertRaises(PyNUTError, self.not_ok_client.fsd, self.valid)

    def test_help(self):
        self.assertEquals(self.client.help(), 'Commands: HELP VER GET LIST SET INSTCMD LOGIN LOGOUT USERNAME PASSWORD STARTTLS\n')

    def test_ver(self):
        self.assertEquals(self.client.ver(), 'Network UPS Tools upsd 2.7.1 - http://www.networkupstools.org/\n')

    def test_list_clients_valid(self):
        clients = self.client.list_clients(self.valid)
        self.assertEquals(type(clients), dict)
        self.assertEquals(clients[self.valid], [self.valid])

    def test_list_clients_invalid(self):
        self.assertRaises(PyNUTError, self.client.list_clients,
                self.invalid)

    def test_list_clients_none(self):
        self.assertRaises(PyNUTError, self.client.list_clients)

    def test_list_clients_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.list_clients,
                self.valid)

    def test_num_logins(self):
        self.assertEquals(self.client.num_logins(self.valid), 1)

    def test_num_logins_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.num_logins,
                self.valid)

    def test_description(self):
        self.assertEquals(self.client.description(self.valid),
                self.valid_desc)

    def test_description_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.description,
                self.valid)

    def test_get(self):
        self.assertEquals(self.client.get(self.valid, self.valid),
                self.valid_value)

    def test_get_var_alias_for_get(self):
        self.assertEquals(self.client.get(self.valid, self.valid),
                self.client.get_var(self.valid, self.valid))

    def test_get_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.get,
                self.valid, self.valid)

    def test_var_description(self):
        self.assertEquals(self.client.var_description(self.valid,
                    self.valid), self.valid_desc)

    def test_var_description_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.var_description,
                self.valid, self.valid)

    def test_command_description(self):
        self.assertEquals(self.client.command_description(self.valid,
                    self.valid), self.valid_desc)

    def test_command_description_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.command_description,
                self.valid, self.valid)

    def test_list_enum(self):
        self.assertEquals(self.client.list_enum(self.valid, self.valid), 
                [self.valid_desc])

    def test_list_enum_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.list_enum,
                self.valid, self.valid)

    def test_list_range(self):
        self.assertEquals(self.client.list_range(self.valid, self.valid), 
                [self.valid_desc])

    def test_list_range_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.list_range,
                self.valid, self.valid)

    def test_var_type(self):
        self.assertEquals(self.client.var_type(self.valid, self.valid), 
                "RW STRING:3")

    def test_var_type_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.var_type,
                self.valid, self.valid)
Ejemplo n.º 3
0
class TestClient(unittest.TestCase):
    def setUp(self):
        self.client = PyNUTClient(connect=False, debug=True)
        self.client._srv_handler = MockServer(broken=False)
        self.broken_client = PyNUTClient(connect=False, debug=True)
        self.broken_client._srv_handler = MockServer(broken=True)
        self.not_ok_client = PyNUTClient(connect=False, debug=True)
        self.not_ok_client._srv_handler = MockServer(ok=False, broken=False)
        self.valid = "test"
        self.invalid = "does_not_exist"
        self.valid_ups_name = "Test UPS 1"
        self.valid_desc = self.valid_ups_name
        self.valid_value = '100'
        self.valid_command_desc = self.valid_desc
        telnetlib.Telnet = Mock()

    def test_init_with_args(self):
        PyNUTClient(connect=False,
                    login='******',
                    password='******',
                    host='test',
                    port=1)

    def test_supports_context_manager(self):
        try:
            with PyNUTClient(connect=False) as client:
                pass
        except AttributeError:
            assert (False)

    def test_connect(self):
        try:
            PyNUTClient()
        except Exception:
            assert (False)

    def test_connect_debug(self):
        try:
            PyNUTClient(debug=True)
        except Exception:
            assert (False)

    def test_connect_broken(self):
        telnetlib.Telnet = MockServer
        client = PyNUTClient(login=self.valid,
                             password=self.valid,
                             connect=False)
        self.assertRaises(PyNUTError, client._connect)

    def test_connect_credentials(self):
        try:
            PyNUTClient(login=self.valid, password=self.valid, debug=True)
        except TypeError:
            pass
        except PyNUTError:
            pass
        except Exception:
            assert (False)

    def test_connect_credentials_username_ok(self):
        try:
            telnetlib.Telnet = MockServer
            PyNUTClient(login=self.valid, password=self.valid, debug=True)
        except TypeError:
            pass
        except PyNUTError:
            pass
        except Exception:
            assert (False)

    def test_get_ups_list(self):
        ups_list = self.client.list_ups()
        self.assertEquals(type(ups_list), dict)
        self.assertEquals(len(ups_list), 2)
        self.assertEquals(ups_list[self.valid], self.valid_ups_name)

    def test_get_ups_list_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.list_ups)

    def test_get_ups_vars_valid_ups(self):
        vars = self.client.list_vars(self.valid)
        self.assertEquals(type(vars), dict)
        self.assertEquals(len(vars), 2)
        self.assertEquals(vars['battery.charge'], '100')

    def test_get_ups_vars_invalid_ups(self):
        self.assertRaises(PyNUTError, self.client.list_vars, self.invalid)

    def test_get_ups_vars_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.list_vars, self.valid)

    def test_get_ups_commands_valid_ups(self):
        commands = self.client.list_commands(self.valid)
        self.assertEquals(type(commands), dict)
        self.assertEquals(len(commands), 1)
        self.assertEquals(commands[self.valid], self.valid_command_desc)

    def test_get_ups_commands_invalid_ups(self):
        self.assertRaises(PyNUTError, self.client.list_commands, self.invalid)

    def test_get_ups_commands_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.list_commands,
                          self.valid)

    def test_get_rw_vars_valid_ups(self):
        vars = self.client.list_rw_vars(self.valid)
        self.assertEquals(type(vars), dict)
        self.assertEquals(vars[self.valid], self.valid)

    def test_get_rw_vars_invalid_ups(self):
        self.assertRaises(PyNUTError, self.client.list_rw_vars, self.invalid)

    def test_set_rw_var_valid(self):
        try:
            self.client.set_var(self.valid, self.valid, self.valid)
        except PyNUTError:
            assert (False)

    def test_set_rw_var_invalid(self):
        self.assertRaises(PyNUTError, self.client.set_var, self.invalid,
                          self.invalid, self.invalid)

    def test_set_rw_var_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.set_var, self.valid,
                          self.valid, self.valid)

    def test_run_ups_command_valid(self):
        try:
            self.client.run_command(self.valid, self.valid)
        except PyNUTError:
            assert (False)

    def test_run_ups_command_invalid(self):
        self.assertRaises(PyNUTError, self.client.run_command, self.invalid,
                          self.invalid)

    def test_run_ups_command_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.run_command,
                          self.valid, self.valid)

    def test_fsd_valid_ups(self):
        try:
            self.client.fsd(self.valid)
        except PyNUTError:
            assert (False)

    def test_fsd_invalid_ups(self):
        self.assertRaises(PyNUTError, self.client.fsd, self.invalid)

    def test_fsd_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.fsd, self.valid)

    def test_fsd_not_ok(self):
        self.assertRaises(PyNUTError, self.not_ok_client.fsd, self.valid)

    def test_help(self):
        self.assertEquals(
            self.client.help(),
            'Commands: HELP VER GET LIST SET INSTCMD LOGIN LOGOUT USERNAME PASSWORD STARTTLS\n'
        )

    def test_ver(self):
        self.assertEquals(
            self.client.ver(),
            'Network UPS Tools upsd 2.7.1 - http://www.networkupstools.org/\n')

    def test_list_clients_valid(self):
        clients = self.client.list_clients(self.valid)
        self.assertEquals(type(clients), dict)
        self.assertEquals(clients[self.valid], [self.valid])

    def test_list_clients_invalid(self):
        self.assertRaises(PyNUTError, self.client.list_clients, self.invalid)

    def test_list_clients_none(self):
        self.assertRaises(PyNUTError, self.client.list_clients)

    def test_list_clients_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.list_clients,
                          self.valid)

    def test_num_logins(self):
        self.assertEquals(self.client.num_logins(self.valid), 1)

    def test_num_logins_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.num_logins,
                          self.valid)

    def test_description(self):
        self.assertEquals(self.client.description(self.valid), self.valid_desc)

    def test_description_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.description,
                          self.valid)

    def test_get(self):
        self.assertEquals(self.client.get(self.valid, self.valid),
                          self.valid_value)

    def test_get_var_alias_for_get(self):
        self.assertEquals(self.client.get(self.valid, self.valid),
                          self.client.get_var(self.valid, self.valid))

    def test_get_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.get, self.valid,
                          self.valid)

    def test_var_description(self):
        self.assertEquals(self.client.var_description(self.valid, self.valid),
                          self.valid_desc)

    def test_var_description_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.var_description,
                          self.valid, self.valid)

    def test_command_description(self):
        self.assertEquals(
            self.client.command_description(self.valid, self.valid),
            self.valid_desc)

    def test_command_description_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.command_description,
                          self.valid, self.valid)

    def test_list_enum(self):
        self.assertEquals(self.client.list_enum(self.valid, self.valid),
                          [self.valid_desc])

    def test_list_enum_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.list_enum, self.valid,
                          self.valid)

    def test_list_range(self):
        self.assertEquals(self.client.list_range(self.valid, self.valid),
                          [self.valid_desc])

    def test_list_range_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.list_range,
                          self.valid, self.valid)

    def test_var_type(self):
        self.assertEquals(self.client.var_type(self.valid, self.valid),
                          "RW STRING:3")

    def test_var_type_broken(self):
        self.assertRaises(PyNUTError, self.broken_client.var_type, self.valid,
                          self.valid)
Ejemplo n.º 4
0
from os import getenv
from nut2 import PyNUTClient
from thingspeak import Channel

CHANNEL_ID = int(getenv("THINGSPEAK_CHANNEL_ID"))
API_KEY = getenv("THINGSPEAK_API_KEY")
UPS_NAME = getenv("NUT_UPS_NAME")

channel = Channel(CHANNEL_ID, api_key=API_KEY)
client = PyNUTClient()
voltage = float(client.get_var(UPS_NAME, 'input.voltage'))
print(voltage, channel.update({"field1": voltage}))
Ejemplo n.º 5
0
class USV:
    name = 'USV'

    def __init__(self, config=None):
        self.configuration = config
        self.timestamp = None
        self.http = None
        self.parsed_data = OrderedDict()
        # self.time_zone = 'Europe/Berlin'
        self.time_zone = 'UTC'
        self.tz = pytz.timezone(self.time_zone)
        self.nut_name = "ups"

        self.nut = PyNUTClient(host=self.configuration[self.name]['IPAdresse'], debug=False, connect=False)
        self.parsed_data = None
        self.id_fields = {
            "battery.charge": ["battery_charge", int],
            # "battery.charge.low": "10",
            # "battery.charge.warning": "50",
            # "battery.date": "2001/09/25",
            # "battery.mfr.date": "2019/04/27",
            "battery.runtime": ["battery_runtime", int],
            # "battery.runtime.low": "battery_runtime_low",
            # "battery.type": "PbAc
            "battery.voltage": ["battery_voltage", float],
            # "battery.voltage.nominal": "12.0
            # "device.mfr": "American Power Conversion
            # "device.model": "Back-UPS XS 700U
            # "device.serial": "3B1917X69838
            # "device.type": "ups
            # "driver.name": "usbhid-ups
            # "driver.parameter.pollfreq": "30
            # "driver.parameter.pollinterval": "5
            # "driver.parameter.port": "auto
            # "driver.version": "DSM6-2-25364-191230
            # "driver.version.data": "APC HID 0.95
            # "driver.version.internal": "0.38
            # "input.sensitivity": "high
            # "input.transfer.high": "290
            # "input.transfer.low": "140
            "input.voltage": ["input_voltage", float],
            # "input.voltage.nominal": "230
            # "ups.beeper.status": "enabled
            # "ups.delay.shutdown": "20
            # "ups.firmware": "924.Z3 .I
            # "ups.firmware.aux": "Z3
            "ups.load": ["ups_load", int],
            # "ups.mfr": "American Power Conversion
            # "ups.mfr.date": "2019/04/27
            # "ups.model": "Back-UPS XS 700U
            # "ups.productid": "0002
            "ups.realpower.nominal": ["nominal_power", int],
            # "ups.serial": "3B1917X69838
            "ups.status": ["ups_status", str],
            # "ups.test.result": "No test initiated
            # "ups.timer.reboot": "0
            # "ups.timer.shutdown": "-1
            # "ups.vendorid": "051d
        }
        self.average_ignores = [
            'TIMESTAMP',
            'time_sec',
            'ups_status',
        ]

    def collect_data(self):
        self.load_data()
        self.add_extra_entries()
        return self.parsed_data

    # @staticmethod
    # def reformat_data(input_dict, dictionary):
    #     output = OrderedDict()
    #     for x in input_dict:
    #         output[dictionary[x['dxsId']]] = x['value']
    #     return output

    # @retry(tries=2, delay=0)
    def load_data(self):
        data = OrderedDict()
        self.nut._connect()
        self.timestamp = datetime.now(self.tz)
        for key in self.id_fields.keys():
            data[self.id_fields[key][0]] = self.id_fields[key][1](self.nut.get_var(self.nut_name, key))
            # print(self.nut.var_description(self.nut_name,key))
            # try:
            #     print(str(self.nut.list_range(self.nut_name,key)))
            # except:
            #     pass
        self.parsed_data = data

    def add_extra_entries(self):
        self.parsed_data['time_sec'] = int(self.timestamp.timestamp())
        self.parsed_data['TIMESTAMP'] = datetime.fromtimestamp(
            self.parsed_data['time_sec'], self.tz).strftime('%Y-%m-%d %H:%M:%S')

    def correct_data(self):
        pass