Example #1
0
/*
 * In the following routines, we do the first kstat_lookup() assuming that
 * the device is gldv3-based and that the kstat name is the one passed in
 * as the "name" argument. If the lookup fails, we redo the kstat_lookup()
 * omitting the kstat name. This second lookup is needed for getting kstats
 * from legacy devices. This can fail too if the device is not attached or
 * the device is legacy and doesn't export the kstats we need.
 */
static void
get_stats(char *module, int instance, char *name, pktsum_t *stats)
{
	kstat_ctl_t	*kcp;
	kstat_t		*ksp;

	if ((kcp = kstat_open()) == NULL) {
		(void) fprintf(stderr,
		    gettext("%s: kstat open operation failed\n"),
		    progname);
		return;
	}

	if ((ksp = kstat_lookup(kcp, module, instance, name)) == NULL &&
	    (ksp = kstat_lookup(kcp, module, instance, NULL)) == NULL) {
		/*
		 * The kstat query could fail if the underlying MAC
		 * driver was already detached.
		 */
		(void) kstat_close(kcp);
		return;
	}

	if (kstat_read(kcp, ksp, NULL) == -1)
		goto bail;

	if (kstat_value(ksp, "ipackets64", KSTAT_DATA_UINT64,
	    &stats->ipackets) < 0)
		goto bail;

	if (kstat_value(ksp, "opackets64", KSTAT_DATA_UINT64,
	    &stats->opackets) < 0)
		goto bail;

	if (kstat_value(ksp, "rbytes64", KSTAT_DATA_UINT64,
	    &stats->rbytes) < 0)
		goto bail;

	if (kstat_value(ksp, "obytes64", KSTAT_DATA_UINT64,
	    &stats->obytes) < 0)
		goto bail;

	if (kstat_value(ksp, "ierrors", KSTAT_DATA_UINT32,
	    &stats->ierrors) < 0)
		goto bail;

	if (kstat_value(ksp, "oerrors", KSTAT_DATA_UINT32,
	    &stats->oerrors) < 0)
		goto bail;

	(void) kstat_close(kcp);
	return;

bail:
	(void) kstat_close(kcp);
}
Example #2
0
int
acquire_sys(struct snapshot *ss, kstat_ctl_t *kc)
{
	size_t i;
	kstat_named_t *knp;
	kstat_t *ksp;

	if ((ksp = kstat_lookup(kc, "unix", 0, "sysinfo")) == NULL)
		return (errno);

	if (kstat_read(kc, ksp, &ss->s_sys.ss_sysinfo) == -1)
		return (errno);

	if ((ksp = kstat_lookup(kc, "unix", 0, "vminfo")) == NULL)
		return (errno);

	if (kstat_read(kc, ksp, &ss->s_sys.ss_vminfo) == -1)
		return (errno);

	if ((ksp = kstat_lookup(kc, "unix", 0, "dnlcstats")) == NULL)
		return (errno);

	if (kstat_read(kc, ksp, &ss->s_sys.ss_nc) == -1)
		return (errno);

	if ((ksp = kstat_lookup(kc, "unix", 0, "system_misc")) == NULL)
		return (errno);

	if (kstat_read(kc, ksp, NULL) == -1)
		return (errno);

	knp = (kstat_named_t *)kstat_data_lookup(ksp, "clk_intr");
	if (knp == NULL)
		return (errno);

	ss->s_sys.ss_ticks = knp->value.l;

	knp = (kstat_named_t *)kstat_data_lookup(ksp, "deficit");
	if (knp == NULL)
		return (errno);

	ss->s_sys.ss_deficit = knp->value.l;

	for (i = 0; i < ss->s_nr_cpus; i++) {
		if (!CPU_ACTIVE(&ss->s_cpus[i]))
			continue;

		if (kstat_add(&ss->s_cpus[i].cs_sys, &ss->s_sys.ss_agg_sys))
			return (errno);
		if (kstat_add(&ss->s_cpus[i].cs_vm, &ss->s_sys.ss_agg_vm))
			return (errno);
		ss->s_nr_active_cpus++;
	}

	return (0);
}
Example #3
0
static int	get_kstat_system_misc(char *key, int *value)
{
	kstat_ctl_t	*kc;
	kstat_t		*ksp;
	kstat_named_t	*kn = NULL;
	int		ret = FAIL;

	if (NULL == (kc = kstat_open()))
		return ret;

	if (NULL == (ksp = kstat_lookup(kc, "unix", 0, "system_misc")))
		goto close;

	if (-1 == kstat_read(kc, ksp, NULL))
		goto close;

	if (NULL == (kn = (kstat_named_t *)kstat_data_lookup(ksp, key)))
		goto close;

	*value = kn->value.ul;

	ret = SUCCEED;
close:
	kstat_close(kc);

	return ret;
}
Example #4
0
JNIEXPORT jlong JNICALL Java_com_jezhumble_javasysmon_SolarisMonitor_uptimeInSeconds (JNIEnv *env, jobject obj)
{
  struct timeval secs;
  kstat_ctl_t   *kc; 
  kstat_t       *ksp; 
  kstat_named_t *knp; 
  unsigned long long uptime;

  if (gettimeofday(&secs, NULL) != 0) {
    return (jlong) 0;
  }
  uptime = (unsigned long long) secs.tv_sec;

  kc = kstat_open();
  if ((ksp = kstat_lookup(kc, "unix", 0, "system_misc")) == NULL) {
    fprintf(stderr, "%s\n", "ERROR: Can't read boot time.");
    return 0;
  }
  if ((kstat_read(kc, ksp, NULL) != -1) &&
  /* lookup the boot time record */
    ((knp = kstat_data_lookup(ksp, "boot_time")) != NULL)) {
      return (jlong) (uptime - knp->value.ui32);
  } else {
    return 0;
  }
}
Example #5
0
int CollectorSolaris::getRawHostDiskLoad(const char *name, uint64_t *disk_ms, uint64_t *total_ms)
{
    int rc = VINF_SUCCESS;
    AssertReturn(strlen(name) < KSTAT_STRLEN, VERR_INVALID_PARAMETER);
    LogFlowThisFunc(("n=%s\n", name));
    kstat_t *ksDisk = kstat_lookup(mKC, NULL, -1, (char *)name);
    if (ksDisk != 0)
    {
        if (kstat_read(mKC, ksDisk, 0) == -1)
        {
            LogRel(("kstat_read(%s) -> %d\n", name, errno));
            rc = VERR_INTERNAL_ERROR;
        }
        else
        {
            kstat_io_t *ksIo = KSTAT_IO_PTR(ksDisk);
            /*
             * We do not care for wrap possibility here, although we may
             * reconsider in about 300 years (9223372036854775807 ns).
             */
            *disk_ms = ksIo->rtime / 1000000;
            *total_ms = ksDisk->ks_snaptime / 1000000;
        }
    }
    else
    {
        LogRel(("kstat_lookup(%s) -> %d\n", name, errno));
        rc = VERR_INTERNAL_ERROR;
    }

    return rc;
}
Example #6
0
static int get_kstat_io(
    const char *name,
    kstat_io_t *returned_data
    )
{
    int result = SYSINFO_RET_FAIL;
    kstat_ctl_t *kc;
    kstat_t *kt;

    kc = kstat_open();
    if (kc)
    {
	kt = kstat_lookup(kc, NULL, -1, (char *) name);
	if (kt)
	{
	    if (kt->ks_type == KSTAT_TYPE_IO)
	    {	
		if(kstat_read(kc, kt, returned_data) != -1)
		{
		    result = SYSINFO_RET_OK;
		}
            }
        }
	kstat_close(kc);
    }
    return result;
}
Example #7
0
/*
 * Retrieve the vopstats.  If kspp (pointer to kstat_t pointer) is non-NULL,
 * then pass it back to the caller.
 *
 * Returns 0 on success, non-zero on failure.
 */
