static int SYSTEM_SWAP_FREE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { #ifdef HAVE_SYSINFO_FREESWAP struct sysinfo info; if( 0 == sysinfo(&info)) { #ifdef HAVE_SYSINFO_MEM_UNIT SET_UI64_RESULT(result, (zbx_uint64_t)info.freeswap * (zbx_uint64_t)info.mem_unit); #else SET_UI64_RESULT(result, info.freeswap); #endif return SYSINFO_RET_OK; } else return SYSINFO_RET_FAIL; /* Solaris */ #else #ifdef HAVE_SYS_SWAP_SWAPTABLE double swaptotal,swapfree; get_swapinfo(&swaptotal,&swapfree); SET_UI64_RESULT(result, swapfree); return SYSINFO_RET_OK; #else return SYSINFO_RET_FAIL; #endif #endif }
static int SYSTEM_SWAP_USED(AGENT_RESULT *result) { #ifdef HAVE_SYSINFO_FREESWAP struct sysinfo info; if (0 == sysinfo(&info)) { #ifdef HAVE_SYSINFO_MEM_UNIT SET_UI64_RESULT(result, ((zbx_uint64_t)info.totalswap - (zbx_uint64_t)info.freeswap) * (zbx_uint64_t)info.mem_unit); #else SET_UI64_RESULT(result, info.totalswap - info.freeswap); #endif return SYSINFO_RET_OK; } else return SYSINFO_RET_FAIL; /* Solaris */ #else #ifdef HAVE_SYS_SWAP_SWAPTABLE double swaptotal, swapfree; get_swapinfo(&swaptotal, &swapfree); SET_UI64_RESULT(result, swaptotal - swapfree); return SYSINFO_RET_OK; #else return SYSINFO_RET_FAIL; #endif #endif }
static int SYSTEM_SWAP_FREE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { zbx_uint64_t total, used; if (SUCCEED != get_swapinfo(&total, &used)) return SYSINFO_RET_FAIL; SET_UI64_RESULT(result, total - used); return SYSINFO_RET_OK; }
static int SYSTEM_SWAP_PFREE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { zbx_uint64_t total, used; if (SUCCEED != get_swapinfo(&total, &used)) return SYSINFO_RET_FAIL; if (0 == total) return SYSINFO_RET_FAIL; SET_DBL_RESULT(result, 100.0 * (double)(total - used) / (double)total); return SYSINFO_RET_OK; }
static int SYSTEM_SWAP_FREE(AGENT_REQUEST *request, AGENT_RESULT *result) { zbx_uint64_t total, free1; char *error; if (SUCCEED != get_swapinfo(&total, &free1, &error)) { SET_MSG_RESULT(result, error); return SYSINFO_RET_FAIL; } SET_UI64_RESULT(result, free1); return SYSINFO_RET_OK; }
/* * check whether device is swapped on */ static int meta_check_swapped( mdsetname_t *sp, mdname_t *np, md_error_t *ep ) { struct swaptable *swtp; int nswap; int i; int rval = 0; /* should have a set */ assert(sp != NULL); /* get swap info */ if (get_swapinfo(&swtp, &nswap, ep) != 0) return (-1); /* look for match */ for (i = 0; ((i < nswap) && (rval == 0)); ++i) { mdname_t *snp; if ((snp = metaname(&sp, swtp->swt_ent[i].ste_path, UNKNOWN, ep)) == NULL) { mdclrerror(ep); continue; } if (np->dev == snp->dev) { rval = mddeverror(ep, MDE_IS_SWAPPED, np->dev, np->cname); } else { /* not swap - does it overlap */ rval = meta_check_overlap(snp->cname, np, 0, -1, snp, 0, -1, ep); if (rval != 0) { (void) mdoverlaperror(ep, MDE_OVERLAP_SWAP, np->cname, NULL, snp->cname); } } } free_swapinfo(swtp); /* return success */ return (rval); }
static error_t rootdir_gc_meminfo (void *hook, char **contents, ssize_t *contents_len) { host_basic_info_data_t hbi; mach_msg_type_number_t cnt; struct vm_statistics vmstats; default_pager_info_t swap; error_t err; err = vm_statistics (mach_task_self (), &vmstats); if (err) return EIO; cnt = HOST_BASIC_INFO_COUNT; err = host_info (mach_host_self (), HOST_BASIC_INFO, (host_info_t) &hbi, &cnt); if (err) return err; err = get_swapinfo (&swap); if (err) return err; assert (cnt == HOST_BASIC_INFO_COUNT); *contents_len = asprintf (contents, "MemTotal: %14lu kB\n" "MemFree: %14lu kB\n" "Active: %14lu kB\n" "Inactive: %14lu kB\n" "Mlocked: %14lu kB\n" "SwapTotal:%14lu kB\n" "SwapFree: %14lu kB\n" , /* TODO: check that these are really 1024-bytes kBs. */ (long unsigned) hbi.memory_size / 1024, (long unsigned) vmstats.free_count * PAGE_SIZE / 1024, (long unsigned) vmstats.active_count * PAGE_SIZE / 1024, (long unsigned) vmstats.inactive_count * PAGE_SIZE / 1024, (long unsigned) vmstats.wire_count * PAGE_SIZE / 1024, (long unsigned) swap.dpi_total_space / 1024, (long unsigned) swap.dpi_free_space / 1024); return 0; }
static int SYSTEM_SWAP_PFREE(AGENT_REQUEST *request, AGENT_RESULT *result) { zbx_uint64_t total, free1; char *error; if (SUCCEED != get_swapinfo(&total, &free1, &error)) { SET_MSG_RESULT(result, error); return SYSINFO_RET_FAIL; } if (0 == total) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot calculate percentage because total is zero.")); return SYSINFO_RET_FAIL; } SET_DBL_RESULT(result, 100.0 * (double)free1 / (double)total); return SYSINFO_RET_OK; }
/* * Is a driver currently swapped on? */ int meta_check_driveswapped( mdsetname_t *sp, mddrivename_t *dnp, md_error_t *ep ) { struct swaptable *swtp; int nswap; int i; int rval = 0; /* should have a set */ assert(sp != NULL); /* get swap info */ if (get_swapinfo(&swtp, &nswap, ep) != 0) return (-1); /* look for match */ for (i = 0; (i < nswap); ++i) { mdname_t *snp; if ((snp = metaname(&sp, swtp->swt_ent[i].ste_path, LOGICAL_DEVICE, ep)) == NULL) { mdclrerror(ep); continue; } if (strcmp(dnp->cname, snp->drivenamep->cname) == 0) { rval = mddeverror(ep, MDE_IS_SWAPPED, NODEV64, dnp->cname); } } free_swapinfo(swtp); /* return success */ return (rval); }
/* * Load the latest memory usage statistics */ int netsnmp_mem_arch_load( netsnmp_cache *cache, void *magic ) { struct pst_static pst; struct pst_dynamic psd; netsnmp_memory_info *mem; long total_swap = 0; long free_swap = 0; long size = 0; /* * Retrieve the memory information from the underlying O/S... */ if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) { snmp_log(LOG_ERR, "memory_hpux: pstat_getstatic failed!\n"); return -1; } if (pstat_getdynamic(&psd, sizeof(psd), (size_t) 1, 0) == -1) { snmp_log(LOG_ERR, "memory_hpux: pstat_getdynamic failed!\n"); return -1; } mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_PHYSMEM, 1 ); if (!mem) { snmp_log_perror("No Memory info entry"); } else { if (!mem->descr) mem->descr = strdup( "Physical memory" ); mem->units = pst.page_size; mem->size = pst.physical_memory; mem->free = psd.psd_free; mem->other = -1; } get_swapinfo(&total_swap, &free_swap, &size); mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SWAP, 1 ); if (!mem) { snmp_log_perror("No Swap info entry"); } else { if (!mem->descr) mem->descr = strdup( "Swap space (total)" ); mem->units = size; mem->size = total_swap; mem->free = free_swap; mem->other = -1; } mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_STEXT, 1 ); if (!mem) { snmp_log_perror("No Swap text entry"); } else { if (!mem->descr) mem->descr = strdup( "Swapped text pages" ); mem->units = pst.page_size; mem->size = psd.psd_vmtxt; mem->free = psd.psd_avmtxt; mem->other = -1; } mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_RTEXT, 1 ); if (!mem) { snmp_log_perror("No real text entry"); } else { if (!mem->descr) mem->descr = strdup( "Real text pages" ); mem->units = pst.page_size; mem->size = psd.psd_rmtxt; mem->free = psd.psd_armtxt; mem->other = -1; } /* mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_MISC, 1 ); if (!mem) { snmp_log_perror("No Buffer, etc info entry"); } else { mem->units = 1024; mem->size = -1; mem->free = (pst.page_size/1024)*psd.psd_free + swap.free_swap; mem->other = -1; } */ return 0; }
/** システム全体の情報を取得する */ static jboolean getsysinfo(struct system_info* info) { int pagesize = sysconf(_SC_PAGESIZE); if (info == NULL) { return JNI_FALSE; } /* get memory usage */ { kstat_t *kstat_syspage; kstat_named_t *kname_freemem; kstat_named_t *kname_physmem; int result_syspage; kstat_syspage = kstat_lookup(g_kstat_ctl, "unix", 0, "system_pages"); if (kstat_syspage == NULL) { return JNI_FALSE; } result_syspage = kstat_read(g_kstat_ctl, kstat_syspage, 0); if (result_syspage == -1) { return JNI_FALSE; } kname_freemem = kstat_data_lookup(kstat_syspage, "freemem"); if (kname_freemem != NULL) { info->mem_free = kname_freemem->value.ul; } kname_physmem = kstat_data_lookup(kstat_syspage, "physmem"); if (kname_physmem != NULL) { info->mem_total = kname_physmem->value.ul; } info->mem_free *= pagesize; info->mem_total *= pagesize; } /* get swap usage */ { uint64_t swap_avail; uint64_t swap_free; uint64_t updates; get_swapinfo(&swap_avail, &swap_free, &updates); info->swap_avail = swap_avail * pagesize; info->swap_free = swap_free * pagesize; info->updates = updates; } /* cpu usage */ /* page-in page-out */ { jlong cpu_user = 0; jlong cpu_sys = 0; jlong cpu_total = 0; jlong page_out = 0; jlong page_in = 0; kstat_t *kstat_cpu; for (kstat_cpu = g_kstat_ctl->kc_chain; kstat_cpu != NULL; kstat_cpu = kstat_cpu->ks_next) { int isCPUSTAT = strncmp(kstat_cpu->ks_name, CPUSTAT, sizeof(CPUSTAT) / sizeof(CPUSTAT[0]) - 1); if (isCPUSTAT == 0) { cpu_stat_t cpu_stat; kstat_read(g_kstat_ctl, kstat_cpu, &cpu_stat); cpu_user += cpu_stat.cpu_sysinfo.cpu[CPU_USER]; cpu_sys += cpu_stat.cpu_sysinfo.cpu[CPU_KERNEL]; cpu_total += cpu_stat.cpu_sysinfo.cpu[CPU_IDLE]; cpu_total += cpu_stat.cpu_sysinfo.cpu[CPU_KERNEL]; cpu_total += cpu_stat.cpu_sysinfo.cpu[CPU_USER]; cpu_total += cpu_stat.cpu_sysinfo.cpu[CPU_WAIT]; cpu_total += cpu_stat.cpu_sysinfo.wait[W_IO]; cpu_total += cpu_stat.cpu_sysinfo.wait[W_SWAP]; cpu_total += cpu_stat.cpu_sysinfo.wait[W_PIO]; page_out += cpu_stat.cpu_vminfo.pgpgout; page_in += cpu_stat.cpu_vminfo.pgpgin; } } info->cpu_sys = TICK2NSEC(cpu_sys) / g_num_cpus; info->cpu_user = TICK2NSEC(cpu_user) / g_num_cpus; info->cpu_total = TICK2NSEC(cpu_total) / g_num_cpus; info->page_in = page_in * pagesize; info->page_out = page_out * pagesize; } }
static u_char * var_extensible_mem(struct variable *vp, oid * name, size_t * length, int exact, size_t * var_len, WriteMethod ** write_method) { struct swapinfo swap; struct pst_static pst; struct pst_dynamic psd; static long long_ret; /* * Initialize the return value to 0 */ long_ret = 0; swap.total_swap = 0; swap.free_swap = 0; if (header_generic(vp, name, length, exact, var_len, write_method)) return (NULL); switch (vp->magic) { case MIBINDEX: long_ret = 0; return ((u_char *) (&long_ret)); case ERRORNAME: /* dummy name */ sprintf(errmsg, "swap"); *var_len = strlen(errmsg); return ((u_char *) (errmsg)); case MEMTOTALSWAP: get_swapinfo(&swap); long_ret = swap.total_swap; return ((u_char *) (&long_ret)); case MEMAVAILSWAP: get_swapinfo(&swap); long_ret = swap.free_swap; return ((u_char *) (&long_ret)); case MEMSWAPMINIMUM: long_ret = minimumswap; return ((u_char *) (&long_ret)); case MEMTOTALREAL: /* * Retrieve the static memory statistics */ if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) { snmp_log(LOG_ERR, "memory_hpux: pstat_getstatic failed!\n"); return (NULL); } long_ret = pst.page_size / 1024 * pst.physical_memory; return ((u_char *) (&long_ret)); case MEMAVAILREAL: /* * Retrieve the static memory statistics */ if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) { snmp_log(LOG_ERR, "memory_hpux: pstat_getstatic failed!\n"); return (NULL); } /* * Retrieve the dynamic memory statistics */ if (pstat_getdynamic(&psd, sizeof(psd), (size_t) 1, 0) == -1) { snmp_log(LOG_ERR, "memory_hpux: pstat_getdynamic failed!\n"); return (NULL); } long_ret = pst.page_size / 1024 * psd.psd_free; return ((u_char *) (&long_ret)); case MEMTOTALSWAPTXT: /* * Retrieve the static memory statistics */ if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) { snmp_log(LOG_ERR, "memory_hpux: pstat_getstatic failed!\n"); return (NULL); } /* * Retrieve the dynamic memory statistics */ if (pstat_getdynamic(&psd, sizeof(psd), (size_t) 1, 0) == -1) { snmp_log(LOG_ERR, "memory_hpux: pstat_getdynamic failed!\n"); return (NULL); } long_ret = pst.page_size / 1024 * psd.psd_vmtxt; return ((u_char *) (&long_ret)); case MEMUSEDSWAPTXT: /* * Retrieve the static memory statistics */ if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) { snmp_log(LOG_ERR, "memory_hpux: pstat_getstatic failed!\n"); return (NULL); } /* * Retrieve the dynamic memory statistics */ if (pstat_getdynamic(&psd, sizeof(psd), (size_t) 1, 0) == -1) { snmp_log(LOG_ERR, "memory_hpux: pstat_getdynamic failed!\n"); return (NULL); } long_ret = pst.page_size / 1024 * (psd.psd_vmtxt - psd.psd_avmtxt); return ((u_char *) (&long_ret)); case MEMTOTALREALTXT: /* * Retrieve the static memory statistics */ if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) { snmp_log(LOG_ERR, "memory_hpux: pstat_getstatic failed!\n"); return (NULL); } /* * Retrieve the dynamic memory statistics */ if (pstat_getdynamic(&psd, sizeof(psd), (size_t) 1, 0) == -1) { snmp_log(LOG_ERR, "memory_hpux: pstat_getdynamic failed!\n"); return (NULL); } long_ret = pst.page_size / 1024 * psd.psd_rmtxt; return ((u_char *) (&long_ret)); case MEMUSEDREALTXT: /* * Retrieve the static memory statistics */ if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) { snmp_log(LOG_ERR, "memory_hpux: pstat_getstatic failed!\n"); return (NULL); } /* * Retrieve the dynamic memory statistics */ if (pstat_getdynamic(&psd, sizeof(psd), (size_t) 1, 0) == -1) { snmp_log(LOG_ERR, "memory_hpux: pstat_getdynamic failed!\n"); return (NULL); } long_ret = pst.page_size / 1024 * (psd.psd_rmtxt - psd.psd_armtxt); return ((u_char *) (&long_ret)); case MEMTOTALFREE: /* * Retrieve the static memory statistics */ if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) { snmp_log(LOG_ERR, "memory_hpux: pstat_getstatic failed!\n"); return (NULL); } /* * Retrieve the dynamic memory statistics */ if (pstat_getdynamic(&psd, sizeof(psd), (size_t) 1, 0) == -1) { snmp_log(LOG_ERR, "memory_hpux: pstat_getdynamic failed!\n"); return (NULL); } get_swapinfo(&swap); long_ret = (pst.page_size / 1024 * psd.psd_free) + swap.free_swap; return ((u_char *) (&long_ret)); case ERRORFLAG: get_swapinfo(&swap); long_ret = (swap.free_swap > minimumswap) ? 0 : 1; return ((u_char *) (&long_ret)); case ERRORMSG: get_swapinfo(&swap); if ((swap.free_swap > minimumswap) ? 0 : 1) sprintf(errmsg, "Running out of swap space (%ld)", long_ret); else errmsg[0] = 0; *var_len = strlen(errmsg); return ((u_char *) (errmsg)); } /* end case */ return (NULL); }
static int get_system_info (kstat_ctl_t *kc, sys_stat *sst) { kstat_t *ksp; kstat_named_t *kn; kid_t kcid, nkcid; int totalswap, freeswap; int i, j; cpu_stat_t cpu_stat[MAX_CPU]; static int pagesize = 0, maxmem = 0; static long cp_time[CPUSTATES]; static long cp_old[CPUSTATES]; static long cp_diff[CPUSTATES]; static kstat_t *cpu_ks[MAX_CPU]; static int ncpu = 0; static int freemem_check_time = 0; int changed = 0; kcid = kc->kc_chain_id; kcid_changed: nkcid = kstat_chain_update (kc); if (nkcid) { kcid = nkcid; changed = 1; } CHECK_KCID (nkcid, 0); ksp = kstat_lookup (kc, "unix", -1, "system_misc"); nkcid = kstat_read (kc, ksp, NULL); CHECK_KCID (nkcid, kcid); /* * collect load average information */ kn = kstat_data_lookup (ksp, "avenrun_1min"); sst->load_avg[0] = kn->value.ui32; kn = kstat_data_lookup (ksp, "avenrun_5min"); sst->load_avg[1] = kn->value.ui32; kn = kstat_data_lookup (ksp, "avenrun_15min"); sst->load_avg[2] = kn->value.ui32; /* * collect cpu information */ if (changed == 1 || ncpu == 0) { ncpu = 0; for (ksp = kc->kc_chain; ksp && ncpu < MAX_CPU; ksp = ksp->ks_next) { if (strncmp (ksp->ks_name, "cpu_stat", 8) == 0) { nkcid = kstat_read (kc, ksp, NULL); CHECK_KCID (nkcid, kcid); cpu_ks[ncpu] = ksp; ncpu++; } } } for (i = 0; i < ncpu; i++) { nkcid = kstat_read (kc, cpu_ks[i], & (cpu_stat[i])); CHECK_KCID (nkcid, kcid); } for (j = 0; j < CPUSTATES; j++) { cp_time[j] = 0L; } for (i = 0; i < ncpu; i++) { for (j = 0; j < CPU_WAIT; j++) { cp_time[j] += (long) cpu_stat[i].cpu_sysinfo.cpu[j]; } cp_time[CPUSTATE_IOWAIT] += (long) cpu_stat[i].cpu_sysinfo.wait[W_IO] + (long) cpu_stat[i].cpu_sysinfo.wait[W_PIO]; cp_time[CPUSTATE_SWAP] = (long) cpu_stat[i].cpu_sysinfo.wait[W_SWAP]; } percentages (CPUSTATES, sst->cpu_states, cp_time, cp_old, cp_diff); /* * collect memory information */ if (pagesize == 0) { pagesize = sysconf (_SC_PAGESIZE); } if (maxmem == 0) { maxmem = sysconf (_SC_PHYS_PAGES); sst->memory_stats[0] = PAGETOM (maxmem, pagesize); } if (time (NULL) - freemem_check_time > 30) { ksp = kstat_lookup (kc, "unix", 0, "system_pages"); nkcid = kstat_read (kc, ksp, NULL); CHECK_KCID (nkcid, kcid); kn = kstat_data_lookup (ksp, "freemem"); sst->memory_stats[2] = PAGETOM (kn->value.ui32, pagesize); if (sst->memory_stats[0] - sst->memory_stats[2] > 0) { sst->memory_stats[1] = sst->memory_stats[0] - sst->memory_stats[2]; } freemem_check_time = time (NULL); } #if 0 kn = kstat_data_lookup (ksp, "availrmem"); #endif get_swapinfo (&totalswap, &freeswap); sst->memory_stats[3] = PAGETOM ((totalswap - freeswap), pagesize); sst->memory_stats[4] = PAGETOM (freeswap, pagesize); return 1; }