diff --git a/.gitignore b/.gitignore index 522d66e1..a8c71744 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,10 @@ /unifi-poller /*.gz /*.1 +/*.deb +/*.rpm +/*.pkg /vendor .DS_Store *~ +/package_build diff --git a/Makefile b/Makefile index d4fffac1..595d9e59 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,7 @@ all: clean man build clean: for p in $(PACKAGES); do rm -f `echo $${p}|cut -d/ -f3`{,.1,.1.gz}; done + rm -rf package_build unifi-poller_*.deb unifi-poller-*.rpm unifi-poller-*.pkg build: for p in $(PACKAGES); do go build -ldflags "-w -s" $${p}; done @@ -12,41 +13,30 @@ build: linux: for p in $(PACKAGES); do GOOS=linux go build -ldflags "-w -s" $${p}; done -install: man test build - @echo "If you get errors, you may need sudo." - # Install binary. binary. - GOBIN=/usr/local/bin go install -ldflags "-w -s" ./... - # Making config folders and installing man page. - mkdir -p /usr/local/etc/$(BINARY) /usr/local/share/man/man1 - mv *.1.gz /usr/local/share/man/man1 - # Installing config file, man page and launch agent or systemd unit file. - test -f /usr/local/etc/$(BINARY)/up.conf || cp up.conf.example /usr/local/etc/$(BINARY)/up.conf - test -d ~/Library/LaunchAgents && cp startup/launchd/com.github.davidnewhall.$(BINARY).plist ~/Library/LaunchAgents || true - test -d /etc/systemd/system && cp startup/systemd/$(BINARY).service /etc/systemd/system || true - # Making systemd happy by telling it to reload. - test -x /bin/systemctl && /bin/systemctl --system daemon-reload || true - @echo - @echo "Installation Complete. Edit the config file @ /usr/local/etc/$(BINARY)/up.conf " - @echo "Then start the daemon with:" - @test -d ~/Library/LaunchAgents && echo " launchctl load ~/Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist" || true - @test -d /etc/systemd/system && echo " sudo /bin/systemctl start $(BINARY)" || true - @echo "Examine the log file at: /usr/local/var/log/$(BINARY).log (logs may go elsewhere on linux, check syslog)" - -uninstall: - @echo "If you get errors, you may need sudo." - # Stopping the daemon - test -x /bin/systemctl && /bin/systemctl stop $(BINARY) || true - test -x /bin/launchctl && /bin/launchctl unload ~/Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist || true - # Deleting config file, binary, man page, launch agent or unit file. - rm -rf /usr/local/{etc,bin}/$(BINARY) /usr/local/share/man/man1/$(BINARY).1.gz - rm -f ~/Library/LaunchAgents/com.github.davidnewhall.$(BINARY).plist - rm -f /etc/systemd/system/$(BINARY).service - # Making systemd happy by telling it to reload. - test -x /bin/systemctl && /bin/systemctl --system daemon-reload || true +darwin: + for p in $(PACKAGES); do GOOS=darwin go build -ldflags "-w -s" $${p}; done test: lint for p in $(PACKAGES) $(LIBRARYS); do go test -race -covermode=atomic $${p}; done +man: + scripts/build_manpages.sh ./ + +rpm: clean man linux + scripts/build_linux_packages.sh rpm + +deb: clean man linux + scripts/build_linux_packages.sh deb + +osxpkg: clean man darwin + scripts/build_osx_package.sh + +install: all + scripts/local_install.sh + +uninstall: + scripts/local_uninstall.sh + lint: goimports -l $(PACKAGES) gofmt -l $(PACKAGES) @@ -54,8 +44,5 @@ lint: golint $(PACKAGES) go vet $(PACKAGES) -man: - script/build_manpages.sh ./ - deps: dep ensure -update diff --git a/README.md b/README.md index 6621065c..6d5204b4 100644 --- a/README.md +++ b/README.md @@ -43,13 +43,6 @@ I only, personally, run this on a Mac 10.13.something. I know others are using Linux and it's working, but I need more feedback. Does the unit file work? Are you able to stop and start the service? Does the Makefile do the right things? -- I probably suck at InfluxDB. - -I don't know what should be a tag and what should be a field. I think -I did my best, but there's certainly room for improvements in both -the data input and the Grafana graphs (output). I'm always iterating, but -if you find a deficiency or something that can be improved, let me know. - - Are there other devices that need to be included? I have: switch, router, access point. Three total, and the type structs are @@ -64,11 +57,6 @@ Issue and lets discuss. If you're a nerd you can probably figure it out. I'd still like some pretty pictures and maybe even a Twitch VOD. -- Sanity Checking - -Did I actually graph the right data in the right way? Some validation would -be nice. - - Radios, Frequencies, Interfaces, vAPs My access points only seem to have two radios, one interface and vAP per radio. @@ -84,12 +72,12 @@ isolate why this happens. We may need to issue a reconnect and get a new cookie. Here's a picture of the Client dashboard. -![image](images/unifi-clients-dashboard.png?raw=true) +![image](examples/unifi-clients-dashboard.png?raw=true) Here's a picture of the UAP dashboard. This only shows one device, but you can select multiple to put specific stats side-by-side. -![image](images/unifi-uap-dashboard.png?raw=true) +![image](examples/unifi-uap-dashboard.png?raw=true) ## Copyright & License diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 00000000..6a66a486 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,8 @@ +# Grafana Dashboards & Examples + +This folder contains grafana dashboards to get you started with the new data pool. +Import these into Grafana to quickly visualize data from your devices. + +They may/do use a few plugins: Clock, Discrete, Singlestat, Table + +This folder also contains an example configuration file and some dashboard screenshots. diff --git a/images/unifi-clients-dashboard.png b/examples/unifi-clients-dashboard.png similarity index 100% rename from images/unifi-clients-dashboard.png rename to examples/unifi-clients-dashboard.png diff --git a/grafana-dashboards/unifi-clients-grafana-dash.json b/examples/unifi-clients-grafana-dash.json similarity index 100% rename from grafana-dashboards/unifi-clients-grafana-dash.json rename to examples/unifi-clients-grafana-dash.json diff --git a/images/unifi-uap-dashboard.png b/examples/unifi-uap-dashboard.png similarity index 100% rename from images/unifi-uap-dashboard.png rename to examples/unifi-uap-dashboard.png diff --git a/grafana-dashboards/unifi-uap-grafana-dash.json b/examples/unifi-uap-grafana-dash.json similarity index 100% rename from grafana-dashboards/unifi-uap-grafana-dash.json rename to examples/unifi-uap-grafana-dash.json diff --git a/grafana-dashboards/unifi-usg-grafana-dash.json b/examples/unifi-usg-grafana-dash.json similarity index 100% rename from grafana-dashboards/unifi-usg-grafana-dash.json rename to examples/unifi-usg-grafana-dash.json diff --git a/up.conf.example b/examples/up.conf.example similarity index 100% rename from up.conf.example rename to examples/up.conf.example diff --git a/grafana-dashboards/README.md b/grafana-dashboards/README.md deleted file mode 100644 index 565178b7..00000000 --- a/grafana-dashboards/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# Grafana Dashboards - -Import these into Grafana to quickly visualize data from your devices. - -They may/do use a few plugins: Clock, Discrete, Singlestat, Table diff --git a/startup/launchd/com.github.davidnewhall.unifi-poller.plist b/init/launchd/com.github.davidnewhall.unifi-poller.plist similarity index 100% rename from startup/launchd/com.github.davidnewhall.unifi-poller.plist rename to init/launchd/com.github.davidnewhall.unifi-poller.plist diff --git a/startup/systemd/unifi-poller.service b/init/systemd/unifi-poller.service similarity index 91% rename from startup/systemd/unifi-poller.service rename to init/systemd/unifi-poller.service index 4576bae8..ba7d1bd7 100644 --- a/startup/systemd/unifi-poller.service +++ b/init/systemd/unifi-poller.service @@ -1,4 +1,3 @@ -# untested, feedback welcomed. [Unit] Description=Unifi Poller - Ubiquiti Metrics->InfluxDB After=network.target diff --git a/scripts/after-install-osx.sh b/scripts/after-install-osx.sh new file mode 100755 index 00000000..bc2ad33c --- /dev/null +++ b/scripts/after-install-osx.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +# This file is used by osxpkg packages. FPM use. + +# Copy the config file into place if it does not exist. +if [ ! -f /usr/local/etc/unifi-poller/up.conf ] && [ -f /usr/local/etc/unifi-poller/up.conf.example ]; then + cp /usr/local/etc/unifi-poller/up.conf.example /usr/local/etc/unifi-poller/up.conf +fi + +# Allow admins to change the configuration and write logs. +chgrp -R admin /usr/local/etc/unifi-poller +chmod -R g+wr /usr/local/etc/unifi-poller + +# Make sure admins can write logs. +chgrp admin /usr/local/var/log +chmod g=rwx /usr/local/var/log + +# This starts it as root. no no no .... not sure how to fix that. +# launchctl load /Library/LaunchAgents/com.github.davidnewhall.unifi-poller.plist diff --git a/scripts/after-install.sh b/scripts/after-install.sh new file mode 100755 index 00000000..2fb3cd87 --- /dev/null +++ b/scripts/after-install.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# This file is used by rpm and deb packages. FPM use. + +systemctl daemon-reload +systemctl enable unifi-poller +systemctl restart unifi-poller diff --git a/scripts/before-remove.sh b/scripts/before-remove.sh new file mode 100755 index 00000000..a1d4895e --- /dev/null +++ b/scripts/before-remove.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +# This file is used by rpm and deb packages. FPM use. + +systemctl stop unifi-poller +systemctl disable unifi-poller diff --git a/scripts/build_linux_packages.sh b/scripts/build_linux_packages.sh new file mode 100755 index 00000000..b579609c --- /dev/null +++ b/scripts/build_linux_packages.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +# This script builds a deb or rpm package. Run by the Makefile. +# Use: `make rpm` or `make deb` + +OUTPUT=$1 +BINARY=unifi-poller +VERSION=$(git tag -l --merged | tail -n1 | tr -d v) + +if [ "$OUTPUT" != "deb" ] && [ "$OUTPUT" != "rpm" ]; then + echo "first argument must be 'deb' or 'rpm'" + exit 1 +fi + +fpm -h > /dev/null 2>&1 +if [ "$?" != "0" ]; then + echo "fpm missing. Install fpm: https://fpm.readthedocs.io/en/latest/installing.html" + exit 1 +fi + +echo "Building '${OUTPUT}' package." + +PREFIX= +BINFIX=/usr + +# Make a build environment. +rm -rf package_build +mkdir -p package_build${BINFIX}/bin package_build${PREFIX}/etc/${BINARY} package_build${BINFIX}/share/man/man1 + +# Copy the binary, config file and man page into the env. +cp ${BINARY} package_build${BINFIX}/bin +cp *.1.gz package_build${BINFIX}/share/man/man1 +cp examples/up.conf.example package_build${PREFIX}/etc/${BINARY}/up.conf + +# Fix the paths in the systemd unit file before copying it into the emv. +mkdir -p package_build/lib/systemd/system +sed "s#ExecStart.*#ExecStart=${BINFIX}/bin/${BINARY} --config=${PREFIX}/etc/${BINARY}/up.conf#" \ + init/systemd/unifi-poller.service > package_build/lib/systemd/system/${BINARY}.service + +fpm -s dir -t ${OUTPUT} \ + --name ${BINARY} \ + --version ${VERSION} \ + --after-install scripts/after-install.sh \ + --before-remove scripts/before-remove.sh \ + --chdir package_build diff --git a/script/build_manpages.sh b/scripts/build_manpages.sh similarity index 88% rename from script/build_manpages.sh rename to scripts/build_manpages.sh index 19493dd9..cf223e1b 100755 --- a/script/build_manpages.sh +++ b/scripts/build_manpages.sh @@ -1,5 +1,6 @@ #!/bin/bash +# This file uses ronn to build a man page for unifi-poller. set -o pipefail OUTPUT=$1 diff --git a/scripts/build_osx_package.sh b/scripts/build_osx_package.sh new file mode 100755 index 00000000..a0af56f9 --- /dev/null +++ b/scripts/build_osx_package.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +# This script builds a simple macos Installer pkg. Run by the Makefile. +# Use: `make osx` + +OUTPUT=$1 +BINARY=unifi-poller +VERSION=$(git tag -l --merged | tail -n1 | tr -d v) + +fpm -h > /dev/null 2>&1 +if [ "$?" != "0" ]; then + echo "fpm missing. Install fpm: https://fpm.readthedocs.io/en/latest/installing.html" + exit 1 +fi + +echo "Building 'osxpkg' package." + +PREFIX=/usr/local +BINFIX=/usr/local + +# Make a build environment. +rm -rf package_build +mkdir -p package_build${BINFIX}/bin package_build${PREFIX}/etc/${BINARY} package_build${BINFIX}/share/man/man1 +mkdir -p package_build${PREFIX}/var/log + +# Copy the binary, config file and man page into the env. +cp ${BINARY} package_build${BINFIX}/bin +cp *.1.gz package_build${BINFIX}/share/man/man1 +cp examples/up.conf.example package_build${PREFIX}/etc/${BINARY}/ + +# Copy in launch agent. +mkdir -p package_build/Library/LaunchAgents +cp init/launchd/com.github.davidnewhall.unifi-poller.plist package_build/Library/LaunchAgents/ + +# Make a package. +fpm -s dir -t osxpkg \ + --name ${BINARY} \ + --version ${VERSION} \ + --after-install scripts/after-install-osx.sh \ + --osxpkg-identifier-prefix com.github.davidnewhall \ + --chdir package_build diff --git a/scripts/local_install.sh b/scripts/local_install.sh new file mode 100755 index 00000000..74a082b0 --- /dev/null +++ b/scripts/local_install.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +# This script creates a local installation of unifi-poller. +# Recommend using Makefile to invoke: make install +# Supports Linux (systemd only) and macOS. + +BINARY=unifi-poller + +echo "Installing unifi-poller. If you get errors, you may need sudo." + +# Install binary. +GOBIN=/usr/local/bin go install -ldflags "-w -s" ./... + +# Making config folders and installing man page. +mkdir -p /usr/local/etc/${BINARY} /usr/local/share/man/man1 +mv *.1.gz /usr/local/share/man/man1 + +# Installing config file, man page and launch agent or systemd unit file. +if [ ! -f /usr/local/etc/${BINARY}/up.conf ]; then + cp examples/up.conf.example /usr/local/etc/${BINARY}/up.conf +fi +if [ -d ~/Library/LaunchAgents ]; then + cp init/launchd/com.github.davidnewhall.${BINARY}.plist ~/Library/LaunchAgents +fi +if [ -d /etc/systemd/system ]; then + cp init/systemd/${BINARY}.service /etc/systemd/system +fi + +# Making systemd happy by telling it to reload. +if [ -x /bin/systemctl ]; then + /bin/systemctl --system daemon-reload + /bin/systemctl start unifi-poller + /bin/systemctl enable unifi-poller +fi + +echo "Installation Complete. Edit the config file @ /usr/local/etc/${BINARY}/up.conf" +echo "Then start the daemon with:" +if [ -d ~/Library/LaunchAgents ]; then + echo " launchctl load ~/Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist" +fi +if [ -x /bin/systemctl ]; then + echo " sudo /bin/systemctl restart ${BINARY}" +fi +echo "Examine the log file at: /usr/local/var/log/${BINARY}.log (logs may go elsewhere on linux, check syslog)" diff --git a/scripts/local_uninstall.sh b/scripts/local_uninstall.sh new file mode 100755 index 00000000..fd5abce8 --- /dev/null +++ b/scripts/local_uninstall.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +# This script removes a local installation of unifi-poller. +# Recommend using Makefile to invoke: make uninstall +# Supports Linux (systemd only) and macOS. + +BINARY=unifi-poller + +echo "Uninstall unifi-poller. You may need sudo on Linux. Do not use sudo on macOS." + +# Stopping the daemon +if [ -x /bin/systemctl ]; then + /bin/systemctl disable ${BINARY} + /bin/systemctl stop ${BINARY} +fi + +if [ -x /bin/launchctl ] && [ -f ~/Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist ]; then + echo Unloading ~/Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist + /bin/launchctl unload ~/Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist || true +fi + +if [ -x /bin/launchctl ] && [ -f /Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist ]; then + echo Unloading /Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist + /bin/launchctl unload /Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist || true + echo "Delete this file manually: sudo rm -f /Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist" +fi + +# Deleting config file, binary, man page, launch agent or unit file. +rm -rf /usr/local/{etc,bin}/${BINARY} /usr/local/share/man/man1/${BINARY}.1.gz +rm -f ~/Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist +rm -f /etc/systemd/system/${BINARY}.service + +# Making systemd happy by telling it to reload. +if [ -x /bin/systemctl ]; then + /bin/systemctl --system daemon-reload +fi