int
get_vopstats(kstat_ctl_t *kc, char *ksname, vopstats_t *vsp, kstat_t **kspp)
{
	kstat_t		*ksp;

	if (ksname == NULL || *ksname == 0)
		return (1);

	errno = 0;
	/* wait for a possibly up-to-date chain */
	while (kstat_chain_update(kc) == -1) {
		if (errno == EAGAIN) {
			errno = 0;
			(void) poll(NULL, 0, RETRY_DELAY);
			continue;
		}
		perror("kstat_chain_update");
		exit(1);
	}

	if ((ksp = kstat_lookup(kc, NULL, -1, ksname)) == NULL) {
		return (1);
	}

	if (kstat_read(kc, ksp, vsp) == -1) {
		return (1);
	}

	if (kspp)
		*kspp = ksp;

	return (0);
}
Example #8
0
JNIEXPORT jobject JNICALL Java_com_jezhumble_javasysmon_SolarisMonitor_cpuTimes (JNIEnv *env, jobject obj)
{
  kstat_ctl_t   *kc;
  kstat_t       *ksp;
  struct cpu_stat cpu_stats;
  int i;
  unsigned long long userticks, systicks, idleticks;
  jclass		cpu_times_class;
  jmethodID	cpu_times_constructor;
  jobject		cpu_times;

  idleticks = systicks = userticks = 0; 
  kc = kstat_open();
  for (i = 0; i < num_cpus; i++) {
    // the next line is wrong: should replace "cpu_stat0" with "cpu_stati"
    // but my C-fu is insufficient to work out how to do this in <10 lines
    if ((ksp = kstat_lookup(kc, "cpu_stat", -1, "cpu_stat0")) != NULL) {
      kstat_read(kc, ksp, &cpu_stats);
      idleticks += cpu_stats.cpu_sysinfo.cpu[CPU_IDLE];
      userticks += cpu_stats.cpu_sysinfo.cpu[CPU_USER];
      systicks += cpu_stats.cpu_sysinfo.cpu[CPU_KERNEL];
      systicks += cpu_stats.cpu_sysinfo.cpu[CPU_WAIT];
    }
  }

  cpu_times_class = (*env)->FindClass(env, "com/jezhumble/javasysmon/CpuTimes");
  cpu_times_constructor = (*env)->GetMethodID(env, cpu_times_class, "<init>", "(JJJ)V");
  cpu_times = (*env)->NewObject(env, cpu_times_class, cpu_times_constructor, (jlong) userticks, (jlong) systicks, (jlong) idleticks);
  (*env)->DeleteLocalRef(env, cpu_times_class);
  return cpu_times;
}
Example #9
0
int	SYSTEM_BOOTTIME(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result)
{
	kstat_ctl_t	*kc;
	kstat_t		*kp;
	kstat_named_t	*kn;
	int		ret = SYSINFO_RET_FAIL;

	if (NULL == (kc = kstat_open()))
		return ret;

	if (NULL != (kp = kstat_lookup(kc, "unix", 0, "system_misc")))
	{
		if (-1 != kstat_read(kc, kp, 0))
		{
			if (NULL != (kn = (kstat_named_t*)kstat_data_lookup(kp, "boot_time")))
			{
				SET_UI64_RESULT(result, (zbx_uint64_t)kn->value.ul);
				ret = SYSINFO_RET_OK;
			}
		}
	}
	kstat_close(kc);

	return ret;
}
Example #10
0
int	KERNEL_MAXPROC(AGENT_REQUEST *request, AGENT_RESULT *result)
{
	int		ret = SYSINFO_RET_FAIL;
	kstat_ctl_t	*kc;
	kstat_t		*kt;
	struct var	*v;

	if (NULL != (kc = kstat_open()))
	{
		if (NULL != (kt = kstat_lookup(kc, "unix", 0, "var")))
		{
			if (KSTAT_TYPE_RAW == kt->ks_type && -1 != kstat_read(kc, kt, NULL))
			{
				v = (struct var *)kt->ks_data;

				/* int	v_proc;	    Max processes system wide */
				SET_UI64_RESULT(result, v->v_proc);
				ret = SYSINFO_RET_OK;
			}
		}
		kstat_close(kc);
	}

	return ret;
}
Example #11
0
static int	get_kstat_system_misc(char *s, int *value)
{
	kstat_ctl_t	*kc;
	kstat_t		*kp;
	kstat_named_t	*kn = NULL;
	int		n, i;

	if (NULL == (kc = kstat_open()))
		return FAIL;

	if (NULL == (kp = kstat_lookup(kc, "unix", 0, "system_misc")))
	{
		kstat_close(kc);
		return FAIL;
	}

	if (-1 == kstat_read(kc, kp, NULL))
	{
		kstat_close(kc);
		return FAIL;
	}

	if (NULL == (kn = (kstat_named_t*)kstat_data_lookup(kp, s)))
	{
		kstat_close(kc);
		return FAIL;
	}

	kstat_close(kc);

	*value = kn->value.ul;

	return SUCCEED;
}
Example #12
0
int	SYSTEM_UPTIME(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result)
{
	kstat_ctl_t	*kc;
	kstat_t		*kp;
	kstat_named_t	*kn;
	time_t		now;
	int		ret = SYSINFO_RET_FAIL;

	assert(result);

	init_result(result);

	if (NULL == (kc = kstat_open()))
		return ret;

	if (NULL != (kp = kstat_lookup(kc, "unix", 0, "system_misc")))
	{
		if (-1 != kstat_read(kc, kp, 0))
		{
			if (NULL != (kn = (kstat_named_t*)kstat_data_lookup(kp, "boot_time")))
			{
				time(&now);
				SET_UI64_RESULT(result, difftime(now, (time_t) kn->value.ul));
				ret = SYSINFO_RET_OK;
			}
		}
	}
	kstat_close(kc);

	return ret;
}
Example #13
0
static int get_kstat_named_field(
    const char *name,
    const char *field,
    kstat_named_t *returned_data
    )
{
    int ret = SYSINFO_RET_FAIL;

    kstat_ctl_t	  *kc;
    kstat_t       *kp;
    kstat_named_t *kn;

    kc = kstat_open();
    if (kc)
    {
	kp = kstat_lookup(kc, NULL, -1, (char*) name);
        if ((kp) && (kstat_read(kc, kp, 0) != -1))
	{
	    kn = (kstat_named_t*) kstat_data_lookup(kp, (char*) field);
	    if(kn)
	    {
            	*returned_data = *kn;
            	ret = SYSINFO_RET_OK;
	    }
        }
	kstat_close(kc);
    }
    return ret;
}
Example #14
0
unsigned long 
SolarisSysUpTime::SysUpTime()
{
  unsigned long  retVal = 0;

  kstat_ctl_t*   ksc = kstat_open();
  if (ksc != NULL) 
  {
    kstat_t* ks = kstat_lookup(ksc, 
			       "unix", 
			       -1, 
			       "system_misc");
    if (ks != NULL) 
    {
      kid_t kid = kstat_read(ksc, ks, NULL);
      if (kid != -1) 
      {
	kstat_named_t* named = 
	  (kstat_named_t*)kstat_data_lookup(ks, "lbolt");
	if (named != NULL) 
	  retVal = named->value.ul;
      }
    }
    kstat_close(ksc);
  }
  return retVal;
}
Example #15
0
static int
get_chipid(kstat_ctl_t *kc, processorid_t cpuid)
{
	kstat_t		*ksp;
	kstat_named_t	*k;

	if ((ksp = kstat_lookup(kc, "cpu_info", cpuid, NULL)) == NULL)
		return (-1);

	if (kstat_read(kc, ksp, NULL) == -1) {
		(void) fprintf(stderr,
		    gettext("%s: kstat_read() failed for cpu %d: %s\n"),
		    opts->pgmname, cpuid, strerror(errno));
		return (-1);
	}

	if ((k = (kstat_named_t *)kstat_data_lookup(ksp, "chip_id")) == NULL) {
		(void) fprintf(stderr,
		    gettext("%s: chip_id not found for cpu %d: %s\n"),
		    opts->pgmname, cpuid, strerror(errno));
		return (-1);
	}

	return (k->value.i32);
}
Example #16
0
void
get_load_average(double *one, double *five, double *fifteen)
{
   kstat_ctl_t        *kc;
   kstat_t            *ks;
   kstat_named_t      *d1, *d5, *d15;

   if (!(kc = kstat_open()))
     {
	SET_AND_RETURN(0, 0, 0);
     }
   if (!(ks = kstat_lookup(kc, "unix", 0, "system_misc")))
     {
	SET_AND_RETURN(0, 0, 0);
     }
   if ((kstat_read(kc, ks, NULL)) < 0)
     {
	SET_AND_RETURN(0, 0, 0);
     }

   d1 = kstat_data_lookup(ks, "avenrun_1min");
   d5 = kstat_data_lookup(ks, "avenrun_5min");
   d15 = kstat_data_lookup(ks, "avenrun_15min");
   kstat_close(kc);

   SET_AND_RETURN(d1->value.ul / FSCALE, d5->value.ul / FSCALE,
		  d15->value.ul / FSCALE);
}
Example #17
0
JNIEXPORT jboolean JNICALL Java_jp_co_acroquest_endosnipe_javelin_resource_proc_SolarisResourceReader_openQuery
  (JNIEnv *env, jobject obj)
{
  kstat_t *kstat_ncpus;
  kstat_named_t *knamed_ncpus;
  int result;

  g_kstat_ctl = kstat_open();

  /* get number of cpus */
  kstat_ncpus = kstat_lookup(g_kstat_ctl, "unix", 0, "system_misc");
  result = kstat_read(g_kstat_ctl, kstat_ncpus, 0);
  if (result == -1)
  {
    return JNI_FALSE;
  }
  
  knamed_ncpus = kstat_data_lookup(kstat_ncpus, "ncpus");
  if (knamed_ncpus != NULL)
  {
    g_num_cpus = knamed_ncpus->value.ui32;
  }
  if (g_num_cpus <= 0)
  {
    g_num_cpus = 1;
  }

  getsysinfo(&g_system_info_prev);
  
  return JNI_TRUE;
}
Example #18
0
/*
 * Return the number of physical CPU cores on the system.
 */
static PyObject *
psutil_cpu_count_phys(PyObject *self, PyObject *args) {
    kstat_ctl_t *kc;
    kstat_t *ksp;
    int ncpus = 0;

    kc = kstat_open();
    if (kc == NULL)
        goto error;
    ksp = kstat_lookup(kc, "cpu_info", -1, NULL);
    if (ksp == NULL)
        goto error;

    for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
        if (strcmp(ksp->ks_module, "cpu_info") != 0)
            continue;
        if (kstat_read(kc, ksp, NULL) == -1)
            goto error;
        ncpus += 1;
    }

    kstat_close(kc);
    if (ncpus > 0)
        return Py_BuildValue("i", ncpus);
    else
        goto error;

error:
    // mimic os.cpu_count()
    if (kc != NULL)
        kstat_close(kc);
    Py_RETURN_NONE;
}
Example #19
0
double
kstat_load_avg(void)
{
	static kstat_ctl_t	*kc = NULL;		/* libkstat cookie */
	static kstat_t		*ksp = NULL;	/* kstat pointer */
	kstat_named_t 		*ksdp = NULL;  	/* kstat data pointer */

	if( ! kc ) {
		if( (kc = kstat_open()) == NULL ) {
			dprintf( D_ALWAYS, "kstat_open() failed, errno = %d\n", errno );
			RETURN;
		}
	}

	if( ! ksp ) {
		if( (ksp = kstat_lookup(kc, "unix", 0, "system_misc")) == NULL ) {
			dprintf( D_ALWAYS, "kstat_lookup() failed, errno = %d\n", errno );
			RETURN;
		}
	}

	if( kstat_read(kc, ksp, NULL) == -1 ) {
		dprintf( D_ALWAYS, "kstat_read() failed, errno = %d\n", errno );
		RETURN;
	}

	ksdp = (kstat_named_t *) kstat_data_lookup(ksp, "avenrun_1min");
	if( ksdp ) {
		return (double) ksdp->value.l / FSCALE;
	} else {
		dprintf( D_ALWAYS, "kstat_data_lookup() failed, errno = %d\n",
				 errno);
		RETURN;
	}		
}
Example #20
0
static int
cpu_get_serialid_kstat(uint32_t cpuid, uint64_t *serialidp)
{
	kstat_named_t *kn;
	kstat_ctl_t *kc;
	kstat_t *ksp;
	int i;

	if ((kc = kstat_open()) == NULL)
		return (-1); /* errno is set for us */

	if ((ksp = kstat_lookup(kc, "cpu_info", cpuid, NULL)) == NULL) {
		(void) kstat_close(kc);
		return (fmd_fmri_set_errno(ENOENT));
	}

	if (kstat_read(kc, ksp, NULL) == -1) {
		int oserr = errno;
		(void) kstat_close(kc);
		return (fmd_fmri_set_errno(oserr));
	}

	for (kn = ksp->ks_data, i = 0; i < ksp->ks_ndata; i++, kn++) {
		if (strcmp(kn->name, "device_ID") == 0) {
			*serialidp = kn->value.ui64;
			(void) kstat_close(kc);
			return (0);
		}
	}

	(void) kstat_close(kc);
	return (fmd_fmri_set_errno(ENOENT));
}
Example #21
0
/*
 * hidden
 */
