diff options
author | stuart nelson <stuartnelson3@gmail.com> | 2016-09-28 21:08:52 +0200 |
---|---|---|
committer | stuart nelson <stuartnelson3@gmail.com> | 2016-11-17 13:23:54 +0100 |
commit | 2b74cf7498d61b68d07a0373c483397232ae9b79 (patch) | |
tree | 160615944329ad0a44498438f71807ba733c2eb6 /collector/devstat_dragonfly.go | |
parent | bb2b984030ab1a31c8455732cb6ec48d2a8d5dc0 (diff) | |
download | prometheus_node_collector-2b74cf7498d61b68d07a0373c483397232ae9b79.tar.bz2 prometheus_node_collector-2b74cf7498d61b68d07a0373c483397232ae9b79.tar.xz prometheus_node_collector-2b74cf7498d61b68d07a0373c483397232ae9b79.zip |
Export devstat for dragonfly
Diffstat (limited to 'collector/devstat_dragonfly.go')
-rw-r--r-- | collector/devstat_dragonfly.go | 232 |
1 files changed, 232 insertions, 0 deletions
diff --git a/collector/devstat_dragonfly.go b/collector/devstat_dragonfly.go new file mode 100644 index 0000000..383460a --- /dev/null +++ b/collector/devstat_dragonfly.go | |||
@@ -0,0 +1,232 @@ | |||
1 | // Copyright 2016 The Prometheus Authors | ||
2 | // Licensed under the Apache License, Version 2.0 (the "License"); | ||
3 | // you may not use this file except in compliance with the License. | ||
4 | // You may obtain a copy of the License at | ||
5 | // | ||
6 | // http://www.apache.org/licenses/LICENSE-2.0 | ||
7 | // | ||
8 | // Unless required by applicable law or agreed to in writing, software | ||
9 | // distributed under the License is distributed on an "AS IS" BASIS, | ||
10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
11 | // See the License for the specific language governing permissions and | ||
12 | // limitations under the License. | ||
13 | |||
14 | // +build !nodevstat | ||
15 | |||
16 | package collector | ||
17 | |||
18 | import ( | ||
19 | "errors" | ||
20 | "fmt" | ||
21 | |||
22 | "github.com/prometheus/client_golang/prometheus" | ||
23 | ) | ||
24 | |||
25 | /* | ||
26 | #cgo LDFLAGS: -ldevstat | ||
27 | #include <devstat.h> | ||
28 | #include <stdlib.h> | ||
29 | #include <string.h> | ||
30 | |||
31 | typedef struct { | ||
32 | char device[DEVSTAT_NAME_LEN]; | ||
33 | int unit; | ||
34 | uint64_t bytes; | ||
35 | uint64_t transfers; | ||
36 | uint64_t blocks; | ||
37 | double kb_per_transfer; | ||
38 | double transfers_per_second; | ||
39 | double mb_per_second; | ||
40 | double blocks_per_second; | ||
41 | double ms_per_transaction; | ||
42 | } Stats; | ||
43 | |||
44 | int _get_ndevs() { | ||
45 | struct statinfo current; | ||
46 | int num_devices; | ||
47 | |||
48 | current.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo)); | ||
49 | if (current.dinfo == NULL) | ||
50 | return -2; | ||
51 | |||
52 | checkversion(); | ||
53 | |||
54 | if (getdevs(¤t) == -1) | ||
55 | return -1; | ||
56 | |||
57 | return current.dinfo->numdevs; | ||
58 | } | ||
59 | |||
60 | Stats _get_stats(int i) { | ||
61 | struct statinfo current; | ||
62 | int num_devices; | ||
63 | |||
64 | current.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo)); | ||
65 | getdevs(¤t); | ||
66 | |||
67 | num_devices = current.dinfo->numdevs; | ||
68 | Stats stats; | ||
69 | |||
70 | uint64_t total_bytes, total_transfers, total_blocks; | ||
71 | long double kb_per_transfer, transfers_per_second, mb_per_second, blocks_per_second, ms_per_transaction; | ||
72 | |||
73 | strcpy(stats.device, current.dinfo->devices[i].device_name); | ||
74 | stats.unit = current.dinfo->devices[i].unit_number; | ||
75 | compute_stats(¤t.dinfo->devices[i], | ||
76 | NULL, | ||
77 | 1.0, | ||
78 | &total_bytes, | ||
79 | &total_transfers, | ||
80 | &total_blocks, | ||
81 | &kb_per_transfer, | ||
82 | &transfers_per_second, | ||
83 | &mb_per_second, | ||
84 | &blocks_per_second, | ||
85 | &ms_per_transaction); | ||
86 | |||
87 | stats.bytes = total_bytes; | ||
88 | stats.transfers = total_transfers; | ||
89 | stats.blocks = total_blocks; | ||
90 | stats.kb_per_transfer = kb_per_transfer; | ||
91 | stats.transfers_per_second = transfers_per_second; | ||
92 | stats.mb_per_second = mb_per_second; | ||
93 | stats.blocks_per_second = blocks_per_second; | ||
94 | stats.ms_per_transaction = ms_per_transaction; | ||
95 | |||
96 | return stats; | ||
97 | } | ||
98 | */ | ||
99 | import "C" | ||
100 | |||
101 | const ( | ||
102 | devstatSubsystem = "devstat" | ||
103 | ) | ||
104 | |||
105 | type devstatCollector struct { | ||
106 | bytes_total *prometheus.CounterVec | ||
107 | transfers_total *prometheus.CounterVec | ||
108 | blocks_total *prometheus.CounterVec | ||
109 | bytes_per_transfer *prometheus.GaugeVec | ||
110 | transfers_per_second *prometheus.GaugeVec | ||
111 | bytes_per_second *prometheus.GaugeVec | ||
112 | blocks_per_second *prometheus.GaugeVec | ||
113 | seconds_per_transaction *prometheus.GaugeVec | ||
114 | } | ||
115 | |||
116 | func init() { | ||
117 | Factories["devstat"] = NewDevstatCollector | ||
118 | } | ||
119 | |||
120 | // Takes a prometheus registry and returns a new Collector exposing | ||
121 | // Device stats. | ||
122 | func NewDevstatCollector() (Collector, error) { | ||
123 | return &devstatCollector{ | ||
124 | bytes_total: prometheus.NewCounterVec( | ||
125 | prometheus.CounterOpts{ | ||
126 | Namespace: Namespace, | ||
127 | Subsystem: devstatSubsystem, | ||
128 | Name: "bytes_total", | ||
129 | Help: "The total number of bytes transferred for reads and writes on the device.", | ||
130 | }, | ||
131 | []string{"device"}, | ||
132 | ), | ||
133 | transfers_total: prometheus.NewCounterVec( | ||
134 | prometheus.CounterOpts{ | ||
135 | Namespace: Namespace, | ||
136 | Subsystem: devstatSubsystem, | ||
137 | Name: "transfers_total", | ||
138 | Help: "The total number of transactions completed.", | ||
139 | }, | ||
140 | []string{"device"}, | ||
141 | ), | ||
142 | blocks_total: prometheus.NewCounterVec( | ||
143 | prometheus.CounterOpts{ | ||
144 | Namespace: Namespace, | ||
145 | Subsystem: devstatSubsystem, | ||
146 | Name: "blocks_total", | ||
147 | Help: "The total number of bytes given in terms of the devices blocksize.", | ||
148 | }, | ||
149 | []string{"device"}, | ||
150 | ), | ||
151 | bytes_per_transfer: prometheus.NewGaugeVec( | ||
152 | prometheus.GaugeOpts{ | ||
153 | Namespace: Namespace, | ||
154 | Subsystem: devstatSubsystem, | ||
155 | Name: "bytes_per_transfer", | ||
156 | Help: "The average number of bytes per transfer.", | ||
157 | }, | ||
158 | []string{"device"}, | ||
159 | ), | ||
160 | transfers_per_second: prometheus.NewGaugeVec( | ||
161 | prometheus.GaugeOpts{ | ||
162 | Namespace: Namespace, | ||
163 | Subsystem: devstatSubsystem, | ||
164 | Name: "transfers_per_second", | ||
165 | Help: "The average number of transfers per second.", | ||
166 | }, | ||
167 | []string{"device"}, | ||
168 | ), | ||
169 | bytes_per_second: prometheus.NewGaugeVec( | ||
170 | prometheus.GaugeOpts{ | ||
171 | Namespace: Namespace, | ||
172 | Subsystem: devstatSubsystem, | ||
173 | Name: "bytes_per_second", | ||
174 | Help: "The average bytes per second.", | ||
175 | }, | ||
176 | []string{"device"}, | ||
177 | ), | ||
178 | blocks_per_second: prometheus.NewGaugeVec( | ||
179 | prometheus.GaugeOpts{ | ||
180 | Namespace: Namespace, | ||
181 | Subsystem: devstatSubsystem, | ||
182 | Name: "blocks_per_second", | ||
183 | Help: "The average blocks per second.", | ||
184 | }, | ||
185 | []string{"device"}, | ||
186 | ), | ||
187 | seconds_per_transaction: prometheus.NewGaugeVec( | ||
188 | prometheus.GaugeOpts{ | ||
189 | Namespace: Namespace, | ||
190 | Subsystem: devstatSubsystem, | ||
191 | Name: "seconds_per_transaction", | ||
192 | Help: "The average number of seconds per transaction.", | ||
193 | }, | ||
194 | []string{"device"}, | ||
195 | ), | ||
196 | }, nil | ||
197 | } | ||
198 | |||
199 | func (c *devstatCollector) Update(ch chan<- prometheus.Metric) (err error) { | ||
200 | count := C._get_ndevs() | ||
201 | if count == -1 { | ||
202 | return errors.New("getdevs() failed") | ||
203 | } | ||
204 | if count == -2 { | ||
205 | return errors.New("calloc() failed") | ||
206 | } | ||
207 | |||
208 | for i := C.int(0); i < count; i++ { | ||
209 | stats := C._get_stats(i) | ||
210 | device := fmt.Sprintf("%s%d", C.GoString(&stats.device[0]), stats.unit) | ||
211 | |||
212 | c.bytes_total.With(prometheus.Labels{"device": device}).Set(float64(stats.bytes)) | ||
213 | c.transfers_total.With(prometheus.Labels{"device": device}).Set(float64(stats.transfers)) | ||
214 | c.blocks_total.With(prometheus.Labels{"device": device}).Set(float64(stats.blocks)) | ||
215 | c.bytes_per_transfer.With(prometheus.Labels{"device": device}).Set(float64(stats.kb_per_transfer) * 1000) | ||
216 | c.bytes_per_second.With(prometheus.Labels{"device": device}).Set(float64(stats.mb_per_second) * 1000000) | ||
217 | c.transfers_per_second.With(prometheus.Labels{"device": device}).Set(float64(stats.transfers_per_second)) | ||
218 | c.blocks_per_second.With(prometheus.Labels{"device": device}).Set(float64(stats.blocks_per_second)) | ||
219 | c.seconds_per_transaction.With(prometheus.Labels{"device": device}).Set(float64(stats.ms_per_transaction) / 1000) | ||
220 | } | ||
221 | |||
222 | c.bytes_total.Collect(ch) | ||
223 | c.transfers_total.Collect(ch) | ||
224 | c.blocks_total.Collect(ch) | ||
225 | c.bytes_per_transfer.Collect(ch) | ||
226 | c.bytes_per_second.Collect(ch) | ||
227 | c.transfers_per_second.Collect(ch) | ||
228 | c.blocks_per_second.Collect(ch) | ||
229 | c.seconds_per_transaction.Collect(ch) | ||
230 | |||
231 | return err | ||
232 | } | ||