Skip to content

Commit

Permalink
Plex webhook fixes. (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnewhall authored Mar 26, 2021
1 parent 2c1888c commit 0299b5d
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ jobs:
- os: osx
osx_image: xcode12
language: go
go: 1.15.x
go: 1.16.x
- os: linux
dist: bionic
services: docker
language: go
go: 1.15.x
go: 1.16.x
git:
depth: false
addons:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/Go-Lift-TV/discordnotifier-client

go 1.15
go 1.16

require (
github.com/BurntSushi/toml v0.3.1
Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import (
)

func main() {
if err := run(); err != nil {
setup()

if err := client.Start(); err != nil {
log.Fatal(err)
}
}

func run() error {
func setup() {
ui.HideConsoleWindow()
// setup log package in case we throw an error for main.go before logging is setup.
log.SetFlags(log.LstdFlags)
Expand All @@ -26,8 +28,6 @@ func run() error {
if err := setTimeZone(os.Getenv("TZ")); err != nil {
log.Print(err)
}

return client.Start()
}

func setTimeZone(tz string) (err error) {
Expand Down
1 change: 1 addition & 0 deletions pkg/client/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func start() error {
func (c *Client) run(newConfig bool) error {
if c.Flags.testSnaps {
c.checkPlex()
c.Config.Snapshot.Validate()
c.logSnaps()

return nil
Expand Down
20 changes: 18 additions & 2 deletions pkg/snapshot/smartctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,17 @@ func getParts(ctx context.Context) ([]string, error) {
}

func (s *Snapshot) getDiskData(ctx context.Context, disk string, useSudo bool) error { //nolint: cyclop
cmd, stdout, wg, err := readyCommand(ctx, useSudo, "smartctl", "-A", disk)
if strings.HasPrefix(disk, "/dev/md") || strings.HasPrefix(disk, "/dev/ram") ||
strings.HasPrefix(disk, "/dev/zram") || strings.HasPrefix(disk, "/dev/synoboot") {
return nil
}

args := []string{"-A", disk}
if s.synology {
args = []string{"-d", "sat", "-a", disk}
}

cmd, stdout, wg, err := readyCommand(ctx, useSudo, "smartctl", args...)
if err != nil {
return err
}
Expand Down Expand Up @@ -117,14 +127,20 @@ func (s *Snapshot) getDiskData(ctx context.Context, disk string, useSudo bool) e
}

func (s *Snapshot) getDiskHealth(ctx context.Context, disk string, useSudo bool) error {
if strings.HasPrefix(disk, "/dev/md") || strings.HasPrefix(disk, "/dev/ram") ||
strings.HasPrefix(disk, "/dev/zram") || strings.HasPrefix(disk, "/dev/synoboot") {
return nil
}

cmd, stdout, wg, err := readyCommand(ctx, useSudo, "smartctl", "-H", disk)
if err != nil {
return err
}

go func() {
for stdout.Scan() {
if text := stdout.Text(); strings.Contains(text, "self-assessment ") {
if text := stdout.Text(); strings.Contains(text, "self-assessment ") ||
strings.Contains(text, "SMART Health Status:") {
s.DiskHealth[disk] = text[strings.LastIndex(text, " ")+1:]
}
}
Expand Down
15 changes: 13 additions & 2 deletions pkg/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"sync"
"time"
Expand All @@ -24,14 +25,15 @@ const (
type Config struct {
Timeout cnfg.Duration `toml:"timeout"` // total run time allowed.
Interval cnfg.Duration `toml:"interval"` // how often to send snaps (cron).
ZFSPools []string `toml:"zfs_pools"` // zfs pools to monitor.
UseSudo bool `toml:"use_sudo"` // use sudo for smartctl commands.
Raid bool `toml:"monitor_raid"` // include mdstat and/or megaraid.
DriveData bool `toml:"monitor_drives"` // smartctl commands.
DiskUsage bool `toml:"monitor_space"` // get disk usage.
Uptime bool `toml:"monitor_uptime"` // all system stats.
CPUMem bool `toml:"monitor_cpuMemory"` // cpu perct and memory used/free.
CPUTemp bool `toml:"monitor_cpuTemp"` // not everything supports temps.
ZFSPools []string `toml:"zfs_pools"` // zfs pools to monitor.
synology bool
}

// Errors this package generates.
Expand Down Expand Up @@ -60,6 +62,7 @@ type Snapshot struct {
DiskUsage map[string]*Partition `json:"diskUsage,omitempty"`
DiskHealth map[string]string `json:"driveHealth,omitempty"`
ZFSPool map[string]*Partition `json:"zfsPools,omitempty"`
synology bool
}

// RaidData contains raid information from mdstat and/or megacli.
Expand All @@ -85,6 +88,10 @@ func (c *Config) Validate() {
} else if c.Interval.Duration < minimumInterval {
c.Interval.Duration = minimumInterval
}

if _, err := os.Stat(synologyConf); err == nil {
c.synology = true
}
}

// GetSnapshot returns a system snapshot based on requested data in the config.
Expand All @@ -98,7 +105,7 @@ func (c *Config) GetSnapshot() (*Snapshot, []error, []error) {
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout.Duration)
defer cancel()

s := &Snapshot{}
s := &Snapshot{synology: c.synology}
errs, debug := c.getSnapshot(ctx, s)

return s, errs, debug
Expand All @@ -111,6 +118,10 @@ func (c *Config) getSnapshot(ctx context.Context, s *Snapshot) ([]error, []error
errs = append(errs, err...)
}

if err := s.GetSynology(c.Uptime); err != nil {
errs = append(errs, err)
}

if err := s.getDisksUsage(ctx, c.DiskUsage); len(err) != 0 {
errs = append(errs, err...)
}
Expand Down
89 changes: 89 additions & 0 deletions pkg/snapshot/synology.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package snapshot

import (
"bufio"
"errors"
"fmt"
"io"
"os"
"strings"
)

const synologyConf = "/etc/synoinfo.conf"

type Synology struct {
Build string `json:"last_admin_login_build"` // 254263
Manager string `json:"manager"` // Synology DiskStation
Vendor string `json:"vender"` // Synology Inc.
Model string `json:"upnpmodelname"` // DS1517+
Version string `json:"udc_check_state"` // 6.2.3
}

/*
"platform": "Synology Inc.",
"platformFamily": "Synology DiskStation DS1517+",
"platformVersion": "6.2.3-254263",
*/

func (s *Snapshot) GetSynology(run bool) error { //nolint:cyclop
if !run || !s.synology {
return nil
}

file, err := os.Open(synologyConf)
if err != nil {
return fmt.Errorf("opening synology conf: %w", err)
}
defer file.Close()

// Start reading from the file with a reader.
var (
reader = bufio.NewReader(file)
syn = &Synology{}
)

for {
line, err := reader.ReadString('\n')
if errors.Is(err, io.EOF) {
break
} else if err != nil {
return fmt.Errorf("reading synology conf: %w", err)
}

lsplit := strings.Split(line, "=")
if len(lsplit) < 2 { //nolint:gomnd
continue
}

switch lsplit[0] {
case "last_admin_login_build":
syn.Build = strings.Trim(lsplit[1], "\n\"")
case "manager":
syn.Manager = strings.Trim(lsplit[1], "\n\"")
case "vender":
syn.Vendor = strings.Trim(lsplit[1], "\n\"")
case "upnpmodelname":
syn.Model = strings.Trim(lsplit[1], "\n\"")
case "udc_check_state":
syn.Version = strings.Trim(lsplit[1], "\n\"")
}
}

s.setSynology(syn)

return nil
}

func (s *Snapshot) setSynology(syn *Synology) {
if s.System.InfoStat.Platform == "" && syn.Vendor != "" {
s.System.InfoStat.Platform = syn.Vendor
}

if s.System.InfoStat.PlatformFamily == "" && syn.Manager != "" {
s.System.InfoStat.PlatformFamily = syn.Manager + " " + syn.Model
}

if s.System.InfoStat.PlatformVersion == "" && syn.Version != "" {
s.System.InfoStat.PlatformVersion = syn.Version + "-" + syn.Build
}
}

0 comments on commit 0299b5d

Please sign in to comment.