void getload(load_list *loadavgs) { sgt_cookie_t cookie; SGT_COOKIE_INIT(&cookie); SGT_COOKIE_SET_KSYM(&cookie, "avenrun"); /* since IRIX uses an integer data type for this stuff, which is * 1024 times the floating-point equiv, we don't need to do any * conversions, because that's what load_list uses (since ttyload * was originally written for IRIX in the first place). */ if(sysget(SGT_KSYM, (char*)loadavgs, 3 * sizeof(load_t), SGT_READ | SGT_SUM, &cookie) != (3 * sizeof(load_t))) { perror("Couldn't read load data, sysget failed"); exit(1); } }
/* * try to get load average * Inputs: pointer to array of doubles, number of elements in array * Returns: 0=array has values, -1=error occurred. */ int try_getloadavg(double *r_ave, size_t s_ave) { #ifndef HAVE_GETLOADAVG #ifdef HAVE_SYS_FIXPOINT_H fix favenrun[3]; #endif #if (defined(ultrix) || defined(sun) || defined(__alpha) || defined(dynix)) int i; #if (defined(sun) || defined(__alpha) || defined(dynix)) long favenrun[3]; if (s_ave > 3) /* bounds check */ return (-1); #define FIX_TO_DBL(_IN) (((double) _IN)/((double) FSCALE)) #endif #endif #if defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7) perfstat_cpu_total_t cs; #endif #if defined(hpux10) || defined(hpux11) struct pst_dynamic pst_buf; #endif #ifdef irix6 int i, favenrun[3]; sgt_cookie_t cookie; #endif #endif /* !HAVE_GETLOADAVG */ #ifdef HAVE_GETLOADAVG if (getloadavg(r_ave, s_ave) == -1) return (-1); #elif defined(linux) { FILE *in = fopen("/proc/loadavg", "r"); if (!in) { NETSNMP_LOGONCE((LOG_ERR, "snmpd: cannot open /proc/loadavg\n")); return (-1); } fscanf(in, "%lf %lf %lf", r_ave, (r_ave + 1), (r_ave + 2)); fclose(in); } #elif (defined(ultrix) || defined(sun) || defined(__alpha) || defined(dynix)) if (auto_nlist(LOADAVE_SYMBOL, (char *) favenrun, sizeof(favenrun)) == 0) return (-1); for (i = 0; i < s_ave; i++) *(r_ave + i) = FIX_TO_DBL(favenrun[i]); #elif defined(hpux10) || defined(hpux11) if (pstat_getdynamic(&pst_buf, sizeof(struct pst_dynamic), 1, 0) < 0) return(-1); r_ave[0] = pst_buf.psd_avg_1_min; r_ave[1] = pst_buf.psd_avg_5_min; r_ave[2] = pst_buf.psd_avg_15_min; #elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7) if(perfstat_cpu_total((perfstat_id_t *)NULL, &cs, sizeof(perfstat_cpu_total_t), 1) > 0) { r_ave[0] = cs.loadavg[0] / 65536.0; r_ave[1] = cs.loadavg[1] / 65536.0; r_ave[2] = cs.loadavg[2] / 65536.0; } #elif defined(irix6) SGT_COOKIE_INIT(&cookie); SGT_COOKIE_SET_KSYM(&cookie, "avenrun"); sysget(SGT_KSYM, (char*)favenrun, sizeof(favenrun), SGT_READ, &cookie); for (i = 0; i < s_ave; i++) r_ave[i] = favenrun[i] / 1000.0; DEBUGMSGTL(("ucd-snmp/loadave", "irix6: %d %d %d\n", favenrun[0], favenrun[1], favenrun[2])); #elif !defined(cygwin) #if defined(NETSNMP_CAN_USE_NLIST) && defined(LOADAVE_SYMBOL) if (auto_nlist(LOADAVE_SYMBOL, (char *) r_ave, sizeof(double) * s_ave) == 0) #endif return (-1); #endif /* * XXX * To calculate this, we need to compare * successive values of the kernel array * '_cp_times', and calculate the resulting * percentage changes. * This calculation needs to be performed * regularly - perhaps as a background process. * * See the source to 'top' for full details. * * The linux SNMP HostRes implementation * uses 'avenrun[0]*100' as an approximation. * This is less than accurate, but has the * advantage of being simple to implement! * * I'm also assuming a single processor */ return 0; }
void get_loadavg (uint16_t *loadavg) { #if defined(__LINUX) /* __LINUX */ FILE *f_loadavg; float a,b,c; if ((f_loadavg = fopen("/proc/loadavg","r")) == NULL) { perror ("get_loadavg: fopen"); exit (1); } fscanf (f_loadavg,"%f %f %f",&a,&b,&c); loadavg[0] = (uint16_t) (a * 100); loadavg[1] = (uint16_t) (b * 100); loadavg[2] = (uint16_t) (c * 100); fclose (f_loadavg); #elif defined(__IRIX) /* __IRIX */ sgt_cookie_t cookie; uint32_t tla[3]; SGT_COOKIE_SET_KSYM (&cookie,KSYM_AVENRUN); if (sysget (SGT_KSYM,(char *)tla,sizeof(uint32_t)*3,SGT_READ,&cookie) == -1) { FILE *uptime; char buf[BUFFERLEN]; char *fd; /* first digit */ float f1,f2,f3; if ((uptime = popen ("/usr/bsd/uptime","r")) == NULL) { fprintf (stderr,"Warning: Problems executing '/usr/bsd/uptime'\n"); tla[0] = tla[1] = tla[2] = 0; } while (fgets (buf,BUFFERLEN,uptime) != NULL) { if ((fd = strstr(buf,"average:")) != NULL) { while (!isdigit((int)*fd)) fd++; if (sscanf (fd,"%f, %f, %f",&f1,&f2,&f3) != 3) { log_auto (L_WARNING,"Problems on get_loadavg\n"); f1 = f2 = f3 = 0; } tla[0] = f1 * 1000; tla[1] = f2 * 1000; tla[2] = f3 * 1000; } } pclose (uptime); } loadavg[0] = (uint16_t) (tla[0]/10); loadavg[1] = (uint16_t) (tla[1]/10); loadavg[2] = (uint16_t) (tla[2]/10); #elif defined(__OSX) || defined(__FREEBSD) double fls[3]; if (getloadavg(fls,3)<3) { log_auto (L_WARNING,"Problems on getloadavg\n"); fls[0]=fls[1]=fls[2]=0.0; } loadavg[0] = (uint16_t) (fls[0] * 100); loadavg[1] = (uint16_t) (fls[1] * 100); loadavg[2] = (uint16_t) (fls[2] * 100); #elif defined(__CYGWIN) /* __CYGWIN */ FILE *f_loadavg; float a,b,c; if ((f_loadavg = fopen("/proc/loadavg","r")) == NULL) { perror ("get_loadavg: fopen"); exit (1); } fscanf (f_loadavg,"%f %f %f",&a,&b,&c); loadavg[0] = a * 100; loadavg[1] = b * 100; loadavg[2] = c * 100; fclose (f_loadavg); #else # error You need to define the OS, or OS defined not supported #endif }