| 1 |
/* |
| 2 |
* swrun_procfs_linux.c: |
| 3 |
* hrSWRunTable data access: |
| 4 |
* /proc/{pid}/status interface - Linux |
| 5 |
*/ |
| 6 |
#include <net-snmp/net-snmp-config.h> |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <ctype.h> |
| 10 |
#ifdef HAVE_STDLIB_H |
| 11 |
#include <stdlib.h> |
| 12 |
#endif |
| 13 |
#ifdef HAVE_UNISTD_H |
| 14 |
#include <unistd.h> |
| 15 |
#endif |
| 16 |
#ifdef HAVE_SYS_TYPES_H |
| 17 |
#include <sys/types.h> |
| 18 |
#endif |
| 19 |
|
| 20 |
#ifdef HAVE_DIRENT_H |
| 21 |
#include <dirent.h> |
| 22 |
#endif |
| 23 |
#ifdef HAVE_LINUX_TASKS_H |
| 24 |
#include <linux/tasks.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <net-snmp/net-snmp-includes.h> |
| 28 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 29 |
#include <net-snmp/library/container.h> |
| 30 |
#include <net-snmp/library/snmp_debug.h> |
| 31 |
#include <net-snmp/data_access/swrun.h> |
| 32 |
#include "swrun_private.h" |
| 33 |
|
| 34 |
static long pagesize; |
| 35 |
static long sc_clk_tck; |
| 36 |
|
| 37 |
/* --------------------------------------------------------------------- |
| 38 |
*/ |
| 39 |
void |
| 40 |
netsnmp_arch_swrun_init(void) |
| 41 |
{ |
| 42 |
#ifdef HAVE_LINUX_TASKS_H |
| 43 |
extern int _swrun_max = NR_TASKS; /* from <linux/tasks.h> */ |
| 44 |
#endif |
| 45 |
|
| 46 |
pagesize = getpagesize(); |
| 47 |
sc_clk_tck = sysconf(_SC_CLK_TCK); |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
/* --------------------------------------------------------------------- |
| 52 |
*/ |
| 53 |
int |
| 54 |
netsnmp_arch_swrun_container_load( netsnmp_container *container, u_int flags) |
| 55 |
{ |
| 56 |
DIR *procdir = NULL; |
| 57 |
struct dirent *procentry_p; |
| 58 |
FILE *fp; |
| 59 |
int pid, i, ret; |
| 60 |
unsigned long long cpu; |
| 61 |
char buf[BUFSIZ], buf2[BUFSIZ], *cp, *cp1; |
| 62 |
netsnmp_swrun_entry *entry; |
| 63 |
|
| 64 |
procdir = opendir("/proc"); |
| 65 |
if ( NULL == procdir ) { |
| 66 |
snmp_log( LOG_ERR, "Failed to open /proc" ); |
| 67 |
return -1; |
| 68 |
} |
| 69 |
|
| 70 |
/* |
| 71 |
* Walk through the list of processes in the /proc tree |
| 72 |
*/ |
| 73 |
while ( NULL != (procentry_p = readdir( procdir ))) { |
| 74 |
pid = atoi( procentry_p->d_name ); |
| 75 |
if ( 0 == pid ) |
| 76 |
continue; /* Presumably '.' or '..' */ |
| 77 |
|
| 78 |
entry = netsnmp_swrun_entry_create(pid); |
| 79 |
if (NULL == entry) |
| 80 |
continue; /* error already logged by function */ |
| 81 |
|
| 82 |
/* |
| 83 |
* Now extract the interesting information |
| 84 |
* from the various /proc{PID}/ interface files |
| 85 |
*/ |
| 86 |
|
| 87 |
/* |
| 88 |
* Name: process name |
| 89 |
*/ |
| 90 |
snprintf( buf2, BUFSIZ, "/proc/%d/status", pid ); |
| 91 |
fp = fopen( buf2, "r" ); |
| 92 |
if (!fp) { |
| 93 |
netsnmp_swrun_entry_free(entry); |
| 94 |
continue; /* file (process) probably went away */ |
| 95 |
} |
| 96 |
memset(buf, 0, sizeof(buf)); |
| 97 |
if (fgets( buf, BUFSIZ-1, fp ) == NULL) { |
| 98 |
fclose(fp); |
| 99 |
netsnmp_swrun_entry_free(entry); |
| 100 |
continue; |
| 101 |
} |
| 102 |
fclose(fp); |
| 103 |
|
| 104 |
for ( cp = buf; *cp != ':'; cp++ ) |
| 105 |
; |
| 106 |
while (isspace(*(++cp))) /* Skip ':' and following spaces */ |
| 107 |
; |
| 108 |
entry->hrSWRunName_len = snprintf(entry->hrSWRunName, |
| 109 |
sizeof(entry->hrSWRunName)-1, "%s", cp); |
| 110 |
if ( '\n' == entry->hrSWRunName[ entry->hrSWRunName_len-1 ]) { |
| 111 |
entry->hrSWRunName[ entry->hrSWRunName_len-1 ] = '\0'; |
| 112 |
entry->hrSWRunName_len--; /* Stamp on trailing newline */ |
| 113 |
} |
| 114 |
|
| 115 |
/* |
| 116 |
* Command Line: |
| 117 |
* argv[0] '\0' argv[1] '\0' .... |
| 118 |
*/ |
| 119 |
snprintf( buf2, BUFSIZ, "/proc/%d/cmdline", pid ); |
| 120 |
fp = fopen( buf2, "r" ); |
| 121 |
if (!fp) { |
| 122 |
netsnmp_swrun_entry_free(entry); |
| 123 |
continue; /* file (process) probably went away */ |
| 124 |
} |
| 125 |
entry->hrSWRunType = HRSWRUNTYPE_APPLICATION; |
| 126 |
memset(buf, 0, sizeof(buf)); |
| 127 |
cp = fgets( buf, BUFSIZ-1, fp ); |
| 128 |
fclose(fp); |
| 129 |
if (cp != NULL) { |
| 130 |
/* |
| 131 |
* argv[0] is hrSWRunPath |
| 132 |
*/ |
| 133 |
ret = snprintf(entry->hrSWRunPath, sizeof(entry->hrSWRunPath), |
| 134 |
"%s", buf); |
| 135 |
|
| 136 |
if (ret < sizeof(entry->hrSWRunPath)) |
| 137 |
entry->hrSWRunPath_len = ret; |
| 138 |
else |
| 139 |
entry->hrSWRunPath_len = sizeof(entry->hrSWRunPath) - 1; |
| 140 |
|
| 141 |
/* |
| 142 |
* Stitch together argv[1..] to construct hrSWRunParameters |
| 143 |
*/ |
| 144 |
for (cp = buf + ret; ! (*cp == '\0' && *(cp + 1) == '\0'); cp++) |
| 145 |
if (*cp == '\0') |
| 146 |
*cp = ' '; |
| 147 |
|
| 148 |
entry->hrSWRunParameters_len |
| 149 |
= sprintf(entry->hrSWRunParameters, "%.*s", |
| 150 |
(int)sizeof(entry->hrSWRunParameters) - 1, |
| 151 |
buf + ret + 1); |
| 152 |
} else { |
| 153 |
/* empty /proc/PID/cmdline, it's probably a kernel thread */ |
| 154 |
entry->hrSWRunPath_len = 0; |
| 155 |
entry->hrSWRunParameters_len = 0; |
| 156 |
entry->hrSWRunType = HRSWRUNTYPE_OPERATINGSYSTEM; |
| 157 |
} |
| 158 |
|
| 159 |
/* |
| 160 |
* {xxx} {xxx} STATUS {xxx}*10 UTIME STIME {xxx}*8 RSS |
| 161 |
*/ |
| 162 |
snprintf( buf, BUFSIZ, "/proc/%d/stat", pid ); |
| 163 |
fp = fopen( buf, "r" ); |
| 164 |
if (!fp) { |
| 165 |
netsnmp_swrun_entry_free(entry); |
| 166 |
continue; /* file (process) probably went away */ |
| 167 |
} |
| 168 |
if (fgets( buf, BUFSIZ-1, fp ) == NULL) { |
| 169 |
fclose(fp); |
| 170 |
netsnmp_swrun_entry_free(entry); |
| 171 |
continue; |
| 172 |
} |
| 173 |
fclose(fp); |
| 174 |
|
| 175 |
cp = buf; |
| 176 |
while ( ' ' != *(cp++)) /* Skip first field */ |
| 177 |
; |
| 178 |
cp1 = cp; /* Skip second field */ |
| 179 |
while (*cp1) { |
| 180 |
if (*cp1 == ')') cp = cp1; |
| 181 |
cp1++; |
| 182 |
} |
| 183 |
cp += 2; |
| 184 |
|
| 185 |
switch (*cp) { |
| 186 |
case 'R': entry->hrSWRunStatus = HRSWRUNSTATUS_RUNNING; |
| 187 |
break; |
| 188 |
case 'S': entry->hrSWRunStatus = HRSWRUNSTATUS_RUNNABLE; |
| 189 |
break; |
| 190 |
case 'D': |
| 191 |
case 'T': entry->hrSWRunStatus = HRSWRUNSTATUS_NOTRUNNABLE; |
| 192 |
break; |
| 193 |
case 'Z': |
| 194 |
default: entry->hrSWRunStatus = HRSWRUNSTATUS_INVALID; |
| 195 |
break; |
| 196 |
} |
| 197 |
for (i=11; i; i--) { /* Skip STATUS + 10 fields */ |
| 198 |
while (' ' != *(++cp)) |
| 199 |
; |
| 200 |
cp++; |
| 201 |
} |
| 202 |
cpu = atol( cp ); /* utime */ |
| 203 |
while ( ' ' != *(++cp)) |
| 204 |
; |
| 205 |
cpu += atol( cp ); /* +stime */ |
| 206 |
entry->hrSWRunPerfCPU = cpu * 100 / sc_clk_tck; |
| 207 |
|
| 208 |
for (i=9; i; i--) { /* Skip stime + 8 fields */ |
| 209 |
while (' ' != *(++cp)) |
| 210 |
; |
| 211 |
cp++; |
| 212 |
} |
| 213 |
entry->hrSWRunPerfMem = atol( cp ); /* rss */ |
| 214 |
entry->hrSWRunPerfMem *= (pagesize/1024); /* in kB */ |
| 215 |
CONTAINER_INSERT(container, entry); |
| 216 |
} |
| 217 |
closedir( procdir ); |
| 218 |
|
| 219 |
DEBUGMSGTL(("swrun:load:arch"," loaded %" NETSNMP_PRIz "d entries\n", |
| 220 |
CONTAINER_SIZE(container))); |
| 221 |
|
| 222 |
return 0; |
| 223 |
} |