diff --git a/.gitignore b/.gitignore index 522d66e1..9e41de53 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,9 @@ /unifi-poller /*.gz /*.1 +/*.deb +/*.rpm /vendor .DS_Store *~ +/package_build diff --git a/Makefile b/Makefile index d4fffac1..48149a89 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,11 @@ PACKAGES=`find ./cmd -mindepth 1 -maxdepth 1 -type d` BINARY=unifi-poller -all: clean man build +all: clean test man build clean: for p in $(PACKAGES); do rm -f `echo $${p}|cut -d/ -f3`{,.1,.1.gz}; done + rm -rf build unifi-poller_*.deb unifi-poller-*.rpm build: for p in $(PACKAGES); do go build -ldflags "-w -s" $${p}; done @@ -12,38 +13,6 @@ 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 - test: lint for p in $(PACKAGES) $(LIBRARYS); do go test -race -covermode=atomic $${p}; done @@ -54,8 +23,20 @@ lint: golint $(PACKAGES) go vet $(PACKAGES) -man: - script/build_manpages.sh ./ - deps: dep ensure -update + +man: + scripts/build_manpages.sh ./ + +rpm: all + scripts/build_packages.sh rpm + +deb: all + scripts/build_packages.sh deb + +install: all + scripts/local_install.sh + +uninstall: + scripts/local_uninstall.sh 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 100% rename from startup/systemd/unifi-poller.service rename to init/systemd/unifi-poller.service diff --git a/scripts/after-install.sh b/scripts/after-install.sh new file mode 100755 index 00000000..a59d5e10 --- /dev/null +++ b/scripts/after-install.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +# This file is used by rpm and deb packages. FPM use. + +systemctl daemon-reload +systemctl restart unifi-poller diff --git a/scripts/before-remove.sh b/scripts/before-remove.sh new file mode 100755 index 00000000..40ca0650 --- /dev/null +++ b/scripts/before-remove.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +# This file is used by rpm and deb packages. FPM use. + +systemctl stop unifi-poller 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_packages.sh b/scripts/build_packages.sh new file mode 100755 index 00000000..9cfaa52a --- /dev/null +++ b/scripts/build_packages.sh @@ -0,0 +1,39 @@ +#!/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." + +# Make a build environment. +mkdir -p package_build/usr/bin package_build/etc/${BINARY} package_build/lib/systemd/system package_build/usr/share/man/man1 + +# Copy the binary, config file and man page into the env. +cp ${BINARY} package_build/usr/bin +cp *.1.gz package_build/usr/share/man/man1 +cp examples/up.conf.example package_build/etc/${BINARY}/up.conf + +# Fix the paths in the systemd unit file before copying it into the emv. +sed "s#ExecStart.*#ExecStart=/usr/bin/${BINARY} --config=/etc/${BINARY}/up.conf#" \ + init/systemd/unifi-poller.service > package_build/lib/systemd/system/${BINARY}.service + +fpm -s dir -t ${OUTPUT} \ + -n ${BINARY} \ + -v ${VERSION} \ + --after-install scripts/after-install.sh \ + -C package_build diff --git a/scripts/local_install.sh b/scripts/local_install.sh new file mode 100755 index 00000000..05a4dbce --- /dev/null +++ b/scripts/local_install.sh @@ -0,0 +1,42 @@ +#!/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 +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 [ -d /etc/systemd/system ]; then + echo " sudo /bin/systemctl start ${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..c814e208 --- /dev/null +++ b/scripts/local_uninstall.sh @@ -0,0 +1,27 @@ +#!/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. If you get errors, you may need sudo." + +# Stopping the daemon +if [ -x /bin/systemctl ]; then + /bin/systemctl stop ${BINARY} +fi +if [ -x /bin/launchctl ] && [ -f ~/Library/LaunchAgents/com.github.davidnewhall.${BINARY}.plist ]; then + /bin/launchctl unload ~/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