-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f060840
commit 8bef384
Showing
2 changed files
with
67 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2019 Lukas Malkmus | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package collector | ||
|
||
import ( | ||
"os/exec" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
const gpuSubsystem = "gpu" | ||
|
||
type gpuCollector struct{} | ||
|
||
func init() { | ||
registerCollector("gpu", defaultEnabled, NewGPUCollector) | ||
} | ||
|
||
// NewGPUCollector returns a new Collector exposing GPU temperature metrics. | ||
func NewGPUCollector() (Collector, error) { | ||
return &gpuCollector{}, nil | ||
} | ||
|
||
// Update implements the Collector interface. | ||
func (c *gpuCollector) Update(ch chan<- prometheus.Metric) error { | ||
// Get temperature string by executing /opt/vc/bin/vcgencmd measure_temp | ||
// and convert it to float64 value. | ||
cmd := exec.Command("/opt/vc/bin/vcgencmd", "measure_temp") | ||
stdout, err := cmd.Output() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// temp=55.3'C => 55.3 | ||
tempStr := strings.TrimPrefix(string(stdout), "temp=") | ||
tempStr = strings.TrimSuffix(tempStr, "'C\n") | ||
temp, err := strconv.ParseFloat(tempStr, 64) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Export the metric. | ||
ch <- prometheus.MustNewConstMetric( | ||
prometheus.NewDesc( | ||
prometheus.BuildFQName(namespace, gpuSubsystem, "temperature_celsius"), | ||
"GPU temperature in degrees celsius (°C).", | ||
nil, nil, | ||
), | ||
prometheus.GaugeValue, temp, | ||
) | ||
return nil | ||
} |