⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hr_storage.c

📁 开发snmp的开发包有两个开放的SNMP开发库
💻 C
📖 第 1 页 / 共 2 页
字号:
        *   should use the first row.        */        if ( exact )            return NULL;        netsnmp_memory_load();        mem = netsnmp_memory_get_first( 0 );    }    else {        /*         * Otherwise, retrieve the requested         *  (or following) row as appropriate.         */        if ( exact && *length > HRSTORE_ENTRY_NAME_LENGTH+1 )            return NULL;   /* Too long for a valid instance */        idx = name[ HRSTORE_ENTRY_NAME_LENGTH ];        if ( idx < NETSNMP_MEM_TYPE_MAX ) {            netsnmp_memory_load();            mem = ( exact ? netsnmp_memory_get_byIdx( idx, 0 ) :                       netsnmp_memory_get_next_byIdx( idx, 0 ));        }    }    /*     * If this matched a memory-based entry, then     *    update the OID parameter(s) for GETNEXT requests.     */    if ( mem ) {        if ( !exact ) {            newname[ HRSTORE_ENTRY_NAME_LENGTH ] = mem->idx;            memcpy((char *) name, (char *) newname,                   ((int) vp->namelen + 1) * sizeof(oid));            *length = vp->namelen + 1;        }    }    /*     * If this didn't match a memory-based entry,     *   then consider the disk-based storage.     */    else {        Init_HR_Store();        for (;;) {            storage_idx = Get_Next_HR_Store();            DEBUGMSG(("host/hr_storage", "(index %d ....", storage_idx));            if (storage_idx == -1)                break;            newname[HRSTORE_ENTRY_NAME_LENGTH] = storage_idx;            DEBUGMSGOID(("host/hr_storage", newname, *length));            DEBUGMSG(("host/hr_storage", "\n"));            result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);            if (exact && (result == 0)) {                LowIndex = storage_idx;                /*                 * Save storage status information                  */                break;            }            if ((!exact && (result < 0)) &&                (LowIndex == -1 || storage_idx < LowIndex)) {                LowIndex = storage_idx;                /*                 * Save storage status information                  */#ifdef HRSTORE_MONOTONICALLY_INCREASING                break;#endif            }        }        if ( LowIndex != -1 ) {            if ( !exact ) {                newname[ HRSTORE_ENTRY_NAME_LENGTH ] = LowIndex;                memcpy((char *) name, (char *) newname,                       ((int) vp->namelen + 1) * sizeof(oid));                *length = vp->namelen + 1;            }            mem = (netsnmp_memory_info*)0xffffffff;   /* To indicate 'success' */        }    }    *write_method = 0;    *var_len = sizeof(long);    /* default to 'long' results */    /*     *  ... and return the appropriate row     */    DEBUGMSGTL(("host/hr_storage", "var_hrstoreEntry: process "));    DEBUGMSGOID(("host/hr_storage", name, *length));    DEBUGMSG(("host/hr_storage", " (%x)\n", mem));    return (void*)mem;}oid             storage_type_id[] = { 1, 3, 6, 1, 2, 1, 25, 2, 1, 1 };  /* hrStorageOther */int             storage_type_len =    sizeof(storage_type_id) / sizeof(storage_type_id[0]);        /*********************	 *	 *  System specific implementation functions	 *	 *********************/static const char *hrs_descr[] = {    NULL,    "Memory Buffers",           /* HRS_TYPE_MBUF */    "Real Memory",              /* HRS_TYPE_MEM */    "Swap Space"                /* HRS_TYPE_SWAP */};u_char         *var_hrstore(struct variable *vp,            oid * name,            size_t * length,            int exact, size_t * var_len, WriteMethod ** write_method){    int             store_idx = 0;    static char     string[1024];    struct HRFS_statfs stat_buf;    void                *ptr;    netsnmp_memory_info *mem = NULL;    if (vp->magic == HRSTORE_MEMSIZE) {        if (header_hrstore(vp, name, length, exact, var_len, write_method)            == MATCH_FAILED)            return NULL;    } else {really_try_next:	ptr = header_hrstoreEntry(vp, name, length, exact, var_len,					write_method);	if (ptr == NULL)	    return NULL;        store_idx = name[ HRSTORE_ENTRY_NAME_LENGTH ];        if (HRFS_entry &&            netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,                                   NETSNMP_DS_AGENT_SKIPNFSINHOSTRESOURCES) &&            Check_HR_FileSys_NFS())            return NULL;        if (store_idx > NETSNMP_MEM_TYPE_MAX ) {	    if (HRFS_statfs(HRFS_entry->HRFS_mount, &stat_buf) < 0) {		snmp_log_perror(HRFS_entry->HRFS_mount);		goto try_next;	    }	} else {	    mem = (netsnmp_memory_info*)ptr;        }    }    switch (vp->magic) {    case HRSTORE_MEMSIZE:        netsnmp_memory_load();        mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_PHYSMEM, 0 );        if ( !mem || mem->size == -1 || mem->units == -1 )	    return NULL;	long_return = mem->size * (mem->units / 1024);        return (u_char *) & long_return;    case HRSTORE_INDEX:        long_return = store_idx;        return (u_char *) & long_return;    case HRSTORE_TYPE:        if (store_idx > NETSNMP_MEM_TYPE_MAX)            if (storageUseNFS && Check_HR_FileSys_NFS())                storage_type_id[storage_type_len - 1] = 10;     /* Network Disk */#if HAVE_HASMNTOPT            else if (hasmntopt(HRFS_entry, "loop") != NULL)                storage_type_id[storage_type_len - 1] = 5;      /* Removable Disk */#endif            else                storage_type_id[storage_type_len - 1] = 4;      /* Assume fixed */        else            switch (store_idx) {            case NETSNMP_MEM_TYPE_PHYSMEM:            case NETSNMP_MEM_TYPE_USERMEM:                storage_type_id[storage_type_len - 1] = 2;      /* RAM */                break;            case NETSNMP_MEM_TYPE_VIRTMEM:            case NETSNMP_MEM_TYPE_SWAP:                storage_type_id[storage_type_len - 1] = 3;      /* Virtual Mem */                break;            default:                storage_type_id[storage_type_len - 1] = 1;      /* Other */                break;            }        *var_len = sizeof(storage_type_id);        return (u_char *) storage_type_id;    case HRSTORE_DESCR:        if (store_idx > NETSNMP_MEM_TYPE_MAX) {            strncpy(string, HRFS_entry->HRFS_mount, sizeof(string)-1);            string[ sizeof(string)-1 ] = 0;            *var_len = strlen(string);            return (u_char *) string;        } else {            if ( !mem || !mem->descr )                goto try_next;            *var_len = strlen(mem->descr);            return (u_char *) mem->descr;        }    case HRSTORE_UNITS:        if (store_idx > NETSNMP_MEM_TYPE_MAX)#if HRFS_HAS_FRSIZE            long_return = stat_buf.f_frsize;#else            long_return = stat_buf.f_bsize;#endif        else {            if ( !mem || mem->units == -1 )                goto try_next;            long_return = mem->units;        }        return (u_char *) & long_return;    case HRSTORE_SIZE:        if (store_idx > NETSNMP_MEM_TYPE_MAX)            long_return = stat_buf.f_blocks;        else {            if ( !mem || mem->size == -1 )                goto try_next;            long_return = mem->size;        }        return (u_char *) & long_return;    case HRSTORE_USED:        if (store_idx > NETSNMP_MEM_TYPE_MAX)            long_return = (stat_buf.f_blocks - stat_buf.f_bfree);        else {            if ( !mem || mem->size == -1 || mem->free == -1 )                goto try_next;            long_return = mem->size - mem->free;        }        return (u_char *) & long_return;    case HRSTORE_FAILS:        if (store_idx > NETSNMP_MEM_TYPE_MAX)#if NETSNMP_NO_DUMMY_VALUES	    goto try_next;#else            long_return = 0;#endif        else {            if ( !mem || mem->other == -1 )                goto try_next;            long_return = mem->other;        }        return (u_char *) & long_return;    default:        DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrstore\n",                    vp->magic));    }    return NULL;  try_next:    if (!exact)        goto really_try_next;    return NULL;}        /*********************	 *	 *  Internal implementation functions	 *	 *********************/static int      FS_storage;static int      HRS_index;voidInit_HR_Store(void){    HRS_index = 0;    Init_HR_FileSys();}intGet_Next_HR_Store(void){    /*     * File-based storage      */	for (;;) {    	HRS_index = Get_Next_HR_FileSys();		if (HRS_index >= 0) {			if (!(netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 							NETSNMP_DS_AGENT_SKIPNFSINHOSTRESOURCES) && 						Check_HR_FileSys_NFS())) {				return HRS_index + NETSNMP_MEM_TYPE_MAX;				}		} else {			return -1;		}		}}#ifdef solaris2voidsol_get_swapinfo(int *totalP, int *usedP){    struct anoninfo ainfo;    if (swapctl(SC_AINFO, &ainfo) < 0) {        *totalP = *usedP = 0;        return;    }    *totalP = ainfo.ani_max;    *usedP = ainfo.ani_resv;}#endif                          /* solaris2 */#ifdef WIN32char *win_realpath(const char *file_name, char *resolved_name){	char szFile[_MAX_PATH + 1];	char *pszRet; 		pszRet = _fullpath(szFile, resolved_name, MAX_PATH); 		return pszRet;  }static int win_statfs (const char *path, struct win_statfs *buf){    HINSTANCE h;    FARPROC f;    int retval = 0;    char tmp [MAX_PATH], resolved_path [MAX_PATH];    GetFullPathName(path, MAX_PATH, resolved_path, NULL);    /* TODO - Fix this! The realpath macro needs defined     * or rewritten into the function.     */        win_realpath(path, resolved_path);        if (!resolved_path)    	retval = - 1;    else {    	/* check whether GetDiskFreeSpaceExA is supported */        h = LoadLibraryA ("kernel32.dll");        if (h)			f = GetProcAddress (h, "GetDiskFreeSpaceExA");        else        	f = NULL;		        if (f) {			ULARGE_INTEGER bytes_free, bytes_total, bytes_free2;            if (!f (resolved_path, &bytes_free2, &bytes_total, &bytes_free)) {				errno = ENOENT;				retval = - 1;			} else {				buf -> f_bsize = FAKED_BLOCK_SIZE;				buf -> f_bfree = (bytes_free.QuadPart) / FAKED_BLOCK_SIZE;				buf -> f_files = buf -> f_blocks = (bytes_total.QuadPart) / FAKED_BLOCK_SIZE;				buf -> f_ffree = buf -> f_bavail = (bytes_free2.QuadPart) / FAKED_BLOCK_SIZE;			}		} else {			DWORD sectors_per_cluster, bytes_per_sector;			if (h) FreeLibrary (h);			if (!GetDiskFreeSpaceA (resolved_path, &sectors_per_cluster,					&bytes_per_sector, &buf -> f_bavail, &buf -> f_blocks)) {                errno = ENOENT;                retval = - 1;            } else {                buf -> f_bsize = sectors_per_cluster * bytes_per_sector;                buf -> f_files = buf -> f_blocks;                buf -> f_ffree = buf -> f_bavail;                buf -> f_bfree = buf -> f_bavail;            }		}		if (h) FreeLibrary (h);	}	/* get the FS volume information */	if (strspn (":", resolved_path) > 0) resolved_path [3] = '\0'; /* we want only the root */    	if (GetVolumeInformation (resolved_path, NULL, 0, &buf -> f_fsid, &buf -> f_namelen, 									NULL, tmp, MAX_PATH)) {		if (strcasecmp ("NTFS", tmp) == 0) {			buf -> f_type = NTFS_SUPER_MAGIC;		} else {			buf -> f_type = MSDOS_SUPER_MAGIC;		}	} else {		errno = ENOENT;		retval = - 1;	}	return retval;}#endif	/* WIN32 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -