1 /** @file sflow.h
2  *  @brief sFlow definitions for gmond
3  *  @author Neil McKee */
4 
5 #ifndef SFLOW_H
6 #define SFLOW_H
7 
8 /* If we use autoconf.  */
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif /* __cplusplus */
16 
17 #include "my_inet_ntop.h"
18 #include "gmond_internal.h"
19 
20 #define SFLOW_IANA_REGISTERED_PORT 6343
21 
22 #define SFLOW_VERSION_5 5
23 #define SFLOW_COUNTERS_SAMPLE 2
24 #define SFLOW_COUNTERS_SAMPLE_EXPANDED 4
25 #define SFLOW_DSCLASS_PHYSICAL_ENTITY 2
26 #define SFLOW_MIN_LEN 36
27 
28 #define SFLOW_MAX_LOST_SAMPLES 7
29 #define SFLOW_COUNTERBLOCK_HOST_HID 2000
30 #define SFLOW_COUNTERBLOCK_HOST_CPU 2003
31 #define SFLOW_COUNTERBLOCK_HOST_MEM 2004
32 #define SFLOW_COUNTERBLOCK_HOST_DSK 2005
33 #define SFLOW_COUNTERBLOCK_HOST_NIO 2006
34 #define SFLOW_COUNTERBLOCK_HOST_VNODE 2100
35 #define SFLOW_COUNTERBLOCK_HOST_VCPU 2101
36 #define SFLOW_COUNTERBLOCK_HOST_VMEM 2102
37 #define SFLOW_COUNTERBLOCK_HOST_VDSK 2103
38 #define SFLOW_COUNTERBLOCK_HOST_VNIO 2104
39 #ifdef SFLOW_MEMCACHE_2200
40 #define SFLOW_COUNTERBLOCK_MEMCACHE_2200 2200
41 #endif
42 #define SFLOW_COUNTERBLOCK_MEMCACHE 2204
43 #define SFLOW_COUNTERBLOCK_HTTP 2201
44 #define SFLOW_COUNTERBLOCK_JVM 2106
45 #define SFLOW_COUNTERBLOCK_NVML_GPU ((5703 << 12) + 1)  /* enterprise 5703 == Nvidia */
46 #define SFLOW_COUNTERBLOCK_WORKERS 2206
47 
48 #define SFLOW_MAX_HOSTNAME_LEN 64
49 #define SFLOW_MAX_FQDN_LEN 256
50 #define SFLOW_MAX_IPSTR_LEN 64
51 #define SFLOW_MAX_SPOOFHOST_LEN SFLOW_MAX_IPSTR_LEN + SFLOW_MAX_HOSTNAME_LEN + 2
52 #define SFLOW_MAX_OSRELEASE_LEN 32
53 #define SFLOW_MAX_UUIDSTR_LEN 37
54 #define SFLOW_MAX_INT32STR_LEN 10
55 #define SFLOW_MAX_DSI_LEN (SFLOW_MAX_IPSTR_LEN + SFLOW_MAX_INT32STR_LEN + 1 + SFLOW_MAX_INT32STR_LEN)
56 
57 #define SFLOW_MAX_METRIC_NAME_LEN 512
58 #define SFLOW_MAX_METRIC_PREFIX_LEN 255
59 
60 typedef struct _SFlowAddr {
61   enum { SFLOW_ADDRTYPE_undefined=0, SFLOW_ADDRTYPE_IP4, SFLOW_ADDRTYPE_IP6 } type;
62   union {
63     uint32_t ip4;
64     uint32_t ip6[4];
65   } a;
66 } SFlowAddr;
67 
68 typedef struct _SFlowXDR {
69   /* cursor */
70   uint32_t *datap;
71   uint32_t i;
72   uint32_t quads;
73   /* timestamp */
74   apr_time_t now;
75   /* agent */
76   SFlowAddr agentAddr;
77   char agentipstr[SFLOW_MAX_IPSTR_LEN];
78   uint32_t subAgentId;
79   uint32_t uptime_mS;
80   /* socket address */
81   apr_sockaddr_t *remotesa;
82   /* datasource */
83   uint32_t dsClass;
84   uint32_t dsIndex;
85   /* sequence numbers */
86   uint32_t datagramSeqNo;
87   uint32_t csSeqNo;
88   /* delta accumulation control */
89   bool_t counterDeltas;
90   /* structure offsets */
91   struct {
92     uint32_t HID;
93     uint32_t CPU;
94     uint32_t MEM;
95     uint32_t DSK;
96     uint32_t NIO;
97     uint32_t VNODE;
98     uint32_t VCPU;
99     uint32_t VMEM;
100     uint32_t VDSK;
101     uint32_t VNIO;
102     uint32_t foundPH;
103     uint32_t foundVM;
104 #ifdef SFLOW_MEMCACHE_2200
105     uint32_t MEMCACHE_2200;
106 #endif
107     uint32_t MEMCACHE;
108     uint32_t HTTP;
109     uint32_t JVM;
110     uint32_t NVML_GPU;
111     uint32_t WORKERS;
112   } offset;
113   /* structure length in quads (where needed) */
114   struct {
115     uint32_t CPU;
116   } st_quads;
117 } SFlowXDR;
118 
119 #define SFLOWXDR_init(x,buf,len) do {  x->datap = (uint32_t *)buf; x->quads = (len >> 2); } while(0)
120 #define SFLOWXDR_next(x) ntohl(x->datap[x->i++])
121 #define SFLOWXDR_next_n(x) x->datap[x->i++]
122 #define SFLOWXDR_more(x,q) ((q + x->i) <= x->quads)
123 #define SFLOWXDR_skip(x,q) x->i += q
124 #define SFLOWXDR_skip_b(x,b) x->i += ((b+3)>>2)
125 #define SFLOWXDR_mark(x,q) x->i + q
126 #define SFLOWXDR_markOK(x,m) (m == x->i)
127 #define SFLOWXDR_off_b() (x->i << 2)
128 #define SFLOWXDR_setc(x,j) x->i = j
129 #define SFLOWXDR_str(x) (char *)(x->datap + x->i)
130 #define SFLOWXDR_next_float(x,pf) do { uint32_t tmp=SFLOWXDR_next(x); memcpy(pf, &tmp, 4); } while(0)
131 #define SFLOWXDR_next_int64(x,pi) do { (*pi) = SFLOWXDR_next(x); (*pi) <<= 32; (*pi) += SFLOWXDR_next(x); } while(0)
132 
133 typedef struct _SFlowCounterState {
134   /* CPU */
135   uint32_t cpu_user;
136   uint32_t cpu_nice;
137   uint32_t cpu_system;
138   uint32_t cpu_idle;
139   uint32_t cpu_wio;
140   uint32_t cpu_intr;
141   uint32_t cpu_sintr;
142   uint32_t interrupts;
143   uint32_t contexts;
144   uint32_t cpu_steal;
145   uint32_t cpu_guest;
146   uint32_t cpu_guest_nice;
147 
148   /* memory */
149   uint32_t page_in;
150   uint32_t page_out;
151   uint32_t swap_in;
152   uint32_t swap_out;
153 
154   /* disk I/O */
155   uint32_t reads;
156   uint64_t bytes_read;
157   uint32_t read_time;
158   uint32_t writes;
159   uint64_t bytes_written;
160   uint32_t write_time;
161 
162   /* NetworkIO */
163   uint64_t bytes_in;
164   uint32_t pkts_in;
165   uint32_t errs_in;
166   uint32_t drops_in;
167   uint64_t bytes_out;
168   uint32_t pkts_out;
169   uint32_t errs_out;
170   uint32_t drops_out;
171 
172   /* VM CPU */
173   uint32_t vcpu_mS;
174   /* JVM CPU */
175   uint32_t jvm_vcpu_mS;
176 
177   /* VM Disk */
178   uint32_t vreads;
179   uint64_t vbytes_read;
180   uint32_t vwrites;
181   uint64_t vbytes_written;
182   uint32_t vdskerrs;
183 
184   /* VM NetworkIO */
185   uint64_t vbytes_in;
186   uint32_t vpkts_in;
187   uint32_t verrs_in;
188   uint32_t vdrops_in;
189   uint64_t vbytes_out;
190   uint32_t vpkts_out;
191   uint32_t verrs_out;
192   uint32_t vdrops_out;
193 
194   /* memcache */
195   uint32_t mc_rusage_user;
196   uint32_t mc_rusage_system;
197   uint32_t mc_total_conns;
198   uint32_t mc_cmd_get;
199   uint32_t mc_cmd_set;
200   uint32_t mc_cmd_flush;
201   uint32_t mc_get_hits;
202   uint32_t mc_get_misses;
203   uint32_t mc_delete_misses;
204   uint32_t mc_delete_hits;
205   uint32_t mc_incr_misses;
206   uint32_t mc_incr_hits;
207   uint32_t mc_decr_misses;
208   uint32_t mc_decr_hits;
209   uint32_t mc_cas_misses;
210   uint32_t mc_cas_hits;
211   uint32_t mc_cas_badval;
212   uint32_t mc_auth_cmds;
213   uint32_t mc_auth_errors;
214   uint32_t mc_bytes_read;
215   uint32_t mc_bytes_written;
216   uint32_t mc_conn_yields;
217   uint32_t mc_bytes;
218   uint32_t mc_total_items;
219   uint32_t mc_evictions;
220   uint32_t mc_cmd_touch;
221   uint32_t mc_rejected_conns;
222   uint32_t mc_reclaimed;
223 
224   /* HTTP */
225   uint32_t http_meth_option;
226   uint32_t http_meth_get;
227   uint32_t http_meth_head;
228   uint32_t http_meth_post;
229   uint32_t http_meth_put;
230   uint32_t http_meth_delete;
231   uint32_t http_meth_trace;
232   uint32_t http_meth_connect;
233   uint32_t http_meth_other;
234   uint32_t http_status_1XX;
235   uint32_t http_status_2XX;
236   uint32_t http_status_3XX;
237   uint32_t http_status_4XX;
238   uint32_t http_status_5XX;
239   uint32_t http_status_other;
240   /* HTTP workers */
241   uint32_t http_workers_req_delayed;
242   uint32_t http_workers_req_dropped;
243 
244   /* Java JVM */
245   uint32_t jvm_thread_started;
246   uint32_t jvm_gc_count;
247   uint32_t jvm_gc_ms;
248   uint32_t jvm_comp_ms;
249 
250   /* NVML GPU */
251   uint32_t nvml_gpu_time;
252   uint32_t nvml_gpu_rw_time;
253   uint32_t nvml_gpu_ecc_errors;
254   uint32_t nvml_gpu_energy;
255 
256 } SFlowCounterState;
257 
258 
259 typedef struct _SFlowDataSource {
260   struct _SFlowDataSource *nxt;
261   uint32_t dsClass;
262   uint32_t dsIndex;
263   uint32_t csSeqNo;
264   apr_time_t last_sample_time;
265   /* strings */
266   char *osrelease;
267   char *uuidstr;
268   char *hostname;
269   /* counter state */
270   SFlowCounterState counterState;
271   /* cache metric_prefix string here */
272   char *metric_prefix;
273 } SFlowDataSource;
274 
275 typedef struct _SFlowSubAgent {
276   struct _SFlowSubAgent *nxt;
277   uint32_t subAgentId;
278   uint32_t datagramSeqNo;
279   SFlowDataSource *dataSources;
280 } SFlowSubAgent;
281 
282 typedef struct _SFlowAgent {
283   SFlowSubAgent *subAgents;
284 } SFlowAgent;
285 
286 typedef enum {
287 #define SFLOW_GMETRIC(tag,mname,units,slope,format,group,desc,title) tag,
288 #include "sflow_gmetric.h"
289 #undef SFLOW_GMETRIC
290   SFLOW_NUM_GMETRICS } EnumSFLOWGMetric;
291 
292 typedef struct _SFLOWGMetric {
293   EnumSFLOWGMetric tag;
294   char *mname;
295   char *units;
296   ganglia_slope_t slope;
297   char *format;
298   char *group;
299   char *desc;
300   char *title;
301 } SFLOWGMetric;
302 
303 static const SFLOWGMetric SFLOWGMetricTable[] = {
304 #define SFLOW_GMETRIC(tag,mname,units,slope,format,group,desc,title) {tag,mname,units,slope,format,group,desc,title},
305 #include "sflow_gmetric.h"
306 #undef SFLOW_GMETRIC
307 };
308 
309 uint16_t init_sflow(cfg_t *config_file);
310 bool_t process_sflow_datagram(apr_sockaddr_t *remotesa, char *buf, apr_size_t len, apr_time_t now, char **errorMsg);
311 
312 
313 #ifdef __cplusplus
314 }
315 #endif /* __cplusplus */
316 #endif /* SFLOW_H */
317