static int get_disk_kstat_record(const char *name,
                                 hrtime_t *crtime,
                                 hrtime_t *snaptime, 
                                 kstat_io_t *returned_data)
{
  int result = SYSINFO_RET_FAIL;
  kstat_ctl_t *kc;

  kc = kstat_open();

  if (kc)
    {
      kstat_t *kt;

      kt = kstat_lookup(kc, NULL, -1, (char *) name);

      if (kt)
	{
	  if (    (kt->ks_type == KSTAT_TYPE_IO)
               && (kstat_read(kc, kt, returned_data) != -1)
	     )
            {
               *crtime = kt->ks_crtime;
	       *snaptime = kt->ks_snaptime;
	       result = SYSINFO_RET_OK;
            }
        }

      kstat_close(kc);
    }

  return result;
}
Example #22
0
/** スワップ使用量を取得する */
int get_swapinfo(uint64_t *pavail, uint64_t *pfree, uint64_t *updates) 
{
  kstat_t *kstat_vminfo;
  kstat_t *kstat_sysinfo;
  kstat_named_t *kname_freemem;
  kstat_named_t *kname_availmem;
  kstat_named_t *kname_updates;
  vminfo_t vminfo;
  sysinfo_t sysinfo;
  int result_vminfo;
  int result_sysinfo;

  if (pavail == NULL || pfree == NULL || updates == NULL)
  {
    return JNI_FALSE;
  }

  kstat_vminfo = kstat_lookup(g_kstat_ctl, "unix", 0, "vminfo");
  if (kstat_vminfo == NULL)
  {
    return JNI_FALSE;
  }

  result_vminfo = kstat_read(g_kstat_ctl, kstat_vminfo, &vminfo);
  if (result_vminfo == -1)
  {
    return JNI_FALSE;
  }

  *pavail = vminfo.swap_avail;
  *pfree = vminfo.swap_resv;
  
  kstat_sysinfo = kstat_lookup(g_kstat_ctl, "unix", 0, "sysinfo");
  if (kstat_sysinfo == NULL)
  {
    return JNI_FALSE;
  }

  result_sysinfo = kstat_read(g_kstat_ctl, kstat_sysinfo, &sysinfo);
  if (result_sysinfo == -1)
  {
    return JNI_FALSE;
  }
  *updates = sysinfo.updates;

  return (0);
}
Example #23
0
static void
find_cpu_model_freq(char **cpu_model, int *frequency) {

  kstat_t *ksp;
  kid_t nkcid;
  kstat_named_t *knp;
  int i,found_brand,found_freq;


  found_brand = 0;
  found_freq = 0;

  kc = kstat_open();

  if (NULL == kc) {
    *cpu_model = strdup("kstat_open");
    *frequency = -1;
    return;
  }

  ksp = kstat_lookup(kc, "cpu_info", 0, NULL);

  if ((NULL == ksp) ||
      ((ksp) && (KSTAT_TYPE_NAMED != ksp->ks_type))) {
    *cpu_model = strdup("kstat_lookup");
    *frequency = -1;
    kstat_close(kc);
    return;
  }

  nkcid = kstat_read(kc, ksp, NULL);

  if (-1 == nkcid) {
    *cpu_model = strdup("kstat_read");
    *frequency = -1;
    kstat_close(kc);
    return;
  }

  for (i = ksp->ks_ndata, knp = ksp->ks_data;
       i > 0;
       knp++, i--) {
    if (!strcmp("brand", knp->name)) {
      *cpu_model = strdup(KSTAT_NAMED_STR_PTR(knp));
      found_brand = 1;
    }
    else if (!strcmp("clock_MHz",knp->name)) {
      *frequency = (int)knp->value.ui32;
      found_freq = 1;
    }
  }
  if (!found_brand)
    *cpu_model = strdup("CPU Not Found");
  if (!found_freq)
    *frequency = -1;

  kstat_close(kc);
}
Example #24
0
void GuidReset(void) {
	char name[256 + 1];
	char salt[32];
#ifdef HAVE_KSTAT_H
	kstat_t *ksp;
	kstat_ctl_t *kc;
#elifdef HAVE_SYS_SYSINFO_H
	struct sysinfo si;
#endif
	struct timeval tv;
	xpl_hash_context context;
	
	XplHashNew(&context, XPLHASH_SHA1);
	
	gettimeofday(&tv, NULL);
	sprintf(salt, "%010lu%010lu", tv.tv_sec, tv.tv_usec);
	XplHashWrite(&context, salt, strlen(salt));

	if (!gethostname(name, sizeof(name) - 1)) {
		XplHashWrite(&context, name, strlen(name));
	} else {
		XplRandomData(name, sizeof(name) - 1);
		XplHashWrite(&context, name, sizeof(name) - 1);
	}

#if HAVE_KSTAT_H
	if ((kc = kstat_open()) &&
		(ksp = kstat_lookup(kc, "unix", 0, "system_misc")) &&
		(kstat_read(kc, ksp, NULL) != -1)) {
		XplHashWrite(&context, ksp, sizeof(kstat_t));
	} else {
		XplRandomData(name, sizeof(name) - 1);
		XplHashWrite(&context, name, sizeof(name) - 1);
	}
	if (kc != NULL) {
		kstat_close(kc);
	}
#elif HAVE_SYS_SYSINFO_H
	if (!sysinfo(&si)) {
		XplHashWrite(&context, &si, sizeof(si));
	} else {
		XplRandomData(name, sizeof(name) - 1);
		XplHashWrite(&context, name, sizeof(name) - 1);
	}
#else 
	/* FIXME: should find a sysinfo equiv. for this */
	XplRandomData(name, sizeof(name) - 1);
	XplHashWrite(&context, name, sizeof(name) - 1);
#endif

	XplHashFinal(&context, XPLHASH_UPPERCASE, StoreAgent.guid.next,
		XPLHASH_SHA1_LENGTH);
	
	memset(StoreAgent.guid.next + NMAP_GUID_PREFIX_LENGTH, '0',
		NMAP_GUID_SUFFIX_LENGTH);

	return;
}
Example #25
0
static int
get_single_mac_stat(const char *dev, const char *name, uint8_t type,
    void *val)
{
	char		module[LIFNAMSIZ];
	int		instance;
	kstat_ctl_t	*kcp;
	kstat_t		*ksp;

	if ((kcp = kstat_open()) == NULL) {
		(void) fprintf(stderr,
		    gettext("%s: kstat open operation failed\n"),
		    progname);
		return (-1);
	}

	if (dlpi_if_parse(dev, module, &instance) != 0)
		return (-1);
	if ((ksp = kstat_lookup(kcp, module, instance, "mac")) == NULL &&
	    (ksp = kstat_lookup(kcp, module, instance, NULL)) == NULL) {
		/*
		 * The kstat query could fail if the underlying MAC
		 * driver was already detached.
		 */
		goto bail;
	}

	if (kstat_read(kcp, ksp, NULL) == -1) {
		(void) fprintf(stderr,
		    gettext("%s: kstat read failed\n"),
		    progname);
		goto bail;
	}

	if (kstat_value(ksp, name, type, val) < 0)
		goto bail;

	(void) kstat_close(kcp);
	return (0);

bail:
	(void) kstat_close(kcp);
	return (-1);
}
Example #26
0
int sigar_mem_get(sigar_t *sigar, sigar_mem_t *mem)
{
    kstat_ctl_t *kc = sigar->kc;
    kstat_t *ksp;
    sigar_uint64_t kern = 0;

    SIGAR_ZERO(mem);

    /* XXX: is mem hot swappable or can we just do this during open ? */
    mem->total = sysconf(_SC_PHYS_PAGES);
    mem->total <<= sigar->pagesize;

    if (sigar_kstat_update(sigar) == -1) {
        return errno;
    }

    if ((ksp = sigar->ks.syspages) && kstat_read(kc, ksp, NULL) >= 0) {
        sigar_koffsets_init_syspages(sigar, ksp);

        mem->free = kSYSPAGES(KSTAT_SYSPAGES_FREE);
        mem->free <<= sigar->pagesize;

        mem->used = mem->total - mem->free;
    }

    if ((ksp = sigar->ks.mempages) && kstat_read(kc, ksp, NULL) >= 0) {
        sigar_koffsets_init_mempages(sigar, ksp);
    }

    /* XXX mdb ::memstat cachelist/freelist not available to kstat, see: */
    /* http://bugs.opensolaris.org/bugdatabase/view_bug.do?bug_id=6821980 */

    /* ZFS ARC cache. see: http://opensolaris.org/jive/thread.jspa?messageID=393695 */
    if ((ksp = kstat_lookup(sigar->kc, "zfs", 0, "arcstats")) &&
        (kstat_read(sigar->kc, ksp, NULL) != -1))
    {
        kstat_named_t *kn;

        if ((kn = (kstat_named_t *)kstat_data_lookup(ksp, "size"))) {
            kern = kn->value.i64;
        }
        if ((kn = (kstat_named_t *)kstat_data_lookup(ksp, "c_min"))) {
            /* c_min cannot be reclaimed they say */
            if (kern > kn->value.i64) {
                kern -= kn->value.i64;
            }
        }
    }

    mem->actual_free = mem->free + kern;
    mem->actual_used = mem->used - kern;

    sigar_mem_calc_ram(sigar, mem);

    return SIGAR_OK;
}
Example #27
0
/*
 * Return system-wide CPU times.
 */
