Hi,

Feel free to ignore or include this, or tell me it's been done better! I
thought I'd put it out for reference if nothing else.

I needed a way to report the RedHat style ethernet port alias names
(/etc/sysconfig/network-scripts/ifcfg-*) as ifDescr for a series of
products.

The, rather basic, patch tucks into the ifTable code and rewrites the
tmp_descr field if it can find an appropriate alias, else it leaves the
field well alone.
Any interface without such a name is therefore unaffected (I assume this
will apply to all other systems, although haven't tested outside of our
environment).

Cheers,

John.

Patch file attached.
--- net-snmp-5.3.0.1/agent/mibgroup/if-mib/ifTable/ifTable.c    2006-04-11 14:48:51.580782604 +0100
+++ net-snmp-5.3.0.1/agent/mibgroup/if-mib/ifTable/ifTable.c.ifDescr_alias    2006-04-11 14:57:13.748530262 +0100
@@ -36,6 +36,9 @@
 #   include "if-mib/ifXTable/ifXTable.h"
 #endif
 
+// RedHat alias includes.
+#include <dirent.h>
+
 oid             ifTable_oid[] = { IFTABLE_OID };
 int             ifTable_oid_size = OID_LENGTH(ifTable_oid);
 
@@ -425,10 +428,13 @@
     } else
         tmp_descr = rowreq_ctx->data.ifDescr;
 
-    if (NULL != tmp_descr)
-        tmp_len = strlen(tmp_descr);
-    else
+    if (NULL != tmp_descr) {
+      //RedHat - Rename interfaces to their RedHat aliases.
+      eth2if(tmp_descr);
+      tmp_len = strlen(tmp_descr);
+    } else {
         tmp_len = 0;
+    }
 
     /*
      * TODO:231:o: |-> Extract the current value of the ifDescr data.
@@ -2307,5 +2313,53 @@
     return rc;
 }                               /* ifTable_check_dependencies */
 
+/*
+ * eth2if takes an interface name and converts it to the RedHat alias
+ * for that port.
+ *
+ * If no alias is found the interface name is unchanged.
+ *
+*/
+void
+eth2if(char* ethname)  
+{
+//sprintf(ethname, "%s-Hi There!", ethname); return;
+  DIR* dir;
+  struct dirent* entry;
+  dir = opendir ("/etc/sysconfig/network-scripts");
+  if (!dir) return;
+  while ((entry = readdir(dir))) {
+    char* filename = strstr(entry->d_name, "ifcfg-");
+    if (filename) {
+      char path[128];
+      sprintf(path, "/etc/sysconfig/network-scripts/%s", entry->d_name);
+      FILE* f = fopen(path, "r");
+      if (!f) {closedir(dir); return;}
+      while (1) {
+        char* lineptr = NULL;
+        size_t length = 0;
+        if (getline(&lineptr, &length, f) == -1)
+          break;
+
+        char* lf = strchr(lineptr, '\n');
+        if (lf) *lf = 0;
+
+        if (!strncmp(lineptr, "DEVICE=", 7)) {
+          if (!strcmp(lineptr + 7, ethname)) {
+            sprintf(ethname, "%s", filename + 6);
+            fclose(f);
+            closedir(dir);
+            return;
+          }
+        }
+        free(lineptr);
+      }
+      fclose(f);
+    }
+  }
+  closedir(dir);
+  return;
+}
+
 /** @} */
 /** @{ */
--- net-snmp-5.3.0.1/agent/mibgroup/if-mib/ifTable/ifTable.h        2006-04-11 14:48:55.927615165 +0100
+++ net-snmp-5.3.0.1/agent/mibgroup/if-mib/ifTable/ifTable.h.ifDescr_Alias    2006-04-11 14:49:12.173989540 +0100
@@ -700,6 +700,9 @@
     int             ifTable_check_dependencies(ifTable_rowreq_ctx * ctx);
 
 
+    // RH definition:
+    void            eth2if(char *ethname);
+
     /*
      * DUMMY markers, ignore
      *

Reply via email to