cups_plugin.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. /*
  3. * netdata cups.plugin
  4. * (C) Copyright 2017-2018 Simon Nagl <simon.nagl@gmx.de>
  5. * Released under GPL v3+
  6. */
  7. #include "libnetdata/libnetdata.h"
  8. #include "libnetdata/required_dummies.h"
  9. #include <cups/cups.h>
  10. #include <limits.h>
  11. // Variables
  12. static int debug = 0;
  13. static int netdata_update_every = 1;
  14. static uint32_t netdata_priority = 100004;
  15. http_t *http; // connection to the cups daemon
  16. /*
  17. * Used to aggregate job metrics for a destination (and all destinations).
  18. */
  19. struct job_metrics {
  20. uint32_t id;
  21. bool is_collected; // flag if this was collected in the current cycle
  22. int num_pending;
  23. int num_processing;
  24. int num_held;
  25. int size_pending; // in kilobyte
  26. int size_processing; // in kilobyte
  27. int size_held; // in kilobyte
  28. };
  29. DICTIONARY *dict_dest_job_metrics = NULL;
  30. struct job_metrics global_job_metrics;
  31. int num_dest_total;
  32. int num_dest_accepting_jobs;
  33. int num_dest_shared;
  34. int num_dest_idle;
  35. int num_dest_printing;
  36. int num_dest_stopped;
  37. void print_help() {
  38. fprintf(stderr,
  39. "\n"
  40. "netdata cups.plugin %s\n"
  41. "\n"
  42. "Copyright (C) 2017-2018 Simon Nagl <simon.nagl@gmx.de>\n"
  43. "Released under GNU General Public License v3+.\n"
  44. "All rights reserved.\n"
  45. "\n"
  46. "This program is a data collector plugin for netdata.\n"
  47. "\n"
  48. "SYNOPSIS: cups.plugin [-d][-h][-v] COLLECTION_FREQUENCY\n"
  49. "\n"
  50. "Options:"
  51. "\n"
  52. " COLLECTION_FREQUENCY data collection frequency in seconds\n"
  53. "\n"
  54. " -d enable verbose output\n"
  55. " default: disabled\n"
  56. "\n"
  57. " -v print version and exit\n"
  58. "\n"
  59. " -h print this message and exit\n"
  60. "\n",
  61. VERSION);
  62. }
  63. void parse_command_line(int argc, char **argv) {
  64. int i;
  65. int freq = 0;
  66. int update_every_found = 0;
  67. for (i = 1; i < argc; i++) {
  68. if (isdigit(*argv[i]) && !update_every_found) {
  69. int n = str2i(argv[i]);
  70. if (n > 0 && n < 86400) {
  71. freq = n;
  72. continue;
  73. }
  74. } else if (strcmp("-v", argv[i]) == 0) {
  75. printf("cups.plugin %s\n", VERSION);
  76. exit(0);
  77. } else if (strcmp("-d", argv[i]) == 0) {
  78. debug = 1;
  79. continue;
  80. } else if (strcmp("-h", argv[i]) == 0) {
  81. print_help();
  82. exit(0);
  83. }
  84. print_help();
  85. exit(1);
  86. }
  87. if (freq >= netdata_update_every) {
  88. netdata_update_every = freq;
  89. } else if (freq) {
  90. netdata_log_error("update frequency %d seconds is too small for CUPS. Using %d.", freq, netdata_update_every);
  91. }
  92. }
  93. /*
  94. * 'cupsGetIntegerOption()' - Get an integer option value.
  95. *
  96. * INT_MIN is returned when the option does not exist, is not an integer, or
  97. * exceeds the range of values for the "int" type.
  98. *
  99. * @since CUPS 2.2.4/macOS 10.13@
  100. */
  101. int /* O - Option value or @code INT_MIN@ */
  102. getIntegerOption(
  103. const char *name, /* I - Name of option */
  104. int num_options, /* I - Number of options */
  105. cups_option_t *options) /* I - Options */
  106. {
  107. const char *value = cupsGetOption(name, num_options, options);
  108. /* String value of option */
  109. char *ptr; /* Pointer into string value */
  110. long intvalue; /* Integer value */
  111. if (!value || !*value)
  112. return (INT_MIN);
  113. intvalue = strtol(value, &ptr, 10);
  114. if (intvalue < INT_MIN || intvalue > INT_MAX || *ptr)
  115. return (INT_MIN);
  116. return ((int)intvalue);
  117. }
  118. static int reset_job_metrics(const DICTIONARY_ITEM *item __maybe_unused, void *entry, void *data __maybe_unused) {
  119. struct job_metrics *jm = (struct job_metrics *)entry;
  120. jm->is_collected = false;
  121. jm->num_held = 0;
  122. jm->num_pending = 0;
  123. jm->num_processing = 0;
  124. jm->size_held = 0;
  125. jm->size_pending = 0;
  126. jm->size_processing = 0;
  127. return 0;
  128. }
  129. void send_job_charts_definitions_to_netdata(const char *name, uint32_t job_id, bool obsolete) {
  130. printf("CHART cups.job_num_%s '' 'Active jobs of %s' jobs '%s' cups.destination_job_num stacked %u %i %s\n",
  131. name, name, name, netdata_priority + job_id, netdata_update_every, obsolete?"obsolete":"");
  132. printf("DIMENSION pending '' absolute 1 1\n");
  133. printf("DIMENSION held '' absolute 1 1\n");
  134. printf("DIMENSION processing '' absolute 1 1\n");
  135. printf("CHART cups.job_size_%s '' 'Active jobs size of %s' KB '%s' cups.destination_job_size stacked %u %i %s\n",
  136. name, name, name, netdata_priority + 1 + job_id, netdata_update_every, obsolete?"obsolete":"");
  137. printf("DIMENSION pending '' absolute 1 1\n");
  138. printf("DIMENSION held '' absolute 1 1\n");
  139. printf("DIMENSION processing '' absolute 1 1\n");
  140. }
  141. struct job_metrics *get_job_metrics(char *dest) {
  142. struct job_metrics *jm = dictionary_get(dict_dest_job_metrics, dest);
  143. if (unlikely(!jm)) {
  144. static uint32_t job_id = 0;
  145. struct job_metrics new_job_metrics = { .id = ++job_id };
  146. jm = dictionary_set(dict_dest_job_metrics, dest, &new_job_metrics, sizeof(struct job_metrics));
  147. send_job_charts_definitions_to_netdata(dest, jm->id, false);
  148. };
  149. return jm;
  150. }
  151. int send_job_metrics_to_netdata(const DICTIONARY_ITEM *item, void *entry, void *data __maybe_unused) {
  152. const char *name = dictionary_acquired_item_name(item);
  153. struct job_metrics *jm = (struct job_metrics *)entry;
  154. if (jm->is_collected) {
  155. printf(
  156. "BEGIN cups.job_num_%s\n"
  157. "SET pending = %d\n"
  158. "SET held = %d\n"
  159. "SET processing = %d\n"
  160. "END\n",
  161. name, jm->num_pending, jm->num_held, jm->num_processing);
  162. printf(
  163. "BEGIN cups.job_size_%s\n"
  164. "SET pending = %d\n"
  165. "SET held = %d\n"
  166. "SET processing = %d\n"
  167. "END\n",
  168. name, jm->size_pending, jm->size_held, jm->size_processing);
  169. }
  170. else {
  171. // mark it obsolete
  172. send_job_charts_definitions_to_netdata(name, jm->id, true);
  173. // delete it
  174. dictionary_del(dict_dest_job_metrics, name);
  175. }
  176. return 0;
  177. }
  178. void reset_metrics() {
  179. num_dest_total = 0;
  180. num_dest_accepting_jobs = 0;
  181. num_dest_shared = 0;
  182. num_dest_idle = 0;
  183. num_dest_printing = 0;
  184. num_dest_stopped = 0;
  185. reset_job_metrics(NULL, &global_job_metrics, NULL);
  186. dictionary_walkthrough_write(dict_dest_job_metrics, reset_job_metrics, NULL);
  187. }
  188. int main(int argc, char **argv) {
  189. clocks_init();
  190. nd_log_initialize_for_external_plugins("cups.plugin");
  191. parse_command_line(argc, argv);
  192. errno = 0;
  193. dict_dest_job_metrics = dictionary_create(DICT_OPTION_SINGLE_THREADED);
  194. // ------------------------------------------------------------------------
  195. // the main loop
  196. if (debug)
  197. fprintf(stderr, "starting data collection\n");
  198. time_t started_t = now_monotonic_sec();
  199. size_t iteration = 0;
  200. usec_t step = netdata_update_every * USEC_PER_SEC;
  201. heartbeat_t hb;
  202. heartbeat_init(&hb);
  203. for (iteration = 0; 1; iteration++) {
  204. heartbeat_next(&hb, step);
  205. if (unlikely(netdata_exit))
  206. break;
  207. reset_metrics();
  208. cups_dest_t *dests;
  209. num_dest_total = cupsGetDests2(http, &dests);
  210. if(unlikely(num_dest_total == 0)) {
  211. // reconnect to cups to check if the server is down.
  212. httpClose(http);
  213. http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 0, netdata_update_every * 1000, NULL);
  214. if(http == NULL) {
  215. netdata_log_error("cups daemon is not running. Exiting!");
  216. exit(1);
  217. }
  218. }
  219. cups_dest_t *curr_dest = dests;
  220. int counter = 0;
  221. while (counter < num_dest_total) {
  222. if (counter != 0) {
  223. curr_dest++;
  224. }
  225. counter++;
  226. const char *printer_uri_supported = cupsGetOption("printer-uri-supported", curr_dest->num_options, curr_dest->options);
  227. if (!printer_uri_supported) {
  228. if(debug)
  229. fprintf(stderr, "destination %s discovered, but not yet setup as a local printer", curr_dest->name);
  230. continue;
  231. }
  232. const char *printer_is_accepting_jobs = cupsGetOption("printer-is-accepting-jobs", curr_dest->num_options, curr_dest->options);
  233. if (printer_is_accepting_jobs && !strcmp(printer_is_accepting_jobs, "true")) {
  234. num_dest_accepting_jobs++;
  235. }
  236. const char *printer_is_shared = cupsGetOption("printer-is-shared", curr_dest->num_options, curr_dest->options);
  237. if (printer_is_shared && !strcmp(printer_is_shared, "true")) {
  238. num_dest_shared++;
  239. }
  240. int printer_state = getIntegerOption("printer-state", curr_dest->num_options, curr_dest->options);
  241. switch (printer_state) {
  242. case 3:
  243. num_dest_idle++;
  244. break;
  245. case 4:
  246. num_dest_printing++;
  247. break;
  248. case 5:
  249. num_dest_stopped++;
  250. break;
  251. case INT_MIN:
  252. if(debug)
  253. fprintf(stderr, "printer state is missing for destination %s", curr_dest->name);
  254. break;
  255. default:
  256. netdata_log_error("Unknown printer state (%d) found.", printer_state);
  257. break;
  258. }
  259. /*
  260. * flag job metrics to print values.
  261. * This is needed to report also destinations with zero active jobs.
  262. */
  263. struct job_metrics *jm = get_job_metrics(curr_dest->name);
  264. jm->is_collected = true;
  265. }
  266. cupsFreeDests(num_dest_total, dests);
  267. if (unlikely(netdata_exit))
  268. break;
  269. cups_job_t *jobs, *curr_job;
  270. int num_jobs = cupsGetJobs2(http, &jobs, NULL, 0, CUPS_WHICHJOBS_ACTIVE);
  271. int i;
  272. for (i = num_jobs, curr_job = jobs; i > 0; i--, curr_job++) {
  273. struct job_metrics *jm = get_job_metrics(curr_job->dest);
  274. jm->is_collected = true;
  275. switch (curr_job->state) {
  276. case IPP_JOB_PENDING:
  277. jm->num_pending++;
  278. jm->size_pending += curr_job->size;
  279. global_job_metrics.num_pending++;
  280. global_job_metrics.size_pending += curr_job->size;
  281. break;
  282. case IPP_JOB_HELD:
  283. jm->num_held++;
  284. jm->size_held += curr_job->size;
  285. global_job_metrics.num_held++;
  286. global_job_metrics.size_held += curr_job->size;
  287. break;
  288. case IPP_JOB_PROCESSING:
  289. jm->num_processing++;
  290. jm->size_processing += curr_job->size;
  291. global_job_metrics.num_processing++;
  292. global_job_metrics.size_processing += curr_job->size;
  293. break;
  294. default:
  295. netdata_log_error("Unsupported job state (%u) found.", curr_job->state);
  296. break;
  297. }
  298. }
  299. cupsFreeJobs(num_jobs, jobs);
  300. dictionary_walkthrough_write(dict_dest_job_metrics, send_job_metrics_to_netdata, NULL);
  301. dictionary_garbage_collect(dict_dest_job_metrics);
  302. static int cups_printer_by_option_created = 0;
  303. if (unlikely(!cups_printer_by_option_created))
  304. {
  305. cups_printer_by_option_created = 1;
  306. printf("CHART cups.dest_state '' 'Destinations by state' dests overview cups.dests_state stacked 100000 %i\n", netdata_update_every);
  307. printf("DIMENSION idle '' absolute 1 1\n");
  308. printf("DIMENSION printing '' absolute 1 1\n");
  309. printf("DIMENSION stopped '' absolute 1 1\n");
  310. printf("CHART cups.dest_option '' 'Destinations by option' dests overview cups.dests_option line 100001 %i\n", netdata_update_every);
  311. printf("DIMENSION total '' absolute 1 1\n");
  312. printf("DIMENSION acceptingjobs '' absolute 1 1\n");
  313. printf("DIMENSION shared '' absolute 1 1\n");
  314. printf("CHART cups.job_num '' 'Active jobs' jobs overview cups.job_num stacked 100002 %i\n", netdata_update_every);
  315. printf("DIMENSION pending '' absolute 1 1\n");
  316. printf("DIMENSION held '' absolute 1 1\n");
  317. printf("DIMENSION processing '' absolute 1 1\n");
  318. printf("CHART cups.job_size '' 'Active jobs size' KB overview cups.job_size stacked 100003 %i\n", netdata_update_every);
  319. printf("DIMENSION pending '' absolute 1 1\n");
  320. printf("DIMENSION held '' absolute 1 1\n");
  321. printf("DIMENSION processing '' absolute 1 1\n");
  322. }
  323. printf(
  324. "BEGIN cups.dest_state\n"
  325. "SET idle = %d\n"
  326. "SET printing = %d\n"
  327. "SET stopped = %d\n"
  328. "END\n",
  329. num_dest_idle, num_dest_printing, num_dest_stopped);
  330. printf(
  331. "BEGIN cups.dest_option\n"
  332. "SET total = %d\n"
  333. "SET acceptingjobs = %d\n"
  334. "SET shared = %d\n"
  335. "END\n",
  336. num_dest_total, num_dest_accepting_jobs, num_dest_shared);
  337. printf(
  338. "BEGIN cups.job_num\n"
  339. "SET pending = %d\n"
  340. "SET held = %d\n"
  341. "SET processing = %d\n"
  342. "END\n",
  343. global_job_metrics.num_pending, global_job_metrics.num_held, global_job_metrics.num_processing);
  344. printf(
  345. "BEGIN cups.job_size\n"
  346. "SET pending = %d\n"
  347. "SET held = %d\n"
  348. "SET processing = %d\n"
  349. "END\n",
  350. global_job_metrics.size_pending, global_job_metrics.size_held, global_job_metrics.size_processing);
  351. fflush(stdout);
  352. if (unlikely(netdata_exit))
  353. break;
  354. // restart check (14400 seconds)
  355. if (!now_monotonic_sec() - started_t > 14400)
  356. break;
  357. fprintf(stdout, "\n");
  358. fflush(stdout);
  359. if (ferror(stdout) && errno == EPIPE) {
  360. netdata_log_error("error writing to stdout: EPIPE. Exiting...");
  361. return 1;
  362. }
  363. }
  364. httpClose(http);
  365. netdata_log_info("CUPS process exiting");
  366. }