static PyObject *
psutil_per_cpu_times(PyObject *self, PyObject *args)
{
    kstat_ctl_t *kc;
    kstat_t *ksp;
    cpu_stat_t cs;
    int numcpus;
    int i;
    PyObject *py_retlist = PyList_New(0);
    PyObject *py_cputime = NULL;

    if (py_retlist == NULL)
        return NULL;

    kc = kstat_open();
    if (kc == NULL) {
        PyErr_SetFromErrno(PyExc_OSError);
        goto error;
    }

    numcpus = sysconf(_SC_NPROCESSORS_ONLN) - 1;
    for (i = 0; i <= numcpus; i++) {
        ksp = kstat_lookup(kc, "cpu_stat", i, NULL);
        if (ksp == NULL) {
            PyErr_SetFromErrno(PyExc_OSError);
            goto error;
        }
        if (kstat_read(kc, ksp, &cs) == -1) {
            PyErr_SetFromErrno(PyExc_OSError);
            goto error;
        }

        py_cputime = Py_BuildValue("ffff",
                                   (float)cs.cpu_sysinfo.cpu[CPU_USER],
                                   (float)cs.cpu_sysinfo.cpu[CPU_KERNEL],
                                   (float)cs.cpu_sysinfo.cpu[CPU_IDLE],
                                   (float)cs.cpu_sysinfo.cpu[CPU_WAIT]);
        if (py_cputime == NULL)
            goto error;
        if (PyList_Append(py_retlist, py_cputime))
            goto error;
        Py_DECREF(py_cputime);

    }

    kstat_close(kc);
    return py_retlist;

error:
    Py_XDECREF(py_cputime);
    Py_DECREF(py_retlist);
    if (kc != NULL)
        kstat_close(kc);
    return NULL;
}
gboolean
get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64 *memory_cache, guint64 *memory_buffers, guint64 *swap_total, guint64 *swap_free)
{
	kstat_t *ksp;
	kstat_named_t *knp;
	gint n;

	if (!kc)
		init_stats();

	if (!(ksp = kstat_lookup (kc, "unix", 0, "system_pages")))
		return FALSE;
	kstat_read (kc, ksp, NULL);
	knp = kstat_data_lookup (ksp, "physmem");
	*memory_total = getpagesize () * knp->value.ui64;
	knp = kstat_data_lookup (ksp, "freemem");
	*memory_free = getpagesize () * knp->value.ui64;
	*memory_cache = 0;
	*memory_buffers = 0;

	*swap_total = *swap_free = 0;
	if ((n = swapctl (SC_GETNSWP, NULL)) > 0)
	{
		struct swaptable *st;
		struct swapent *swapent;
		gchar path[MAXPATHLEN];
		gint i;

		if ((st = malloc (sizeof (int) + n * sizeof (swapent_t))) == NULL)
			return FALSE;
		st->swt_n = n;

		swapent = st->swt_ent;
		for (i = 0; i < n; i++, swapent++)
			swapent->ste_path = path;

		if ((swapctl (SC_LIST, st)) == -1)
		{
			free (st);
			return FALSE;
		}

		swapent = st->swt_ent;
		for (i = 0; i < n; i++, swapent++)
		{
			*swap_total += swapent->ste_pages * getpagesize ();
			*swap_free += swapent->ste_free * getpagesize ();
		}

		free (st);
	}

	return TRUE;
}
Example #29
0
static kstat_t *
kstat_lookup_read(kstat_ctl_t *kc, char *module,
		int instance, char *name)
{
	kstat_t *ksp = kstat_lookup(kc, module, instance, name);
	if (ksp == NULL)
		return (NULL);
	if (kstat_read(kc, ksp, NULL) == -1)
		return (NULL);
	return (ksp);
}
Example #30
0
char* SolarisProcessList_readZoneName(kstat_ctl_t* kd, SolarisProcess* sproc) {
  char* zname;
  if ( sproc->zoneid == 0 ) {
     zname = xStrdup("global    ");
  } else if ( kd == NULL ) {
     zname = xStrdup("unknown   ");
  } else {
     kstat_t* ks = kstat_lookup( kd, "zones", sproc->zoneid, NULL );
     zname = xStrdup(ks->ks_name);
  }
  return zname;
}