Skip to content

Commit

Permalink
feat: add smart section
Browse files Browse the repository at this point in the history
  • Loading branch information
jon4hz committed Jan 18, 2024
1 parent ec22a49 commit a09a1b0
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 2 deletions.
8 changes: 7 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package config

type Config struct {
Hostname Hostname
Smart Smart
}

type Hostname struct {
Disable bool
Disabled bool
Color string
Figlet bool
FigletFont string
FigletFontDir string
}

type Smart struct {
Disabled bool
Disks []string
}
11 changes: 11 additions & 0 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Context struct {
Sysinfo *SysInfo
Zpool *Zpool
Docker *Docker
Smart *Smart
}

type Runtime struct {
Expand Down Expand Up @@ -84,3 +85,13 @@ type DockerContainer struct {
State string
Healthy bool
}

type Smart struct {
Disks []DiskInfo
}

type DiskInfo struct {
Name string
Temperature uint64
HasError bool
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/jon4hz/gmotd
go 1.21

require (
github.com/anatol/smart.go v0.0.0-20230705044831-c3b27137baa3
github.com/charmbracelet/bubbles v0.17.1
github.com/charmbracelet/lipgloss v0.9.1
github.com/docker/docker v24.0.7+incompatible
Expand Down
13 changes: 12 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOEl
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/Microsoft/go-winio v0.4.14 h1:+hMXMk01us9KgxGb7ftKQt2Xpf5hH/yky+TDA+qxleU=
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
github.com/anatol/smart.go v0.0.0-20230705044831-c3b27137baa3 h1:kAF2MWFD8tyDqD74OQizymjj2cnZAURwSzBrEslCDnI=
github.com/anatol/smart.go v0.0.0-20230705044831-c3b27137baa3/go.mod h1:llkexGSe52bW0OjNva0kvIqGZxfSnVfpKHrnKBI2+pU=
github.com/anatol/vmtest v0.0.0-20220413190228-7a42f1f6d7b8 h1:t4JGeY9oaF5LB4Rdx9e2wARRRPAYt8Ow4eCf5SwO3fA=
github.com/anatol/vmtest v0.0.0-20220413190228-7a42f1f6d7b8/go.mod h1:oPm5wWoqTSkeoPe1Q3sPryTK8o24Jcbwh8dKOiiIobk=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/bubbles v0.17.1 h1:0SIyjOnkrsfDo88YvPgAWvZMwXe26TP6drRvmkjyUu4=
Expand Down Expand Up @@ -41,6 +45,8 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b h1:wDUNC2eKiL35DbLvsDhiblTUXHxcOPwQSCzi7xpQUN4=
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b/go.mod h1:VzxiSdG6j1pi7rwGm/xYI5RbtpBgM8sARDXlvEvxlu0=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -97,12 +103,15 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tklauser/go-sysconf v0.3.9 h1:JeUVdAOWhhxVcU6Eqr/ATFHgXk/mmiItdKeJPev3vTo=
github.com/tklauser/go-sysconf v0.3.9/go.mod h1:11DU/5sG7UexIrp/O6g35hrWzu0JxlwQ3LSFUzyeuhs=
github.com/tklauser/numcpus v0.3.0 h1:ILuRUQBtssgnxw0XXIjKUC56fgnOrFoQQ/4+DeU2biQ=
github.com/tklauser/numcpus v0.3.0/go.mod h1:yFGUr7TUHQRAhyqBcEg0Ge34zDBAsIvJJcyE6boqnA8=
github.com/tmc/scp v0.0.0-20170824174625-f7b48647feef h1:7D6Nm4D6f0ci9yttWaKjM1TMAXrH5Su72dojqYGntFY=
github.com/tmc/scp v0.0.0-20170824174625-f7b48647feef/go.mod h1:WLFStEdnJXpjK8kd4qKLwQKX/1vrDzp5BcDyiZJBHJM=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
Expand All @@ -116,6 +125,8 @@ go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95a
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM=
golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand Down
2 changes: 2 additions & 0 deletions message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/jon4hz/gmotd/context"
"github.com/jon4hz/gmotd/section/docker"
"github.com/jon4hz/gmotd/section/hostname"
"github.com/jon4hz/gmotd/section/smart"
"github.com/jon4hz/gmotd/section/sysinfo"
"github.com/jon4hz/gmotd/section/uptime"
"github.com/jon4hz/gmotd/section/zpool"
Expand All @@ -24,4 +25,5 @@ var Message = []Section{
sysinfo.Section{},
zpool.Section{},
docker.Section{},
smart.Section{},
}
117 changes: 117 additions & 0 deletions section/smart/smart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package smart

import (
"fmt"
"log"

"github.com/anatol/smart.go"
"github.com/charmbracelet/lipgloss"
"github.com/jon4hz/gmotd/context"
"github.com/jon4hz/gmotd/styles"
)

type Section struct{}

func (Section) String() string { return "smart" }

func (Section) Gather(c *context.Context) error {

// tmp
c.Config.Smart.Disks = []string{"/dev/sda", "/dev/sdb", "/dev/sdc", "/dev/sdd", "/dev/sde", "/dev/sdf", "/dev/sdg", "/dev/sdh"}

disk := make([]context.DiskInfo, 0, len(c.Config.Smart.Disks))
for _, d := range c.Config.Smart.Disks {
dev, err := smart.Open(d)
if err != nil {
log.Printf("failed to open smart device: %s", err)
continue
}
defer dev.Close()

a, err := dev.ReadGenericAttributes()
if err != nil {
log.Printf("failed to read generic attributes: %s", err)
continue
}

hasErr := false
switch sm := dev.(type) {
case *smart.SataDevice:
stLog, err := sm.ReadSMARTSelfTestLog()
if err != nil {
log.Printf("failed to read smart self test log: %s", err)
continue
}
for _, s := range stLog.Entry {
if s.Status != 0 {
hasErr = true
}
}
}

disk = append(disk, context.DiskInfo{
Name: d,
Temperature: a.Temperature,
HasError: hasErr,
})
}

c.Smart = &context.Smart{Disks: disk}

return nil
}

const maxTemp = 40

var leftColumnStyle = lipgloss.NewStyle().PaddingRight(4)

func (Section) Print(ctx *context.Context) string {
c := ctx.Smart
if c == nil || len(c.Disks) == 0 {
return ""
}

disks := make([]string, 0, len(c.Disks))
oddAdd := 0
if cap(disks)%2 != 0 {
oddAdd = 1
}

for _, d := range c.Disks {
disks = append(disks, renderDisk(d))
}

leftColumn := lipgloss.JoinVertical(lipgloss.Top, disks[:len(disks)/2+oddAdd]...)
bothColumns := lipgloss.JoinHorizontal(lipgloss.Left,
leftColumnStyle.Render(leftColumn),
lipgloss.JoinVertical(lipgloss.Top,
disks[len(disks)/2+oddAdd:]...,
),
)

return lipgloss.JoinVertical(lipgloss.Top,
"smart status:",
styles.Indent.Render(bothColumns),
)
}

func renderDisk(d context.DiskInfo) string {
return fmt.Sprintf("%s: %s | %s", d.Name, renderTemp(d.Temperature), renderError(d.HasError))
}

func renderTemp(t uint64) string {
if t == 0 {
return styles.Red.Render("--C")
}
if t > maxTemp {
return styles.Red.Render(fmt.Sprintf("%dC", t))
}
return styles.Green.Render(fmt.Sprintf("%dC", t))
}

func renderError(e bool) string {
if e {
return styles.Red.Render("with errors")
}
return styles.Green.Render("without errors")
}

0 comments on commit a09a1b0

Please sign in to comment.