Skip to content

Commit

Permalink
Fix computersystems field (#70)
Browse files Browse the repository at this point in the history
* fix graceful shutdown logic in main func, improve unmarshalling logic for different possible json responses

* update CHANGELOG and README
  • Loading branch information
derrick-dacosta authored Apr 25, 2024
1 parent c928ebf commit 0d7bc0e
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 78 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ log is based on the [Keep a CHANGELOG](http://keepachangelog.com/) project.
- Thermal Summary, Power total consumed for Cisco servers and Chassis, memory metrics for Gen9 server models [#53](https://github.com/Comcast/fishymetrics/issues/53)
- Firmware gathering endpoint update and add device info to other HP models [#55](https://github.com/Comcast/fishymetrics/issues/55)
- C220 drive metrics on hosts with fw < 4.0, psu metrics result and label values [#57](https://github.com/Comcast/fishymetrics/issues/57)
- Chassis ComputerSystems field is handled improperly [#68](https://github.com/Comcast/fishymetrics/issues/68)

## Updated

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This app can support any chassis that has the redfish API available. If one need

To run it:

```bash
```
$ ./fishymetrics --help
usage: fishymetrics [<flags>]
Expand Down Expand Up @@ -57,7 +57,7 @@ Flags:

Or set the following ENV Variables:

```bash
```
BMC_USERNAME=<string>
BMC_PASSWORD=<string>
BMC_TIMEOUT=<duration> (Default: 15s)
Expand Down
31 changes: 20 additions & 11 deletions cmd/fishymetrics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"html/template"
"io"
logg "log"
"net"
"net/http"
"os"
"os/signal"
Expand Down Expand Up @@ -319,17 +320,27 @@ func main() {
Handler: loggingHandler(mux),
}

wg.Add(1)
go func() {
defer wg.Done()

if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Error("starting "+app+" service failed", zap.Error(err))
}
}()

signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)

listener, err := net.Listen("tcp4", ":"+*exporterPort)
if err != nil {
log.Error("starting "+app+" service failed", zap.Error(err))
signals <- syscall.SIGTERM
} else {
wg.Add(1)
go func() {
defer wg.Done()

if err := srv.Serve(listener); err != nil && err != http.ErrServerClosed {
log.Error("http server received an error", zap.Error(err))
signals <- syscall.SIGTERM
}
}()

log.Info("started " + app + " service")
}

wg.Add(1)
go func() {
defer wg.Done()
Expand All @@ -346,8 +357,6 @@ func main() {
doneRenew <- true
}()

log.Info("started " + app + " service")

wg.Wait()
}

Expand Down
34 changes: 22 additions & 12 deletions oem/chassis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package oem

import (
"bytes"
"encoding/json"
)

Expand Down Expand Up @@ -62,25 +61,36 @@ type LinksWrapper struct {

func (w *LinksWrapper) UnmarshalJSON(data []byte) error {
// because of a change in output between firmware versions we need to account for this
if bytes.Compare([]byte("[{"), data[0:2]) == 0 {
var linksTmp []struct {
URL string `json:"@odata.id,omitempty"`
HRef string `json:"href,omitempty"`
}
err := json.Unmarshal(data, &linksTmp)
if len(linksTmp) > 0 {
for _, l := range linksTmp {
// try to unmarshal as a slice of structs
// [
// {
// "@odata.id": "/redfish/v1/Systems/XXXX"
// }
// ]
var links []struct {
URL string `json:"@odata.id,omitempty"`
HRef string `json:"href,omitempty"`
}
err := json.Unmarshal(data, &links)
if err == nil {
if len(links) > 0 {
for _, l := range links {
if l.URL != "" {
w.LinksURLSlice = append(w.LinksURLSlice, l.URL)
} else {
w.LinksURLSlice = append(w.LinksURLSlice, l.HRef)
}
}
return nil
}
return err
} else {
// try to unmarshal as a slice of strings
// [
// "/redfish/v1/Systems/XXXX"
// ]
return json.Unmarshal(data, &w.LinksURLSlice)
}
return json.Unmarshal(data, &w.LinksURLSlice)

return nil
}

type ChassisStorageBattery struct {
Expand Down
35 changes: 24 additions & 11 deletions oem/drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package oem

import (
"bytes"
"encoding/json"
)

Expand Down Expand Up @@ -81,22 +80,36 @@ type LocationWrapper struct {

func (w *LocationWrapper) UnmarshalJSON(data []byte) error {
// because of a change in output between firmware versions we need to account for this
if bytes.Compare([]byte("[{"), data[0:2]) == 0 {
var locTmp []struct {
Loc string `json:"Info,omitempty"`
}
err := json.Unmarshal(data, &locTmp)
if len(locTmp) > 0 {
for _, l := range locTmp {
// try to unmarshal as a slice of structs
// [
// {
// "Info": "location data"
// }
// ]
var loc []struct {
Loc string `json:"Info,omitempty"`
}
err := json.Unmarshal(data, &loc)
if err == nil {
if len(loc) > 0 {
for _, l := range loc {
if l.Loc != "" {
w.Location = l.Loc
}
}
return nil
}
return err
} else {
// try to unmarshal as a string
// {
// ...
// "Location": "location data"
// ...
// }
return json.Unmarshal(data, &w.Location)
}
return json.Unmarshal(data, &w.Location)

return nil

}

// GenericDrive is used to iterate over differing drive endpoints
Expand Down
46 changes: 28 additions & 18 deletions oem/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package oem

import (
"bytes"
"encoding/json"
)

Expand Down Expand Up @@ -45,26 +44,37 @@ type ServerManagerURLWrapper struct {
}

func (w *ServerManagerURLWrapper) UnmarshalJSON(data []byte) error {
// because of a change in output betwen c220 firmware versions we need to account for this
if bytes.Compare([]byte("[{"), data[0:2]) == 0 {
var serMgrTmp []struct {
UrlLinks string `json:"@odata.id,omitempty"`
Urllinks string `json:"href,omitempty"`
}
err := json.Unmarshal(data, &serMgrTmp)
if len(serMgrTmp) > 0 {
s := make([]string, 0)
if serMgrTmp[0].UrlLinks != "" {
s = append(s, serMgrTmp[0].UrlLinks)
} else {
s = append(s, serMgrTmp[0].Urllinks)
// because of a change in output between firmware versions we need to account for this
// try to unmarshal as a slice of structs
// [
// {
// "@odata.id": "/redfish/v1/Systems/XXXX"
// }
// ]
var svrMgr []struct {
URL string `json:"@odata.id,omitempty"`
HRef string `json:"href,omitempty"`
}
err := json.Unmarshal(data, &svrMgr)
if err == nil {
if len(svrMgr) > 0 {
for _, l := range svrMgr {
if l.URL != "" {
w.ServerManagerURLSlice = append(w.ServerManagerURLSlice, l.URL)
} else {
w.ServerManagerURLSlice = append(w.ServerManagerURLSlice, l.HRef)
}
}
w.ServerManagerURLSlice = s
return nil
}
return err
} else {
// try to unmarshal as a slice of strings
// [
// "/redfish/v1/Systems/XXXX"
// ]
return json.Unmarshal(data, &w.ServerManagerURLSlice)
}
return json.Unmarshal(data, &w.ServerManagerURLSlice)

return nil
}

type IloSelfTest struct {
Expand Down
21 changes: 10 additions & 11 deletions oem/power.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package oem

import (
"bytes"
"encoding/json"
)

Expand Down Expand Up @@ -50,19 +49,19 @@ type PowerControlWrapper struct {
}

func (w *PowerControlWrapper) UnmarshalJSON(data []byte) error {
// because of a change in output betwen firmware versions we need to account for this
if bytes.Compare([]byte("{"), data[0:1]) == 0 {
var powCtlSlice PowerControl
err := json.Unmarshal(data, &powCtlSlice)
if err != nil {
return err
}
// because of a change in output between firmware versions we need to account for this
// PowerControl can either be []PowerControl or a singular PowerControl
var powCtl PowerControl
err := json.Unmarshal(data, &powCtl)
if err == nil {
s := make([]PowerControl, 0)
s = append(s, powCtlSlice)
s = append(s, powCtl)
w.PowerControl = s
return nil
} else {
return json.Unmarshal(data, &w.PowerControl)
}
return json.Unmarshal(data, &w.PowerControl)

return nil
}

// PowerMetric contains avg/min/max power metadata
Expand Down
25 changes: 12 additions & 13 deletions oem/storage_ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package oem

import (
"bytes"
"encoding/json"
)

Expand Down Expand Up @@ -48,19 +47,19 @@ type StorageControllerWrapper struct {
}

func (w *StorageControllerWrapper) UnmarshalJSON(data []byte) error {
// because of a change in output betwen c220 firmware versions we need to account for this
if bytes.Compare([]byte("{"), data[0:1]) == 0 {
var storCtlSlice StorageController
err := json.Unmarshal(data, &storCtlSlice)
if err != nil {
return err
}
s := make([]StorageController, 0)
s = append(s, storCtlSlice)
w.StorageController = s
return nil
// because of a change in output between firmware versions we need to account for this
// StorageController can either be []StorageController or a singular StorageController
var storCtl StorageController
err := json.Unmarshal(data, &storCtl)
if err == nil {
sc := make([]StorageController, 0)
sc = append(sc, storCtl)
w.StorageController = sc
} else {
return json.Unmarshal(data, &w.StorageController)
}
return json.Unmarshal(data, &w.StorageController)

return nil
}

type Drive struct {
Expand Down

0 comments on commit 0d7bc0e

Please sign in to comment.