📄 libmetrics.c
字号:
return val;}g_val_tmem_shared_func ( void ){ char *p; g_val_t val; /* ** Broken since linux-2.5.52 when Memshared was removed !! */ p = strstr( update_file(&proc_meminfo), "MemShared:" ); if (p) { p = skip_token(p); val.uint32 = strtol( p, (char **)NULL, 10 ); } else { val.uint32 = 0; } return val;}g_val_tmem_buffers_func ( void ){ char *p; g_val_t val; p = strstr( update_file(&proc_meminfo), "Buffers:" ); if(p) { p = skip_token(p); val.uint32 = strtol( p, (char **)NULL, 10 ); } else { val.uint32 = 0; } return val;}g_val_tmem_cached_func ( void ){ char *p; g_val_t val; p = strstr( update_file(&proc_meminfo), "Cached:"); if(p) { p = skip_token(p); val.uint32 = strtol( p, (char **)NULL, 10 ); } else { val.uint32 = 0; } return val;}g_val_tswap_free_func ( void ){ char *p; g_val_t val; p = strstr( update_file(&proc_meminfo), "SwapFree:" ); if(p) { p = skip_token(p); val.uint32 = strtol( p, (char **)NULL, 10 ); } else { val.uint32 = 0; } return val;}/* --------------------------------------------------------------------------- */g_val_tmtu_func ( void ){ /* We want to find the minimum MTU (Max packet size) over all UP interfaces. */ g_val_t val; val.uint32 = get_min_mtu(); /* A val of 0 means there are no UP interfaces. Shouldn't happen. */ return val;}/*----------------------------------------------------------------------------*//*add by zyl 20060920*/g_val_t getmacaddr_func ( void ){ g_val_t val; struct ifreq ifr; int fd; char* pa; char* pa1; char s[3]; int c; int i; char mac[20]; bzero((void*)&ifr, (int)sizeof(ifr)); //init value strncpy(ifr.ifr_name, "eth0",4); fd=socket(AF_INET, SOCK_DGRAM,0 ); if( fd < 0) { perror("socket()"); return val; } if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) { perror("ioctl() get hardware address error:"); close(fd); return val; } else { char my[16]; strcpy(my,ifr.ifr_hwaddr.sa_data); printf("my:%s\n",my); pa1=pa=(char*) &ifr.ifr_hwaddr.sa_data; for(i=0; i<5; i++) { c=*pa; c&=0x000000ff; sprintf(s,"%02x",c); mac[i*3] = s[0]; mac[i*3+1] = s[1]; mac[i*3+2] = '.'; pa++; } c=*pa; c&=0x000000ff; sprintf(s,"%02x",c); mac[i*3] = s[0]; mac[i*3+1] = s[1]; mac[i*3+2] = 0; strcpy(val.str, mac); } close(fd); return val;}/* ===================================================== *//* Disk Usage. Using fsusage.c from the GNU fileutils-4.1.5 package. */#include <inttypes.h>/* Linux Specific, but we are in the Linux machine file. */#define MOUNTS "/proc/mounts"struct nlist { struct nlist *next; char *name;};#define DFHASHSIZE 101static struct nlist *DFhashvector[DFHASHSIZE];/* --------------------------------------------------------------------------- */unsigned int DFhash(const char *s){ unsigned int hashval; for (hashval=0; *s != '\0'; s++) hashval = *s + 31 * hashval; return hashval % DFHASHSIZE;}/* --------------------------------------------------------------------------- *//* From K&R C book, pp. 144-145 */struct nlist * seen_before(const char *name){ struct nlist *found=0, *np; unsigned int hashval; /* lookup */ hashval=DFhash(name); for (np=DFhashvector[hashval]; np; np=np->next) { if (!strcmp(name,np->name)) { found=np; break; } } if (!found) { /* not found */ np = (struct nlist *) malloc(sizeof(*np)); if (!np || !(np->name = (char *) strdup(name))) return NULL; np->next = DFhashvector[hashval]; DFhashvector[hashval] = np; return NULL; } else /* found name */ return found;}/* --------------------------------------------------------------------------- */void DFcleanup(){ struct nlist *np, *next; int i; for (i=0; i<DFHASHSIZE; i++) { /* Non-standard for loop. Note the last clause happens at the end of the loop. */ for (np = DFhashvector[i]; np; np=next) { next=np->next; free(np->name); free(np); } DFhashvector[i] = 0; }}/* --------------------------------------------------------------------------- */int remote_mount(const char *device, const char *type){ /* From ME_REMOTE macro in mountlist.h: A file system is `remote' if its Fs_name contains a `:' or if (it is of type smbfs and its Fs_name starts with `//'). */ return ((strchr(device,':') != 0) || (!strcmp(type, "smbfs") && device[0]=='/' && device[1]=='/') || (!strcmp(type, "autofs")) || (!strcmp(type,"gfs")) || (!strcmp(type,"none")) );}#if HAVE_INTTYPES_H# include <inttypes.h>#endif#include <sys/types.h>#include <sys/stat.h>#if HAVE_LIMITS_H# include <limits.h>#endif#ifndef CHAR_BIT# define CHAR_BIT 8#endifint statfs ();#if HAVE_SYS_PARAM_H# include <sys/param.h>#endif#if HAVE_SYS_MOUNT_H# include <sys/mount.h>#endif#if HAVE_SYS_VFS_H# include <sys/vfs.h>#endif#if HAVE_SYS_FS_S5PARAM_H /* Fujitsu UXP/V */# include <sys/fs/s5param.h>#endif#if defined (HAVE_SYS_FILSYS_H) && !defined (_CRAY)# include <sys/filsys.h> /* SVR2 */#endif//#if HAVE_FCNTL_H//# include <fcntl.h>//#endif#if HAVE_SYS_STATFS_H# include <sys/statfs.h>#endif#if HAVE_DUSTAT_H /* AIX PS/2 */# include <sys/dustat.h>#endif#if HAVE_SYS_STATVFS_H /* SVR4 */# include <sys/statvfs.h>int statvfs ();#endif/* Fill in the fields of FSP with information about space usage for the filesystem on which PATH resides. DISK is the device on which PATH is mounted, for space-getting methods that need to know it. Return 0 if successful, -1 if not. When returning -1, ensure that ERRNO is either a system error value, or zero if DISK is NULL on a system that requires a non-NULL value. */intget_fs_usage (const char *path, const char *disk, struct fs_usage *fsp){#ifdef STAT_STATFS3_OSF1 struct statfs fsd; if (statfs (path, &fsd, sizeof (struct statfs)) != 0) return -1; fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_fsize);#endif /* STAT_STATFS3_OSF1 */#ifdef STAT_STATFS2_FS_DATA /* Ultrix */ struct fs_data fsd; if (statfs (path, &fsd) != 1) return -1; fsp->fsu_blocksize = 1024; fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.fd_req.btot); fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.fd_req.bfree); fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.fd_req.bfreen); fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.fd_req.bfreen) != 0; fsp->fsu_files = PROPAGATE_ALL_ONES (fsd.fd_req.gtot); fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.fd_req.gfree);#endif /* STAT_STATFS2_FS_DATA */#ifdef STAT_READ_FILSYS /* SVR2 */# ifndef SUPERBOFF# define SUPERBOFF (SUPERB * 512)# endif struct filsys fsd; int fd; if (! disk) { errno = 0; return -1; } fd = open (disk, O_RDONLY); if (fd < 0) return -1; lseek (fd, (off_t) SUPERBOFF, 0); if (safe_read (fd, (char *) &fsd, sizeof fsd) != sizeof fsd) { close (fd); return -1; } close (fd); fsp->fsu_blocksize = (fsd.s_type == Fs2b ? 1024 : 512); fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.s_fsize); fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.s_tfree); fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.s_tfree); fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.s_tfree) != 0; fsp->fsu_files = (fsd.s_isize == -1 ? (uintmax_t) -1 : (fsd.s_isize - 2) * INOPB * (fsd.s_type == Fs2b ? 2 : 1)); fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.s_tinode);#endif /* STAT_READ_FILSYS */#ifdef STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX */ struct statfs fsd; if (statfs (path, &fsd) < 0) return -1; fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_bsize);# ifdef STATFS_TRUNCATES_BLOCK_COUNTS /* In SunOS 4.1.2, 4.1.3, and 4.1.3_U1, the block counts in the struct statfs are truncated to 2GB. These conditions detect that truncation, presumably without botching the 4.1.1 case, in which the values are not truncated. The correct counts are stored in undocumented spare fields. */ if (fsd.f_blocks == 0x7fffffff / fsd.f_bsize && fsd.f_spare[0] > 0) { fsd.f_blocks = fsd.f_spare[0]; fsd.f_bfree = fsd.f_spare[1]; fsd.f_bavail = fsd.f_spare[2]; }# endif /* STATFS_TRUNCATES_BLOCK_COUNTS */#endif /* STAT_STATFS2_BSIZE */#ifdef STAT_STATFS2_FSIZE /* 4.4BSD */ struct statfs fsd; if (statfs (path, &fsd) < 0) return -1; fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_fsize);#endif /* STAT_STATFS2_FSIZE */#ifdef STAT_STATFS4 /* SVR3, Dynix, Irix, AIX */# if !_AIX && !defined _SEQUENT_ && !defined DOLPHIN# define f_bavail f_bfree# endif struct statfs fsd; if (statfs (path, &fsd, sizeof fsd, 0) < 0) return -1; /* Empirically, the block counts on most SVR3 and SVR3-derived systems seem to always be in terms of 512-byte blocks, no matter what value f_bsize has. */# if _AIX || defined(_CRAY) fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_bsize);# else fsp->fsu_blocksize = 512;# endif#endif /* STAT_STATFS4 */#ifdef STAT_STATVFS /* SVR4 */ struct statvfs fsd; if (statvfs (path, &fsd) < 0) return -1; /* f_frsize isn't guaranteed to be supported. */ fsp->fsu_blocksize = PROPAGATE_ALL_ONES (fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize);#endif /* STAT_STATVFS */#if !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ_FILSYS) /* !Ultrix && !SVR2 *//* fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.f_blocks); fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.f_bfree); fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.f_bavail); fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.f_bavail) != 0; fsp->fsu_files = PROPAGATE_ALL_ONES (fsd.f_files); fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.f_ffree);*/#endif /* not STAT_STATFS2_FS_DATA && not STAT_READ_FILSYS */ return 0;}/* --------------------------------------------------------------------------- */float device_space(char *mount, char *device, double *total_size, double *total_free){ struct fs_usage fsu; uint32_t blocksize; uint32_t free; uint32_t size; /* The percent used: used/total * 100 */ float pct=0.0; /* Avoid multiply-mounted disks - not done in df. */ if (seen_before(device)) return pct; if (get_fs_usage(mount, device, &fsu)) { /* Ignore funky devices... */ return pct; } free = fsu.fsu_bavail; /* Is the space available negative? */ if (fsu.fsu_bavail_top_bit_set) free = 0; size = fsu.fsu_blocks; blocksize = fsu.fsu_blocksize; /* Keep running sum of total used, free local disk space. */ *total_size += size * (double) blocksize; *total_free += free * (double) blocksize; /* The percentage of space used on this partition. */ pct = size ? ((size - free) / (float) size) * 100 : 0.0; return pct;}/* --------------------------------------------------------------------------- */float find_disk_space(double *total_size, double *total_free){ FILE *mounts; char procline[256]; char mount[128], device[128], type[32]; /* We report in GB = 1e9 bytes. */ double reported_units = 1e9; /* Track the most full disk partition, report with a percentage. */ float thispct, max=0.0; int rc; /* Read all currently mounted filesystems. */ mounts=fopen(MOUNTS,"r"); if (!mounts) { debug_msg("Df Error: could not open mounts file %s. Are we on the right OS?\n", MOUNTS); return max; } while ( fgets(procline, sizeof(procline), mounts) ) { rc=sscanf(procline, "%s %s %s ", device, mount, type); if (!rc) continue; if (remote_mount(device, type)) continue; if (strncmp(device, "/dev/", 5) != 0 && strncmp(device, "/dev2/", 6) != 0) continue; thispct = device_space(mount, device, total_size, total_free); debug_msg("Counting device %s (%.2f %%)", device, thispct); if (!max || max<thispct) max = thispct; } fclose(mounts); *total_size = *total_size / reported_units; *total_free = *total_free / reported_units; debug_msg("For all disks: %.3f GB total, %.3f GB free for users.", *total_size, *total_free); DFcleanup(); return max;}/* --------------------------------------------------------------------------- */g_val_tdisk_free_func( void ){ double total_free=0.0; double total_size=0.0; g_val_t val; find_disk_space(&total_size, &total_free); val.d = total_free; return val;}/* --------------------------------------------------------------------------- */g_val_tdisk_total_func( void ){ double total_free=0.0; double total_size=0.0; g_val_t val; find_disk_space(&total_size, &total_free); val.d = total_size; return val;}/* --------------------------------------------------------------------------- */g_val_tpart_max_used_func( void ){ double total_free=0.0; double total_size=0.0; float most_full; g_val_t val; most_full = find_disk_space(&total_size, &total_free); val.f = most_full; return val;}/* ---------------------------------- *//* * Stub for doing stand-alone testing * * Compile with: * *gcc -g -O2 -D_REENTRANT -Wall -I.. -I../lib -I../lib/dnet -DHAVE_CONFIG_H \ -DSTAND_ALONE_TEST -o gmo.tst machine.c ../lib/.libs/libganglia.a \ ../lib/libgetopthelper.a -ldl -lresolv -lnsl -lpthread * */g_val_tmetrics_sens(unsigned int i){ g_val_t val; cl_sensors_env* env = cl_env_init(); sensors_dataitem* item = cl_get_item(env,i); if(item) val.d=(double)item->cur; cl_env_exit(env); return val;}#ifdef STAND_ALONE_TEST#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -