dinsdag 14 juli 2015

mpower ubiquiti to Carbon/Grapite

mpower ubiquiti to Carbon/Grapite

Since the graphite interface seems to be a standard in statistics gathering (also supported by opentsdb and influxdb) Its a much nicer setup if we cut out the collectd middleman:


This script will poll the the mpower twice per minute if you put it in cron, it will send the data to to your carbon host using the plaintext protocol (http://graphite.wikidot.com/getting-your-data-into-graphite)

mpower2carbon.sh                                                                                     
#!/bin/bash                                                                     
MPOWERNAME=MPBureau
MPOWERHOST=192.168.1.100                                                       GRAPHITESERVER=192.168.1.125
GRAPHITEPORT=2003
curl -X POST -d "username=admin&password=yourpassword" -b "AIROS_SESSIONID=01234567890123456789012345678901" $MPOWERHOST/login.cgi
for i in 1 2
do
  for MP in 1 2 3
        do
        SENSOR=$(curl -b "AIROS_SESSIONID=01234567890123456789012345678901" $MPOWERHOST/sensors$MP)
          MPPOWER=$(echo $SENSOR | cut -d "," -f3 | cut -d ":" -f2)
            echo "mpower.$MPOWERNAME.power $MPPOWER `date +%s`" | nc -q0 ${GRAPHITESERVER} ${GRAPHITEPORT}

          MPCURRENT=$(echo $SENSOR | cut -d "," -f5 | cut -d ":" -f2)
   echo "mpower.$MPOWERNAME.current $MPCURRENT `date +%s`" | nc -q0 ${GRAPHITESERVER} ${GRAPHITEPORT}

          MPVOLTAGE=$(echo $SENSOR | cut -d "," -f6 | cut -d ":" -f2)
            echo "mpower.$MPOWERNAME.voltage $MPCURRENT `date +%s`" | nc -q0 ${GRAPHITESERVER} ${GRAPHITEPORT}

          MPPOFA=$(echo "scale=2 ; ($(echo $SENSOR | cut -d "," -f7 | cut -d ":" -f2 )*100)" | bc)
            echo "mpower.$MPOWERNAME.powerfactor $MPPOFA `date +%s`" | nc -q0 ${GRAPHITESERVER} ${GRAPHITEPORT}
        done 
  sleep 20
done

woensdag 3 december 2014

Ubiquiti Networks mPower data in collectd

Recently I got a hold of an mPower powerstrip from Ubiquity networks. Pretty neat things,

Remember the remote controlled powertrips from the early 2000's with infrared ? In comes wifi ... And in these days of global warming, they also give you power-measurements for each of the three plugs.
(http://www.ubnt.com/mfi/mpower/)

Anyway, enough marketing talk, ...
When you buy your strip you can go 2 routes, either install the bundled software (windlows, osx, and linux), or you can start tinkering. Obviously I want for the second option.

Plug the thing in, and look for a wifi network called ubnt, connect to it.
Open a browser and go to https://192.168.2.20 ; your'll get a certificate warning, ignore it ...
This is where you configure the wifi settings. Once thats done , reboot the mpower and you should see a new client on your lan. Give it a static DHCP reservation , this'll save you a ton of headaches lateron.
Setting username and password was in there somewhere too :)

Next you can surf to your new mPower strip and you should be able to check the measurements.

Now the magic part ... getting this data into Collectd:

First of , CollectD has a datatype for everything we're measuring here. There's voltage(V) Current(A) power(W), the last one is the power factor, there's no unit for that one , but its a cosine of the angle between U(v) and I(a), so it is always between 0and 1, perfect for a percentage.

After some googling I founf out you can curl yourself past the login, and get to the data, It looks like json but the collectd curl_json plugin didn't work for me , so i parsed it using  a small bash script.

This script is loaded into cron, but as cron has a maximum resolution of 1 minute. i have it sleep half a minute and do it's thing again. This way collectd has data every 30 secs.
#!/bin/bash
MPOWERHOST=192.168.1.100
curl -X POST -d "username=admin&password=mypassword" -b "AIROS_SESSIONID=01234567890123456789012345678901" $MPOWERHOST/login.cgi
for i in 1 2
do
  for MP in 1 2 3
        do  
        SENSOR=$(curl -b "AIROS_SESSIONID=01234567890123456789012345678901" $MPOWERHOST/sensors$MP)
          echo $SENSOR | cut -d "," -f3 | cut -d ":" -f2 > /run/shm/mp${MP}power
          echo $SENSOR | cut -d "," -f5 | cut -d ":" -f2 > /run/shm/mp${MP}current
          echo $SENSOR | cut -d "," -f6 | cut -d ":" -f2 > /run/shm/mp${MP}voltage
          echo "scale=2 ; ($(echo $SENSOR | cut -d "," -f7 | cut -d ":" -f2 )*100)" | bc > /run/shm/mp${MP}powerfactor
        done 
  sleep 20
done

That's the data parsed ready for collectd to ingest it with the next piece of config for the curl plugin.

<Plugin curl>

  <Page "mPower">

    URL "file:///run/shm/mp1power"
      <Match>
        Regex "([0-9]*)"
        DSType "GaugeLast"
        Type "power"
        Instance "Power1"
      </Match>
  </Page>
  <Page "mPower">
    URL "file:///run/shm/mp2power"
      <Match>
        Regex "([0-9]*)"
        DSType "GaugeLast"
        Type "power"
        Instance "Power2"
      </Match>
  </Page>
  <Page "mPower">
    URL "file:///run/shm/mp3power"
      <Match>
        Regex "([0-9]*)"
        DSType "GaugeLast"
        Type "power"
        Instance "Power3"
      </Match>
  </Page>

  <Page "mCurrent">
    URL "file:///run/shm/mp1current"
      <Match>
        Regex "([0-9]*\\.[0-9]+)" 
        DSType "GaugeLast"
        Type "current"
        Instance "Current1"
      </Match>
  </Page>
  <Page "mCurrent">
    URL "file:///run/shm/mp2current"
      <Match>
        Regex "([0-9]*\\.[0-9]+)" 
        DSType "GaugeLast"
        Type "current"
        Instance "Current2"
      </Match>
  </Page>
  <Page "mCurrent">
    URL "file:///run/shm/mp3current"
      <Match>
        Regex "([0-9]*\\.[0-9]+)" 
        DSType "GaugeLast"
        Type "current"
        Instance "Current3"
      </Match>
  </Page>

  <Page "mVoltage">
    URL "file:///run/shm/mp1voltage"
      <Match>
        Regex "([0-9]*)"
        DSType "GaugeLast"
        Type "voltage"
        Instance "Voltage1"
      </Match>
  </Page>
  <Page "mVoltage">
    URL "file:///run/shm/mp2voltage"
      <Match>
        Regex "([0-9]*)"
        DSType "GaugeLast"
        Type "voltage"
        Instance "Voltage2"
      </Match>
  </Page>
  <Page "mVoltage">
    URL "file:///run/shm/mp3voltage"
      <Match>
        Regex "([0-9]*)"
        DSType "GaugeLast"
        Type "voltage"
        Instance "Voltage3"
      </Match>
  </Page>

  <Page "mPowerfactor">

    URL "file:///run/shm/mp1powerfactor"
      <Match>
        Regex "([0-9]*\\.[0-9]+)"
        DSType "GaugeLast"
        Type "percent"
        Instance "PowerFactor1"
      </Match>
  </Page>
  <Page "mPowerfactor">
    URL "file:///run/shm/mp2powerfactor"
      <Match>
        Regex "([0-9]*\\.[0-9]+)"
        DSType "GaugeLast"
        Type "percent"
        Instance "PowerFactor2"
      </Match>
  </Page>
  <Page "mPowerfactor">
    URL "file:///run/shm/mp3powerfactor"
      <Match>
        Regex "([0-9]*\\.[0-9]+)"
        DSType "GaugeLast"
        Type "percent"
        Instance "PowerFactor3"
      </Match>
  </Page>
</Plugin>

Once all that's done ...
... you get graphs !
graphgraphgraphgraph