Skip to content
This repository has been archived by the owner on Jul 29, 2020. It is now read-only.

not parsing UPS NUT response strings correctly #4

Open
rezgalis opened this issue Feb 8, 2019 · 3 comments
Open

not parsing UPS NUT response strings correctly #4

rezgalis opened this issue Feb 8, 2019 · 3 comments

Comments

@rezgalis
Copy link

rezgalis commented Feb 8, 2019

On my UPS the regular ups.status response would be "OL CHRG"

When I add ups.status to nut_vars JSON, I get the following error:

VAR ups ups.status "OL CHRG"
Traceback (most recent call last):
File "/etc/nut/influx_nut/influx_nut.py", line 337, in
cli()
File "/etc/nut/influx_nut/influx_nut.py", line 332, in cli
influx_creds=config['influx_creds'])
File "/etc/nut/influx_nut/influx_nut.py", line 274, in update
var_value = info['type'](nut_conn.request_var(nut_ups, var))
File "/etc/nut/influx_nut/influx_nut.py", line 159, in request_var
resp = VarResponse(*(raw_resp.split(' ')))
TypeError: new() takes 5 positional arguments but 6 were given

This suggests that raw_resp is split by space char and this results in splitting "OL CHRG" into two separate elements.

My apologies beforehand, because my Python programming skills are beyond horrible, but the way I fixed it for myself is by rewriting request_var function (lines mentioning / working with list "tmp_resp" are the new ones):

def request_var(self, ups, var) -> str:
	VarResponse = collections.namedtuple('VarResponse',('type', 'ups', 'var', 'value'))
	req = 'GET VAR {} {}'.format(ups, var)
	raw_resp = self.request(req)
	tmp_resp = raw_resp.split(' ')
	if len(tmp_resp)>4:
		value = tmp_resp[3]
		while len(tmp_resp)>4:
			value +='-' + tmp_resp[len(tmp_resp)-1]
			tmp_resp.pop(len(tmp_resp)-1)
		tmp_resp[3] = value
	resp = VarResponse(*(tmp_resp))
	return resp.value.strip('"')
@jValdron
Copy link

jValdron commented Jul 3, 2019

Had the same issue parsing my UPS's model, went in a different direction, did a simple regex. Don't know if it will parse everything or not, but seems to work for my basic needs.

diff --git a/influx_nut.py b/influx_nut.py
index 83ebc1e..d57efcf 100644
--- a/influx_nut.py
+++ b/influx_nut.py
@@ -4,6 +4,7 @@ import time
 import json
 import os
 import sys
+import re
 from typing import Tuple, Mapping, Iterable
 
 import requests
@@ -147,7 +148,8 @@ class NUTConnection:
                                              ('type', 'ups', 'var', 'value'))
         req = 'GET VAR {} {}'.format(ups, var)
         raw_resp = self.request(req)
-        resp = VarResponse(*(raw_resp.split(' ')))
+        matches = re.match(r'(.*) (.*) (.*) "(.*)"', raw_resp, re.I)
+        resp = VarResponse(*(matches.groups()))
         return resp.value.strip('"')

     def request_list(self, thing):

@lf-
Copy link
Owner

lf- commented Jul 4, 2019

@jValdron thanks! I think it's possible to fix this with split() by changing it to split(' ', 3) (I just found out about that optional parameter after reading the docs today). I don't have a UPS which causes this bug so I can't test such a fix. I would gladly accept a pull request to fix this bug if you want to make one.

@jValdron
Copy link

jValdron commented Jul 4, 2019

Thanks @lf-, I'll try out that split(' ', 3) with my UPS!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants