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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
// SPDX-License-Identifier: GPL-3.0-or-later
/*
* netdata cups.plugin
* (C) Copyright 2017-2018 Simon Nagl <simon.nagl@gmx.de>
* Released under GPL v3+
*/
#include "libnetdata/libnetdata.h"
#include "libnetdata/required_dummies.h"
#include <cups/cups.h>
#include <limits.h>
// Variables
static int debug = 0;
static int netdata_update_every = 1;
static uint32_t netdata_priority = 100004;
http_t *http; // connection to the cups daemon
/*
* Used to aggregate job metrics for a destination (and all destinations).
*/
struct job_metrics {
uint32_t id;
bool is_collected; // flag if this was collected in the current cycle
int num_pending;
int num_processing;
int num_held;
int size_pending; // in kilobyte
int size_processing; // in kilobyte
int size_held; // in kilobyte
};
DICTIONARY *dict_dest_job_metrics = NULL;
struct job_metrics global_job_metrics;
int num_dest_total;
int num_dest_accepting_jobs;
int num_dest_shared;
int num_dest_idle;
int num_dest_printing;
int num_dest_stopped;
void print_help() {
fprintf(stderr,
"\n"
"netdata cups.plugin %s\n"
"\n"
"Copyright (C) 2017-2018 Simon Nagl <simon.nagl@gmx.de>\n"
"Released under GNU General Public License v3+.\n"
"All rights reserved.\n"
"\n"
"This program is a data collector plugin for netdata.\n"
"\n"
"SYNOPSIS: cups.plugin [-d][-h][-v] COLLECTION_FREQUENCY\n"
"\n"
"Options:"
"\n"
" COLLECTION_FREQUENCY data collection frequency in seconds\n"
"\n"
" -d enable verbose output\n"
" default: disabled\n"
"\n"
" -v print version and exit\n"
"\n"
" -h print this message and exit\n"
"\n",
VERSION);
}
void parse_command_line(int argc, char **argv) {
int i;
int freq = 0;
int update_every_found = 0;
for (i = 1; i < argc; i++) {
if (isdigit(*argv[i]) && !update_every_found) {
int n = str2i(argv[i]);
if (n > 0 && n < 86400) {
freq = n;
continue;
}
} else if (strcmp("-v", argv[i]) == 0) {
printf("cups.plugin %s\n", VERSION);
exit(0);
} else if (strcmp("-d", argv[i]) == 0) {
debug = 1;
continue;
} else if (strcmp("-h", argv[i]) == 0) {
print_help();
exit(0);
}
print_help();
exit(1);
}
if (freq >= netdata_update_every) {
netdata_update_every = freq;
} else if (freq) {
netdata_log_error("update frequency %d seconds is too small for CUPS. Using %d.", freq, netdata_update_every);
}
}
/*
* 'cupsGetIntegerOption()' - Get an integer option value.
*
* INT_MIN is returned when the option does not exist, is not an integer, or
* exceeds the range of values for the "int" type.
*
* @since CUPS 2.2.4/macOS 10.13@
*/
int /* O - Option value or @code INT_MIN@ */
getIntegerOption(
const char *name, /* I - Name of option */
int num_options, /* I - Number of options */
cups_option_t *options) /* I - Options */
{
const char *value = cupsGetOption(name, num_options, options);
/* String value of option */
char *ptr; /* Pointer into string value */
long intvalue; /* Integer value */
if (!value || !*value)
return (INT_MIN);
intvalue = strtol(value, &ptr, 10);
if (intvalue < INT_MIN || intvalue > INT_MAX || *ptr)
return (INT_MIN);
return ((int)intvalue);
}
static int reset_job_metrics(const DICTIONARY_ITEM *item __maybe_unused, void *entry, void *data __maybe_unused) {
struct job_metrics *jm = (struct job_metrics *)entry;
jm->is_collected = false;
jm->num_held = 0;
jm->num_pending = 0;
jm->num_processing = 0;
jm->size_held = 0;
jm->size_pending = 0;
jm->size_processing = 0;
return 0;
}
void send_job_charts_definitions_to_netdata(const char *name, uint32_t job_id, bool obsolete) {
printf("CHART cups.job_num_%s '' 'Active jobs of %s' jobs '%s' cups.destination_job_num stacked %u %i %s\n",
name, name, name, netdata_priority + job_id, netdata_update_every, obsolete?"obsolete":"");
printf("DIMENSION pending '' absolute 1 1\n");
printf("DIMENSION held '' absolute 1 1\n");
printf("DIMENSION processing '' absolute 1 1\n");
printf("CHART cups.job_size_%s '' 'Active jobs size of %s' KB '%s' cups.destination_job_size stacked %u %i %s\n",
name, name, name, netdata_priority + 1 + job_id, netdata_update_every, obsolete?"obsolete":"");
printf("DIMENSION pending '' absolute 1 1\n");
printf("DIMENSION held '' absolute 1 1\n");
printf("DIMENSION processing '' absolute 1 1\n");
}
struct job_metrics *get_job_metrics(char *dest) {
struct job_metrics *jm = dictionary_get(dict_dest_job_metrics, dest);
if (unlikely(!jm)) {
static uint32_t job_id = 0;
struct job_metrics new_job_metrics = { .id = ++job_id };
jm = dictionary_set(dict_dest_job_metrics, dest, &new_job_metrics, sizeof(struct job_metrics));
send_job_charts_definitions_to_netdata(dest, jm->id, false);
};
return jm;
}
int send_job_metrics_to_netdata(const DICTIONARY_ITEM *item, void *entry, void *data __maybe_unused) {
const char *name = dictionary_acquired_item_name(item);
struct job_metrics *jm = (struct job_metrics *)entry;
if (jm->is_collected) {
printf(
"BEGIN cups.job_num_%s\n"
"SET pending = %d\n"
"SET held = %d\n"
"SET processing = %d\n"
"END\n",
name, jm->num_pending, jm->num_held, jm->num_processing);
printf(
"BEGIN cups.job_size_%s\n"
"SET pending = %d\n"
"SET held = %d\n"
"SET processing = %d\n"
"END\n",
name, jm->size_pending, jm->size_held, jm->size_processing);
}
else {
// mark it obsolete
send_job_charts_definitions_to_netdata(name, jm->id, true);
// delete it
dictionary_del(dict_dest_job_metrics, name);
}
return 0;
}
void reset_metrics() {
num_dest_total = 0;
num_dest_accepting_jobs = 0;
num_dest_shared = 0;
num_dest_idle = 0;
num_dest_printing = 0;
num_dest_stopped = 0;
reset_job_metrics(NULL, &global_job_metrics, NULL);
dictionary_walkthrough_write(dict_dest_job_metrics, reset_job_metrics, NULL);
}
int main(int argc, char **argv) {
stderror = stderr;
|