That-Dude / MCutie

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MCutie

MCutie is a cross platform agent that publishes device metrics to your MQTT broker. It can also receive published commands to execute locally.

I've (lightly) tested it on macOS Big Sur and Windows 10 (x64) , it complies fine for Linux but I've not had a chance to test it yet.

Whilst it works fine for me, this should be considered Alpha software because:

  • I'm a rookie coder! Golang is my first experience at coding in a 'proper' language

  • I've not read up on how to test code yet!

  • I basically use StackExchange to solve all of my problems :-)

Motivation

I developed this agent specifically so that the computers in my local network could publish stats that show up in the HomeAssistant Dashboard:

Home-Assistant-Dashboard-screen-shot.png

You can also execute commands from Home Assistant using the Scripts module like this:

Home-Assistant-Action-screenshot.png

Installation

Clone the repository or download the ZIP file somewhere.

Rename config.yaml.sample to config.yaml

Edit config.yaml with your MQTT broker settings

url: ssl://ha.yourdomain.com:8883
username: mqttuser
password: Your-Passowrd
updateinterval: "9"

Update internal is how frequently you want the agent to publish your computer stats to the MQTT server. I guess in a larger network you might want to increate this to 60 seconds or more to prevent load on your MQTT broker.

MacOS

I've writen a Bash script to install MCutie as a local user service:

chmod +x osx-install.sh

./osx-install.sh -i

To un-install:

./osx-install.sh -u

It will now run at boot and continually attempt to re-start itself after 60 seconds after failure , e.g. you disconnect from your network or your MQTT broker goes offline.

Windows

The program runs and works as expected but I've not decided how best to run it as a user land service yet.

Debugging

MCutie outputs to a log file located in the same folder as the executable file e.g. on MacOS

cat ~/.mcutie/mcutie.log

time="2020-12-23T23:38:23Z" level=info msg="***"
time="2020-12-23T23:38:23Z" level=info msg="*** Start program execution ***"
time="2020-12-23T23:38:23Z" level=info msg="***"
time="2020-12-23T23:38:23Z" level=info msg="Cleaning up hostname to remove suffix (if present)"
time="2020-12-23T23:38:23Z" level=info msg="Safe hostname = DT-MBP13"
time="2020-12-23T23:38:23Z" level=info msg="read data from config.yaml"
time="2020-12-23T23:38:23Z" level=info msg="Connect to MQQT server"
time="2020-12-23T23:38:24Z" level=info msg="Subscribe to 'command' topic"
time="2020-12-23T23:38:24Z" level=info msg="Connected to MQTT server"
time="2020-12-23T23:38:24Z" level=info msg="Subscribed to topic: mcutie/DT-MBP13/command"
time="2020-12-23T23:38:24Z" level=info msg="Publish HA auto-config sensors"
time="2020-12-23T23:38:24Z" level=info msg="Fuction: publishHomeAssistantAutoConfigData hostNameSafe = DT-MBP13"
time="2020-12-23T23:38:24Z" level=info msg="Fuction: publishHomeAssistantAutoConfigData hostNameSafe = DT-MBP13"
time="2020-12-23T23:38:24Z" level=info msg="Fuction: publishHomeAssistantAutoConfigData hostNameSafe = DT-MBP13"
time="2020-12-23T23:38:24Z" level=info msg="Fuction: publishHomeAssistantAutoConfigData hostNameSafe = DT-MBP13"
time="2020-12-23T23:38:24Z" level=info msg="Fuction: publishHomeAssistantAutoConfigData hostNameSafe = DT-MBP13"
time="2020-12-23T23:38:24Z" level=info msg="Fuction: publishHomeAssistantAutoConfigData hostNameSafe = DT-MBP13"
time="2020-12-23T23:38:24Z" level=info msg="Fuction: publishHomeAssistantAutoConfigData hostNameSafe = DT-MBP13"
time="2020-12-23T23:38:24Z" level=info msg="Fuction: publishHomeAssistantAutoConfigData hostNameSafe = DT-MBP13"
time="2020-12-23T23:38:24Z" level=info msg="Fuction: publishHomeAssistantAutoConfigData hostNameSafe = DT-MBP13"
time="2020-12-23T23:38:24Z" level=info msg="Fuction: publishHomeAssistantAutoConfigData hostNameSafe = DT-MBP13"
time="2020-12-23T23:38:24Z" level=info msg="Fuction: publishHomeAssistantAutoConfigData hostNameSafe = DT-MBP13"
time="2020-12-23T23:38:24Z" level=info msg="Fuction: publishHomeAssistantAutoConfigData hostNameSafe = DT-MBP13"
time="2020-12-23T23:38:24Z" level=info msg="Fuction: publishHomeAssistantAutoConfigData hostNameSafe = DT-MBP13"
time="2020-12-23T23:38:24Z" level=info msg="Fuction: publishHomeAssistantAutoConfigData hostNameSafe = DT-MBP13"
time="2020-12-23T23:38:24Z" level=info msg="Fuction: publishHomeAssistantAutoConfigData hostNameSafe = DT-MBP13"
time="2020-12-23T23:38:24Z" level=info msg="Publish device stats in a loop"

