python-diamond/Diamond

ApcupsdCollector also working with apcaccess binary instead of network

Opened this issue · 0 comments

FYI, ApcupsdCollector is also working locally if there is an apcaccess binary on your system:

--- /usr/local/share/diamond/collectors/apcupsd/apcupsd.py2     2019-06-04 17:16:21.211027758 +0200
+++ /usr/local/share/diamond/collectors/apcupsd/apcupsd.py      2019-06-04 17:16:57.163027758 +0200
@@ -18,6 +18,7 @@
 from struct import pack
 import re
 import time
+import subprocess
 
 
 class ApcupsdCollector(diamond.collector.Collector):
@@ -51,19 +52,22 @@
 
     def getData(self):
         # Get the data via TCP stream
-        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-        s.connect((self.config['hostname'], int(self.config['port'])))
+        #s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        #s.connect((self.config['hostname'], int(self.config['port'])))
 
         # Packet is pad byte, size byte, and command
-        s.send(pack('xb6s', 6, 'status'))
+        #s.send(pack('xb6s', 6, 'status'))
 
         # Ditch the header
-        s.recv(1024)
-        time.sleep(.25)
-        data = s.recv(4096)
+        #s.recv(1024)
+        #time.sleep(.25)
+        #data = s.recv(4096)
 
         # We're done. Close the socket  
-        s.close()  
+        #s.close()
+
+        out_bytes = subprocess.check_output(['apcaccess'])
+        data = out_bytes.decode('utf-8')
         return data
 
     def collect(self):
@@ -72,7 +76,7 @@
 
         data = self.getData()
 
-        data = data.split('\n\x00')
+        data = data.split('\n')
         for d in data:
             matches = re.search("([A-Z]+)\s+:\s+(.*)$", d)
             if matches: