static int dispatch_values (const data_set_t *ds, value_list_t *vl, FILE *fh, char *buffer) { int status; status = parse_values (buffer, vl, ds); if (status != 0) { print_to_socket (fh, "-1 Parsing the values string failed.\n"); return (-1); } plugin_dispatch_values (vl); return (0); } /* int dispatch_values */
int handle_listval (FILE *fh, char *buffer) { char *command; char **names = NULL; cdtime_t *times = NULL; size_t number = 0; size_t i; int status; DEBUG ("utils_cmd_listval: handle_listval (fh = %p, buffer = %s);", (void *) fh, buffer); command = NULL; status = parse_string (&buffer, &command); if (status != 0) { print_to_socket (fh, "-1 Cannot parse command.\n"); free_everything_and_return (-1); } assert (command != NULL); if (strcasecmp ("LISTVAL", command) != 0) { print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command); free_everything_and_return (-1); } if (*buffer != 0) { print_to_socket (fh, "-1 Garbage after end of command: %s\n", buffer); free_everything_and_return (-1); } status = uc_get_names (&names, ×, &number); if (status != 0) { DEBUG ("command listval: uc_get_names failed with status %i", status); print_to_socket (fh, "-1 uc_get_names failed.\n"); free_everything_and_return (-1); } print_to_socket (fh, "%i Value%s found\n", (int) number, (number == 1) ? "" : "s"); for (i = 0; i < number; i++) print_to_socket (fh, "%.3f %s\n", CDTIME_T_TO_DOUBLE (times[i]), names[i]); free_everything_and_return (0); } /* int handle_listval */
int handle_putval (FILE *fh, char *buffer) { char *command; char *identifier; char *hostname; char *plugin; char *plugin_instance; char *type; char *type_instance; int status; int values_submitted; char *identifier_copy; const data_set_t *ds; value_list_t vl = VALUE_LIST_INIT; DEBUG ("utils_cmd_putval: handle_putval (fh = %p, buffer = %s);", (void *) fh, buffer); command = NULL; status = parse_string (&buffer, &command); if (status != 0) { print_to_socket (fh, "-1 Cannot parse command.\n"); return (-1); } assert (command != NULL); if (strcasecmp ("PUTVAL", command) != 0) { print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command); return (-1); } identifier = NULL; status = parse_string (&buffer, &identifier); if (status != 0) { print_to_socket (fh, "-1 Cannot parse identifier.\n"); return (-1); } assert (identifier != NULL); /* parse_identifier() modifies its first argument, * returning pointers into it */ identifier_copy = sstrdup (identifier); status = parse_identifier (identifier_copy, &hostname, &plugin, &plugin_instance, &type, &type_instance); if (status != 0) { DEBUG ("handle_putval: Cannot parse identifier `%s'.", identifier); print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n", identifier); sfree (identifier_copy); return (-1); } if ((strlen (hostname) >= sizeof (vl.host)) || (strlen (plugin) >= sizeof (vl.plugin)) || ((plugin_instance != NULL) && (strlen (plugin_instance) >= sizeof (vl.plugin_instance))) || ((type_instance != NULL) && (strlen (type_instance) >= sizeof (vl.type_instance)))) { print_to_socket (fh, "-1 Identifier too long.\n"); sfree (identifier_copy); return (-1); } sstrncpy (vl.host, hostname, sizeof (vl.host)); sstrncpy (vl.plugin, plugin, sizeof (vl.plugin)); sstrncpy (vl.type, type, sizeof (vl.type)); if (plugin_instance != NULL) sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance)); if (type_instance != NULL) sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); ds = plugin_get_ds (type); if (ds == NULL) { print_to_socket (fh, "-1 Type `%s' isn't defined.\n", type); sfree (identifier_copy); return (-1); } /* Free identifier_copy */ hostname = NULL; plugin = NULL; plugin_instance = NULL; type = NULL; type_instance = NULL; sfree (identifier_copy); vl.values_len = ds->ds_num; vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t)); if (vl.values == NULL) { print_to_socket (fh, "-1 malloc failed.\n"); return (-1); } /* All the remaining fields are part of the optionlist. */ values_submitted = 0; while (*buffer != 0) { char *string = NULL; char *value = NULL; status = parse_option (&buffer, &string, &value); if (status < 0) { /* parse_option failed, buffer has been modified. * => we need to abort */ print_to_socket (fh, "-1 Misformatted option.\n"); return (-1); } else if (status == 0) { assert (string != NULL); assert (value != NULL); set_option (&vl, string, value); continue; } /* else: parse_option but buffer has not been modified. This is * the default if no `=' is found.. */ status = parse_string (&buffer, &string); if (status != 0) { print_to_socket (fh, "-1 Misformatted value.\n"); return (-1); } assert (string != NULL); status = dispatch_values (ds, &vl, fh, string); if (status != 0) { /* An error has already been printed. */ return (-1); } values_submitted++; } /* while (*buffer != 0) */ /* Done parsing the options. */ print_to_socket (fh, "0 Success: %i %s been dispatched.\n", values_submitted, (values_submitted == 1) ? "value has" : "values have"); sfree (vl.values); return (0); } /* int handle_putval */
int handle_getthreshold (FILE *fh, char *buffer) { char *command; char *identifier; char *identifier_copy; char *host; char *plugin; char *plugin_instance; char *type; char *type_instance; value_list_t vl; threshold_t threshold; int status; size_t i; if ((fh == NULL) || (buffer == NULL)) return (-1); DEBUG ("utils_cmd_getthreshold: handle_getthreshold (fh = %p, buffer = %s);", (void *) fh, buffer); command = NULL; status = parse_string (&buffer, &command); if (status != 0) { print_to_socket (fh, "-1 Cannot parse command.\n"); return (-1); } assert (command != NULL); if (strcasecmp ("GETTHRESHOLD", command) != 0) { print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command); return (-1); } identifier = NULL; status = parse_string (&buffer, &identifier); if (status != 0) { print_to_socket (fh, "-1 Cannot parse identifier.\n"); return (-1); } assert (identifier != NULL); if (*buffer != 0) { print_to_socket (fh, "-1 Garbage after end of command: %s\n", buffer); return (-1); } /* parse_identifier() modifies its first argument, * returning pointers into it */ identifier_copy = sstrdup (identifier); status = parse_identifier (identifier_copy, &host, &plugin, &plugin_instance, &type, &type_instance); if (status != 0) { DEBUG ("handle_getthreshold: Cannot parse identifier `%s'.", identifier); print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n", identifier); sfree (identifier_copy); return (-1); } memset (&vl, 0, sizeof (vl)); sstrncpy (vl.host, host, sizeof (vl.host)); sstrncpy (vl.plugin, plugin, sizeof (vl.plugin)); if (plugin_instance != NULL) sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance)); sstrncpy (vl.type, type, sizeof (vl.type)); if (type_instance != NULL) sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); sfree (identifier_copy); memset (&threshold, 0, sizeof (threshold)); status = ut_search_threshold (&vl, &threshold); if (status == ENOENT) { print_to_socket (fh, "-1 No threshold found for identifier %s\n", identifier); return (0); } else if (status != 0) { print_to_socket (fh, "-1 Error while looking up threshold: %i\n", status); return (-1); } /* Lets count the number of lines we'll return. */ i = 0; if (threshold.host[0] != 0) i++; if (threshold.plugin[0] != 0) i++; if (threshold.plugin_instance[0] != 0) i++; if (threshold.type[0] != 0) i++; if (threshold.type_instance[0] != 0) i++; if (threshold.data_source[0] != 0) i++; if (!isnan (threshold.warning_min)) i++; if (!isnan (threshold.warning_max)) i++; if (!isnan (threshold.failure_min)) i++; if (!isnan (threshold.failure_max)) i++; if (threshold.hysteresis > 0.0) i++; if (threshold.hits > 1) i++; /* Print the response */ print_to_socket (fh, "%zu Threshold found\n", i); if (threshold.host[0] != 0) print_to_socket (fh, "Host: %s\n", threshold.host) if (threshold.plugin[0] != 0) print_to_socket (fh, "Plugin: %s\n", threshold.plugin) if (threshold.plugin_instance[0] != 0) print_to_socket (fh, "Plugin Instance: %s\n", threshold.plugin_instance) if (threshold.type[0] != 0) print_to_socket (fh, "Type: %s\n", threshold.type) if (threshold.type_instance[0] != 0) print_to_socket (fh, "Type Instance: %s\n", threshold.type_instance) if (threshold.data_source[0] != 0) print_to_socket (fh, "Data Source: %s\n", threshold.data_source) if (!isnan (threshold.warning_min)) print_to_socket (fh, "Warning Min: %g\n", threshold.warning_min) if (!isnan (threshold.warning_max)) print_to_socket (fh, "Warning Max: %g\n", threshold.warning_max) if (!isnan (threshold.failure_min)) print_to_socket (fh, "Failure Min: %g\n", threshold.failure_min) if (!isnan (threshold.failure_max)) print_to_socket (fh, "Failure Max: %g\n", threshold.failure_max) if (threshold.hysteresis > 0.0) print_to_socket (fh, "Hysteresis: %g\n", threshold.hysteresis) if (threshold.hits > 1) print_to_socket (fh, "Hits: %i\n", threshold.hits) return (0); } /* int handle_getthreshold */
int handle_getval (FILE *fh, char *buffer) { char *command; char *identifier; char *identifier_copy; char *hostname; char *plugin; char *plugin_instance; char *type; char *type_instance; gauge_t *values; size_t values_num; const data_set_t *ds; int status; size_t i; if ((fh == NULL) || (buffer == NULL)) return (-1); DEBUG ("utils_cmd_getval: handle_getval (fh = %p, buffer = %s);", (void *) fh, buffer); command = NULL; status = parse_string (&buffer, &command); if (status != 0) { print_to_socket (fh, "-1 Cannot parse command.\n"); return (-1); } assert (command != NULL); if (strcasecmp ("GETVAL", command) != 0) { print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command); return (-1); } identifier = NULL; status = parse_string (&buffer, &identifier); if (status != 0) { print_to_socket (fh, "-1 Cannot parse identifier.\n"); return (-1); } assert (identifier != NULL); if (*buffer != 0) { print_to_socket (fh, "-1 Garbage after end of command: %s\n", buffer); return (-1); } /* parse_identifier() modifies its first argument, * returning pointers into it */ identifier_copy = sstrdup (identifier); status = parse_identifier (identifier_copy, &hostname, &plugin, &plugin_instance, &type, &type_instance); if (status != 0) { DEBUG ("handle_getval: Cannot parse identifier `%s'.", identifier); print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n", identifier); sfree (identifier_copy); return (-1); } ds = plugin_get_ds (type); if (ds == NULL) { DEBUG ("handle_getval: plugin_get_ds (%s) == NULL;", type); print_to_socket (fh, "-1 Type `%s' is unknown.\n", type); sfree (identifier_copy); return (-1); } values = NULL; values_num = 0; status = uc_get_rate_by_name (identifier, &values, &values_num); if (status != 0) { print_to_socket (fh, "-1 No such value\n"); sfree (identifier_copy); return (-1); } if (ds->ds_num != values_num) { ERROR ("ds[%s]->ds_num = %zu, " "but uc_get_rate_by_name returned %zu values.", ds->type, ds->ds_num, values_num); print_to_socket (fh, "-1 Error reading value from cache.\n"); sfree (values); sfree (identifier_copy); return (-1); } print_to_socket (fh, "%zu Value%s found\n", values_num, (values_num == 1) ? "" : "s"); for (i = 0; i < values_num; i++) { print_to_socket (fh, "%s=", ds->ds[i].name); if (isnan (values[i])) { print_to_socket (fh, "NaN\n"); } else { print_to_socket (fh, "%12e\n", values[i]); } } sfree (values); sfree (identifier_copy); return (0); } /* int handle_getval */
int handle_putnotif(FILE *fh, char *buffer) { char *command; notification_t n = {0}; int status; if ((fh == NULL) || (buffer == NULL)) return -1; DEBUG("utils_cmd_putnotif: handle_putnotif (fh = %p, buffer = %s);", (void *)fh, buffer); command = NULL; status = parse_string(&buffer, &command); if (status != 0) { print_to_socket(fh, "-1 Cannot parse command.\n"); return -1; } assert(command != NULL); if (strcasecmp("PUTNOTIF", command) != 0) { print_to_socket(fh, "-1 Unexpected command: `%s'.\n", command); return -1; } status = 0; while (*buffer != 0) { char *key; char *value; status = parse_option(&buffer, &key, &value); if (status != 0) { print_to_socket(fh, "-1 Malformed option.\n"); break; } status = set_option(&n, key, value); if (status != 0) { print_to_socket(fh, "-1 Error parsing option `%s'\n", key); break; } } /* for (i) */ /* Check for required fields and complain if anything is missing. */ if ((status == 0) && (n.severity == 0)) { print_to_socket(fh, "-1 Option `severity' missing.\n"); status = -1; } if ((status == 0) && (n.time == 0)) { print_to_socket(fh, "-1 Option `time' missing.\n"); status = -1; } if ((status == 0) && (strlen(n.message) == 0)) { print_to_socket(fh, "-1 No message or message of length 0 given.\n"); status = -1; } /* If status is still zero the notification is fine and we can finally * dispatch it. */ if (status == 0) { plugin_dispatch_notification(&n); print_to_socket(fh, "0 Success\n"); } return 0; } /* int handle_putnotif */