preparing health configuration file parsing
Costa Tsaousis committed
Aug 13, 2016 at 04:31 UTC
d3e9d7418020d77ac3a3494a0a59b4251d3ddf91
4 files changed
+867
-755
src/common.c
+732
-744
@@ -16,25 +16,25 @@ volatile sig_atomic_t netdata_exit = 0;
16
17
char *strdupz(const char *s) {
18
char *t = strdup(s);
19
- if(unlikely(!t)) fatal("Cannot strdup() string '%s'", s);
19
+ if (unlikely(!t)) fatal("Cannot strdup() string '%s'", s);
20
return t;
21
}
22
23
void *mallocz(size_t size) {
24
void *p = malloc(size);
25
- if(unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", size);
25
+ if (unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", size);
26
return p;
27
}
28
29
void *callocz(size_t nmemb, size_t size) {
30
void *p = calloc(nmemb, size);
31
- if(unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", nmemb * size);
31
+ if (unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", nmemb * size);
32
return p;
33
}
34
35
void *reallocz(void *ptr, size_t size) {
36
void *p = realloc(ptr, size);
37
- if(unlikely(!p)) fatal("Cannot re-allocate memory to %zu bytes.", size);
37
+ if (unlikely(!p)) fatal("Cannot re-allocate memory to %zu bytes.", size);
38
return p;
39
}
40
@@ -46,597 +46,596 @@ void freez(void *ptr) {
46
// time functions
47
48
inline unsigned long long timeval_usec(struct timeval *tv) {
49
- return tv->tv_sec * 1000000ULL + tv->tv_usec;
49
+ return tv->tv_sec * 1000000ULL + tv->tv_usec;
50
}
51
52
// time(NULL) in nanoseconds
53
inline unsigned long long time_usec(void) {
54
- struct timeval now;
55
- gettimeofday(&now, NULL);
56
- return timeval_usec(&now);
54
+ struct timeval now;
55
+ gettimeofday(&now, NULL);
56
+ return timeval_usec(&now);
57
}
58
59
inline unsigned long long usec_dt(struct timeval *now, struct timeval *old) {
60
- unsigned long long tv1 = timeval_usec(now);
61
- unsigned long long tv2 = timeval_usec(old);
62
- return (tv1 > tv2)?(tv1 - tv2):(tv2 - tv1);
60
+ unsigned long long tv1 = timeval_usec(now);
61
+ unsigned long long tv2 = timeval_usec(old);
62
+ return (tv1 > tv2) ? (tv1 - tv2) : (tv2 - tv1);
63
}
64
65
int sleep_usec(unsigned long long usec) {
66
67
#ifndef NETDATA_WITH_USLEEP
68
- // we expect microseconds (1.000.000 per second)
69
- // but timespec is nanoseconds (1.000.000.000 per second)
70
- struct timespec rem, req = {
71
- .tv_sec = (time_t)(usec / 1000000),
72
- .tv_nsec = (suseconds_t)((usec % 1000000) * 1000)
68
+ // we expect microseconds (1.000.000 per second)
69
+ // but timespec is nanoseconds (1.000.000.000 per second)
70
+ struct timespec rem, req = {
71
+ .tv_sec = (time_t) (usec / 1000000),
72
+ .tv_nsec = (suseconds_t) ((usec % 1000000) * 1000)
73
};
74
75
- while(nanosleep(&req, &rem) == -1) {
76
- if(likely(errno == EINTR)) {
77
- info("nanosleep() interrupted (while sleeping for %llu microseconds).", usec);
78
- req.tv_sec = rem.tv_sec;
79
- req.tv_nsec = rem.tv_nsec;
80
- }
81
- else {
82
- error("Cannot nanosleep() for %llu microseconds.", usec);
83
- break;
84
- }
85
- }
86
-
87
- return 0;
75
+ while (nanosleep(&req, &rem) == -1) {
76
+ if (likely(errno == EINTR)) {
77
+ info("nanosleep() interrupted (while sleeping for %llu microseconds).", usec);
78
+ req.tv_sec = rem.tv_sec;
79
+ req.tv_nsec = rem.tv_nsec;
80
+ } else {
81
+ error("Cannot nanosleep() for %llu microseconds.", usec);
82
+ break;
83
+ }
84
+ }
85
+
86
+ return 0;
87
#else
89
- int ret = usleep(usec);
90
- if(unlikely(ret == -1 && errno == EINVAL)) {
91
- // on certain systems, usec has to be up to 999999
92
- if(usec > 999999) {
93
- int counter = usec / 999999;
94
- while(counter--)
95
- usleep(999999);
96
-
97
- usleep(usec % 999999);
98
- }
99
- else {
100
- error("Cannot usleep() for %llu microseconds.", usec);
101
- return ret;
102
- }
103
- }
104
-
105
- if(ret != 0)
106
- error("usleep() failed for %llu microseconds.", usec);
107
-
108
- return ret;
88
+ int ret = usleep(usec);
89
+ if(unlikely(ret == -1 && errno == EINVAL)) {
90
+ // on certain systems, usec has to be up to 999999
91
+ if(usec > 999999) {
92
+ int counter = usec / 999999;
93
+ while(counter--)
94
+ usleep(999999);
95
+
96
+ usleep(usec % 999999);
97
+ }
98
+ else {
99
+ error("Cannot usleep() for %llu microseconds.", usec);
100
+ return ret;
101
+ }
102
+ }
103
+
104
+ if(ret != 0)
105
+ error("usleep() failed for %llu microseconds.", usec);
106
+
107
+ return ret;
108
#endif
109
}
110
111
unsigned char netdata_map_chart_names[256] = {
113
- [0] = '\0', //
114
- [1] = '_', //
115
- [2] = '_', //
116
- [3] = '_', //
117
- [4] = '_', //
118
- [5] = '_', //
119
- [6] = '_', //
120
- [7] = '_', //
121
- [8] = '_', //
122
- [9] = '_', //
123
- [10] = '_', //
124
- [11] = '_', //
125
- [12] = '_', //
126
- [13] = '_', //
127
- [14] = '_', //
128
- [15] = '_', //
129
- [16] = '_', //
130
- [17] = '_', //
131
- [18] = '_', //
132
- [19] = '_', //
133
- [20] = '_', //
134
- [21] = '_', //
135
- [22] = '_', //
136
- [23] = '_', //
137
- [24] = '_', //
138
- [25] = '_', //
139
- [26] = '_', //
140
- [27] = '_', //
141
- [28] = '_', //
142
- [29] = '_', //
143
- [30] = '_', //
144
- [31] = '_', //
145
- [32] = '_', //
146
- [33] = '_', // !
147
- [34] = '_', // "
148
- [35] = '_', // #
149
- [36] = '_', // $
150
- [37] = '_', // %
151
- [38] = '_', // &
152
- [39] = '_', // '
153
- [40] = '_', // (
154
- [41] = '_', // )
155
- [42] = '_', // *
156
- [43] = '_', // +
157
- [44] = '.', // ,
158
- [45] = '-', // -
159
- [46] = '.', // .
160
- [47] = '/', // /
161
- [48] = '0', // 0
162
- [49] = '1', // 1
163
- [50] = '2', // 2
164
- [51] = '3', // 3
165
- [52] = '4', // 4
166
- [53] = '5', // 5
167
- [54] = '6', // 6
168
- [55] = '7', // 7
169
- [56] = '8', // 8
170
- [57] = '9', // 9
171
- [58] = '_', // :
172
- [59] = '_', // ;
173
- [60] = '_', // <
174
- [61] = '_', // =
175
- [62] = '_', // >
176
- [63] = '_', // ?
177
- [64] = '_', // @
178
- [65] = 'a', // A
179
- [66] = 'b', // B
180
- [67] = 'c', // C
181
- [68] = 'd', // D
182
- [69] = 'e', // E
183
- [70] = 'f', // F
184
- [71] = 'g', // G
185
- [72] = 'h', // H
186
- [73] = 'i', // I
187
- [74] = 'j', // J
188
- [75] = 'k', // K
189
- [76] = 'l', // L
190
- [77] = 'm', // M
191
- [78] = 'n', // N
192
- [79] = 'o', // O
193
- [80] = 'p', // P
194
- [81] = 'q', // Q
195
- [82] = 'r', // R
196
- [83] = 's', // S
197
- [84] = 't', // T
198
- [85] = 'u', // U
199
- [86] = 'v', // V
200
- [87] = 'w', // W
201
- [88] = 'x', // X
202
- [89] = 'y', // Y
203
- [90] = 'z', // Z
204
- [91] = '_', // [
205
- [92] = '/', // backslash
206
- [93] = '_', // ]
207
- [94] = '_', // ^
208
- [95] = '_', // _
209
- [96] = '_', // `
210
- [97] = 'a', // a
211
- [98] = 'b', // b
212
- [99] = 'c', // c
213
- [100] = 'd', // d
214
- [101] = 'e', // e
215
- [102] = 'f', // f
216
- [103] = 'g', // g
217
- [104] = 'h', // h
218
- [105] = 'i', // i
219
- [106] = 'j', // j
220
- [107] = 'k', // k
221
- [108] = 'l', // l
222
- [109] = 'm', // m
223
- [110] = 'n', // n
224
- [111] = 'o', // o
225
- [112] = 'p', // p
226
- [113] = 'q', // q
227
- [114] = 'r', // r
228
- [115] = 's', // s
229
- [116] = 't', // t
230
- [117] = 'u', // u
231
- [118] = 'v', // v
232
- [119] = 'w', // w
233
- [120] = 'x', // x
234
- [121] = 'y', // y
235
- [122] = 'z', // z
236
- [123] = '_', // {
237
- [124] = '_', // |
238
- [125] = '_', // }
239
- [126] = '_', // ~
240
- [127] = '_', //
241
- [128] = '_', //
242
- [129] = '_', //
243
- [130] = '_', //
244
- [131] = '_', //
245
- [132] = '_', //
246
- [133] = '_', //
247
- [134] = '_', //
248
- [135] = '_', //
249
- [136] = '_', //
250
- [137] = '_', //
251
- [138] = '_', //
252
- [139] = '_', //
253
- [140] = '_', //
254
- [141] = '_', //
255
- [142] = '_', //
256
- [143] = '_', //
257
- [144] = '_', //
258
- [145] = '_', //
259
- [146] = '_', //
260
- [147] = '_', //
261
- [148] = '_', //
262
- [149] = '_', //
263
- [150] = '_', //
264
- [151] = '_', //
265
- [152] = '_', //
266
- [153] = '_', //
267
- [154] = '_', //
268
- [155] = '_', //
269
- [156] = '_', //
270
- [157] = '_', //
271
- [158] = '_', //
272
- [159] = '_', //
273
- [160] = '_', //
274
- [161] = '_', //
275
- [162] = '_', //
276
- [163] = '_', //
277
- [164] = '_', //
278
- [165] = '_', //
279
- [166] = '_', //
280
- [167] = '_', //
281
- [168] = '_', //
282
- [169] = '_', //
283
- [170] = '_', //
284
- [171] = '_', //
285
- [172] = '_', //
286
- [173] = '_', //
287
- [174] = '_', //
288
- [175] = '_', //
289
- [176] = '_', //
290
- [177] = '_', //
291
- [178] = '_', //
292
- [179] = '_', //
293
- [180] = '_', //
294
- [181] = '_', //
295
- [182] = '_', //
296
- [183] = '_', //
297
- [184] = '_', //
298
- [185] = '_', //
299
- [186] = '_', //
300
- [187] = '_', //
301
- [188] = '_', //
302
- [189] = '_', //
303
- [190] = '_', //
304
- [191] = '_', //
305
- [192] = '_', //
306
- [193] = '_', //
307
- [194] = '_', //
308
- [195] = '_', //
309
- [196] = '_', //
310
- [197] = '_', //
311
- [198] = '_', //
312
- [199] = '_', //
313
- [200] = '_', //
314
- [201] = '_', //
315
- [202] = '_', //
316
- [203] = '_', //
317
- [204] = '_', //
318
- [205] = '_', //
319
- [206] = '_', //
320
- [207] = '_', //
321
- [208] = '_', //
322
- [209] = '_', //
323
- [210] = '_', //
324
- [211] = '_', //
325
- [212] = '_', //
326
- [213] = '_', //
327
- [214] = '_', //
328
- [215] = '_', //
329
- [216] = '_', //
330
- [217] = '_', //
331
- [218] = '_', //
332
- [219] = '_', //
333
- [220] = '_', //
334
- [221] = '_', //
335
- [222] = '_', //
336
- [223] = '_', //
337
- [224] = '_', //
338
- [225] = '_', //
339
- [226] = '_', //
340
- [227] = '_', //
341
- [228] = '_', //
342
- [229] = '_', //
343
- [230] = '_', //
344
- [231] = '_', //
345
- [232] = '_', //
346
- [233] = '_', //
347
- [234] = '_', //
348
- [235] = '_', //
349
- [236] = '_', //
350
- [237] = '_', //
351
- [238] = '_', //
352
- [239] = '_', //
353
- [240] = '_', //
354
- [241] = '_', //
355
- [242] = '_', //
356
- [243] = '_', //
357
- [244] = '_', //
358
- [245] = '_', //
359
- [246] = '_', //
360
- [247] = '_', //
361
- [248] = '_', //
362
- [249] = '_', //
363
- [250] = '_', //
364
- [251] = '_', //
365
- [252] = '_', //
366
- [253] = '_', //
367
- [254] = '_', //
368
- [255] = '_' //
112
+ [0] = '\0', //
113
+ [1] = '_', //
114
+ [2] = '_', //
115
+ [3] = '_', //
116
+ [4] = '_', //
117
+ [5] = '_', //
118
+ [6] = '_', //
119
+ [7] = '_', //
120
+ [8] = '_', //
121
+ [9] = '_', //
122
+ [10] = '_', //
123
+ [11] = '_', //
124
+ [12] = '_', //
125
+ [13] = '_', //
126
+ [14] = '_', //
127
+ [15] = '_', //
128
+ [16] = '_', //
129
+ [17] = '_', //
130
+ [18] = '_', //
131
+ [19] = '_', //
132
+ [20] = '_', //
133
+ [21] = '_', //
134
+ [22] = '_', //
135
+ [23] = '_', //
136
+ [24] = '_', //
137
+ [25] = '_', //
138
+ [26] = '_', //
139
+ [27] = '_', //
140
+ [28] = '_', //
141
+ [29] = '_', //
142
+ [30] = '_', //
143
+ [31] = '_', //
144
+ [32] = '_', //
145
+ [33] = '_', // !
146
+ [34] = '_', // "
147
+ [35] = '_', // #
148
+ [36] = '_', // $
149
+ [37] = '_', // %
150
+ [38] = '_', // &
151
+ [39] = '_', // '
152
+ [40] = '_', // (
153
+ [41] = '_', // )
154
+ [42] = '_', // *
155
+ [43] = '_', // +
156
+ [44] = '.', // ,
157
+ [45] = '-', // -
158
+ [46] = '.', // .
159
+ [47] = '/', // /
160
+ [48] = '0', // 0
161
+ [49] = '1', // 1
162
+ [50] = '2', // 2
163
+ [51] = '3', // 3
164
+ [52] = '4', // 4
165
+ [53] = '5', // 5
166
+ [54] = '6', // 6
167
+ [55] = '7', // 7
168
+ [56] = '8', // 8
169
+ [57] = '9', // 9
170
+ [58] = '_', // :
171
+ [59] = '_', // ;
172
+ [60] = '_', // <
173
+ [61] = '_', // =
174
+ [62] = '_', // >
175
+ [63] = '_', // ?
176
+ [64] = '_', // @
177
+ [65] = 'a', // A
178
+ [66] = 'b', // B
179
+ [67] = 'c', // C
180
+ [68] = 'd', // D
181
+ [69] = 'e', // E
182
+ [70] = 'f', // F
183
+ [71] = 'g', // G
184
+ [72] = 'h', // H
185
+ [73] = 'i', // I
186
+ [74] = 'j', // J
187
+ [75] = 'k', // K
188
+ [76] = 'l', // L
189
+ [77] = 'm', // M
190
+ [78] = 'n', // N
191
+ [79] = 'o', // O
192
+ [80] = 'p', // P
193
+ [81] = 'q', // Q
194
+ [82] = 'r', // R
195
+ [83] = 's', // S
196
+ [84] = 't', // T
197
+ [85] = 'u', // U
198
+ [86] = 'v', // V
199
+ [87] = 'w', // W
200
+ [88] = 'x', // X
201
+ [89] = 'y', // Y
202
+ [90] = 'z', // Z
203
+ [91] = '_', // [
204
+ [92] = '/', // backslash
205
+ [93] = '_', // ]
206
+ [94] = '_', // ^
207
+ [95] = '_', // _
208
+ [96] = '_', // `
209
+ [97] = 'a', // a
210
+ [98] = 'b', // b
211
+ [99] = 'c', // c
212
+ [100] = 'd', // d
213
+ [101] = 'e', // e
214
+ [102] = 'f', // f
215
+ [103] = 'g', // g
216
+ [104] = 'h', // h
217
+ [105] = 'i', // i
218
+ [106] = 'j', // j
219
+ [107] = 'k', // k
220
+ [108] = 'l', // l
221
+ [109] = 'm', // m
222
+ [110] = 'n', // n
223
+ [111] = 'o', // o
224
+ [112] = 'p', // p
225
+ [113] = 'q', // q
226
+ [114] = 'r', // r
227
+ [115] = 's', // s
228
+ [116] = 't', // t
229
+ [117] = 'u', // u
230
+ [118] = 'v', // v
231
+ [119] = 'w', // w
232
+ [120] = 'x', // x
233
+ [121] = 'y', // y
234
+ [122] = 'z', // z
235
+ [123] = '_', // {
236
+ [124] = '_', // |
237
+ [125] = '_', // }
238
+ [126] = '_', // ~
239
+ [127] = '_', //
240
+ [128] = '_', //
241
+ [129] = '_', //
242
+ [130] = '_', //
243
+ [131] = '_', //
244
+ [132] = '_', //
245
+ [133] = '_', //
246
+ [134] = '_', //
247
+ [135] = '_', //
248
+ [136] = '_', //
249
+ [137] = '_', //
250
+ [138] = '_', //
251
+ [139] = '_', //
252
+ [140] = '_', //
253
+ [141] = '_', //
254
+ [142] = '_', //
255
+ [143] = '_', //
256
+ [144] = '_', //
257
+ [145] = '_', //
258
+ [146] = '_', //
259
+ [147] = '_', //
260
+ [148] = '_', //
261
+ [149] = '_', //
262
+ [150] = '_', //
263
+ [151] = '_', //
264
+ [152] = '_', //
265
+ [153] = '_', //
266
+ [154] = '_', //
267
+ [155] = '_', //
268
+ [156] = '_', //
269
+ [157] = '_', //
270
+ [158] = '_', //
271
+ [159] = '_', //
272
+ [160] = '_', //
273
+ [161] = '_', //
274
+ [162] = '_', //
275
+ [163] = '_', //
276
+ [164] = '_', //
277
+ [165] = '_', //
278
+ [166] = '_', //
279
+ [167] = '_', //
280
+ [168] = '_', //
281
+ [169] = '_', //
282
+ [170] = '_', //
283
+ [171] = '_', //
284
+ [172] = '_', //
285
+ [173] = '_', //
286
+ [174] = '_', //
287
+ [175] = '_', //
288
+ [176] = '_', //
289
+ [177] = '_', //
290
+ [178] = '_', //
291
+ [179] = '_', //
292
+ [180] = '_', //
293
+ [181] = '_', //
294
+ [182] = '_', //
295
+ [183] = '_', //
296
+ [184] = '_', //
297
+ [185] = '_', //
298
+ [186] = '_', //
299
+ [187] = '_', //
300
+ [188] = '_', //
301
+ [189] = '_', //
302
+ [190] = '_', //
303
+ [191] = '_', //
304
+ [192] = '_', //
305
+ [193] = '_', //
306
+ [194] = '_', //
307
+ [195] = '_', //
308
+ [196] = '_', //
309
+ [197] = '_', //
310
+ [198] = '_', //
311
+ [199] = '_', //
312
+ [200] = '_', //
313
+ [201] = '_', //
314
+ [202] = '_', //
315
+ [203] = '_', //
316
+ [204] = '_', //
317
+ [205] = '_', //
318
+ [206] = '_', //
319
+ [207] = '_', //
320
+ [208] = '_', //
321
+ [209] = '_', //
322
+ [210] = '_', //
323
+ [211] = '_', //
324
+ [212] = '_', //
325
+ [213] = '_', //
326
+ [214] = '_', //
327
+ [215] = '_', //
328
+ [216] = '_', //
329
+ [217] = '_', //
330
+ [218] = '_', //
331
+ [219] = '_', //
332
+ [220] = '_', //
333
+ [221] = '_', //
334
+ [222] = '_', //
335
+ [223] = '_', //
336
+ [224] = '_', //
337
+ [225] = '_', //
338
+ [226] = '_', //
339
+ [227] = '_', //
340
+ [228] = '_', //
341
+ [229] = '_', //
342
+ [230] = '_', //
343
+ [231] = '_', //
344
+ [232] = '_', //
345
+ [233] = '_', //
346
+ [234] = '_', //
347
+ [235] = '_', //
348
+ [236] = '_', //
349
+ [237] = '_', //
350
+ [238] = '_', //
351
+ [239] = '_', //
352
+ [240] = '_', //
353
+ [241] = '_', //
354
+ [242] = '_', //
355
+ [243] = '_', //
356
+ [244] = '_', //
357
+ [245] = '_', //
358
+ [246] = '_', //
359
+ [247] = '_', //
360
+ [248] = '_', //
361
+ [249] = '_', //
362
+ [250] = '_', //
363
+ [251] = '_', //
364
+ [252] = '_', //
365
+ [253] = '_', //
366
+ [254] = '_', //
367
+ [255] = '_' //
368
};
369
370
// make sure the supplied string
371
// is good for a netdata chart/dimension ID/NAME
372
void netdata_fix_chart_name(char *s) {
374
- while((*s = netdata_map_chart_names[(unsigned char)*s])) s++;
373
+ while ((*s = netdata_map_chart_names[(unsigned char) *s])) s++;
374
}
375
376
unsigned char netdata_map_chart_ids[256] = {
378
- [0] = '\0', //
379
- [1] = '_', //
380
- [2] = '_', //
381
- [3] = '_', //
382
- [4] = '_', //
383
- [5] = '_', //
384
- [6] = '_', //
385
- [7] = '_', //
386
- [8] = '_', //
387
- [9] = '_', //
388
- [10] = '_', //
389
- [11] = '_', //
390
- [12] = '_', //
391
- [13] = '_', //
392
- [14] = '_', //
393
- [15] = '_', //
394
- [16] = '_', //
395
- [17] = '_', //
396
- [18] = '_', //
397
- [19] = '_', //
398
- [20] = '_', //
399
- [21] = '_', //
400
- [22] = '_', //
401
- [23] = '_', //
402
- [24] = '_', //
403
- [25] = '_', //
404
- [26] = '_', //
405
- [27] = '_', //
406
- [28] = '_', //
407
- [29] = '_', //
408
- [30] = '_', //
409
- [31] = '_', //
410
- [32] = '_', //
411
- [33] = '_', // !
412
- [34] = '_', // "
413
- [35] = '_', // #
414
- [36] = '_', // $
415
- [37] = '_', // %
416
- [38] = '_', // &
417
- [39] = '_', // '
418
- [40] = '_', // (
419
- [41] = '_', // )
420
- [42] = '_', // *
421
- [43] = '_', // +
422
- [44] = '.', // ,
423
- [45] = '-', // -
424
- [46] = '.', // .
425
- [47] = '_', // /
426
- [48] = '0', // 0
427
- [49] = '1', // 1
428
- [50] = '2', // 2
429
- [51] = '3', // 3
430
- [52] = '4', // 4
431
- [53] = '5', // 5
432
- [54] = '6', // 6
433
- [55] = '7', // 7
434
- [56] = '8', // 8
435
- [57] = '9', // 9
436
- [58] = '_', // :
437
- [59] = '_', // ;
438
- [60] = '_', // <
439
- [61] = '_', // =
440
- [62] = '_', // >
441
- [63] = '_', // ?
442
- [64] = '_', // @
443
- [65] = 'a', // A
444
- [66] = 'b', // B
445
- [67] = 'c', // C
446
- [68] = 'd', // D
447
- [69] = 'e', // E
448
- [70] = 'f', // F
449
- [71] = 'g', // G
450
- [72] = 'h', // H
451
- [73] = 'i', // I
452
- [74] = 'j', // J
453
- [75] = 'k', // K
454
- [76] = 'l', // L
455
- [77] = 'm', // M
456
- [78] = 'n', // N
457
- [79] = 'o', // O
458
- [80] = 'p', // P
459
- [81] = 'q', // Q
460
- [82] = 'r', // R
461
- [83] = 's', // S
462
- [84] = 't', // T
463
- [85] = 'u', // U
464
- [86] = 'v', // V
465
- [87] = 'w', // W
466
- [88] = 'x', // X
467
- [89] = 'y', // Y
468
- [90] = 'z', // Z
469
- [91] = '_', // [
470
- [92] = '/', // backslash
471
- [93] = '_', // ]
472
- [94] = '_', // ^
473
- [95] = '_', // _
474
- [96] = '_', // `
475
- [97] = 'a', // a
476
- [98] = 'b', // b
477
- [99] = 'c', // c
478
- [100] = 'd', // d
479
- [101] = 'e', // e
480
- [102] = 'f', // f
481
- [103] = 'g', // g
482
- [104] = 'h', // h
483
- [105] = 'i', // i
484
- [106] = 'j', // j
485
- [107] = 'k', // k
486
- [108] = 'l', // l
487
- [109] = 'm', // m
488
- [110] = 'n', // n
489
- [111] = 'o', // o
490
- [112] = 'p', // p
491
- [113] = 'q', // q
492
- [114] = 'r', // r
493
- [115] = 's', // s
494
- [116] = 't', // t
495
- [117] = 'u', // u
496
- [118] = 'v', // v
497
- [119] = 'w', // w
498
- [120] = 'x', // x
499
- [121] = 'y', // y
500
- [122] = 'z', // z
501
- [123] = '_', // {
502
- [124] = '_', // |
503
- [125] = '_', // }
504
- [126] = '_', // ~
505
- [127] = '_', //
506
- [128] = '_', //
507
- [129] = '_', //
508
- [130] = '_', //
509
- [131] = '_', //
510
- [132] = '_', //
511
- [133] = '_', //
512
- [134] = '_', //
513
- [135] = '_', //
514
- [136] = '_', //
515
- [137] = '_', //
516
- [138] = '_', //
517
- [139] = '_', //
518
- [140] = '_', //
519
- [141] = '_', //
520
- [142] = '_', //
521
- [143] = '_', //
522
- [144] = '_', //
523
- [145] = '_', //
524
- [146] = '_', //
525
- [147] = '_', //
526
- [148] = '_', //
527
- [149] = '_', //
528
- [150] = '_', //
529
- [151] = '_', //
530
- [152] = '_', //
531
- [153] = '_', //
532
- [154] = '_', //
533
- [155] = '_', //
534
- [156] = '_', //
535
- [157] = '_', //
536
- [158] = '_', //
537
- [159] = '_', //
538
- [160] = '_', //
539
- [161] = '_', //
540
- [162] = '_', //
541
- [163] = '_', //
542
- [164] = '_', //
543
- [165] = '_', //
544
- [166] = '_', //
545
- [167] = '_', //
546
- [168] = '_', //
547
- [169] = '_', //
548
- [170] = '_', //
549
- [171] = '_', //
550
- [172] = '_', //
551
- [173] = '_', //
552
- [174] = '_', //
553
- [175] = '_', //
554
- [176] = '_', //
555
- [177] = '_', //
556
- [178] = '_', //
557
- [179] = '_', //
558
- [180] = '_', //
559
- [181] = '_', //
560
- [182] = '_', //
561
- [183] = '_', //
562
- [184] = '_', //
563
- [185] = '_', //
564
- [186] = '_', //
565
- [187] = '_', //
566
- [188] = '_', //
567
- [189] = '_', //
568
- [190] = '_', //
569
- [191] = '_', //
570
- [192] = '_', //
571
- [193] = '_', //
572
- [194] = '_', //
573
- [195] = '_', //
574
- [196] = '_', //
575
- [197] = '_', //
576
- [198] = '_', //
577
- [199] = '_', //
578
- [200] = '_', //
579
- [201] = '_', //
580
- [202] = '_', //
581
- [203] = '_', //
582
- [204] = '_', //
583
- [205] = '_', //
584
- [206] = '_', //
585
- [207] = '_', //
586
- [208] = '_', //
587
- [209] = '_', //
588
- [210] = '_', //
589
- [211] = '_', //
590
- [212] = '_', //
591
- [213] = '_', //
592
- [214] = '_', //
593
- [215] = '_', //
594
- [216] = '_', //
595
- [217] = '_', //
596
- [218] = '_', //
597
- [219] = '_', //
598
- [220] = '_', //
599
- [221] = '_', //
600
- [222] = '_', //
601
- [223] = '_', //
602
- [224] = '_', //
603
- [225] = '_', //
604
- [226] = '_', //
605
- [227] = '_', //
606
- [228] = '_', //
607
- [229] = '_', //
608
- [230] = '_', //
609
- [231] = '_', //
610
- [232] = '_', //
611
- [233] = '_', //
612
- [234] = '_', //
613
- [235] = '_', //
614
- [236] = '_', //
615
- [237] = '_', //
616
- [238] = '_', //
617
- [239] = '_', //
618
- [240] = '_', //
619
- [241] = '_', //
620
- [242] = '_', //
621
- [243] = '_', //
622
- [244] = '_', //
623
- [245] = '_', //
624
- [246] = '_', //
625
- [247] = '_', //
626
- [248] = '_', //
627
- [249] = '_', //
628
- [250] = '_', //
629
- [251] = '_', //
630
- [252] = '_', //
631
- [253] = '_', //
632
- [254] = '_', //
633
- [255] = '_' //
377
+ [0] = '\0', //
378
+ [1] = '_', //
379
+ [2] = '_', //
380
+ [3] = '_', //
381
+ [4] = '_', //
382
+ [5] = '_', //
383
+ [6] = '_', //
384
+ [7] = '_', //
385
+ [8] = '_', //
386
+ [9] = '_', //
387
+ [10] = '_', //
388
+ [11] = '_', //
389
+ [12] = '_', //
390
+ [13] = '_', //
391
+ [14] = '_', //
392
+ [15] = '_', //
393
+ [16] = '_', //
394
+ [17] = '_', //
395
+ [18] = '_', //
396
+ [19] = '_', //
397
+ [20] = '_', //
398
+ [21] = '_', //
399
+ [22] = '_', //
400
+ [23] = '_', //
401
+ [24] = '_', //
402
+ [25] = '_', //
403
+ [26] = '_', //
404
+ [27] = '_', //
405
+ [28] = '_', //
406
+ [29] = '_', //
407
+ [30] = '_', //
408
+ [31] = '_', //
409
+ [32] = '_', //
410
+ [33] = '_', // !
411
+ [34] = '_', // "
412
+ [35] = '_', // #
413
+ [36] = '_', // $
414
+ [37] = '_', // %
415
+ [38] = '_', // &
416
+ [39] = '_', // '
417
+ [40] = '_', // (
418
+ [41] = '_', // )
419
+ [42] = '_', // *
420
+ [43] = '_', // +
421
+ [44] = '.', // ,
422
+ [45] = '-', // -
423
+ [46] = '.', // .
424
+ [47] = '_', // /
425
+ [48] = '0', // 0
426
+ [49] = '1', // 1
427
+ [50] = '2', // 2
428
+ [51] = '3', // 3
429
+ [52] = '4', // 4
430
+ [53] = '5', // 5
431
+ [54] = '6', // 6
432
+ [55] = '7', // 7
433
+ [56] = '8', // 8
434
+ [57] = '9', // 9
435
+ [58] = '_', // :
436
+ [59] = '_', // ;
437
+ [60] = '_', // <
438
+ [61] = '_', // =
439
+ [62] = '_', // >
440
+ [63] = '_', // ?
441
+ [64] = '_', // @
442
+ [65] = 'a', // A
443
+ [66] = 'b', // B
444
+ [67] = 'c', // C
445
+ [68] = 'd', // D
446
+ [69] = 'e', // E
447
+ [70] = 'f', // F
448
+ [71] = 'g', // G
449
+ [72] = 'h', // H
450
+ [73] = 'i', // I
451
+ [74] = 'j', // J
452
+ [75] = 'k', // K
453
+ [76] = 'l', // L
454
+ [77] = 'm', // M
455
+ [78] = 'n', // N
456
+ [79] = 'o', // O
457
+ [80] = 'p', // P
458
+ [81] = 'q', // Q
459
+ [82] = 'r', // R
460
+ [83] = 's', // S
461
+ [84] = 't', // T
462
+ [85] = 'u', // U
463
+ [86] = 'v', // V
464
+ [87] = 'w', // W
465
+ [88] = 'x', // X
466
+ [89] = 'y', // Y
467
+ [90] = 'z', // Z
468
+ [91] = '_', // [
469
+ [92] = '/', // backslash
470
+ [93] = '_', // ]
471
+ [94] = '_', // ^
472
+ [95] = '_', // _
473
+ [96] = '_', // `
474
+ [97] = 'a', // a
475
+ [98] = 'b', // b
476
+ [99] = 'c', // c
477
+ [100] = 'd', // d
478
+ [101] = 'e', // e
479
+ [102] = 'f', // f
480
+ [103] = 'g', // g
481
+ [104] = 'h', // h
482
+ [105] = 'i', // i
483
+ [106] = 'j', // j
484
+ [107] = 'k', // k
485
+ [108] = 'l', // l
486
+ [109] = 'm', // m
487
+ [110] = 'n', // n
488
+ [111] = 'o', // o
489
+ [112] = 'p', // p
490
+ [113] = 'q', // q
491
+ [114] = 'r', // r
492
+ [115] = 's', // s
493
+ [116] = 't', // t
494
+ [117] = 'u', // u
495
+ [118] = 'v', // v
496
+ [119] = 'w', // w
497
+ [120] = 'x', // x
498
+ [121] = 'y', // y
499
+ [122] = 'z', // z
500
+ [123] = '_', // {
501
+ [124] = '_', // |
502
+ [125] = '_', // }
503
+ [126] = '_', // ~
504
+ [127] = '_', //
505
+ [128] = '_', //
506
+ [129] = '_', //
507
+ [130] = '_', //
508
+ [131] = '_', //
509
+ [132] = '_', //
510
+ [133] = '_', //
511
+ [134] = '_', //
512
+ [135] = '_', //
513
+ [136] = '_', //
514
+ [137] = '_', //
515
+ [138] = '_', //
516
+ [139] = '_', //
517
+ [140] = '_', //
518
+ [141] = '_', //
519
+ [142] = '_', //
520
+ [143] = '_', //
521
+ [144] = '_', //
522
+ [145] = '_', //
523
+ [146] = '_', //
524
+ [147] = '_', //
525
+ [148] = '_', //
526
+ [149] = '_', //
527
+ [150] = '_', //
528
+ [151] = '_', //
529
+ [152] = '_', //
530
+ [153] = '_', //
531
+ [154] = '_', //
532
+ [155] = '_', //
533
+ [156] = '_', //
534
+ [157] = '_', //
535
+ [158] = '_', //
536
+ [159] = '_', //
537
+ [160] = '_', //
538
+ [161] = '_', //
539
+ [162] = '_', //
540
+ [163] = '_', //
541
+ [164] = '_', //
542
+ [165] = '_', //
543
+ [166] = '_', //
544
+ [167] = '_', //
545
+ [168] = '_', //
546
+ [169] = '_', //
547
+ [170] = '_', //
548
+ [171] = '_', //
549
+ [172] = '_', //
550
+ [173] = '_', //
551
+ [174] = '_', //
552
+ [175] = '_', //
553
+ [176] = '_', //
554
+ [177] = '_', //
555
+ [178] = '_', //
556
+ [179] = '_', //
557
+ [180] = '_', //
558
+ [181] = '_', //
559
+ [182] = '_', //
560
+ [183] = '_', //
561
+ [184] = '_', //
562
+ [185] = '_', //
563
+ [186] = '_', //
564
+ [187] = '_', //
565
+ [188] = '_', //
566
+ [189] = '_', //
567
+ [190] = '_', //
568
+ [191] = '_', //
569
+ [192] = '_', //
570
+ [193] = '_', //
571
+ [194] = '_', //
572
+ [195] = '_', //
573
+ [196] = '_', //
574
+ [197] = '_', //
575
+ [198] = '_', //
576
+ [199] = '_', //
577
+ [200] = '_', //
578
+ [201] = '_', //
579
+ [202] = '_', //
580
+ [203] = '_', //
581
+ [204] = '_', //
582
+ [205] = '_', //
583
+ [206] = '_', //
584
+ [207] = '_', //
585
+ [208] = '_', //
586
+ [209] = '_', //
587
+ [210] = '_', //
588
+ [211] = '_', //
589
+ [212] = '_', //
590
+ [213] = '_', //
591
+ [214] = '_', //
592
+ [215] = '_', //
593
+ [216] = '_', //
594
+ [217] = '_', //
595
+ [218] = '_', //
596
+ [219] = '_', //
597
+ [220] = '_', //
598
+ [221] = '_', //
599
+ [222] = '_', //
600
+ [223] = '_', //
601
+ [224] = '_', //
602
+ [225] = '_', //
603
+ [226] = '_', //
604
+ [227] = '_', //
605
+ [228] = '_', //
606
+ [229] = '_', //
607
+ [230] = '_', //
608
+ [231] = '_', //
609
+ [232] = '_', //
610
+ [233] = '_', //
611
+ [234] = '_', //
612
+ [235] = '_', //
613
+ [236] = '_', //
614
+ [237] = '_', //
615
+ [238] = '_', //
616
+ [239] = '_', //
617
+ [240] = '_', //
618
+ [241] = '_', //
619
+ [242] = '_', //
620
+ [243] = '_', //
621
+ [244] = '_', //
622
+ [245] = '_', //
623
+ [246] = '_', //
624
+ [247] = '_', //
625
+ [248] = '_', //
626
+ [249] = '_', //
627
+ [250] = '_', //
628
+ [251] = '_', //
629
+ [252] = '_', //
630
+ [253] = '_', //
631
+ [254] = '_', //
632
+ [255] = '_' //
633
};
634
635
// make sure the supplied string
636
// is good for a netdata chart/dimension ID/NAME
637
void netdata_fix_chart_id(char *s) {
639
- while((*s = netdata_map_chart_ids[(unsigned char)*s])) s++;
638
+ while ((*s = netdata_map_chart_ids[(unsigned char) *s])) s++;
639
}
640
641
/*
@@ -658,37 +657,37 @@ uint32_t simple_hash(const char *name)
657
658
// http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
659
uint32_t simple_hash(const char *name) {
661
- unsigned char *s = (unsigned char *)name;
662
- uint32_t hval = 0x811c9dc5;
663
-
664
- // FNV-1a algorithm
665
- while (*s) {
666
- // multiply by the 32 bit FNV magic prime mod 2^32
667
- // NOTE: No need to optimize with left shifts.
668
- // GCC will use imul instruction anyway.
669
- // Tested with 'gcc -O3 -S'
670
- //hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
671
- hval *= 16777619;
672
-
673
- // xor the bottom with the current octet
674
- hval ^= (uint32_t)*s++;
675
- }
676
-
677
- // fprintf(stderr, "HASH: %u = %s\n", hval, name);
678
- return hval;
660
+ unsigned char *s = (unsigned char *) name;
661
+ uint32_t hval = 0x811c9dc5;
662
+
663
+ // FNV-1a algorithm
664
+ while (*s) {
665
+ // multiply by the 32 bit FNV magic prime mod 2^32
666
+ // NOTE: No need to optimize with left shifts.
667
+ // GCC will use imul instruction anyway.
668
+ // Tested with 'gcc -O3 -S'
669
+ //hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
670
+ hval *= 16777619;
671
+
672
+ // xor the bottom with the current octet
673
+ hval ^= (uint32_t) *s++;
674
+ }
675
+
676
+ // fprintf(stderr, "HASH: %u = %s\n", hval, name);
677
+ return hval;
678
}
679
680
uint32_t simple_uhash(const char *name) {
682
- unsigned char *s = (unsigned char *)name;
683
- uint32_t hval = 0x811c9dc5, c;
684
-
685
- // FNV-1a algorithm
686
- while((c = *s++)) {
687
- if(unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
688
- hval *= 16777619;
689
- hval ^= c;
690
- }
691
- return hval;
681
+ unsigned char *s = (unsigned char *) name;
682
+ uint32_t hval = 0x811c9dc5, c;
683
+
684
+ // FNV-1a algorithm
685
+ while ((c = *s++)) {
686
+ if (unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
687
+ hval *= 16777619;
688
+ hval ^= c;
689
+ }
690
+ return hval;
691
}
692
693
/*
@@ -714,80 +713,74 @@ uint32_t simple_hash(const char *name) {
713
}
714
*/
715
717
-void strreverse(char* begin, char* end)
718
-{
719
- char aux;
720
-
721
- while (end > begin)
722
- {
723
- // clearer code.
724
- aux = *end;
725
- *end-- = *begin;
726
- *begin++ = aux;
727
- }
716
+void strreverse(char *begin, char *end) {
717
+ char aux;
718
+
719
+ while (end > begin) {
720
+ // clearer code.
721
+ aux = *end;
722
+ *end-- = *begin;
723
+ *begin++ = aux;
724
+ }
725
}
726
730
-char *mystrsep(char **ptr, char *s)
731
-{
732
- char *p = "";
733
- while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
734
- return(p);
727
+char *mystrsep(char **ptr, char *s) {
728
+ char *p = "";
729
+ while (p && !p[0] && *ptr) p = strsep(ptr, s);
730
+ return (p);
731
}
732
737
-char *trim(char *s)
738
-{
739
- // skip leading spaces
740
- // and 'comments' as well!?
741
- while(*s && isspace(*s)) s++;
742
- if(!*s || *s == '#') return NULL;
743
-
744
- // skip tailing spaces
745
- // this way is way faster. Writes only one NUL char.
746
- ssize_t l = strlen(s);
747
- if (--l >= 0)
748
- {
749
- char *p = s + l;
750
- while (p > s && isspace(*p)) p--;
751
- *++p = '\0';
752
- }
733
+char *trim(char *s) {
734
+ // skip leading spaces
735
+ // and 'comments' as well!?
736
+ while (*s && isspace(*s)) s++;
737
+ if (!*s || *s == '#') return NULL;
738
754
- if(!*s) return NULL;
739
+ // skip tailing spaces
740
+ // this way is way faster. Writes only one NUL char.
741
+ ssize_t l = strlen(s);
742
+ if (--l >= 0) {
743
+ char *p = s + l;
744
+ while (p > s && isspace(*p)) p--;
745
+ *++p = '\0';
746
+ }
747
756
- return s;
748
+ if (!*s) return NULL;
749
+
750
+ return s;
751
}
752
753
void *mymmap(const char *filename, size_t size, int flags, int ksm) {
760
- static int log_madvise_1 = 1;
754
+ static int log_madvise_1 = 1;
755
#ifdef MADV_MERGEABLE
762
- static int log_madvise_2 = 1, log_madvise_3 = 1;
756
+ static int log_madvise_2 = 1, log_madvise_3 = 1;
757
#endif
764
- int fd;
765
- void *mem = NULL;
758
+ int fd;
759
+ void *mem = NULL;
760
767
- errno = 0;
768
- fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
769
- if(fd != -1) {
770
- if(lseek(fd, size, SEEK_SET) == (off_t)size) {
771
- if(write(fd, "", 1) == 1) {
772
- if(ftruncate(fd, size))
773
- error("Cannot truncate file '%s' to size %zu. Will use the larger file.", filename, size);
761
+ errno = 0;
762
+ fd = open(filename, O_RDWR | O_CREAT | O_NOATIME, 0664);
763
+ if (fd != -1) {
764
+ if (lseek(fd, size, SEEK_SET) == (off_t) size) {
765
+ if (write(fd, "", 1) == 1) {
766
+ if (ftruncate(fd, size))
767
+ error("Cannot truncate file '%s' to size %zu. Will use the larger file.", filename, size);
768
769
#ifdef MADV_MERGEABLE
776
- if(flags & MAP_SHARED || !enable_ksm || !ksm) {
770
+ if (flags & MAP_SHARED || !enable_ksm || !ksm) {
771
#endif
778
- mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
779
- if(mem != MAP_FAILED) {
780
- int advise = MADV_SEQUENTIAL|MADV_DONTFORK;
781
- if(flags & MAP_SHARED) advise |= MADV_WILLNEED;
782
-
783
- if(madvise(mem, size, advise) != 0 && log_madvise_1) {
784
- error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
785
- log_madvise_1--;
786
- }
787
- }
772
+ mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
773
+ if (mem != MAP_FAILED) {
774
+ int advise = MADV_SEQUENTIAL | MADV_DONTFORK;
775
+ if (flags & MAP_SHARED) advise |= MADV_WILLNEED;
776
+
777
+ if (madvise(mem, size, advise) != 0 && log_madvise_1) {
778
+ error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
779
+ log_madvise_1--;
780
+ }
781
+ }
782
#ifdef MADV_MERGEABLE
789
- }
790
- else {
783
+ } else {
784
/*
785
// test - load the file into memory
786
mem = calloc(1, size);
@@ -800,68 +793,67 @@ void *mymmap(const char *filename, size_t size, int flags, int ksm) {
793
error("Cannot seek to beginning of file '%s'.", filename);
794
}
795
*/
803
- mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags|MAP_ANONYMOUS, -1, 0);
804
- if(mem != MAP_FAILED) {
805
- if(lseek(fd, 0, SEEK_SET) == 0) {
806
- if(read(fd, mem, size) != (ssize_t)size)
807
- error("Cannot read from file '%s'", filename);
808
- }
809
- else
810
- error("Cannot seek to beginning of file '%s'.", filename);
811
-
812
- // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
813
- if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK) != 0 && log_madvise_2) {
814
- error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.", filename);
815
- log_madvise_2--;
816
- }
817
-
818
- if(madvise(mem, size, MADV_MERGEABLE) != 0 && log_madvise_3) {
819
- error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.", filename);
820
- log_madvise_3--;
821
- }
822
- }
823
- else
824
- error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
825
- }
796
+ mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flags | MAP_ANONYMOUS, -1, 0);
797
+ if (mem != MAP_FAILED) {
798
+ if (lseek(fd, 0, SEEK_SET) == 0) {
799
+ if (read(fd, mem, size) != (ssize_t) size)
800
+ error("Cannot read from file '%s'", filename);
801
+ } else
802
+ error("Cannot seek to beginning of file '%s'.", filename);
803
+
804
+ // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
805
+ if (madvise(mem, size, MADV_SEQUENTIAL | MADV_DONTFORK) != 0 && log_madvise_2) {
806
+ error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.",
807
+ filename);
808
+ log_madvise_2--;
809
+ }
810
+
811
+ if (madvise(mem, size, MADV_MERGEABLE) != 0 && log_madvise_3) {
812
+ error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.",
813
+ filename);
814
+ log_madvise_3--;
815
+ }
816
+ } else
817
+ error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
818
+ }
819
#endif
827
- }
828
- else error("Cannot write to file '%s' at position %zu.", filename, size);
829
- }
830
- else error("Cannot seek file '%s' to size %zu.", filename, size);
820
+ } else
821
+ error("Cannot write to file '%s' at position %zu.", filename, size);
822
+ } else
823
+ error("Cannot seek file '%s' to size %zu.", filename, size);
824
832
- close(fd);
833
- }
834
- else error("Cannot create/open file '%s'.", filename);
825
+ close(fd);
826
+ } else
827
+ error("Cannot create/open file '%s'.", filename);
828
836
- return mem;
829
+ return mem;
830
}
831
839
-int savememory(const char *filename, void *mem, size_t size)
840
-{
841
- char tmpfilename[FILENAME_MAX + 1];
832
+int savememory(const char *filename, void *mem, size_t size) {
833
+ char tmpfilename[FILENAME_MAX + 1];
834
843
- snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
835
+ snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long) getpid());
836
845
- int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
846
- if(fd < 0) {
847
- error("Cannot create/open file '%s'.", filename);
848
- return -1;
849
- }
837
+ int fd = open(tmpfilename, O_RDWR | O_CREAT | O_NOATIME, 0664);
838
+ if (fd < 0) {
839
+ error("Cannot create/open file '%s'.", filename);
840
+ return -1;
841
+ }
842
851
- if(write(fd, mem, size) != (ssize_t)size) {
852
- error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
853
- close(fd);
854
- return -1;
855
- }
843
+ if (write(fd, mem, size) != (ssize_t) size) {
844
+ error("Cannot write to file '%s' %ld bytes.", filename, (long) size);
845
+ close(fd);
846
+ return -1;
847
+ }
848
857
- close(fd);
849
+ close(fd);
850
859
- if(rename(tmpfilename, filename)) {
860
- error("Cannot rename '%s' to '%s'", tmpfilename, filename);
861
- return -1;
862
- }
851
+ if (rename(tmpfilename, filename)) {
852
+ error("Cannot rename '%s' to '%s'", tmpfilename, filename);
853
+ return -1;
854
+ }
855
864
- return 0;
856
+ return 0;
857
}
858
859
int fd_is_valid(int fd) {
@@ -875,75 +867,71 @@ int fd_is_valid(int fd) {
867
*/
868
unsigned int hz;
869
878
-void get_HZ(void)
879
-{
880
- long ticks;
870
+void get_HZ(void) {
871
+ long ticks;
872
882
- if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
883
- perror("sysconf");
884
- }
873
+ if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
874
+ perror("sysconf");
875
+ }
876
886
- hz = (unsigned int) ticks;
877
+ hz = (unsigned int) ticks;
878
}
879
889
-pid_t gettid(void)
890
-{
891
- return syscall(SYS_gettid);
880
+pid_t gettid(void) {
881
+ return (pid_t)syscall(SYS_gettid);
882
}
883
884
char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
895
- char *s = fgets(buf, buf_size, fp);
896
- if(!s) return NULL;
885
+ char *s = fgets(buf, (int)buf_size, fp);
886
+ if (!s) return NULL;
887
898
- char *t = s;
899
- if(*t != '\0') {
900
- // find the string end
901
- while (*++t != '\0');
888
+ char *t = s;
889
+ if (*t != '\0') {
890
+ // find the string end
891
+ while (*++t != '\0');
892
903
- // trim trailing spaces/newlines/tabs
904
- while (--t > s && *t == '\n')
905
- *t = '\0';
906
- }
893
+ // trim trailing spaces/newlines/tabs
894
+ while (--t > s && *t == '\n')
895
+ *t = '\0';
896
+ }
897
908
- if(len)
909
- *len = t - s + 1;
898
+ if (len)
899
+ *len = t - s + 1;
900
911
- return s;
901
+ return s;
902
}
903
904
char *strncpyz(char *dst, const char *src, size_t n) {
915
- char *p = dst;
905
+ char *p = dst;
906
917
- while(*src && n--)
918
- *dst++ = *src++;
907
+ while (*src && n--)
908
+ *dst++ = *src++;
909
920
- *dst = '\0';
910
+ *dst = '\0';
911
922
- return p;
912
+ return p;
913
}
914
915
int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
926
- int size;
916
+ int size = vsnprintf(dst, n, fmt, args);
917
928
- size = vsnprintf(dst, n, fmt, args);
929
-
930
- if(unlikely((size_t)size > n)) {
931
- // there is bug in vsnprintf() and it returns
932
- // a number higher to len, but it does not
933
- // overflow the buffer.
934
- size = n;
935
- }
918
+ if (unlikely((size_t) size > n)) {
919
+ // there is bug in vsnprintf() and it returns
920
+ // a number higher to len, but it does not
921
+ // overflow the buffer.
922
+ size = (int)n;
923
+ }
924
937
- dst[size] = '\0';
938
- return size;
925
+ dst[size] = '\0';
926
+ return size;
927
}
928
929
int snprintfz(char *dst, size_t n, const char *fmt, ...) {
942
- va_list args;
930
+ va_list args;
931
944
- va_start(args, fmt);
945
- int ret = vsnprintfz(dst, n, fmt, args);
946
- va_end(args);
932
+ va_start(args, fmt);
933
+ int ret = vsnprintfz(dst, n, fmt, args);
934
+ va_end(args);
935
948
- return ret;
936
+ return ret;
937
}
src/health.c
+118
@@ -656,3 +656,121 @@ void rrdcalc_free(RRDHOST *host, RRDCALC *rc) {
656
657
freez(rc);
658
}
659
+
660
+// ----------------------------------------------------------------------------
661
+// load health configuration
662
+
663
+#define HEALTH_CONF_MAX_LINE 4096
664
+
665
+int health_readfile(const char *path, const char *filename) {
666
+ char buffer[HEALTH_CONF_MAX_LINE + 1];
667
+
668
+ info("Reading file '%s/%s'", path, filename);
669
+
670
+ snprintfz(buffer, HEALTH_CONF_MAX_LINE, "%s/%s", path, filename);
671
+ FILE *fp = fopen(buffer, "r");
672
+ if(!fp) {
673
+ error("Cannot read file '%s' for reading health configuration.", buffer);
674
+ return 0;
675
+ }
676
+
677
+ size_t line = 0, append = 0;
678
+ char *s;
679
+ while((s = fgets(&buffer[append], (int)(HEALTH_CONF_MAX_LINE - append), fp)) || append) {
680
+ int stop_appending = !s;
681
+ line++;
682
+ // info("Line %zu of file '%s/%s': '%s'", line, path, filename, s);
683
+ s = trim(buffer);
684
+ if(!s) continue;
685
+ // info("Trimmed line %zu of file '%s/%s': '%s'", line, path, filename, s);
686
+
687
+ append = strlen(s);
688
+ if(!stop_appending && s[append - 1] == '\\') {
689
+ s[append - 1] = ' ';
690
+ append = &s[append] - buffer;
691
+ if(append < HEALTH_CONF_MAX_LINE)
692
+ continue;
693
+ continue;
694
+ }
695
+ append = 0;
696
+
697
+ char *key = s;
698
+ while(*s && *s != ':') s++;
699
+ if(!*s) {
700
+ error("Invalid line %zu of file '%s/%s'. It does not contain a ':'. Ignoring it.", line, path, filename);
701
+ continue;
702
+ }
703
+ *s = '\0';
704
+ s++;
705
+
706
+ char *value = s;
707
+ key = trim(key);
708
+ value = trim(value);
709
+
710
+ if(!key) {
711
+ error("Invalid line %zu of file '%s/%s'. Keyword is empty. Ignoring it.", line, path, filename);
712
+ continue;
713
+ }
714
+
715
+ if(!value) {
716
+ error("Invalid line %zu of file '%s/%s'. value is empty. Ignoring it.", line, path, filename);
717
+ continue;
718
+ }
719
+
720
+ // info("Health file '%s/%s', key '%s', value '%s'", path, filename, key, value);
721
+ }
722
+
723
+ fclose(fp);
724
+ return 1;
725
+}
726
+
727
+void health_readdir(const char *path) {
728
+ size_t pathlen = strlen(path);
729
+
730
+ info("Reading directory '%s'", path);
731
+
732
+ DIR *dir = opendir(path);
733
+ if (!dir) {
734
+ error("Cannot open directory '%s' for reading health configuration files.", path);
735
+ return;
736
+ }
737
+
738
+ struct dirent *de = NULL;
739
+ while ((de = readdir(dir))) {
740
+ size_t len = strlen(de->d_name);
741
+
742
+ if(de->d_type == DT_DIR
743
+ && (
744
+ (de->d_name[0] == '.' && de->d_name[1] == '\0')
745
+ || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
746
+ ))
747
+ continue;
748
+
749
+ else if(de->d_type == DT_DIR) {
750
+ char *s = mallocz(pathlen + strlen(de->d_name) + 2);
751
+ strcpy(s, path);
752
+ strcat(s, "/");
753
+ strcat(s, de->d_name);
754
+ health_readdir(s);
755
+ freez(s);
756
+ continue;
757
+ }
758
+
759
+ else if((de->d_type == DT_LNK || de->d_type == DT_REG) &&
760
+ len > 5 && !strcmp(&de->d_name[len - 5], ".conf")) {
761
+ health_readfile(path, de->d_name);
762
+ }
763
+ }
764
+}
765
+
766
+void health_init(void) {
767
+ char *path;
768
+
769
+ {
770
+ char buffer[FILENAME_MAX + 1];
771
+ snprintfz(buffer, FILENAME_MAX, "%s/health.d", config_get("global", "config directory", CONFIG_DIR));
772
+ path = config_get("health", "configuration files in directory", buffer);
773
+ }
774
+
775
+ health_readdir(path);
776
+}
src/health.h
+2
@@ -210,4 +210,6 @@ extern void rrddimvar_free(RRDDIMVAR *rs);
210
extern void rrdsetcalc_link_matching(RRDSET *st);
211
extern void rrdsetcalc_unlink(RRDCALC *rc);
212
213
+extern void health_init(void);
214
+
215
#endif //NETDATA_HEALTH_H
src/main.c
+15
-11
@@ -363,10 +363,9 @@ int main(int argc, char **argv)
363
// TODO: Outsource version to makefile which can compute version from git.
364
printf("netdata 1.2.1_master\n");
365
return 0;
366
- break;
366
case 'W':
367
{
369
- char* stacksize = "stacksize=";
368
+ char* stacksize_string = "stacksize=";
369
char* debug_flags_string = "debug_flags=";
370
if(strcmp(optarg, "unittest") == 0) {
371
rrd_update_every = 1;
@@ -374,8 +373,8 @@ int main(int argc, char **argv)
373
if(unit_test_storage()) exit(1);
374
fprintf(stderr, "\n\nALL TESTS PASSED\n\n");
375
exit(0);
377
- } else if(strncmp(optarg, stacksize, strlen(stacksize)) == 0) {
378
- optarg += strlen(stacksize);
376
+ } else if(strncmp(optarg, stacksize_string, strlen(stacksize_string)) == 0) {
377
+ optarg += strlen(stacksize_string);
378
config_set("global", "pthread stack size", optarg);
379
} else if(strncmp(optarg, debug_flags_string, strlen(debug_flags_string)) == 0) {
380
optarg += strlen(debug_flags_string);
@@ -564,7 +563,7 @@ int main(int argc, char **argv)
563
else
564
debug(D_OPTIONS, "initial pthread stack size is %zu bytes", stacksize);
565
567
- wanted_stacksize = config_get_number("global", "pthread stack size", stacksize);
566
+ wanted_stacksize = (size_t)config_get_number("global", "pthread stack size", (long)stacksize);
567
568
// --------------------------------------------------------------------
569
@@ -593,10 +592,6 @@ int main(int argc, char **argv)
592
// initialize the log files
593
open_all_log_files();
594
596
- // become daemon, switch user, create pid file, set process attributes
597
- if(become_daemon(dont_fork, user) == -1)
598
- fatal("Cannot demonize myself.");
599
-
595
#ifdef NETDATA_INTERNAL_CHECKS
596
if(debug_flags != 0) {
597
struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
@@ -606,7 +601,11 @@ int main(int argc, char **argv)
601
}
602
#endif /* NETDATA_INTERNAL_CHECKS */
603
609
- info("NetData started on pid %d", getpid());
604
+ // fork, switch user, create pid file, set process priority
605
+ if(become_daemon(dont_fork, user) == -1)
606
+ fatal("Cannot demonize myself.");
607
+
608
+ info("NetData started on pid %d", getpid());
609
610
611
// ------------------------------------------------------------------------
@@ -620,11 +619,16 @@ int main(int argc, char **argv)
619
info("Successfully set pthread stacksize to %zu bytes", wanted_stacksize);
620
}
621
623
- // --------------------------------------------------------------------
622
+ // ------------------------------------------------------------------------
623
// initialize the registry
624
625
registry_init();
626
627
+ // ------------------------------------------------------------------------
628
+ // initialize health monitoring
629
+
630
+ health_init();
631
+
632
// ------------------------------------------------------------------------
633
// spawn the threads
634