diff options
Diffstat (limited to '')
-rw-r--r-- | collectors/cups.plugin/Makefile.am | 8 | ||||
l--------- | collectors/cups.plugin/README.md | 1 | ||||
-rw-r--r-- | collectors/cups.plugin/cups_plugin.c | 437 | ||||
-rw-r--r-- | collectors/cups.plugin/integrations/cups.md | 141 | ||||
-rw-r--r-- | collectors/cups.plugin/metadata.yaml | 131 |
5 files changed, 718 insertions, 0 deletions
diff --git a/collectors/cups.plugin/Makefile.am b/collectors/cups.plugin/Makefile.am new file mode 100644 index 00000000..161784b8 --- /dev/null +++ b/collectors/cups.plugin/Makefile.am @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-3.0-or-later + +AUTOMAKE_OPTIONS = subdir-objects +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in + +dist_noinst_DATA = \ + README.md \ + $(NULL) diff --git a/collectors/cups.plugin/README.md b/collectors/cups.plugin/README.md new file mode 120000 index 00000000..e3257063 --- /dev/null +++ b/collectors/cups.plugin/README.md @@ -0,0 +1 @@ +integrations/cups.md
\ No newline at end of file diff --git a/collectors/cups.plugin/cups_plugin.c b/collectors/cups.plugin/cups_plugin.c new file mode 100644 index 00000000..8efd32e3 --- /dev/null +++ b/collectors/cups.plugin/cups_plugin.c @@ -0,0 +1,437 @@ +// 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) { + clocks_init(); + nd_log_initialize_for_external_plugins("cups.plugin"); + + parse_command_line(argc, argv); + + errno = 0; + + dict_dest_job_metrics = dictionary_create(DICT_OPTION_SINGLE_THREADED); + + // ------------------------------------------------------------------------ + // the main loop + + if (debug) + fprintf(stderr, "starting data collection\n"); + + time_t started_t = now_monotonic_sec(); + size_t iteration = 0; + usec_t step = netdata_update_every * USEC_PER_SEC; + + heartbeat_t hb; + heartbeat_init(&hb); + for (iteration = 0; 1; iteration++) { + heartbeat_next(&hb, step); + + if (unlikely(netdata_exit)) + break; + + reset_metrics(); + + cups_dest_t *dests; + num_dest_total = cupsGetDests2(http, &dests); + + if(unlikely(num_dest_total == 0)) { + // reconnect to cups to check if the server is down. + httpClose(http); + http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 0, netdata_update_every * 1000, NULL); + if(http == NULL) { + netdata_log_error("cups daemon is not running. Exiting!"); + exit(1); + } + } + + cups_dest_t *curr_dest = dests; + int counter = 0; + while (counter < num_dest_total) { + if (counter != 0) { + curr_dest++; + } + counter++; + + const char *printer_uri_supported = cupsGetOption("printer-uri-supported", curr_dest->num_options, curr_dest->options); + if (!printer_uri_supported) { + if(debug) + fprintf(stderr, "destination %s discovered, but not yet setup as a local printer", curr_dest->name); + continue; + } + + const char *printer_is_accepting_jobs = cupsGetOption("printer-is-accepting-jobs", curr_dest->num_options, curr_dest->options); + if (printer_is_accepting_jobs && !strcmp(printer_is_accepting_jobs, "true")) { + num_dest_accepting_jobs++; + } + + const char *printer_is_shared = cupsGetOption("printer-is-shared", curr_dest->num_options, curr_dest->options); + if (printer_is_shared && !strcmp(printer_is_shared, "true")) { + num_dest_shared++; + } + + int printer_state = getIntegerOption("printer-state", curr_dest->num_options, curr_dest->options); + switch (printer_state) { + case 3: + num_dest_idle++; + break; + case 4: + num_dest_printing++; + break; + case 5: + num_dest_stopped++; + break; + case INT_MIN: + if(debug) + fprintf(stderr, "printer state is missing for destination %s", curr_dest->name); + break; + default: + netdata_log_error("Unknown printer state (%d) found.", printer_state); + break; + } + + /* + * flag job metrics to print values. + * This is needed to report also destinations with zero active jobs. + */ + struct job_metrics *jm = get_job_metrics(curr_dest->name); + jm->is_collected = true; + } + cupsFreeDests(num_dest_total, dests); + + if (unlikely(netdata_exit)) + break; + + cups_job_t *jobs, *curr_job; + int num_jobs = cupsGetJobs2(http, &jobs, NULL, 0, CUPS_WHICHJOBS_ACTIVE); + int i; + for (i = num_jobs, curr_job = jobs; i > 0; i--, curr_job++) { + struct job_metrics *jm = get_job_metrics(curr_job->dest); + jm->is_collected = true; + + switch (curr_job->state) { + case IPP_JOB_PENDING: + jm->num_pending++; + jm->size_pending += curr_job->size; + global_job_metrics.num_pending++; + global_job_metrics.size_pending += curr_job->size; + break; + case IPP_JOB_HELD: + jm->num_held++; + jm->size_held += curr_job->size; + global_job_metrics.num_held++; + global_job_metrics.size_held += curr_job->size; + break; + case IPP_JOB_PROCESSING: + jm->num_processing++; + jm->size_processing += curr_job->size; + global_job_metrics.num_processing++; + global_job_metrics.size_processing += curr_job->size; + break; + default: + netdata_log_error("Unsupported job state (%u) found.", curr_job->state); + break; + } + } + cupsFreeJobs(num_jobs, jobs); + + dictionary_walkthrough_write(dict_dest_job_metrics, send_job_metrics_to_netdata, NULL); + dictionary_garbage_collect(dict_dest_job_metrics); + + static int cups_printer_by_option_created = 0; + if (unlikely(!cups_printer_by_option_created)) + { + cups_printer_by_option_created = 1; + printf("CHART cups.dest_state '' 'Destinations by state' dests overview cups.dests_state stacked 100000 %i\n", netdata_update_every); + printf("DIMENSION idle '' absolute 1 1\n"); + printf("DIMENSION printing '' absolute 1 1\n"); + printf("DIMENSION stopped '' absolute 1 1\n"); + + printf("CHART cups.dest_option '' 'Destinations by option' dests overview cups.dests_option line 100001 %i\n", netdata_update_every); + printf("DIMENSION total '' absolute 1 1\n"); + printf("DIMENSION acceptingjobs '' absolute 1 1\n"); + printf("DIMENSION shared '' absolute 1 1\n"); + + printf("CHART cups.job_num '' 'Active jobs' jobs overview cups.job_num stacked 100002 %i\n", netdata_update_every); + 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 '' 'Active jobs size' KB overview cups.job_size stacked 100003 %i\n", netdata_update_every); + printf("DIMENSION pending '' absolute 1 1\n"); + printf("DIMENSION held '' absolute 1 1\n"); + printf("DIMENSION processing '' absolute 1 1\n"); + } + + printf( + "BEGIN cups.dest_state\n" + "SET idle = %d\n" + "SET printing = %d\n" + "SET stopped = %d\n" + "END\n", + num_dest_idle, num_dest_printing, num_dest_stopped); + printf( + "BEGIN cups.dest_option\n" + "SET total = %d\n" + "SET acceptingjobs = %d\n" + "SET shared = %d\n" + "END\n", + num_dest_total, num_dest_accepting_jobs, num_dest_shared); + printf( + "BEGIN cups.job_num\n" + "SET pending = %d\n" + "SET held = %d\n" + "SET processing = %d\n" + "END\n", + global_job_metrics.num_pending, global_job_metrics.num_held, global_job_metrics.num_processing); + printf( + "BEGIN cups.job_size\n" + "SET pending = %d\n" + "SET held = %d\n" + "SET processing = %d\n" + "END\n", + global_job_metrics.size_pending, global_job_metrics.size_held, global_job_metrics.size_processing); + + fflush(stdout); + + if (unlikely(netdata_exit)) + break; + + // restart check (14400 seconds) + if (!now_monotonic_sec() - started_t > 14400) + break; + + fprintf(stdout, "\n"); + fflush(stdout); + if (ferror(stdout) && errno == EPIPE) { + netdata_log_error("error writing to stdout: EPIPE. Exiting..."); + return 1; + } + } + + httpClose(http); + netdata_log_info("CUPS process exiting"); +} diff --git a/collectors/cups.plugin/integrations/cups.md b/collectors/cups.plugin/integrations/cups.md new file mode 100644 index 00000000..a8ea5b15 --- /dev/null +++ b/collectors/cups.plugin/integrations/cups.md @@ -0,0 +1,141 @@ +<!--startmeta +custom_edit_url: "https://github.com/netdata/netdata/edit/master/collectors/cups.plugin/README.md" +meta_yaml: "https://github.com/netdata/netdata/edit/master/collectors/cups.plugin/metadata.yaml" +sidebar_label: "CUPS" +learn_status: "Published" +learn_rel_path: "Data Collection/Hardware Devices and Sensors" +most_popular: False +message: "DO NOT EDIT THIS FILE DIRECTLY, IT IS GENERATED BY THE COLLECTOR'S metadata.yaml FILE" +endmeta--> + +# CUPS + + +<img src="https://netdata.cloud/img/cups.png" width="150"/> + + +Plugin: cups.plugin +Module: cups.plugin + +<img src="https://img.shields.io/badge/maintained%20by-Netdata-%2300ab44" /> + +## Overview + +Monitor CUPS performance for achieving optimal printing system operations. Monitor job statuses, queue lengths, and error rates to ensure smooth printing tasks. + +The plugin uses CUPS shared library to connect and monitor the server. + +This collector is supported on all platforms. + +This collector supports collecting metrics from multiple instances of this integration, including remote instances. + +The plugin needs to access the server. Netdata sets permissions during installation time to reach the server through its library. + +### Default Behavior + +#### Auto-Detection + +The plugin detects when CUPS server is running and tries to connect to it. + +#### Limits + +The default configuration for this integration does not impose any limits on data collection. + +#### Performance Impact + +The default configuration for this integration is not expected to impose a significant performance impact on the system. + + +## Metrics + +Metrics grouped by *scope*. + +The scope defines the instance that the metric belongs to. An instance is uniquely identified by a set of labels. + + + +### Per CUPS instance + + + +This scope has no labels. + +Metrics: + +| Metric | Dimensions | Unit | +|:------|:----------|:----| +| cups.dests_state | idle, printing, stopped | dests | +| cups.dests_option | total, acceptingjobs, shared | dests | +| cups.job_num | pending, held, processing | jobs | +| cups.job_size | pending, held, processing | KB | + +### Per destination + + + +This scope has no labels. + +Metrics: + +| Metric | Dimensions | Unit | +|:------|:----------|:----| +| cups.destination_job_num | pending, held, processing | jobs | +| cups.destination_job_size | pending, held, processing | KB | + + + +## Alerts + +There are no alerts configured by default for this integration. + + +## Setup + +### Prerequisites + +#### Minimum setup + +The CUPS server must be installed and running. If you installed `netdata` using a package manager, it is also necessary to install the package `netdata-plugin-cups`. + + +### Configuration + +#### File + +The configuration file name for this integration is `netdata.conf`. +Configuration for this specific integration is located in the `[plugin:cups]` section within that file. + +The file format is a modified INI syntax. The general structure is: + +```ini +[section1] + option1 = some value + option2 = some other value + +[section2] + option3 = some third value +``` +You can edit the configuration file using the `edit-config` script from the +Netdata [config directory](https://github.com/netdata/netdata/blob/master/docs/configure/nodes.md#the-netdata-config-directory). + +```bash +cd /etc/netdata 2>/dev/null || cd /opt/netdata/etc/netdata +sudo ./edit-config netdata.conf +``` +#### Options + + + +<details><summary>Config options</summary> + +| Name | Description | Default | Required | +|:----|:-----------|:-------|:--------:| +| update every | Data collection frequency. | 1 | no | +| command options | Additional parameters for the collector | | no | + +</details> + +#### Examples +There are no configuration examples. + + diff --git a/collectors/cups.plugin/metadata.yaml b/collectors/cups.plugin/metadata.yaml new file mode 100644 index 00000000..9ec2f411 --- /dev/null +++ b/collectors/cups.plugin/metadata.yaml @@ -0,0 +1,131 @@ +plugin_name: cups.plugin +modules: + - meta: + plugin_name: cups.plugin + module_name: cups.plugin + monitored_instance: + name: CUPS + link: "https://www.cups.org/" + categories: + - data-collection.hardware-devices-and-sensors + icon_filename: "cups.png" + related_resources: + integrations: + list: [] + info_provided_to_referring_integrations: + description: "" + keywords: [] + most_popular: false + overview: + data_collection: + metrics_description: "Monitor CUPS performance for achieving optimal printing system operations. Monitor job statuses, queue lengths, and error rates to ensure smooth printing tasks." + method_description: "The plugin uses CUPS shared library to connect and monitor the server." + supported_platforms: + include: [] + exclude: [] + multi_instance: true + additional_permissions: + description: "The plugin needs to access the server. Netdata sets permissions during installation time to reach the server through its library." + default_behavior: + auto_detection: + description: "The plugin detects when CUPS server is running and tries to connect to it." + limits: + description: "" + performance_impact: + description: "" + setup: + prerequisites: + list: + - title: Minimum setup + description: "The CUPS server must be installed and running. If you installed `netdata` using a package manager, it is also necessary to install the package `netdata-plugin-cups`." + configuration: + file: + name: "netdata.conf" + section_name: "[plugin:cups]" + description: "The netdata main configuration file." + options: + description: "" + folding: + title: "Config options" + enabled: true + list: + - name: update every + description: Data collection frequency. + default_value: 1 + required: false + - name: command options + description: Additional parameters for the collector + default_value: "" + required: false + examples: + folding: + enabled: true + title: "" + list: [] + troubleshooting: + problems: + list: [] + alerts: [] + metrics: + folding: + title: Metrics + enabled: false + description: "" + availability: [] + scopes: + - name: global + description: "" + labels: [] + metrics: + - name: cups.dests_state + description: Destinations by state + unit: "dests" + chart_type: stacked + dimensions: + - name: idle + - name: printing + - name: stopped + - name: cups.dests_option + description: Destinations by option + unit: "dests" + chart_type: line + dimensions: + - name: total + - name: acceptingjobs + - name: shared + - name: cups.job_num + description: Active jobs + unit: "jobs" + chart_type: stacked + dimensions: + - name: pending + - name: held + - name: processing + - name: cups.job_size + description: Active jobs size + unit: "KB" + chart_type: stacked + dimensions: + - name: pending + - name: held + - name: processing + - name: destination + description: "" + labels: [] + metrics: + - name: cups.destination_job_num + description: Active jobs of {destination} + unit: "jobs" + chart_type: stacked + dimensions: + - name: pending + - name: held + - name: processing + - name: cups.destination_job_size + description: Active jobs size of {destination} + unit: "KB" + chart_type: stacked + dimensions: + - name: pending + - name: held + - name: processing |