1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
+
3
+/*
4
+ * netdata cups.plugin
5
+ * (C) Copyright 2017-2018 Simon Nagl <simon.nagl@gmx.de>
6
+ * Released under GPL v3+
7
+ */
8
+
9
+#include "../../libnetdata/libnetdata.h"
10
+#include <limits.h>
11
+
12
+// callback required by fatal()
13
+void netdata_cleanup_and_exit(int ret) {
14
+ exit(ret);
15
+}
16
+
17
+// callbacks required by popen()
18
+void signals_block(void) {};
19
+void signals_unblock(void) {};
20
+void signals_reset(void) {};
21
+
22
+// callback required by eval()
23
+int health_variable_lookup(const char *variable, uint32_t hash, struct rrdcalc *rc, calculated_number *result) {
24
+ (void)variable;
25
+ (void)hash;
26
+ (void)rc;
27
+ (void)result;
28
+ return 0;
29
+};
30
+
31
+// required by get_system_cpus()
32
+char *netdata_configured_host_prefix = "";
33
+
34
+// Variables
35
+
36
+static int debug = 0;
37
+
38
+static int netdata_update_every = 1;
39
+static int netdata_priority = 100004;
40
+
41
+
42
+#ifdef HAVE_CUPS
43
+#include <cups/cups.h>
44
+
45
+http_t *http; // connection to the cups daemon
46
+
47
+/*
48
+ * Used to aggregate job metrics for a destination (and all destianations).
49
+ */
50
+struct job_metrics {
51
+ int is_collected; // flag if this was collected in the current cycle
52
+
53
+ int num_pending;
54
+ int num_processing;
55
+ int num_held;
56
+
57
+ int size_pending; // in kilobyte
58
+ int size_processing; // in kilobyte
59
+ int size_held; // in kilobyte
60
+};
61
+DICTIONARY *dict_dest_job_metrics = NULL;
62
+struct job_metrics global_job_metrics;
63
+
64
+int num_dest_total;
65
+int num_dest_accepting_jobs;
66
+int num_dest_shared;
67
+
68
+int num_dest_idle;
69
+int num_dest_printing;
70
+int num_dest_stopped;
71
+
72
+void print_help() {
73
+ fprintf(stderr,
74
+ "\n"
75
+ "netdata cups.plugin %s\n"
76
+ "\n"
77
+ "Copyright (C) 2017-2018 Simon Nagl <simon.nagl@gmx.de>\n"
78
+ "Released under GNU General Public License v3+.\n"
79
+ "All rights reserved.\n"
80
+ "\n"
81
+ "This program is a data collector plugin for netdata.\n"
82
+ "\n"
83
+ "SYNOPSIS: cups.plugin [-d][-h][-v] COLLECTION_FREQUENCY\n"
84
+ "\n"
85
+ "Options:"
86
+ "\n"
87
+ " COLLECTION_FREQUENCY data collection frequency in seconds\n"
88
+ "\n"
89
+ " -d enable verbose output\n"
90
+ " default: disabled\n"
91
+ "\n"
92
+ " -v print version and exit\n"
93
+ "\n"
94
+ " -h print this message and exit\n"
95
+ "\n",
96
+ VERSION);
97
+}
98
+
99
+void parse_command_line(int argc, char **argv) {
100
+ int i;
101
+ int freq = 0;
102
+ int update_every_found = 0;
103
+ for (i = 1; i < argc; i++) {
104
+ if (isdigit(*argv[i]) && !update_every_found) {
105
+ int n = str2i(argv[i]);
106
+ if (n > 0 && n < 86400) {
107
+ freq = n;
108
+ continue;
109
+ }
110
+ } else if (strcmp("-v", argv[i]) == 0) {
111
+ printf("cups.plugin %s\n", VERSION);
112
+ exit(0);
113
+ } else if (strcmp("-d", argv[i]) == 0) {
114
+ debug = 1;
115
+ continue;
116
+ } else if (strcmp("-h", argv[i]) == 0) {
117
+ print_help();
118
+ exit(0);
119
+ }
120
+
121
+ print_help();
122
+ exit(1);
123
+ }
124
+
125
+ if (freq >= netdata_update_every) {
126
+ netdata_update_every = freq;
127
+ } else if (freq) {
128
+ error("update frequency %d seconds is too small for CUPS. Using %d.", freq, netdata_update_every);
129
+ }
130
+}
131
+
132
+int reset_job_metrics(void *entry, void *data) {
133
+ (void)data;
134
+
135
+ struct job_metrics *jm = (struct job_metrics *)entry;
136
+
137
+ jm->is_collected = 0;
138
+ jm->num_held = 0;
139
+ jm->num_pending = 0;
140
+ jm->num_processing = 0;
141
+ jm->size_held = 0;
142
+ jm->size_pending = 0;
143
+ jm->size_processing = 0;
144
+
145
+ return 0;
146
+}
147
+
148
+struct job_metrics *get_job_metrics(char *dest) {
149
+ struct job_metrics *jm = dictionary_get(dict_dest_job_metrics, dest);
150
+
151
+ if (unlikely(!jm)) {
152
+ struct job_metrics new_job_metrics;
153
+ reset_job_metrics(&new_job_metrics, NULL);
154
+ jm = dictionary_set(dict_dest_job_metrics, dest, &new_job_metrics, sizeof(struct job_metrics));
155
+
156
+ printf("CHART cups.job_num_%s '' 'Active job number of destination %s' jobs '%s' job_num stacked %i %i\n", dest, dest, dest, netdata_priority++, netdata_update_every);
157
+ printf("DIMENSION pending '' absolute 1 1\n");
158
+ printf("DIMENSION held '' absolute 1 1\n");
159
+ printf("DIMENSION processing '' absolute 1 1\n");
160
+
161
+ printf("CHART cups.job_size_%s '' 'Active job size of destination %s' KB '%s' job_size stacked %i %i\n", dest, dest, dest, netdata_priority++, netdata_update_every);
162
+ printf("DIMENSION pending '' absolute 1 1\n");
163
+ printf("DIMENSION held '' absolute 1 1\n");
164
+ printf("DIMENSION processing '' absolute 1 1\n");
165
+ };
166
+ return jm;
167
+}
168
+
169
+int collect_job_metrics(char *name, void *entry, void *data) {
170
+ (void)data;
171
+
172
+ struct job_metrics *jm = (struct job_metrics *)entry;
173
+
174
+ if (jm->is_collected) {
175
+ printf(
176
+ "BEGIN cups.job_num_%s\n"
177
+ "SET pending = %d\n"
178
+ "SET held = %d\n"
179
+ "SET processing = %d\n"
180
+ "END\n",
181
+ name, jm->num_pending, jm->num_held, jm->num_processing);
182
+ printf(
183
+ "BEGIN cups.job_size_%s\n"
184
+ "SET pending = %d\n"
185
+ "SET held = %d\n"
186
+ "SET processing = %d\n"
187
+ "END\n",
188
+ name, jm->size_pending, jm->size_held, jm->size_processing);
189
+ } else {
190
+ printf("CHART cups.job_num_%s '' 'Active job number of destination %s' jobs '%s' job_num stacked 1 %i 'obsolete'\n", name, name, name, netdata_update_every);
191
+ printf("DIMENSION pending '' absolute 1 1\n");
192
+ printf("DIMENSION held '' absolute 1 1\n");
193
+ printf("DIMENSION processing '' absolute 1 1\n");
194
+
195
+ printf("CHART cups.job_size_%s '' 'Active job size of destination %s' KB '%s' job_size stacked 1 %i 'obsolete'\n", name, name, name, netdata_update_every);
196
+ printf("DIMENSION pending '' absolute 1 1\n");
197
+ printf("DIMENSION held '' absolute 1 1\n");
198
+ printf("DIMENSION processing '' absolute 1 1\n");
199
+ dictionary_del(dict_dest_job_metrics, name);
200
+ }
201
+
202
+ return 0;
203
+}
204
+
205
+void reset_metrics() {
206
+ num_dest_total = 0;
207
+ num_dest_accepting_jobs = 0;
208
+ num_dest_shared = 0;
209
+
210
+ num_dest_idle = 0;
211
+ num_dest_printing = 0;
212
+ num_dest_stopped = 0;
213
+
214
+ reset_job_metrics(&global_job_metrics, NULL);
215
+ dictionary_get_all(dict_dest_job_metrics, reset_job_metrics, NULL);
216
+}
217
+
218
+int main(int argc, char **argv) {
219
+
220
+ // ------------------------------------------------------------------------
221
+ // initialization of netdata plugin
222
+
223
+ program_name = "cups.plugin";
224
+
225
+ // disable syslog
226
+ error_log_syslog = 0;
227
+
228
+ // set errors flood protection to 100 logs per hour
229
+ error_log_errors_per_period = 100;
230
+ error_log_throttle_period = 3600;
231
+
232
+ parse_command_line(argc, argv);
233
+
234
+ errno = 0;
235
+
236
+ dict_dest_job_metrics = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED);
237
+
238
+ // ------------------------------------------------------------------------
239
+ // the main loop
240
+
241
+ if (debug)
242
+ fprintf(stderr, "starting data collection\n");
243
+
244
+ time_t started_t = now_monotonic_sec();
245
+ size_t iteration = 0;
246
+ usec_t step = netdata_update_every * USEC_PER_SEC;
247
+
248
+ heartbeat_t hb;
249
+ heartbeat_init(&hb);
250
+ for (iteration = 0; 1; iteration++)
251
+ {
252
+ heartbeat_next(&hb, step);
253
+
254
+ if (unlikely(netdata_exit))
255
+ {
256
+ break;
257
+ }
258
+
259
+ reset_metrics();
260
+
261
+ cups_dest_t *dests;
262
+ num_dest_total = cupsGetDests2(http, &dests);
263
+
264
+ if(unlikely(num_dest_total == 0)) {
265
+ // reconnect to cups to check if the server is down.
266
+ httpClose(http);
267
+ http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 0, netdata_update_every * 1000, NULL);
268
+ if(http == NULL) {
269
+ error("cups daemon is not running. Exiting!");
270
+ exit(1);
271
+ }
272
+ }
273
+
274
+ cups_dest_t *curr_dest = dests;
275
+ int counter = 0;
276
+ while (counter < num_dest_total) {
277
+ if (counter != 0) {
278
+ curr_dest++;
279
+ }
280
+ counter++;
281
+
282
+ const char *printer_uri_supported = cupsGetOption("printer-uri-supported", curr_dest->num_options, curr_dest->options);
283
+ if (!printer_uri_supported) {
284
+ if(debug)
285
+ fprintf(stderr, "destination %s discovered, but not yet setup as a local printer", curr_dest->name);
286
+ continue;
287
+ }
288
+
289
+ const char *printer_is_accepting_jobs = cupsGetOption("printer-is-accepting-jobs", curr_dest->num_options, curr_dest->options);
290
+ if (printer_is_accepting_jobs && !strcmp(printer_is_accepting_jobs, "true")) {
291
+ num_dest_accepting_jobs++;
292
+ }
293
+
294
+ const char *printer_is_shared = cupsGetOption("printer-is-shared", curr_dest->num_options, curr_dest->options);
295
+ if (printer_is_shared && !strcmp(printer_is_shared, "true")) {
296
+ num_dest_shared++;
297
+ }
298
+
299
+ // TODO use cupsGetIntegerOption
300
+ int printer_state = cupsGetIntegerOption("printer-state", curr_dest->num_options, curr_dest->options);
301
+ switch (printer_state) {
302
+ case 3:
303
+ num_dest_idle++;
304
+ break;
305
+ case 4:
306
+ num_dest_printing++;
307
+ break;
308
+ case 5:
309
+ num_dest_stopped++;
310
+ break;
311
+ case INT_MIN:
312
+ if(debug)
313
+ fprintf(stderr, "printer state is missing for destination %s", curr_dest->name);
314
+ break;
315
+ default:
316
+ error("Unknown printer state (%d) found.", printer_state);
317
+ break;
318
+ }
319
+
320
+ /*
321
+ * flag job metrics to print values.
322
+ * This is needed to report also destinations with zero active jobs.
323
+ */
324
+ struct job_metrics *jm = get_job_metrics(curr_dest->name);
325
+ jm->is_collected = 1;
326
+ }
327
+ cupsFreeDests(num_dest_total, dests);
328
+
329
+ if (unlikely(netdata_exit))
330
+ break;
331
+
332
+ cups_job_t *jobs, *curr_job;
333
+ int num_jobs = cupsGetJobs2(http, &jobs, NULL, 0, CUPS_WHICHJOBS_ACTIVE);
334
+ int i;
335
+ for (i = num_jobs, curr_job = jobs; i > 0; i--, curr_job++) {
336
+ struct job_metrics *jm = get_job_metrics(curr_job->dest);
337
+ jm->is_collected = 1;
338
+
339
+ switch (curr_job->state) {
340
+ case IPP_JOB_PENDING:
341
+ jm->num_pending++;
342
+ jm->size_pending += curr_job->size;
343
+ global_job_metrics.num_pending++;
344
+ global_job_metrics.size_pending += curr_job->size;
345
+ break;
346
+ case IPP_JOB_HELD:
347
+ jm->num_held++;
348
+ jm->size_held += curr_job->size;
349
+ global_job_metrics.num_held++;
350
+ global_job_metrics.size_held += curr_job->size;
351
+ break;
352
+ case IPP_JOB_PROCESSING:
353
+ jm->num_processing++;
354
+ jm->size_processing += curr_job->size;
355
+ global_job_metrics.num_processing++;
356
+ global_job_metrics.size_processing += curr_job->size;
357
+ break;
358
+ default:
359
+ error("Unsupported job state (%u) found.", curr_job->state);
360
+ break;
361
+ }
362
+ }
363
+ cupsFreeJobs(num_jobs, jobs);
364
+
365
+ dictionary_get_all_name_value(dict_dest_job_metrics, collect_job_metrics, NULL);
366
+
367
+ static int cups_printer_by_option_created = 0;
368
+ if (unlikely(!cups_printer_by_option_created))
369
+ {
370
+ cups_printer_by_option_created = 1;
371
+ printf("CHART cups.dest_state '' 'Destinations by state' dests overview dests stacked 100000 %i\n", netdata_update_every);
372
+ printf("DIMENSION idle '' absolute 1 1\n");
373
+ printf("DIMENSION printing '' absolute 1 1\n");
374
+ printf("DIMENSION stopped '' absolute 1 1\n");
375
+
376
+ printf("CHART cups.dest_option '' 'Destinations by option' dests overview dests line 100001 %i\n", netdata_update_every);
377
+ printf("DIMENSION total '' absolute 1 1\n");
378
+ printf("DIMENSION acceptingjobs '' absolute 1 1\n");
379
+ printf("DIMENSION shared '' absolute 1 1\n");
380
+
381
+ printf("CHART cups.job_num '' 'Total active job number' jobs overview job_num stacked 100002 %i\n", netdata_update_every);
382
+ printf("DIMENSION pending '' absolute 1 1\n");
383
+ printf("DIMENSION held '' absolute 1 1\n");
384
+ printf("DIMENSION processing '' absolute 1 1\n");
385
+
386
+ printf("CHART cups.job_size '' 'Total active job size' KB overview job_size stacked 100003 %i\n", netdata_update_every);
387
+ printf("DIMENSION pending '' absolute 1 1\n");
388
+ printf("DIMENSION held '' absolute 1 1\n");
389
+ printf("DIMENSION processing '' absolute 1 1\n");
390
+ }
391
+
392
+ printf(
393
+ "BEGIN cups.dest_state\n"
394
+ "SET idle = %d\n"
395
+ "SET printing = %d\n"
396
+ "SET stopped = %d\n"
397
+ "END\n",
398
+ num_dest_idle, num_dest_printing, num_dest_stopped);
399
+ printf(
400
+ "BEGIN cups.dest_option\n"
401
+ "SET total = %d\n"
402
+ "SET acceptingjobs = %d\n"
403
+ "SET shared = %d\n"
404
+ "END\n",
405
+ num_dest_total, num_dest_accepting_jobs, num_dest_shared);
406
+ printf(
407
+ "BEGIN cups.job_num\n"
408
+ "SET pending = %d\n"
409
+ "SET held = %d\n"
410
+ "SET processing = %d\n"
411
+ "END\n",
412
+ global_job_metrics.num_pending, global_job_metrics.num_held, global_job_metrics.num_processing);
413
+ printf(
414
+ "BEGIN cups.job_size\n"
415
+ "SET pending = %d\n"
416
+ "SET held = %d\n"
417
+ "SET processing = %d\n"
418
+ "END\n",
419
+ global_job_metrics.size_pending, global_job_metrics.size_held, global_job_metrics.size_processing);
420
+
421
+ fflush(stdout);
422
+
423
+ if (unlikely(netdata_exit))
424
+ break;
425
+
426
+ // restart check (14400 seconds)
427
+ if (!now_monotonic_sec() - started_t > 14400)
428
+ break;
429
+ }
430
+
431
+ httpClose(http);
432
+ info("CUPS process exiting");
433
+}
434
+
435
+#else // !HAVE_CUPS
436
+
437
+int main(int argc, char **argv)
438
+{
439
+ fatal("cups.plugin is not compiled.");
440
+}
441
+
442
+#endif // !HAVE_CUPS