/** * Get system local date and time in a foramt suitable for DateAndTime TC: * field octets contents range * ----- ------ -------- ----- * 1 1-2 year* 0..65536 * 2 3 month 1..12 * 3 4 day 1..31 * 4 5 hour 0..23 * 5 6 minutes 0..59 * 6 7 seconds 0..60 * (use 60 for leap-second) * 7 8 deci-seconds 0..9 * 8 9 direction from UTC '+' / '-' * 9 10 hours from UTC* 0..13 * 10 11 minutes from UTC 0..59 * * * Notes: * - the value of year is in network-byte order * - daylight saving time in New Zealand is +13 * * For example, Tuesday May 26, 1992 at 1:30:15 PM EDT would be * displayed as: * * 1992-5-26,13:30:15.0,-4:0 * * Returns -1 in case of an error or the length of the string (8 or 11) * Actually returns always 11 on freebsd */ static int OS_getSystemDate(struct snmp_value *value) { u_char s_date_time[11]; struct tm tloc_tm; time_t tloc_time_t; struct timeval right_now; int string_len; if (gettimeofday(&right_now, NULL) < 0) { syslog(LOG_ERR, "gettimeofday failed: %m"); return (SNMP_ERR_GENERR); } tloc_time_t = right_now.tv_sec; if (localtime_r(&tloc_time_t, &tloc_tm) == NULL) { syslog(LOG_ERR, "localtime_r() failed: %m "); return (SNMP_ERR_GENERR); } string_len = make_date_time(s_date_time, &tloc_tm, right_now.tv_usec / 100000); return (string_get(value, s_date_time, string_len)); }
/** * Get the *running* O/S identification */ static void swins_get_OS_ident(void) { struct utsname os_id; char os_string[SW_NAME_MLEN] = ""; struct swins_entry *entry; u_char *boot; struct stat sb; struct tm k_ts; if (uname(&os_id) == -1) { syslog(LOG_WARNING, "%s: %m", __func__); return; } snprintf(os_string, sizeof(os_string), "%s: %s", os_id.sysname, os_id.version); if ((entry = swins_find_by_name(os_string)) != NULL || (entry = swins_entry_create(os_string)) == NULL) return; entry->flags |= (HR_SWINSTALLED_FOUND | HR_SWINSTALLED_IMMUTABLE); entry->id = &oid_zeroDotZero; entry->type = (int32_t)SWI_OPERATING_SYSTEM; memset(entry->date, 0, sizeof(entry->date)); if (OS_getSystemInitialLoadParameters(&boot) == SNMP_ERR_NOERROR && strlen(boot) > 0 && stat(boot, &sb) == 0 && localtime_r(&sb.st_ctime, &k_ts) != NULL) entry->date_len = make_date_time(entry->date, &k_ts, 0); }
/** * Read the installed packages */ static int swins_get_packages(void) { struct stat sb; DIR *p_dir; struct dirent *ent; struct tm k_ts; char *pkg_file; struct swins_entry *entry; int ret = 0; if (pkg_dir == NULL) /* initialisation may have failed */ return (-1); if (stat(pkg_dir, &sb) != 0) { syslog(LOG_ERR, "hrSWInstalledTable: stat(\"%s\") failed: %m", pkg_dir); return (-1); } if (!S_ISDIR(sb.st_mode)) { syslog(LOG_ERR, "hrSWInstalledTable: \"%s\" is not a directory", pkg_dir); return (-1); } if (sb.st_ctime <= os_pkg_last_change) { HRDBG("no need to rescan installed packages -- " "directory time-stamp unmodified"); TAILQ_FOREACH(entry, &swins_tbl, link) entry->flags |= HR_SWINSTALLED_FOUND; return (0); } if ((p_dir = opendir(pkg_dir)) == NULL) { syslog(LOG_ERR, "hrSWInstalledTable: opendir(\"%s\") failed: " "%m", pkg_dir); return (-1); } while (errno = 0, (ent = readdir(p_dir)) != NULL) { HRDBG(" pkg file: %s", ent->d_name); /* check that the contents file is a regular file */ if (asprintf(&pkg_file, "%s/%s/%s", pkg_dir, ent->d_name, CONTENTS_FNAME) == -1) continue; if (stat(pkg_file, &sb) != 0 ) { free(pkg_file); continue; } if (!S_ISREG(sb.st_mode)) { syslog(LOG_ERR, "hrSWInstalledTable: \"%s\" not a " "regular file -- skipped", pkg_file); free(pkg_file); continue; } free(pkg_file); /* read directory timestamp on package */ if (asprintf(&pkg_file, "%s/%s", pkg_dir, ent->d_name) == -1) continue; if (stat(pkg_file, &sb) == -1 || localtime_r(&sb.st_ctime, &k_ts) == NULL) { free(pkg_file); continue; } free(pkg_file); /* update or create entry */ if ((entry = swins_find_by_name(ent->d_name)) == NULL && (entry = swins_entry_create(ent->d_name)) == NULL) { ret = -1; goto PKG_LOOP_END; } entry->flags |= HR_SWINSTALLED_FOUND; entry->id = &oid_zeroDotZero; entry->type = (int32_t)SWI_APPLICATION; entry->date_len = make_date_time(entry->date, &k_ts, 0); } if (errno != 0) { syslog(LOG_ERR, "hrSWInstalledTable: readdir_r(\"%s\") failed:" " %m", pkg_dir); ret = -1; } else { /* * save the timestamp of directory * to avoid any further scanning */ os_pkg_last_change = sb.st_ctime; } PKG_LOOP_END: (void)closedir(p_dir); return (ret); }