forked from utkuozdemir/nvidia_gpu_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
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
9330fd5
commit bfa82ba
Showing
7 changed files
with
231 additions
and
171 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 |
---|---|---|
@@ -1,43 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"nvidia-smi-exporter/internal/exporter" | ||
"os" | ||
"os/exec" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var ( | ||
fieldRegex = regexp.MustCompile(`(?m)\n\s*\n^"([^"]+)"`) | ||
) | ||
|
||
func main() { | ||
var nvidiaSmiCommand string | ||
flag.StringVar(&nvidiaSmiCommand, "nvidia-smi-command", "nvidia-smi", "command to run nvidia-smi") | ||
flag.Parse() | ||
|
||
cmdAndArgs := strings.Fields(nvidiaSmiCommand) | ||
cmdAndArgs = append(cmdAndArgs, "--help-query-gpu") | ||
cmd := exec.Command(cmdAndArgs[0], cmdAndArgs[1:]...) | ||
|
||
var stdout bytes.Buffer | ||
cmd.Stdout = &stdout | ||
err := cmd.Run() | ||
fields, err := exporter.ParseQueryFields(nvidiaSmiCommand) | ||
if err != nil { | ||
fmt.Printf("error: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
out := stdout.String() | ||
found := fieldRegex.FindAllStringSubmatch(out, -1) | ||
|
||
var fields []string | ||
for _, ss := range found { | ||
fields = append(fields, ss[1]) | ||
} | ||
|
||
fmt.Printf("Fields: %s\n", strings.Join(fields, ",")) | ||
} |
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,77 @@ | ||
package exporter | ||
|
||
import "strings" | ||
|
||
type table struct { | ||
rows []row | ||
returnedFieldNames []string | ||
queryFieldNameToCells map[string][]cell | ||
} | ||
|
||
type row struct { | ||
queryFieldNameToCells map[string]cell | ||
cells []cell | ||
} | ||
|
||
type cell struct { | ||
queryFieldName string | ||
returnedFieldName string | ||
rawValue string | ||
} | ||
|
||
func parseCSVIntoTable(queryResult string, queryFieldNames []string) table { | ||
lines := strings.Split(strings.TrimSpace(queryResult), "\n") | ||
titlesLine := lines[0] | ||
valuesLines := lines[1:] | ||
returnedFieldNames := parseCSVLine(titlesLine) | ||
|
||
numCols := len(queryFieldNames) | ||
numRows := len(valuesLines) | ||
|
||
rows := make([]row, numRows) | ||
|
||
queryFieldNameToCells := make(map[string][]cell) | ||
for _, queryFieldName := range queryFieldNames { | ||
queryFieldNameToCells[queryFieldName] = make([]cell, numRows) | ||
} | ||
|
||
for rowIndex, valuesLine := range valuesLines { | ||
queryFieldNameToCell := make(map[string]cell, numCols) | ||
cells := make([]cell, numCols) | ||
rawValues := parseCSVLine(valuesLine) | ||
for colIndex, rawValue := range rawValues { | ||
queryFieldName := queryFieldNames[colIndex] | ||
gm := cell{ | ||
queryFieldName: queryFieldName, | ||
returnedFieldName: returnedFieldNames[colIndex], | ||
rawValue: rawValue, | ||
} | ||
queryFieldNameToCell[queryFieldName] = gm | ||
cells[colIndex] = gm | ||
queryFieldNameToCells[queryFieldName][rowIndex] = gm | ||
} | ||
|
||
gmc := row{ | ||
queryFieldNameToCells: queryFieldNameToCell, | ||
cells: cells, | ||
} | ||
|
||
rows[rowIndex] = gmc | ||
|
||
} | ||
|
||
return table{ | ||
rows: rows, | ||
returnedFieldNames: returnedFieldNames, | ||
queryFieldNameToCells: queryFieldNameToCells, | ||
} | ||
} | ||
|
||
func parseCSVLine(line string) []string { | ||
values := strings.Split(line, ",") | ||
result := make([]string, len(values)) | ||
for i, field := range values { | ||
result[i] = strings.TrimSpace(field) | ||
} | ||
return result | ||
} |
Oops, something went wrong.