Merge pull request #2354 from rpv-tomsk/issue-2344-nut-rework
authorRuben Kerkhof <ruben@rubenkerkhof.com>
Sat, 15 Jul 2017 13:55:52 +0000 (15:55 +0200)
committerGitHub <noreply@github.com>
Sat, 15 Jul 2017 13:55:52 +0000 (15:55 +0200)
nut plugin: Multi-threaded ups polling with connect timeout.

contrib/systemd.collectd.service
src/collectd.conf.pod
src/ipc.c
src/snmp_agent.c

index 853363d..6333d49 100644 (file)
@@ -30,8 +30,6 @@ ProtectHome=true
 # By default, drop all capabilities:
 CapabilityBoundingSet=
 
-NoNewPrivileges=true
-
 # Tell systemd it will receive a notification from collectd over its control
 # socket once the daemon is ready. See systemd.service(5) for more details.
 Type=notify
index de5dc98..67ee0a9 100644 (file)
@@ -7194,6 +7194,7 @@ B<Synopsis:>
   <Plugin snmp_agent>
     <Data "memAvailReal">
       Plugin "memory"
+      #PluginInstance "some"
       Type "memory"
       TypeInstance "free"
       OIDs "1.3.6.1.4.1.2021.4.6.0"
@@ -7237,6 +7238,12 @@ scalar data type B<Instance> has no effect and can be omitted.
 
 Read plugin name whose collected data will be mapped to specified OIDs.
 
+=item B<PluginInstance> I<String>
+
+Read plugin instance whose collected data will be mapped to specified OIDs.
+The field is optional and by default there is no plugin instance check.
+Allowed only if B<Data> block defines scalar data type.
+
 =item B<Type> I<String>
 
 Collectd's type that is to be used for specified OID, e.E<nbsp>g. "if_octets"
@@ -7249,9 +7256,9 @@ Collectd's type-instance that is to be used for specified OID.
 =item B<OIDs> I<OID> [I<OID> ...]
 
 Configures the OIDs to be handled by I<snmp_agent> plugin. Values for these OIDs
-are taken from collectd data type specified by B<Plugin>, B<Type>,
-B<TypeInstance> fields of this B<Data> block. Number of the OIDs configured
-should correspond to number of values in specified B<Type>.
+are taken from collectd data type specified by B<Plugin>, B<PluginInstance>,
+B<Type>, B<TypeInstance> fields of this B<Data> block. Number of the OIDs
+configured should correspond to number of values in specified B<Type>.
 For example two OIDs "IF-MIB::ifInOctets" "IF-MIB::ifOutOctets" can be mapped to
 "rx" and "tx" values of "if_octets" type.
 
index 432c5e0..ed8a0c0 100644 (file)
--- a/src/ipc.c
+++ b/src/ipc.c
@@ -251,14 +251,14 @@ static int ipc_read_shm(void) /* {{{ */
   ipcinfo_shm_t *pshm;
   unsigned int shm_segments = 0;
   size64_t shm_bytes = 0;
-  int n;
+  int i, n;
 
   ipcinfo_shm = (ipcinfo_shm_t *)ipc_get_info(
       0, GET_IPCINFO_SHM_ALL, IPCINFO_SHM_VERSION, sizeof(ipcinfo_shm_t), &n);
   if (ipcinfo_shm == NULL)
     return -1;
 
