1 improvements to HOST-RESOURCES-MIB::hrSWInstalled on debian systems
3 - add Install Date information instead of bogus 0-1-1
4 - include Arch in package names, like rpm output does
5 - use debian native separators `_` for package components, not rpm ones: `-`
8 snmpbulkwalk localhost HOST-RESOURCES-MIB::hrSWInstalled
10 Signed-Off-By: Elan Ruusamäe <glen@pld-linux.org>
12 --- net-snmp-5.7.3/agent/mibgroup/host/data_access/swinst_apt.c~ 2016-04-23 17:40:44.000000000 +0300
13 +++ net-snmp-5.7.3/agent/mibgroup/host/data_access/swinst_apt.c 2016-04-23 17:40:49.740392789 +0300
18 +#include <sys/stat.h>
20 #include <net-snmp/net-snmp-includes.h>
21 #include <net-snmp/agent/net-snmp-agent-includes.h>
23 #include <net-snmp/library/snmp_debug.h>
24 #include <net-snmp/data_access/swinst.h>
26 +netsnmp_feature_require(date_n_time)
28 char pkg_directory[SNMP_MAXBUF];
29 static char apt_fmt[SNMP_MAXBUF];
30 +static char file[SNMP_MAXBUF];
32 /* ---------------------------------------------------------------------
35 netsnmp_swinst_arch_init(void)
37 strlcpy(pkg_directory, "/var/lib/dpkg/info", sizeof(pkg_directory));
38 - snprintf(apt_fmt, SNMP_MAXBUF, "%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%ds",
39 - SNMP_MAXBUF-1, SNMP_MAXBUF-1, SNMP_MAXBUF-1,
40 + snprintf(apt_fmt, SNMP_MAXBUF, "%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%ds",
41 + SNMP_MAXBUF-1, SNMP_MAXBUF-1, SNMP_MAXBUF-1, SNMP_MAXBUF-1,
42 SNMP_MAXBUF-1, SNMP_MAXBUF-1, SNMP_MAXBUF-1);
47 netsnmp_swinst_arch_load( netsnmp_container *container, u_int flags)
49 - FILE *p = popen("dpkg-query --show --showformat '${Package}#${Version}#${Section}#${Priority}#${Essential}#${Status}\n'", "r");
50 + FILE *p = popen("dpkg-query --show --showformat '${Package}#${Version}#${Section}#${Priority}#${Essential}#${Archidecture}#${Status}\n'", "r");
51 char package[SNMP_MAXBUF];
52 char version[SNMP_MAXBUF];
53 char section[SNMP_MAXBUF];
54 char priority[SNMP_MAXBUF];
55 char essential[SNMP_MAXBUF];
56 + char arch[SNMP_MAXBUF];
57 char status[SNMP_MAXBUF];
59 + struct stat stat_buf;
60 netsnmp_swinst_entry *entry;
67 continue; /* error already logged by function */
68 CONTAINER_INSERT(container, entry);
70 - sscanf(buf, apt_fmt, package, version, section, priority, essential, status);
71 + sscanf(buf, apt_fmt, package, version, section, priority, essential, arch, status);
72 if (strstr(status, "not-installed"))
75 entry->swName_len = snprintf( entry->swName, sizeof(entry->swName),
76 - "%s-%s", package, version);
77 + "%s_%s_%s", package, version, arch);
78 if (entry->swName_len >= sizeof(entry->swName))
79 entry->swName_len = sizeof(entry->swName)-1;
80 entry->swType = (strcmp(essential, "yes") == 0)
81 ? 2 /* operatingSystem */
82 : 4; /* application */
84 - entry->swDate_len = 8;
85 - memcpy(entry->swDate, "\0\0\1\1\0\0\0\0", 8);
86 + /* get the last mod date */
87 + snprintf(file, sizeof(file), "%s/%s.list", pkg_directory, package);
88 + if(stat(file, &stat_buf) != -1) {
89 + date_buf = date_n_time(&stat_buf.st_mtime, &date_len);
90 + entry->swDate_len = date_len;
91 + memcpy(entry->swDate, date_buf, entry->swDate_len);
93 + /* somewhy some files include :arch in .list name */
94 + snprintf(file, sizeof(file), "%s/%s:%s.list", pkg_directory, package, arch);
95 + date_buf = date_n_time(&stat_buf.st_mtime, &date_len);
96 + entry->swDate_len = date_len;
97 + memcpy(entry->swDate, date_buf, entry->swDate_len);
99 + /* FIXME, or fallback to whatever nonsesnse was here before, or leave it uninitialied?
101 + entry->swDate_len = 8;
102 + memcpy(entry->swDate, "\0\0\1\1\0\0\0\0", 8);
107 DEBUGMSGTL(("swinst:load:arch"," loaded %d entries\n",