cups_plugin.c 15 KB

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