-  for (int i = 0, pshm = ipcinfo_shm; i < n; i++, pshm++) {
+  for (i = 0, pshm = ipcinfo_shm; i < n; i++, pshm++) {
     shm_segments++;
     shm_bytes += pshm->shm_segsz;
   }
index 0908b4e..6a19685 100644 (file)
@@ -98,6 +98,7 @@ static snmp_agent_ctx_t *g_agent = NULL;
       (_dd->type ? !strcmp(_dd->type, _t) : 0) &&                              \
       (_dd->type_instance ? !strcmp(_dd->type_instance, _ti) : 1)
 
+static int snmp_agent_shutdown(void);
 static void *snmp_agent_thread_run(void *arg);
 static int snmp_agent_register_oid(oid_t *oid, Netsnmp_Node_Handler *handler);
 static int snmp_agent_set_vardata(void *dst_buf, size_t *dst_buf_len,
@@ -420,28 +421,6 @@ static void snmp_agent_free_data(data_definition_t **dd) {
     for (size_t i = 0; i < (*dd)->oids_len; i++)
       unregister_mib((*dd)->oids[i].oid, (*dd)->oids[i].oid_len);
   }
-  if (!(*dd)->table->index_oid.oid_len) {
-    char *instance;
-
-    c_avl_iterator_t *iter = c_avl_get_iterator((*dd)->table->instance_index);
-    while (c_avl_iterator_next(iter, (void *)&instance, (void *)&instance) ==
-           0) {
-      for (size_t i = 0; i < (*dd)->oids_len; i++)
-        snmp_agent_unregister_oid_string(&(*dd)->oids[i], instance);
-    }
-    c_avl_iterator_destroy(iter);
-  } else {
-    /* unregister all table OIDs */
-    int *index;
-    char *value;
-
-    c_avl_iterator_t *iter = c_avl_get_iterator((*dd)->table->index_instance);
-    while (c_avl_iterator_next(iter, (void *)&index, (void *)&value) == 0) {
-      for (size_t i = 0; i < (*dd)->oids_len; i++)
-        snmp_agent_unregister_oid_index(&(*dd)->oids[i], *index);
-    }
-    c_avl_iterator_destroy(iter);
-  }
 
   sfree((*dd)->name);
   sfree((*dd)->plugin);
@@ -455,6 +434,43 @@ static void snmp_agent_free_data(data_definition_t **dd) {
   return;
 }
 
+static void snmp_agent_free_table_columns(table_definition_t *td) {
+  if (td->columns == NULL)
+    return;
+
+  for (llentry_t *de = llist_head(td->columns); de != NULL; de = de->next) {
+    data_definition_t *dd = de->value;
+
+    if (td->index_oid.oid_len) {
+      int *index;
+      char *instance;
+
+      c_avl_iterator_t *iter = c_avl_get_iterator(td->index_instance);
+      while (c_avl_iterator_next(iter, (void *)&index, (void *)&instance) ==
+             0) {
+        for (size_t i = 0; i < dd->oids_len; i++)
+          snmp_agent_unregister_oid_index(&dd->oids[i], *index);
+      }
+      c_avl_iterator_destroy(iter);
+    } else {
+      char *instance;
+
+      c_avl_iterator_t *iter = c_avl_get_iterator(dd->table->instance_index);
+      while (c_avl_iterator_next(iter, (void *)&instance, (void *)&instance) ==
+             0) {
+        for (size_t i = 0; i < dd->oids_len; i++)
+          snmp_agent_unregister_oid_string(&dd->oids[i], instance);
+      }
+      c_avl_iterator_destroy(iter);
+    }
+
+    snmp_agent_free_data(&dd);
+  }
+
+  llist_destroy(td->columns);
+  td->columns = NULL;
+} /* void snmp_agent_free_table_columns */
+
 static void snmp_agent_free_table(table_definition_t **td) {
 
   if (td == NULL || *td == NULL)
@@ -463,23 +479,20 @@ static void snmp_agent_free_table(table_definition_t **td) {
   if ((*td)->size_oid.oid_len)
     unregister_mib((*td)->size_oid.oid, (*td)->size_oid.oid_len);
 
+  /* Unregister Index OIDs */
   if ((*td)->index_oid.oid_len) {
     int *index;
-    char *value;
+    char *instance;
 
     c_avl_iterator_t *iter = c_avl_get_iterator((*td)->index_instance);
-    while (c_avl_iterator_next(iter, (void *)&index, (void *)&value) == 0)
+    while (c_avl_iterator_next(iter, (void *)&index, (void *)&instance) == 0)
       snmp_agent_unregister_oid_index(&(*td)->index_oid, *index);
 
     c_avl_iterator_destroy(iter);
   }
 
-  for (llentry_t *de = llist_head((*td)->columns); de != NULL; de = de->next) {
-    data_definition_t *dd = de->value;
-    snmp_agent_free_data(&dd);
-  }
-
-  llist_destroy((*td)->columns);
+  /* Unregister all table columns and their registered OIDs */
+  snmp_agent_free_table_columns(*td);
 
   void *key = NULL;
   void *value = NULL;
@@ -1364,12 +1377,21 @@ static int snmp_agent_preinit(void) {
   g_agent->tables = llist_create();
   g_agent->scalars = llist_create();
 
+  if (g_agent->tables == NULL || g_agent->scalars == NULL) {
+    ERROR(PLUGIN_NAME ": llist_create() failed");
+    llist_destroy(g_agent->scalars);
+    llist_destroy(g_agent->tables);
+    return -ENOMEM;
+  }
+
   int err;
   /* make us a agentx client. */
   err = netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE,
                                1);
   if (err != 0) {
     ERROR(PLUGIN_NAME ": Failed to set agent role (%d)", err);
+    llist_destroy(g_agent->scalars);
+    llist_destroy(g_agent->tables);
     return -1;
   }
 
@@ -1381,6 +1403,8 @@ static int snmp_agent_preinit(void) {
   err = init_agent(PLUGIN_NAME);
   if (err != 0) {
     ERROR(PLUGIN_NAME ": Failed to initialize the agent library (%d)", err);
+    llist_destroy(g_agent->scalars);
+    llist_destroy(g_agent->tables);
     return -1;
   }
 
@@ -1394,9 +1418,13 @@ static int snmp_agent_preinit(void) {
 static int snmp_agent_init(void) {
   int ret;
 
-  ret = snmp_agent_preinit();
-  if (ret != 0)
-    return ret;
+  if (g_agent == NULL || ((llist_head(g_agent->scalars) == NULL) &&
+                          (llist_head(g_agent->tables) == NULL))) {
+    ERROR(PLUGIN_NAME ": snmp_agent_init: plugin not configured");
+    return -EINVAL;
+  }
+
+  plugin_register_shutdown(PLUGIN_NAME, snmp_agent_shutdown);
 
   ret = snmp_agent_register_scalar_oids();
   if (ret != 0)
@@ -1406,13 +1434,6 @@ static int snmp_agent_init(void) {
   if (ret != 0)
     return ret;
 
-  /* create a second thread to listen for requests from AgentX*/
-  ret = pthread_create(&g_agent->thread, NULL, &snmp_agent_thread_run, NULL);
-  if (ret != 0) {
-    ERROR(PLUGIN_NAME ": Failed to create a separate thread, err %u", ret);
-    return ret;
-  }
-
   ret = pthread_mutex_init(&g_agent->lock, NULL);
   if (ret != 0) {
     ERROR(PLUGIN_NAME ": Failed to initialize mutex, err %u", ret);
@@ -1425,6 +1446,18 @@ static int snmp_agent_init(void) {
     return ret;
   }
 
+  /* create a second thread to listen for requests from AgentX*/
+  ret = pthread_create(&g_agent->thread, NULL, &snmp_agent_thread_run, NULL);
+  if (ret != 0) {
+    ERROR(PLUGIN_NAME ": Failed to create a separate thread, err %u", ret);
+    return ret;
+  }
+
+  if (llist_head(g_agent->tables) != NULL) {
+    plugin_register_write(PLUGIN_NAME, snmp_agent_collect, NULL);
+    plugin_register_missing(PLUGIN_NAME, snmp_agent_clear_missing, NULL);
+  }
+
   return 0;
 }
 
@@ -1570,7 +1603,4 @@ static int snmp_agent_config(oconfig_item_t *ci) {
 void module_register(void) {
   plugin_register_init(PLUGIN_NAME, snmp_agent_init);
   plugin_register_complex_config(PLUGIN_NAME, snmp_agent_config);
-  plugin_register_write(PLUGIN_NAME, snmp_agent_collect, NULL);
-  plugin_register_missing(PLUGIN_NAME, snmp_agent_clear_missing, NULL);
-  plugin_register_shutdown(PLUGIN_NAME, snmp_agent_shutdown);
 }