📄 hr_swinst.c
字号:
/* * Host Resources MIB - Installed Software group implementation - hr_swinst.c * */#include <net-snmp/net-snmp-config.h>#if HAVE_SYS_PARAM_H#include <sys/param.h>#endif#include <sys/stat.h>#if TIME_WITH_SYS_TIME# include <sys/time.h># include <time.h>#else# if HAVE_SYS_TIME_H# include <sys/time.h># else# include <time.h># endif#endif#if HAVE_DIRENT_H#include <dirent.h>#else# define dirent direct# if HAVE_SYS_NDIR_H# include <sys/ndir.h># endif# if HAVE_SYS_DIR_H# include <sys/dir.h># endif# if HAVE_NDIR_H# include <ndir.h># endif#endif#ifdef HAVE_PKGLOCS_H#include <pkglocs.h>#endif#ifdef HAVE_PKGINFO_H#include <pkginfo.h>#endif#ifdef HAVE_LIBRPM#include <rpm/rpmlib.h>#include <rpm/header.h>#include <fcntl.h>#ifdef HAVE_RPMGETPATH#include <rpm/rpmmacro.h>#endif#ifdef HAVE_RPM_RPMDB_H#include <rpm/rpmdb.h>#endif#endif#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#include "host_res.h"#include "hr_swinst.h"#include <net-snmp/utilities.h>#define HRSWINST_MONOTONICALLY_INCREASING /********************* * * Kernel & interface information, * and internal forward declarations * *********************//* * Reorganize the global data into a single static structure. * * Old New *====================================================== * HRSW_directory swi->swi_directory * HRSW_name[100] swi->swi_name[SNMP_MAXPATH] * HRSW_index swi->swi_index * * swi->swi_dbpath (RPM only) * swi->swi_maxrec (RPM only) * swi->swi_nrec (RPM only) * swi->swi_recs (RPM only) * rpm_db swi->swi_rpmdb (RPM only) * swi->swi_h (RPM only) * swi->swi_prevx (RPM only) * * dp swi->swi_dp * de_p swi->swi_dep */typedef struct {#if HAVE_LIBRPM char *swi_directory;#else const char *swi_directory;#endif char swi_name[SNMP_MAXPATH]; /* XXX longest file name */ int swi_index;#if HAVE_LIBRPM const char *swi_dbpath; time_t swi_timestamp; /* modify time on database */ int swi_maxrec; /* no. of allocations */ int swi_nrec; /* no. of valid offsets */ int *swi_recs; /* db record offsets */ rpmdb swi_rpmdb; Header swi_h; int swi_prevx;#else DIR *swi_dp; struct dirent *swi_dep;#endif} SWI_t;static SWI_t _myswi = { NULL, "", 0 }; /* XXX static for now */int header_hrswinst(struct variable *, oid *, size_t *, int, size_t *, WriteMethod **);int header_hrswInstEntry(struct variable *, oid *, size_t *, int, size_t *, WriteMethod **);extern struct timeval starttime; /********************* * * Initialisation & common implementation functions * *********************/extern void Init_HR_SWInst(void);extern int Get_Next_HR_SWInst(void);extern void End_HR_SWInst(void);extern void Save_HR_SW_info(int ix);#ifdef HAVE_LIBRPMstatic void Mark_HRSW_token(void);static void Release_HRSW_token(void);#else#define Mark_HRSW_token()#define Release_HRSW_token()#endif#define HRSWINST_CHANGE 1#define HRSWINST_UPDATE 2#define HRSWINST_INDEX 3#define HRSWINST_NAME 4#define HRSWINST_ID 5#define HRSWINST_TYPE 6#define HRSWINST_DATE 7struct variable4 hrswinst_variables[] = { {HRSWINST_CHANGE, ASN_TIMETICKS, RONLY, var_hrswinst, 1, {1}}, {HRSWINST_UPDATE, ASN_TIMETICKS, RONLY, var_hrswinst, 1, {2}}, {HRSWINST_INDEX, ASN_INTEGER, RONLY, var_hrswinst, 3, {3, 1, 1}}, {HRSWINST_NAME, ASN_OCTET_STR, RONLY, var_hrswinst, 3, {3, 1, 2}}, {HRSWINST_ID, ASN_OBJECT_ID, RONLY, var_hrswinst, 3, {3, 1, 3}}, {HRSWINST_TYPE, ASN_INTEGER, RONLY, var_hrswinst, 3, {3, 1, 4}}, {HRSWINST_DATE, ASN_OCTET_STR, RONLY, var_hrswinst, 3, {3, 1, 5}}};oid hrswinst_variables_oid[] = { 1, 3, 6, 1, 2, 1, 25, 6 };#ifdef PKGLOC /* Description from HRSW_dir/.../pkginfo: DESC= */#define _PATH_HRSW_directory PKGLOC#endif#ifdef hpux9 /* Description from HRSW_dir/.../index: fd: */#define _PATH_HRSW_directory "/system"#endif#ifdef hpux10 /* Description from HRSW_dir/.../pfiles/INDEX: title */#define _PATH_HRSW_directory "/var/adm/sw/products"#endif#ifdef hpux11 /* Description from HRSW_dir/.../pfiles/INDEX: title */#define _PATH_HRSW_directory "/var/adm/sw/products"#endif#ifdef freebsd2#define _PATH_HRSW_directory "/var/db/pkg"#endifvoidinit_hr_swinst(void){#if defined(HAVE_LIBRPM) || defined(_PATH_HRSW_directory) SWI_t *swi = &_myswi; /* XXX static for now */#endif#ifdef HAVE_LIBRPM struct stat stat_buf;#endif /* * Read settings from config file, * or take system-specific defaults */#ifdef HAVE_LIBRPM if (swi->swi_directory == NULL) { char path[SNMP_MAXPATH]; /* * XXX distinguish between rpm-2.5.x and rpm-2.9x */#ifdef HAVE_RPMGETPATH rpmReadConfigFiles(NULL, NULL); swi->swi_dbpath = rpmGetPath("%{_dbpath}", NULL);#else rpmReadConfigFiles(NULL, NULL, NULL, 0); swi->swi_dbpath = rpmGetVar(RPMVAR_DBPATH);#endif if (swi->swi_directory != NULL) free(swi->swi_directory); snprintf(path, sizeof(path), "%s/Packages", swi->swi_dbpath); if (stat(path, &stat_buf) == -1) snprintf(path, sizeof(path), "%s/packages.rpm", swi->swi_dbpath); path[ sizeof(path)-1 ] = 0; swi->swi_directory = strdup(path); }#else# ifdef _PATH_HRSW_directory if (swi->swi_directory == NULL) { swi->swi_directory = _PATH_HRSW_directory; } strcpy(swi->swi_name, "[installed name]"); /* default name */# else /* * XXX SunOS4 package directory is ?? -MJS */ return; /* packages not known - don't register */# endif#endif REGISTER_MIB("host/hr_swinst", hrswinst_variables, variable4, hrswinst_variables_oid);}/* * header_hrswinst(... * Arguments: * vp IN - pointer to variable entry that points here * name IN/OUT - IN/name requested, OUT/name found * length IN/OUT - length of IN/OUT oid's * exact IN - TRUE if an exact match was requested * var_len OUT - length of variable or 0 if function returned * write_method */intheader_hrswinst(struct variable *vp, oid * name, size_t * length, int exact, size_t * var_len, WriteMethod ** write_method){#define HRSWINST_NAME_LENGTH 9 oid newname[MAX_OID_LEN]; int result; DEBUGMSGTL(("host/hr_swinst", "var_hrswinst: ")); DEBUGMSGOID(("host/hr_swinst", name, *length)); DEBUGMSG(("host/hr_swinst", " %d\n", exact)); memcpy((char *) newname, (char *) vp->name, vp->namelen * sizeof(oid)); newname[HRSWINST_NAME_LENGTH] = 0; result = snmp_oid_compare(name, *length, newname, vp->namelen + 1); if ((exact && (result != 0)) || (!exact && (result >= 0))) return (MATCH_FAILED); memcpy((char *) name, (char *) newname, (vp->namelen + 1) * sizeof(oid)); *length = vp->namelen + 1; *write_method = 0; *var_len = sizeof(long); /* default to 'long' results */ return (MATCH_SUCCEEDED);}intheader_hrswInstEntry(struct variable *vp, oid * name, size_t * length, int exact, size_t * var_len, WriteMethod ** write_method){#define HRSWINST_ENTRY_NAME_LENGTH 11 oid newname[MAX_OID_LEN]; int swinst_idx, LowIndex = -1; int result; DEBUGMSGTL(("host/hr_swinst", "var_hrswinstEntry: ")); DEBUGMSGOID(("host/hr_swinst", name, *length)); DEBUGMSG(("host/hr_swinst", " %d\n", exact)); memcpy((char *) newname, (char *) vp->name, vp->namelen * sizeof(oid)); /* * Find "next" installed software entry */ Init_HR_SWInst(); while ((swinst_idx = Get_Next_HR_SWInst()) != -1) { DEBUGMSG(("host/hr_swinst", "(index %d ....", swinst_idx)); newname[HRSWINST_ENTRY_NAME_LENGTH] = swinst_idx; DEBUGMSGOID(("host/hr_swinst", newname, *length)); DEBUGMSG(("host/hr_swinst", "\n")); result = snmp_oid_compare(name, *length, newname, vp->namelen + 1); if (exact && (result == 0)) { LowIndex = swinst_idx; Save_HR_SW_info(LowIndex); break; } if ((!exact && (result < 0)) && (LowIndex == -1 || swinst_idx < LowIndex)) { LowIndex = swinst_idx; Save_HR_SW_info(LowIndex);#ifdef HRSWINST_MONOTONICALLY_INCREASING break;#endif } } Mark_HRSW_token(); End_HR_SWInst(); if (LowIndex == -1) { DEBUGMSGTL(("host/hr_swinst", "... index out of range\n")); return (MATCH_FAILED); } memcpy((char *) name, (char *) newname, (vp->namelen + 1) * sizeof(oid)); *length = vp->namelen + 1; *write_method = 0; *var_len = sizeof(long); /* default to 'long' results */ DEBUGMSGTL(("host/hr_inst", "... get installed S/W stats ")); DEBUGMSGOID(("host/hr_inst", name, *length)); DEBUGMSG(("host/hr_inst", "\n")); return LowIndex;} /********************* * * System specific implementation functions
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -