forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpdk_utils.go
116 lines (95 loc) · 2.6 KB
/
dpdk_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// +build linux
package dpdk
import (
"encoding/json"
"fmt"
"os"
"reflect"
"strconv"
"strings"
"github.com/influxdata/telegraf/filter"
)
func commandWithParams(command string, params string) string {
if params != "" {
return command + "," + params
}
return command
}
func stripParams(command string) string {
index := strings.IndexRune(command, ',')
if index == -1 {
return command
}
return command[:index]
}
// Since DPDK is an open-source project, developers can use their own format of params
// so it could "/command,1,3,5,123" or "/command,userId=1, count=1234".
// To avoid issues with different formats of params, all params are returned as single string
func getParams(command string) string {
index := strings.IndexRune(command, ',')
if index == -1 {
return ""
}
return command[index+1:]
}
// Checks if provided path points to socket
func isSocket(path string) error {
pathInfo, err := os.Lstat(path)
if os.IsNotExist(err) {
return fmt.Errorf("provided path does not exist: '%v'", path)
}
if err != nil {
return fmt.Errorf("cannot get system information of '%v' file: %v", path, err)
}
if pathInfo.Mode()&os.ModeSocket != os.ModeSocket {
return fmt.Errorf("provided path does not point to a socket file: '%v'", path)
}
return nil
}
// Converts JSON array containing devices identifiers from DPDK response to string slice
func jsonToArray(input []byte, command string) ([]string, error) {
if len(input) == 0 {
return nil, fmt.Errorf("got empty object instead of json")
}
var rawMessage map[string]json.RawMessage
err := json.Unmarshal(input, &rawMessage)
if err != nil {
return nil, err
}
var intArray []int64
var stringArray []string
err = json.Unmarshal(rawMessage[command], &intArray)
if err != nil {
return nil, fmt.Errorf("failed to unmarshall json response - %v", err)
}
for _, value := range intArray {
stringArray = append(stringArray, strconv.FormatInt(value, 10))
}
return stringArray, nil
}
func removeSubset(elements []string, excludedFilter filter.Filter) []string {
if excludedFilter == nil {
return elements
}
var result []string
for _, element := range elements {
if !excludedFilter.Match(element) {
result = append(result, element)
}
}
return result
}
func uniqueValues(values []string) []string {
in := make(map[string]bool)
result := make([]string, 0, len(values))
for _, value := range values {
if !in[value] {
in[value] = true
result = append(result, value)
}
}
return result
}
func isEmpty(value interface{}) bool {
return value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil())
}