python-diamond / Diamond

Diamond is a python daemon that collects system metrics and publishes them to Graphite (and others). It is capable of collecting cpu, memory, network, i/o, load and disk metrics. Additionally, it features an API for implementing custom collectors for gathering metrics from almost any source.
http://diamond.readthedocs.org/
MIT License
1.74k stars 599 forks source link

ApcupsdCollector also working with apcaccess binary instead of network #734

Open pzystorm opened 5 years ago

pzystorm commented 5 years ago

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: