diff --git a/cmd/exporter/main.go b/cmd/exporter/main.go deleted file mode 100644 index 4da2c51..0000000 --- a/cmd/exporter/main.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import ( - "fmt" - "os" - - "git.xbudex.com/buddy/open-hardware-monitor/lib/app" -) - -func main() { - a := app.New() - handleErr(a.RunMetrics(os.Args)) -} - -func handleErr(err error) { - if err == nil { - return - } - fmt.Fprintf(os.Stderr, "%s\n", err) - os.Exit(1) -} diff --git a/cmd/client/main.go b/cmd/ohwm-client/main.go similarity index 87% rename from cmd/client/main.go rename to cmd/ohwm-client/main.go index 52a6e32..3b8c582 100644 --- a/cmd/client/main.go +++ b/cmd/ohwm-client/main.go @@ -9,7 +9,7 @@ import ( func main() { a := app.New() - handleErr(a.RunClient(os.Args)) + handleErr(a.Run(os.Args)) } func handleErr(err error) { diff --git a/go.mod b/go.mod index d387f0f..e8ad8a9 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,9 @@ module git.xbudex.com/buddy/open-hardware-monitor go 1.13 -require github.com/urfave/cli v1.22.1 +require ( + github.com/gosimple/slug v1.7.0 + github.com/prometheus/client_golang v1.1.0 + github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be // indirect + github.com/urfave/cli v1.22.1 +) diff --git a/go.sum b/go.sum index d75daa5..d4db51e 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,80 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= +github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/gosimple/slug v1.7.0 h1:BlCZq+BMGn+riOZuRKnm60Fe7+jX9ck6TzzkN1r8TW8= +github.com/gosimple/slug v1.7.0/go.mod h1:ER78kgg1Mv0NQGlXiDe57DpCyfbNywXXZ9mIorhxAf0= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= +github.com/prometheus/client_golang v1.1.0 h1:BQ53HtBmfOitExawJ6LokA4x8ov/z0SYYb0+HxJfRI8= +github.com/prometheus/client_golang v1.1.0/go.mod h1:I1FGZT9+L76gKKOs5djB6ezCbFQP1xR9D75/vuwEF3g= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx/RA8rT8tKFRuGUZhuA90OyIBpPCXkcbwU8DE= +github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= +github.com/prometheus/common v0.6.0 h1:kRhiuYSXR3+uv2IbVbZhUxK5zVD/2pp3Gd2PpvPkpEo= +github.com/prometheus/common v0.6.0/go.mod h1:eBmuwkDJBwy6iBfxCBob6t6dR6ENT/y+J+Zk0j9GMYc= +github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/prometheus/procfs v0.0.3 h1:CTwfnzjQ+8dS6MhHHu4YswVAD99sL2wjPqP+VkURmKE= +github.com/prometheus/procfs v0.0.3/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDaekg4FpcdQ= +github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be h1:ta7tUOvsPHVHGom5hKW5VXNc2xZIkfCKP8iaqOyYtUQ= +github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be/go.mod h1:MIDFMn7db1kT65GmV94GzpX9Qdi7N/pQlwb+AN8wh+Q= github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +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/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/lib/app/app.go b/lib/app/app.go index c73c086..91cd47f 100644 --- a/lib/app/app.go +++ b/lib/app/app.go @@ -3,10 +3,15 @@ package app import ( "fmt" "io" + "log" + "net/http" "net/url" "os" "git.xbudex.com/buddy/open-hardware-monitor/lib/client" + "git.xbudex.com/buddy/open-hardware-monitor/lib/metrics" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/urfave/cli" ) @@ -41,6 +46,14 @@ var ( Value: "/data.json", Usage: "open hardware monitor path (ie /data.json)", } + + // FlagInterface flag for host + FlagInterface = cli.StringFlag{ + Name: "http", + EnvVar: "OHWM_HTTP", + Value: ":9200", + Usage: "interface to serve http requests", + } ) // New returns instance of an app @@ -50,15 +63,43 @@ func New() *App { out: os.Stdout, cli: cliapp, } + + app.cli.Name = "ohwm-client" + app.cli.Description = "Open Hardware Manager Client" + app.cli.UsageText = "ohwm-client [flags] [flags]" + app.cli.HideVersion = true + app.cli.Flags = []cli.Flag{ FlagHost, FlagScheme, FlagPath, } + + app.cli.Commands = []cli.Command{ + cli.Command{ + Name: "print", + Usage: "pretty prints api result", + Action: app.actionPrint, + }, + cli.Command{ + Name: "export", + Usage: "runs http exporter for prometheus", + Action: app.actionMetrics, + Flags: []cli.Flag{ + FlagInterface, + }, + }, + } + app.cli.Before = app.before return app } +// Run runs cli action +func (a *App) Run(args []string) error { + return a.cli.Run(args) +} + // Before sets up app func (a *App) before(ctx *cli.Context) error { a.client = &client.Client{ @@ -71,28 +112,40 @@ func (a *App) before(ctx *cli.Context) error { return nil } -func (a *App) clientAction(_ *cli.Context) error { +func (a *App) actionPrint(_ *cli.Context) error { node, err := a.client.Fetch() if err != nil { return err } - fmt.Fprint(a.out, node.String()) + fmt.Fprint(a.out, node.Stringify()) return nil } -func (a *App) actionMetrics(_ *cli.Context) error { - fmt.Fprintln(a.out, "todo") - return nil -} +func (a *App) actionMetrics(ctx *cli.Context) error { + addr := ctx.String("http") + http.HandleFunc("/metrics", func(resp http.ResponseWriter, req *http.Request) { + log.Println("GET /metrics") + root, err := a.client.Fetch() + if err != nil { + return + } + vals, err := root.Values() + if err != nil { + return + } + collection := metrics.FromVals(vals) + registry := prometheus.NewRegistry() + if err := registry.Register(collection); err != nil { + return + } -// RunClient runs client action -func (a *App) RunClient(args []string) error { - a.cli.Action = a.clientAction - return a.cli.Run(args) -} + handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{ + Registry: registry, + }) -// RunMetrics runs metrics server -func (a *App) RunMetrics(args []string) error { - a.cli.Action = a.actionMetrics - return a.cli.Run(args) + handler.ServeHTTP(resp, req) + registry.Unregister(collection) + }) + log.Println("listen on: " + addr) + return http.ListenAndServe(addr, nil) } diff --git a/lib/client/client.go b/lib/client/client.go index 5e879b8..f8d499c 100644 --- a/lib/client/client.go +++ b/lib/client/client.go @@ -6,9 +6,375 @@ import ( "io" "net/http" "net/url" + "path" + "strconv" + "strings" "time" + + "github.com/gosimple/slug" +) + +// HardwareType type +// https://github.com/openhardwaremonitor/openhardwaremonitor/blob/e199e0ccc69b4da92495266ebc0faf7daad97608/Hardware/IHardware.cs +type HardwareType int + +const ( + // UnknownHardware type + UnknownHardware HardwareType = iota + // Mainboard type + Mainboard + // SuperIO type + SuperIO + // CPU type + CPU + // RAM type + RAM + // GpuNvidia type + GpuNvidia + // GpuAti type + GpuAti + // TBalancer type + TBalancer + // Heatmaster type + Heatmaster + // HDD type + HDD + // Controller type for TBalancer or Heatmaster + Controller + // Computer type for host + Computer ) +// Hardware value +type Hardware struct { + Type HardwareType + Value string + TypeIndex int + TypeCount int +} + +// Sensor for unit +// https://github.com/openhardwaremonitor/openhardwaremonitor/blob/e199e0ccc69b4da92495266ebc0faf7daad97608/Hardware/ISensor.cs +type Sensor int + +const ( + // UnknownSensor in V + UnknownSensor Sensor = iota + // Voltage in V + Voltage + // Clock in MHz + Clock + // Temperature in °C + Temperature + // Load in % + Load + // Fan in RPM + Fan + // Flow in L/h + Flow + // Control in % + Control + // Level in % + Level + // Factor in 1 + Factor + // Power in W + Power + // Data in GB = 2^30 Bytes + Data + // SmallData in MB = 2^20 Bytes + SmallData +) + +var hardwareToMetric = map[HardwareType]string{ + UnknownHardware: "unknown", + Mainboard: "mainboard", + SuperIO: "superio", + CPU: "cpu", + RAM: "ram", + GpuNvidia: "gpu", + GpuAti: "gpu", + TBalancer: "tbalancer", + Heatmaster: "heatmaster", + HDD: "hdd", + Controller: "controller", + Computer: "host", +} + +var sensorToMetric = map[Sensor]string{ + UnknownSensor: "unknown", + Voltage: "voltage", + Clock: "mhz", + Temperature: "temp", + Load: "load", + Fan: "rpm", + Flow: "flow", + Control: "control", + Level: "level", + Factor: "factor", + Power: "power", + Data: "gb", + SmallData: "mb", +} + +var imageToHardware = map[string]HardwareType{ + "ati.png": GpuAti, + // "bigng.png": Heatmaster, + // "bigng.png": TBalancer, + "bigng.png": Controller, + "chip.png": SuperIO, + "cpu.png": CPU, + "hdd.png": HDD, + "mainboard.png": Mainboard, + "nvidia.png": GpuNvidia, + "ram.png": RAM, + "computer.png": Computer, +} + +var sensorToType = map[string]Sensor{ + "Clocks": Clock, + "Controls": Control, + "Data": Data, + "Factors": Factor, + "Fans": Fan, + "Flows": Flow, + "Levels": Level, + "Load": Load, + "Powers": Power, + "Temperatures": Temperature, + "Voltages": Voltage, +} + +func (s Sensor) String() string { + switch s { + case Clock: + return "Clock" + case Control: + return "Control" + case Data: + return "Data" + case Factor: + return "Factor" + case Fan: + return "Fan" + case Flow: + return "Flow" + case Level: + return "Level" + case Load: + return "Load" + case Power: + return "Power" + case SmallData: + return "SmallData" + case Temperature: + return "Temperature" + case Voltage: + return "Voltage" + } + return "Unknown" +} + +func (h HardwareType) String() string { + switch h { + case UnknownHardware: + return "Unknown" + case Mainboard: + return "Mainboard" + case SuperIO: + return "SuperIO" + case CPU: + return "CPU" + case RAM: + return "RAM" + case GpuNvidia: + return "GpuNvidia" + case GpuAti: + return "GpuAti" + case TBalancer: + return "TBalancer" + case Heatmaster: + return "Heatmaster" + case HDD: + return "HDD" + case Controller: + return "Controller" + case Computer: + return "Computer" + } + return "Unknown" +} + +// Value from ohwm +type Value struct { + Hardware []Hardware + Unit Sensor + Label string + Value string +} + +// Float64 gets value as float64 +func (v *Value) Float64() float64 { + val := strings.Split(v.Value, " ") + if len(val) == 0 { + return 0 + } + ret, err := strconv.ParseFloat(val[0], 64) + if err != nil { + return 0 + } + return ret +} + +// MetricName returns value as a metric name +func (v *Value) MetricName() string { + segments := []string{"ohwm"} + for _, hw := range v.Hardware { + metricname, ok := hardwareToMetric[hw.Type] + if !ok { + metricname = hardwareToMetric[UnknownHardware] + } + segments = append(segments, metricname) + if hw.TypeCount > 1 { + segments = append(segments, fmt.Sprintf("%d", hw.TypeIndex)) + } + } + + sensorname, ok := sensorToMetric[v.Unit] + if !ok { + sensorname = sensorToMetric[UnknownSensor] + } + segments = append(segments, sensorname) + + sluglabel := slug.Make(v.Label) + segments = append(segments, sluglabel) + + ret := strings.Join(segments, "_") + return strings.ReplaceAll(ret, "-", "_") +} + +// IsValue true if node has children +func (j *JSON) IsValue() bool { + if j.Children == nil { + return false + } + return len(j.Children) == 0 +} + +// IsRoot returns true if json is a root node +func (j *JSON) IsRoot() bool { + return j.ID == 0 +} + +// IsSensor returns true if json node is for a sensor type +func (j *JSON) IsSensor() bool { + if j.ImageURL == "" || j.Text == "" { + return false + } + _, ok := sensorToType[j.Text] + return ok +} + +// SensorType returns type of sensor +func (j *JSON) SensorType() Sensor { + st, _ := sensorToType[j.Text] + // Figure out if data is in GB (Data) or MB (SmallData) + if st == Data { + if len(j.Children) > 0 { + val := j.Children[0] + if strings.HasSuffix(val.Value, "MB") { + st = SmallData + } + } + } + return st +} + +// IsHardware returns true if json node is for hardware +func (j *JSON) IsHardware() bool { + if j.ImageURL == "" || j.Text == "" { + return false + } + _, ok := imageToHardware[path.Base(j.ImageURL)] + return ok +} + +// HardwareType returns HardwareType of node +func (j *JSON) HardwareType() HardwareType { + ht, _ := imageToHardware[path.Base(j.ImageURL)] + return ht +} + +// Visitor callback function +type Visitor func(v Value) error + +// Walk from json root +func (j *JSON) Walk(fn Visitor) error { + return j.walk(fn, Value{Hardware: []Hardware{}}, 0, 0) +} + +func (j *JSON) childDeviceTotals() map[HardwareType]int { + totals := map[HardwareType]int{} + for _, child := range j.Children { + totals[child.HardwareType()]++ + } + return totals +} + +func (j *JSON) walk(fn Visitor, ctx Value, hwIndex, hwTotal int) error { + if j.IsValue() { + ctx.Label = j.Text + ctx.Value = j.Value + return fn(ctx) + } + if j.IsSensor() { + ctx.Unit = j.SensorType() + } + if j.IsHardware() { + ctx.Hardware = append(ctx.Hardware, Hardware{ + Type: j.HardwareType(), + Value: j.Text, + TypeIndex: hwIndex, + TypeCount: hwTotal, + }) + } + totals := j.childDeviceTotals() + deviceIndex := map[HardwareType]int{} + for _, child := range j.Children { + deviceType := child.HardwareType() + index := deviceIndex[deviceType] + if err := child.walk(fn, ctx, index, totals[deviceType]); err != nil { + return err + } + deviceIndex[deviceType]++ + } + return nil +} + +// Values from json root +func (j *JSON) Values() ([]Value, error) { + ret := []Value{} + err := j.Walk(func(val Value) error { + ret = append(ret, val) + return nil + }) + if err != nil { + return nil, err + } + return ret, nil +} + +// JSON from data +type JSON struct { + ID int `json:"id"` + ImageURL string + Max string + Min string + Text string + Value string + Children []JSON +} + // Client for open hardware monitor type Client struct { Timeout time.Duration @@ -16,7 +382,7 @@ type Client struct { } // Fetch requests -func (c *Client) Fetch() (*Node, error) { +func (c *Client) Fetch() (*JSON, error) { client := http.Client{Timeout: c.Timeout} resp, err := client.Get(c.URL.String()) if err != nil { @@ -27,8 +393,8 @@ func (c *Client) Fetch() (*Node, error) { } // Decode json -func (c *Client) Decode(r io.Reader) (*Node, error) { - node := &Node{} +func (c *Client) Decode(r io.Reader) (*JSON, error) { + node := &JSON{} decoder := json.NewDecoder(r) if err := decoder.Decode(node); err != nil { return nil, err @@ -36,36 +402,26 @@ func (c *Client) Decode(r io.Reader) (*Node, error) { return node, nil } -// Node from data -type Node struct { - ID int `json:"id"` - ImageURL string - Max string - Min string - Text string - Value string - Children []Node -} - -func (n *Node) String() string { - return n.stringify(0) +// Stringify tree +func (j *JSON) Stringify() string { + return j.stringify(0) } -func (n *Node) stringify(indent int) string { +func (j *JSON) stringify(indent int) string { prefix := "" for i := 0; i < indent; i++ { prefix += " " } - ret := prefix + n.Text - if n.Value != "" { - ret += ": " + n.Value + ret := prefix + j.Text + if j.Value != "" { + ret += ": " + j.Value } - if n.Max != "" && n.Min != "" && n.Max != "-" && n.Min != "-" { - ret += fmt.Sprintf(" (%s - %s)", n.Min, n.Max) + if j.Max != "" && j.Min != "" && j.Max != "-" && j.Min != "-" { + ret += fmt.Sprintf(" (%s - %s)", j.Min, j.Max) } ret += "\n" - for _, child := range n.Children { + for _, child := range j.Children { ret += child.stringify(indent + 1) } diff --git a/lib/metrics/metrics.go b/lib/metrics/metrics.go new file mode 100644 index 0000000..9787fe0 --- /dev/null +++ b/lib/metrics/metrics.go @@ -0,0 +1,60 @@ +package metrics + +import ( + "git.xbudex.com/buddy/open-hardware-monitor/lib/client" + "github.com/gosimple/slug" + "github.com/prometheus/client_golang/prometheus" +) + +// Collector type +type Collector struct { + vals []client.Value + descs []*prometheus.Desc +} + +// FromVals gets a collector +func FromVals(vals []client.Value) *Collector { + descs := make([]*prometheus.Desc, len(vals)) + for i, val := range vals { + params := []string{} + for _, hw := range val.Hardware { + hwt := hw.Type + typeslug := slug.Make(hwt.String()) + params = append(params, typeslug) + } + params = append(params, "unit") + name := val.MetricName() + descs[i] = prometheus.NewDesc( + name, + name, + params, + nil, + ) + } + + c := &Collector{ + vals: vals, + descs: descs, + } + return c +} + +// Describe method for interface +func (c *Collector) Describe(ch chan<- *prometheus.Desc) { + for _, d := range c.descs { + ch <- d + } +} + +// Collect method for interface +func (c *Collector) Collect(ch chan<- prometheus.Metric) { + for i, val := range c.vals { + desc := c.descs[i] + labelVals := []string{} + for _, hw := range val.Hardware { + labelVals = append(labelVals, hw.Value) + } + labelVals = append(labelVals, slug.Make(val.Unit.String())) + ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, val.Float64(), labelVals...) + } +}