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

📄 hr_swrun.c

📁 非常不错的网管开发包
💻 C
📖 第 1 页 / 共 2 页
字号:
	    else		string[0] = '\0';#elif defined(solaris2)#ifdef _SLASH_PROC_METHOD_	    cp = strchr(proc_buf->pr_psargs, ' ');	    if (cp) strcpy(string, cp+1);	    else string[0] = 0;#else	    cp = proc_buf->p_user.u_psargs;	    while (*cp && *cp != ' ') cp++;	    if (*cp == ' ') cp++;	    strcpy (string, cp);#endif#elif HAVE_KVM_GETPROCS	    string[0] = 0;	    argv = kvm_getargv(kd, proc_table+LowProcIndex, sizeof(string));	    if (argv) argv++;	    while (argv && *argv) {		if (string[0] != 0) strcat(string, " ");		strcat(string, *argv);		argv++;	    }#elif defined(linux)	    sprintf( string, "/proc/%d/cmdline", pid );	    if ((fp = fopen( string, "r")) == NULL) return NULL;	    memset( buf, 0, sizeof(buf) );	    fgets( buf, sizeof(buf), fp );   /* argv[0] '\0' argv[1] '\0' .... */		/* Skip over argv[0] */	    cp = buf;	    while ( *cp )		++cp;	    ++cp;		/* Now join together separate arguments. */	    while ( 1 ) {	        while ( *cp )		    ++cp;		if ( *(cp+1) == '\0' )		    break;	/* '\0''\0' => End of command line */		*cp = ' ';	    }		    cp = buf;	    while ( *cp )		++cp;	    ++cp;	    strcpy( string, cp );            fclose(fp);#else#if NO_DUMMY_VALUES	    return NULL;#endif	    sprintf(string, "-h -q -v");#endif	    *var_len = strlen(string);	    return (u_char *) string;	case HRSWRUN_TYPE:#ifdef PID_MAXSYS	    if ( pid < PID_MAXSYS )		long_return = 2;	/* operatingSystem */	    else#endif		long_return = 4;	/* application */	    return (u_char *)&long_return;	case HRSWRUN_STATUS:#ifndef linux#ifdef hpux10	    switch ( proc_table[LowProcIndex].pst_stat ) {		case PS_STOP:	    		long_return = 3;	/* notRunnable */			break;		case PS_SLEEP:	    		long_return = 2;	/* runnable */			break;		case PS_RUN:	    		long_return = 1;	/* running */			break;		case PS_ZOMBIE:		case PS_IDLE:		case PS_OTHER:		default:	    		long_return = 4;	/* invalid */			break;	    }#else#if HAVE_KVM_GETPROCS	    switch ( proc_table[LowProcIndex].kp_proc.p_stat ) {#elif defined(solaris2)#if _SLASH_PROC_METHOD_	    switch (proc_buf->pr_lwp.pr_state) {#else	    switch ( proc_buf->p_stat ) {#endif#else	    switch ( proc_table[LowProcIndex].p_stat ) {#endif		case SSTOP:	    		long_return = 3;	/* notRunnable */			break;		case 0:		case SSLEEP:#ifdef SWAIT		case SWAIT:#endif	    		long_return = 2;	/* runnable */			break;		case SRUN:#ifdef SONPROC		case SONPROC:#endif	    		long_return = 1;	/* running */			break;		case SIDL:		case SZOMB:		default:	    		long_return = 4;	/* invalid */			break;	    }#endif#else	    sprintf( string, "/proc/%d/stat", pid );	    if ((fp = fopen( string, "r")) == NULL) return NULL;	    fgets( buf, sizeof(buf), fp );	    cp = buf;	    for ( i = 0 ; i < 2 ; ++i ) {	/* skip two fields */		while ( *cp != ' ')		    ++cp;		++cp;	    }	    switch ( *cp ) {		case 'R':	    		long_return = 1;	/* running */			break;		case 'S':	    		long_return = 2;	/* runnable */			break;		case 'D':		case 'T':	    		long_return = 3;	/* notRunnable */			break;		case 'Z':		default:	    		long_return = 4;	/* invalid */			break;	    }            fclose(fp);#endif	    return (u_char *)&long_return;	case HRSWRUNPERF_CPU:#ifdef HAVE_SYS_PSTAT_H	    long_return = proc_buf.pst_cptickstotal;				/*				 * Not convinced this is right, but....				 */#elif defined(solaris2)#if _SLASH_PROC_METHOD_	    long_return = proc_buf->pr_time.tv_sec * 100 +			  proc_buf->pr_time.tv_nsec/10000000;#else	    long_return = proc_buf->p_utime*100 +	    		  proc_buf->p_stime*100;#endif#elif HAVE_KVM_GETPROCS	    long_return = proc_table[LowProcIndex].kp_proc.p_uticks +	    		  proc_table[LowProcIndex].kp_proc.p_sticks +	    		  proc_table[LowProcIndex].kp_proc.p_iticks;#elif defined(linux)	    sprintf( string, "/proc/%d/stat", pid );	    if ((fp = fopen( string, "r")) == NULL) return NULL;	    fgets( buf, sizeof(buf), fp );	    cp = buf;	    for ( i = 0 ; i < 13 ; ++i ) {	/* skip 13 fields */		while ( *cp != ' ')		    ++cp;		++cp;	    }	    long_return = atoi( cp );		/* utime */	    while ( *cp != ' ' )		++cp;	    ++cp;	    long_return += atoi( cp );		/* + stime */            fclose(fp);#elif defined(sunos4)	    long_return = proc_table[LowProcIndex].p_time;#else	    long_return = proc_table[LowProcIndex].p_utime.tv_sec*100 +			  proc_table[LowProcIndex].p_utime.tv_usec/10000 +	    		  proc_table[LowProcIndex].p_stime.tv_sec*100 +			  proc_table[LowProcIndex].p_stime.tv_usec/10000;#endif	    return (u_char *)&long_return;	case HRSWRUNPERF_MEM:#ifdef HAVE_SYS_PSTAT_H	    long_return = (proc_buf.pst_rssize << PGSHIFT)/1024;#elif defined(solaris2)#if _SLASH_PROC_METHOD_	    long_return = proc_buf->pr_rssize;#else	    long_return = proc_buf->p_swrss;#endif#elif HAVE_KVM_GETPROCS	    long_return = proc_table[LowProcIndex].kp_eproc.e_xrssize;#elif defined(linux)	    sprintf( string, "/proc/%d/stat", pid );	    if ((fp = fopen( string, "r")) == NULL) return NULL;	    fgets( buf, sizeof(buf), fp );	    cp = buf;	    for ( i = 0 ; i < 23 ; ++i ) {	/* skip 23 fields */		while ( *cp != ' ')		    ++cp;	        ++cp;	    }	    long_return = atoi( cp ) * (getpagesize()/1024);		/* rss */            fclose(fp);#else#if NO_DUMMY_VALUES	    return NULL;#endif	    long_return = 16*1024;	/* XXX - 16M! */#endif	    return (u_char *)&long_return;	default:	    DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrswrun\n", vp->magic));    }    return NULL;}	/*********************	 *	 *  Internal implementation functions	 *	 *********************/#ifndef linuxstatic int nproc;voidInit_HR_SWRun (void){    size_t bytes;    static time_t iwhen = 0;    time_t now;    time(&now);    if (now == iwhen) {	current_proc_entry = 0;	return;    }    iwhen = now;#if defined(hpux10)    pstat_getdynamic( &pst_dyn, sizeof( struct pst_dynamic ),			1, 0 );    nproc = pst_dyn.psd_activeprocs ;    bytes = nproc*sizeof(struct pst_status);    if ((proc_table=(struct pst_status *) realloc(proc_table, bytes)) == NULL ) {	current_proc_entry = nproc+1;	return;    }    pstat_getproc( proc_table, sizeof( struct pst_status ),			nproc, 0 );#elif defined(solaris2)    if (!getKstatInt("unix", "system_misc", "nproc", &nproc)) {    	current_proc_entry = nproc+1;	return;    }    bytes = nproc*sizeof(int);    if ((proc_table=(int *) realloc(proc_table, bytes)) == NULL ) {	current_proc_entry = nproc+1;	return;    }    {	DIR *f;	struct dirent *dp;#if _SLASH_PROC_METHOD_ == 0	if (kd == NULL)	    kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "hr_swrun");	if (kd == NULL) {	    current_proc_entry = nproc+1;	    return;	}#endif	f = opendir("/proc");	current_proc_entry = 0;	while ((dp = readdir(f)) != NULL && current_proc_entry < nproc)	    if (dp->d_name[0] != '.')		proc_table[current_proc_entry++] = atoi(dp->d_name);	closedir(f);    }#elif HAVE_KVM_GETPROCS    {	if (kd == NULL)	    kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_getprocs");	proc_table = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc);    }#else    current_proc_entry = 1;#ifndef bsdi2    nproc = 0;    if (auto_nlist(NPROC_SYMBOL, (char *)&nproc, sizeof(int)) == 0) {        snmp_log_perror("Init_HR_SWRun-auto_nlist NPROC");        return;    }#endif    bytes = nproc*sizeof(struct proc);    if (proc_table) free((char *)proc_table);    if ((proc_table=(struct proc *) malloc(bytes)) == NULL ) {        nproc = 0;        snmp_log_perror("Init_HR_SWRun-malloc");        return;    }    {   int proc_table_base;        if (auto_nlist(PROC_SYMBOL, (char *)&proc_table_base, sizeof(proc_table_base)) == 0) {            nproc = 0;            snmp_log_perror("Init_HR_SWRun-auto_nlist PROC");            return;        }        if (klookup( proc_table_base, (char *)proc_table, bytes) == 0) {            nproc = 0;            snmp_log_perror("Init_HR_SWRun-klookup");            return;        }    }#endif    current_proc_entry = 0;}intGet_Next_HR_SWRun (void){    while ( current_proc_entry < nproc ) {#ifdef hpux10	return proc_table[current_proc_entry++].pst_pid;#elif defined(solaris2)	return proc_table[current_proc_entry++];#elif HAVE_KVM_GETPROCS	if ( proc_table[current_proc_entry].kp_proc.p_stat != 0 )	    return proc_table[current_proc_entry++].kp_proc.p_pid;#else	if ( proc_table[current_proc_entry].p_stat != 0 )	    return proc_table[current_proc_entry++].p_pid;	else	    ++current_proc_entry;#endif    }    End_HR_SWRun();    return -1;}voidEnd_HR_SWRun (void){    current_proc_entry = nproc+1;}#else /* linux */DIR *procdir = NULL;struct dirent *procentry_p;voidInit_HR_SWRun (void){    if ( procdir != NULL )        closedir( procdir );    procdir = opendir("/proc");}intGet_Next_HR_SWRun (void){   int pid;   procentry_p = readdir( procdir );   if ( procentry_p == NULL )	return -1;   pid = atoi(procentry_p->d_name);   if ( pid == 0 )	return( Get_Next_HR_SWRun());   return pid;}voidEnd_HR_SWRun (void){   if (procdir) closedir( procdir );   procdir = NULL;}#endifint count_processes (void){#ifndef linux    int i;#endif    int total=0;    Init_HR_SWRun();#if defined(hpux10) || HAVE_KVM_GETPROCS || defined(solaris2)    total = nproc;#else#ifndef linux    for ( i = 0 ; i<nproc ; ++i ) {	if ( proc_table[i].p_stat != 0 )#else    while ( Get_Next_HR_SWRun() != -1  ) {#endif	    ++total;    }#endif /* !hpux10 */    End_HR_SWRun();    return total;}

⌨️ 快捷键说明

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