src/graph_instance.[ch]: Implement "inst_matches_field".
[collection4.git] / src / graph_instance.c
index bad0bcd..109316f 100644 (file)
@@ -4,10 +4,11 @@
 #include <time.h>
 
 #include "graph_instance.h"
+#include "graph_def.h"
 #include "graph_ident.h"
 #include "graph_list.h"
 #include "common.h"
-#include "utils_params.h"
+#include "utils_cgi.h"
 
 #include <fcgiapp.h>
 #include <fcgi_stdio.h>
@@ -18,8 +19,6 @@ struct graph_instance_s /* {{{ */
 
   graph_ident_t **files;
   size_t files_num;
-
-  graph_instance_t *next;
 }; /* }}} struct graph_instance_s */
 
 struct def_callback_data_s
@@ -190,21 +189,16 @@ graph_instance_t *inst_create (graph_config_t *cfg, /* {{{ */
   i->files = NULL;
   i->files_num = 0;
 
-  i->next = NULL;
-
   return (i);
 } /* }}} graph_instance_t *inst_create */
 
 void inst_destroy (graph_instance_t *inst) /* {{{ */
 {
-  graph_instance_t *next;
   size_t i;
 
   if (inst == NULL)
     return;
 
-  next = inst->next;
-
   ident_destroy (inst->select);
 
   for (i = 0; i < inst->files_num; i++)
@@ -212,8 +206,6 @@ void inst_destroy (graph_instance_t *inst) /* {{{ */
   free (inst->files);
 
   free (inst);
-
-  inst_destroy (next);
 } /* }}} void inst_destroy */
 
 int inst_add_file (graph_instance_t *inst, /* {{{ */
@@ -239,9 +231,12 @@ graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
 {
   const char *host = get_part_from_param ("inst_host", "host");
   const char *plugin = get_part_from_param ("inst_plugin", "plugin");
-  const char *plugin_instance = get_part_from_param ("inst_plugin_instance", "plugin_instance");
+  const char *plugin_instance = get_part_from_param ("inst_plugin_instance",
+      "plugin_instance");
   const char *type = get_part_from_param ("inst_type", "type");
-  const char *type_instance = get_part_from_param ("inst_type_instance", "type_instance");
+  const char *type_instance = get_part_from_param ("inst_type_instance",
+      "type_instance");
+
   graph_ident_t *ident;
   graph_instance_t *inst;
 
@@ -263,19 +258,16 @@ graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
   }
 
   ident = ident_create (host, plugin, plugin_instance, type, type_instance);
-
-  for (inst = graph_get_instances (cfg); inst != NULL; inst = inst->next)
+  if (ident == NULL)
   {
-    if (ident_compare (ident, inst->select) != 0)
-      continue;
-
-    ident_destroy (ident);
-    return (inst);
+    fprintf (stderr, "inst_get_selected: ident_create failed\n");
+    return (NULL);
   }
 
-  DEBUG ("inst_get_selected: No match found.\n");
+  inst = graph_inst_find_exact (cfg, ident);
   ident_destroy (ident);
-  return (NULL);
+
+  return (inst);
 } /* }}} graph_instance_t *inst_get_selected */
 
 int inst_get_rrdargs (graph_config_t *cfg, /* {{{ */
@@ -379,56 +371,89 @@ int inst_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
   return (0);
 } /* }}} int inst_get_params */
 
-int inst_append (graph_instance_t *head, graph_instance_t *inst) /* {{{ */
+int inst_compare_ident (graph_instance_t *inst, /* {{{ */
+    const graph_ident_t *ident)
 {
-  graph_instance_t *ptr;
-
-  if ((head == NULL) || (inst == NULL))
-    return (EINVAL);
+  if ((inst == NULL) || (ident == NULL))
+    return (0);
 
-  ptr = head;
-  while (ptr->next != NULL)
-    ptr = ptr->next;
+  return (ident_compare (inst->select, ident));
+} /* }}} int inst_compare_ident */
 
-  ptr->next = inst;
+_Bool inst_matches_ident (graph_instance_t *inst, /* {{{ */
+    const graph_ident_t *ident)
+{
+  if ((inst == NULL) || (ident == NULL))
+    return (0);
 
-  return (0);
-} /* }}} int inst_append */
+  return (ident_matches (inst->select, ident));
+} /* }}} _Bool inst_matches_ident */
 
-int inst_foreach (graph_instance_t *inst, /* {{{ */
-               inst_callback_t cb, void *user_data)
+_Bool inst_matches_string (graph_config_t *cfg, /* {{{ */
+    graph_instance_t *inst,
+    const char *term)
 {
-  graph_instance_t *ptr;
+  char buffer[1024];
+  int status;
 
-  if ((inst == NULL) || (cb == NULL))
-    return (EINVAL);
+  if ((cfg == NULL) || (inst == NULL) || (term == NULL))
+    return (0);
 
-  for (ptr = inst; ptr != NULL; ptr = ptr->next)
+  status = inst_describe (cfg, inst, buffer, sizeof (buffer));
+  if (status != 0)
   {
-    int status;
-
-    status = (*cb) (ptr, user_data);
-    if (status != 0)
-      return (status);
+    fprintf (stderr, "inst_matches_string: inst_describe failed\n");
+    return (status);
   }
 
-  return (0);
-} /* }}} int inst_foreach */
+  strtolower (buffer);
 
-graph_instance_t *inst_find_matching (graph_instance_t *inst, /* {{{ */
-    const graph_ident_t *ident)
+  /* no match */
+  if (strstr (buffer, term) == NULL)
+    return (0);
+
+  return (1);
+} /* }}} _Bool inst_matches_string */
+
+_Bool inst_matches_field (graph_instance_t *inst, /* {{{ */
+    graph_ident_field_t field, const char *field_value)
 {
-  graph_instance_t *ptr;
+  const char *selector_field;
+  size_t i;
 
-  if ((inst == NULL) || (ident == NULL))
-    return (NULL);
+  if ((inst == NULL) || (field_value == NULL))
+    return (0);
 
-  for (ptr = inst; ptr != NULL; ptr = ptr->next)
-    if (ident_matches (ptr->select, ident))
-      return (ptr);
+  selector_field = ident_get_field (inst->select, field);
+  if (selector_field == NULL)
+    return (0);
+
+  assert (!IS_ANY (selector_field));
+  if (!IS_ALL (selector_field))
+  {
+    if (strcasecmp (selector_field, field_value) == 0)
+      return (1);
+    else
+      return (0);
+  }
+
+  /* The selector field is an ALL selector
+   * => we need to check the files to see if the instance matches. */
+  for (i = 0; i < inst->files_num; i++)
+  {
+    selector_field = ident_get_field (inst->files[i], field);
+    if (selector_field == NULL)
+      continue;
 
-  return (NULL);
-} /* }}} graph_instance_t *inst_find_matching */
+    assert (!IS_ANY (selector_field));
+    assert (!IS_ALL (selector_field));
+
+    if (strcasecmp (selector_field, field_value) == 0)
+      return (1);
+  } /* for files */
+
+  return (0);
+} /* }}} _Bool inst_matches_field */
 
 int inst_describe (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
     char *buffer, size_t buffer_size)