Help with apcupsd

Posted on
Tue Sep 10, 2013 9:35 am
durosity offline
User avatar
Posts: 4320
Joined: May 10, 2012
Location: Newcastle Upon Tyne, Ye Ol' England.

Re: Help with apcupsd

berkinet wrote:
Well, so far no bribe has arrived...


It was in that brown envelope i left on your desk.

In all seriousness though if you were planning on making it into a full on plugin with multiple UPS support (i have 4 i'd like to monitor) i'd love to contrubute a little towards it, or perhaps make a donation to your favourite charity if that's more your liking :)

Computer says no.

Posted on
Tue Sep 10, 2013 10:34 am
bschollnick2 offline
Posts: 1355
Joined: Oct 17, 2004
Location: Rochester, Ny

Re: Help with apcupsd

durosity wrote:
berkinet wrote:
Well, so far no bribe has arrived...


It was in that brown envelope i left on your desk.

In all seriousness though if you were planning on making it into a full on plugin with multiple UPS support (i have 4 i'd like to monitor) i'd love to contrubute a little towards it, or perhaps make a donation to your favourite charity if that's more your liking :)


Well, now that I have the possibility of more free time on my hands, I am looking at possibly adding APCUSD support into my plugin. If anyone is interested, please feel free to let me know. My main stumbling block is that I simply don't have a network based UPS to test against.

- Ben

------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33

Posted on
Tue Sep 10, 2013 10:40 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Help with apcupsd

Well, until the daemon works on 10.8 any thought of a plugin is moot. However, when considering a plugin, there is no real advantage over the script. Both are 100% read only facilities. The only functional difference is the script stuffs values into variables and the plugin stuffs values into states. OTOH, a plugin is easier to configure because of the Config GUI.

Ben, while I see what can be done to get the daemon working, you can take a look at the python script I posted - it has the core of what a plugin would need -- really quite simple.

Posted on
Tue Sep 10, 2013 11:23 am
durosity offline
User avatar
Posts: 4320
Joined: May 10, 2012
Location: Newcastle Upon Tyne, Ye Ol' England.

Re: Help with apcupsd

berkinet wrote:
However, when considering a plugin, there is no real advantage over the script. .


Yup, really i just find it a lot tidier. At the moment i have variables which are grouped together in folders per device (e.g. a Sky HD box which outputs the current channel, show name, etc via serial that is then put into variables using a rather poorly made applescript).. the only real problem with this approach is that i’ve got so many variables that whenever i’m modifying a control page it’s a pain finding the particular one i’m looking for. Considering there’s almost 40 fields of information that APCupsd can report on for my current UPS, if i have 4 UPSes that’s well over 100 extra variables to scroll through each time. I’m very slowly learning python and i’m hoping to build some simple little plugins myself in the future to help manage all these bits of info.. but i’m still a fair bit off from being able to do that myself.

I suppose the only other major benefit of having a plugin would be it’s just a bit easier to add multiple devices.. just select the device you want to report on in the GUI

Computer says no.

Posted on
Tue Sep 10, 2013 12:39 pm
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Help with apcupsd

Well, this project is going nowhere fast. apcupsd will not build on 10.8. I have searched to see if others have tried and found nothing.

There are some reports of problems with the binary distribution on 10.8. But, none like I am seeing: the daemon starts and answers 1, or a part of 1, query and the quits.

I'll post a note on the sourceforge page and see if I get a response. But, the traffic there is pretty light so don't hold your breath.

Posted on
Wed Sep 11, 2013 5:14 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Help with apcupsd

Ok, back on track. The problem was actually simple, a short pause was needed between reading the apcupsd headers and the data. I am not sure why that would crash the apcupsd daemon, and I have reported that issue on their mail-list.

Now that I have the code running again, next step is to have it create variables as necessary and find a way to run multiple instances. Both should be easy. After that, maybe a plugin.

YMMV, but this code works for me...
Code: Select all
"""Revised by berkinet from
https://github.com/BrightcoveOS/Diamond/blob/master/src/collectors/apcupsd/apcupsd.py

11/9/13 added small sleep  between data receives
"""
import socket
from struct import pack
import re
import time
import indigo


def getData(apcHostname, apcPort):
    # Get the data via TCP stream
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((apcHostname, apcPort))

    # Packet is pad byte, size byte, and command
    s.send(pack('xb6s', 6, 'status' ))

    # Ditch the header
    s.recv(1024)
    time.sleep(1)

    data = s.recv(4096)

    # We're done. Close the socket
    s.close()
    return data

apcHostname = indigo.variables["apcUpsHost"].value
apcPort = int(indigo.variables["apcUpsPort"].value)

metrics = {}
raw = {}

data = getData(apcHostname, apcPort)

# print data

data = data.split('\n\x00')
for d in data:
    matches = re.search("([A-Z]+)\s+:\s+(.*)$", d)

    if matches:
        value = matches.group(2).strip()
        raw[matches.group(1)] = matches.group(2).strip()

        metrics[matches.group(1)] = value

for metric in metrics:  # config['metrics']:
    value = metrics[metric]

    try:
        print('metric:%s, val:%s' % (metric, value))
        indigo.variable.updateValue(metric, value)
    except:
        pass

Posted on
Wed Sep 11, 2013 8:42 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Help with apcupsd

I wrote:
Now that I have the code running again, next step is to have it create variables as necessary and find a way to run multiple instances. Both should be easy. After that, maybe a plugin.

And, on second thought. I'll just do the plugin. Probably less work in the long run, especially for dealing with the states you wish to see.


BRB

Posted on
Wed Sep 11, 2013 9:59 am
durosity offline
User avatar
Posts: 4320
Joined: May 10, 2012
Location: Newcastle Upon Tyne, Ye Ol' England.

Re: Help with apcupsd

berkinet wrote:
And, on second thought. I'll just do the plugin. Probably less work in the long run, especially for dealing with the states you wish to see.


I just squealed like a schoolgirl. On a side note I'm going to try to become much more self reliant on these matters... might look into some proper python courses.. old ones of course :/

Computer says no.

Posted on
Wed Sep 11, 2013 2:34 pm
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Help with apcupsd

I am very close to releasing a first go at a plugin for the apcupsd daemon. The plugin will query an apcupsd server for details on the state of an attached UPS.

The device configuration will let you select which UPS Report states to use as device states and which state to display in the Devices window state column. I will post more details, as they become available, in the 3rd Party Developers Forum in this thread.

Posted on
Wed Sep 11, 2013 4:04 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Help with apcupsd

So does the APCUPSD the work on 10.8? I have tried to load it a couple of times without success; would be glad to hear there was a reason besides it being "me" again... :roll:

Posted on
Wed Sep 11, 2013 4:12 pm
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Help with apcupsd

Yes. It works fine on 10.8. Perhaps you have some issues with your apcupsd.conf file. Did you edit that for your site specifics.

Who is online

Users browsing this forum: No registered users and 30 guests