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

📄 vmstat_solaris2.c

📁 非常不错的网管开发包
💻 C
📖 第 1 页 / 共 2 页
字号:
	      blocks_write[cpu_num] = cs.cpu_sysinfo.bwrite - blocks_write[cpu_num];	      /* Get new number of blocks read and calculate difference */	      blocks_read[cpu_num] = cs.cpu_sysinfo.bread - blocks_read[cpu_num];	      /* Get new number of interrupts and calculate difference */	      interrupts[cpu_num] = cs.cpu_sysinfo.intr - interrupts[cpu_num];	      /* Get new number of context switches and calculate difference */	      context_sw[cpu_num] = cs.cpu_sysinfo.pswitch - context_sw[cpu_num];	      	      /* Increment CPUs found count */	      cpu_num++;	      	    } /* end else */	} /* end for */            /* Calculate averages for all CPUs and return single value */      /* Since these are static variables we need to initialize them properly */      swapin_avg = 0;      swapout_avg = 0;      blocks_write_avg = 0;      blocks_read_avg =0;      interrupts_avg = 0;      context_sw_avg = 0;            /* First sum up all values */      for (cpu_num=0;cpu_num<num_cpu;cpu_num++)	{	  swapin_avg += swapin[cpu_num];	  swapout_avg += swapout[cpu_num];	  blocks_write_avg += blocks_write[cpu_num];	  blocks_read_avg += blocks_read[cpu_num];	  interrupts_avg += interrupts[cpu_num];	  context_sw_avg +=context_sw[cpu_num];	}      /* swapin and swapout are in pages, MIB wants kB/s, we sleep(1) so we just need to get kB */      /* getpagesize() returns pagesize in bytes */      swapin_avg = swapin_avg * (getpagesize() / 1024);      swapout_avg = swapout_avg * (getpagesize() / 1024);      /* Then divide by number of CPUs, discarding fractions */      swapin_avg /= num_cpu;      swapout_avg /= num_cpu;      blocks_write_avg /= num_cpu;      blocks_read_avg /= num_cpu;      interrupts_avg /= num_cpu;      context_sw_avg /= num_cpu;          } /* end if (timestap_new > (timestamp_old_1 + 1)) */    /* Return the requested variable, casting to long */  switch (what)    {    case SWAPIN:      return((long) swapin_avg);    case SWAPOUT:      return((long) swapout_avg);    case IOSENT:      return((long) blocks_write_avg);    case IORECEIVE:      return((long) blocks_read_avg);    case SYSINTERRUPTS:      return((long) interrupts_avg);    case SYSCONTEXT:      return((long) context_sw_avg);    default:      snmp_log(LOG_ERR,"vmstat_solaris2 (getMisc): No data found.\n");      return(-1);    } /* end switch */} /* end function getMisc *//* getCPU: get percentages for CPU utilisation *//* state: CPU_IDLE, CPU_USER, CPU_KERNEL + CPU_WAIT = CPU_SYSTEM -> 0, 1, 4 */long getCPU(int state){    /* Variables start here */    /* From sys/kstat.h (included from kstat.h): */  /* Pointer to current kstat */  kstat_t *ksp;    /* As always, we need s.th. to count on, aehm, by */  int i=0;    /* Some variables declared global, see "Global Variables" section */  /* This array holds the averaged percentages for all CPU on this machine.  Thus it's declared */  /* static so we can reuse it if it's new enough. */  static float cpu_perc_avg[CPU_STATES+1];  /* From time.h, get seconds since start of UNIX time... */  time_t timestamp_new;  static time_t timestamp_old_2;    /* From sys/sysinfo.h: */  /* Structure which can hold all the CPU info kstat provides */  cpu_stat_t cs;    /* Counters */  int cpu_slot = 0;  int cpu_num = 0;    /* Variables end here */    /* Get time */  time(&timestamp_new);    /* If we have just gotten the data, return the values from last run (skip if) */  /* This happens on a snmpwalk request.  No need to read the kstat again */  /* if we just did it less than a second ago */  /* +1 b/c we sleep(1) */    if (timestamp_new > (timestamp_old_2 + 1))    {            /* Update timer */      timestamp_old_2 = timestamp_new;            /* Look thru all the cpu slots on the machine whether they holds a CPU */      /* and if so, get the data from that CPU */      /* Important: num_cpu might be 12.  Then cpu_num is from 0 to 11, not 1 to 12 ! */      for (cpu_slot=0;cpu_num<num_cpu;cpu_slot++)	{ 	  /* If ksp is NULL we don't have a CPU :) */	  /* For MP machines: We hit an empty CPU board, trying next one... */	  if ((ksp = kstat_lookup(kstat_fd, string_cpu_stat, cpu_slot, NULL)) != NULL)	    {	      /* Yeah, we found a CPU. */	      /* Read data from kstat into cs structure */	      /* kc is the control structure, ksp the kstat we are reading */	      /* and cs the buffer we are writing to. */	      /* Memory allocation is done automagically by the kstat library. */	      	      if (kstat_read(kstat_fd, ksp, &cs) == -1)		{		  snmp_log(LOG_ERR, "vmstat_solaris2 (getCPU): error getting cs.\n");		  return(-1);		}	      	      /* CPU_STATES defined in sys/sysinfo.h */	      	      for (i=0 ; i < CPU_STATES ; i++)		{		  cpu_state_old[cpu_num][i] = cs.cpu_sysinfo.cpu[i];		}	      	      /* Counter for number of CPUs found. */	      /* cpu_slot might go from 0 to 15 (available CPU slots) while */	      /* cpu_num keeps track about actual number of CPUs read. */	      cpu_num++;	      	    } /* end else */	} /* end for */            /* Trying not to destroy the probed object with the probe... */      /* 1 sec delay between getting values from cs structure. */      sleep(1);      /* Look thru all the cpu slots on the machine whether it holds a CPU */      /* Important: num_cpu might be 12.  Then cpu_num is from 0 to 11, not 1 to 12 ! */      /* This is the same loop like above before the sleep(1).  Could be improved to */      /* cache the CPU slots that hold a CPU or similar */             /* We have to start over again */      cpu_num = 0;            /* Look thru all the cpu slots on the machine whether they holds a CPU */      /* and if so, get the data from that CPU */      /* Important: num_cpu might be 12.  Then cpu_num is from 0 to 11, not 1 to 12 ! */      for (cpu_slot=0;cpu_num<num_cpu;cpu_slot++)	{         	  /* If ksp is NULL we don't have a CPU :) */	  /* For MP machines: We hit an empty CPU board, trying next one... */	  if ((ksp = kstat_lookup(kstat_fd, string_cpu_stat, cpu_slot, NULL)) != NULL)	    {	      /* Yeah, we found a CPU. */	      /* Read data from kstat into cs structure */	      /* kc is the control structure, ksp the kstat we are reading */	      /* and cs the buffer we are writing to. */	      /* Memory allocation is done automagically by the kstat library. */	      	      /* Update cs structure with new kstat values after we are awake again. */	      	      if (kstat_read(kstat_fd, ksp, &cs) == -1)		{		  snmp_log(LOG_ERR, "vmstat_solaris2 (getCPU): error getting cs.\n");		  return(-1);		}	       	      /* CPU_STATES defined in sys/sysinfo.h */	      /* Get new samples after waiting for counters to increments */	      /* thru system activity. */	      /* Reset CPU activity counter */	      cpu_sum[cpu_num] = 0;	      	      	      /* Get new CPU data */	      for (i=0 ; i < CPU_STATES ; i++)		{		  cpu_state[cpu_num][i] = cs.cpu_sysinfo.cpu[i] - cpu_state_old[cpu_num][i];		  cpu_sum[cpu_num] += cpu_state[cpu_num][i];		}	      	      /* Calculate percentage values for CPU utilisation */	      for (i=0 ; i < CPU_STATES ; i++)		{		  /* Cast from ulong to float */		  cpu_perc[cpu_num][i]= (((float) cpu_state[cpu_num][i] / cpu_sum[cpu_num]) * 100);		}	      	      /* MIB wants CPU_SYSTEM which is CPU_KERNEL + CPU_WAIT */	      cpu_perc[cpu_num][CPU_SYSTEM] = cpu_perc[cpu_num][CPU_KERNEL] + cpu_perc[cpu_num][CPU_WAIT];	      	      /* Increment CPUs found count */	      cpu_num++;	      	    } /* end else */	} /* end for */      /* Calculate avarages here FIXME */      /* First sum up all values */      /* Calculate percentage values for CPU utilisation */      for (i=0 ; i < CPU_STATES ; i++)	{	  /* Reset counter */	  cpu_perc_avg[i] = 0;	  for (cpu_num=0;cpu_num<num_cpu;cpu_num++)	    {	      cpu_perc_avg[i] += cpu_perc[cpu_num][i];	    } /* end for */	  cpu_perc_avg[i] /= (float) num_cpu;	} /* end for */            cpu_perc_avg[CPU_SYSTEM] = cpu_perc_avg[CPU_KERNEL] + cpu_perc_avg[CPU_WAIT];          } /* end if (timestamp_new > (timestamp_old_2 + 1)) */    /* Returns the requested percentage value, dropping fractions b/c casting to long */  return((long) cpu_perc_avg[state]);  } /* end function getCPU *//* *var_extensible_vmstat starts here *//* The guts of the module, this routine gets called to service a request */unsigned char *var_extensible_vmstat(struct variable *vp,                                     oid *name,                                     size_t *length,                                     int exact,                                     size_t *var_len,                                     WriteMethod **write_method){  static long long_ret;  static char errmsg[300];    long_ret = 0;  /* set to 0 as default */  /* generic check whether the options passed make sense and whether the */  /* right variable is requested */  if (header_generic(vp,name,length,exact,var_len, write_method) != MATCH_SUCCEEDED)    {      return(NULL);    }  /* The function that actually returns s.th. */  switch (vp->magic) {  case MIBINDEX:    long_ret = 1;    return((u_char *) (&long_ret));  case ERRORNAME:    /* dummy name */    sprintf(errmsg,"systemStats");    *var_len = strlen(errmsg);    return((u_char *) (errmsg));  case SWAPIN:    long_ret = getMisc(SWAPIN);    return((u_char *) (&long_ret));  case SWAPOUT:    long_ret = getMisc(SWAPOUT);    return((u_char *) (&long_ret));  case IOSENT:    long_ret = getMisc(IOSENT);    return((u_char *) (&long_ret));  case IORECEIVE:    long_ret = getMisc(IORECEIVE);    return((u_char *) (&long_ret));  case SYSINTERRUPTS:    long_ret = getMisc(SYSINTERRUPTS);    return((u_char *) (&long_ret));  case SYSCONTEXT:    long_ret = getMisc(SYSCONTEXT);    return((u_char *) (&long_ret));  case CPUUSER:    long_ret = getCPU(CPU_USER);    return((u_char *) (&long_ret));  case CPUSYSTEM:    long_ret = getCPU(CPU_SYSTEM);    return((u_char *) (&long_ret));  case CPUIDLE:    long_ret = getCPU(CPU_IDLE);    return((u_char *) (&long_ret));    /* reserved for future use */    /*      case ERRORFLAG:      return((u_char *) (&long_ret));      case ERRORMSG:      return((u_char *) (&long_ret));      */      default:    snmp_log(LOG_ERR,"vmstat_solaris2: Error in request, no match found.\n");  }  return(NULL);} /* *var_extensible_vmstat ends here *//* Functions end here *//* Program ends here */

⌨️ 快捷键说明

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