Example Home Assistant actions

dt_mbp13_notification_message:
  sequence:

- service: mqtt.publish
  data:
    topic: mcutie/DT-MBP13/command
    payload: '{"prog": "notify", "arg1": "TITLE HERE", "arg2": "Subtitle here", "arg3": "Main message here"}'
  mode: single

dt_mbp13_tts:
  alias: DT-mbp13 Text To Speech
  sequence:
- service: mqtt.publish
  data:
    topic: mcutie/DT-mbp13/command
    payload: '{   "prog": "execute", "arg1": "/usr/bin/say", "arg2": "Super calla frajalistic expee alla dough-sus", "arg3": "" }'
  mode: single

dt_mbp13_vol_mute:
  sequence:
- service: mqtt.publish
  data:
    topic: mcutie/DT-mbp13/command
    payload: '{"prog": "VolMacos", "arg1": "0", "arg2": "", "arg3": ""}'
  mode: single
  
dt_mbp13_vol_50:
  sequence:
  - service: mqtt.publish
    data:
      topic: mcutie/DT-mbp13/command
      payload: '{"prog": "VolMacos", "arg1": "50", "arg2": "", "arg3": ""}'
    mode: single
  
dt_mbp13_vol_100:
  sequence:
- service: mqtt.publish
  data:
    topic: mcutie/DT-mbp13/command
    payload: '{"prog": "VolMacos", "arg1": "100", "arg2": "", "arg3": ""}'
  mode: single

dt_mbp13_display_sleep:
  sequence:
- service: mqtt.publish
  data:
    topic: mcutie/DT-mbp13/command
    payload: '{"prog": "execute", "arg1": "/usr/bin/pmset", "arg2": "displaysleepnow",
  
      "arg3": ""}'
  mode: single

dt_mbp13_kill_safari:
  alias: DT-mbp13 kill a running process
  sequence:
- service: mqtt.publish
  data:
    topic: mcutie/DT-mbp13/command
    payload: '{"prog": "execute", "arg1": "/usr/bin/pkill", "arg2": "-x", "arg3": "Safari"}'
  mode: single

Notes

  1. Did I mention this is alpha software under active development from a rookie coder?

  2. I decided early on that I didn't want MCutie to run as root, it's a big security burden that I didn't want to be responsible for and 99% of what I wanted to achieve can be done running in user space.

Credits

stackexchange.com

github.com/shirou/gopsutil

github.com/sirupsen/logrus

github.com/eclipse/paho.mqtt.golang

github.com/zpatrick/go-config

github.com/deckarep/gosx-notifier

About


Languages

Language:Go 73.2%Language:Shell 20.3%Language:PowerShell 6.5%