| 1 |
/* |
| 2 |
* swrun_kvm_proc.c: |
| 3 |
* hrSWRunTable data access: |
| 4 |
* kvm_getproc() interface - Solaris |
| 5 |
*/ |
| 6 |
#include <net-snmp/net-snmp-config.h> |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#ifdef HAVE_STDLIB_H |
| 10 |
#include <stdlib.h> |
| 11 |
#endif |
| 12 |
#ifdef HAVE_UNISTD_H |
| 13 |
#include <unistd.h> |
| 14 |
#endif |
| 15 |
#ifdef HAVE_SYS_TYPES_H |
| 16 |
#include <sys/types.h> |
| 17 |
#endif |
| 18 |
#ifdef HAVE_SYS_PARAM_H |
| 19 |
#include <sys/param.h> |
| 20 |
#endif |
| 21 |
#ifdef HAVE_SYS_TIME_H |
| 22 |
#include <sys/time.h> |
| 23 |
#endif |
| 24 |
#ifdef HAVE_SYS_USER_H |
| 25 |
#define _KMEMUSER |
| 26 |
#include <sys/user.h> |
| 27 |
#endif |
| 28 |
#ifdef HAVE_SYS_PROC_H |
| 29 |
#include <sys/proc.h> |
| 30 |
#endif |
| 31 |
/* XXX - should really be protected */ |
| 32 |
#include <sys/var.h> |
| 33 |
|
| 34 |
#ifdef HAVE_DIRENT_H |
| 35 |
#include <dirent.h> |
| 36 |
#endif |
| 37 |
#ifdef HAVE_KVM_H |
| 38 |
#include <kvm.h> |
| 39 |
#endif |
| 40 |
#ifdef HAVE_KSTAT_H |
| 41 |
#include <kstat.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#include <net-snmp/net-snmp-includes.h> |
| 45 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 46 |
#include <net-snmp/library/container.h> |
| 47 |
#include <net-snmp/library/snmp_debug.h> |
| 48 |
#include <net-snmp/data_access/swrun.h> |
| 49 |
#include "swrun_private.h" |
| 50 |
#include "kernel.h" |
| 51 |
#include "kernel_sunos5.h" |
| 52 |
|
| 53 |
/* --------------------------------------------------------------------- |
| 54 |
*/ |
| 55 |
void |
| 56 |
netsnmp_arch_swrun_init(void) |
| 57 |
{ |
| 58 |
extern int _swrun_max; |
| 59 |
kstat_ctl_t *ksc; |
| 60 |
kstat_t *ks; |
| 61 |
struct var v; |
| 62 |
|
| 63 |
if (NULL != (ksc = kstat_open())) { |
| 64 |
if ((NULL != (ks = kstat_lookup(ksc, "unix", 0, "var"))) && |
| 65 |
( -1 != kstat_read(ksc, ks, &v ))) { |
| 66 |
_swrun_max = v.v_proc; |
| 67 |
} |
| 68 |
kstat_close(ksc); |
| 69 |
} |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
/* --------------------------------------------------------------------- |
| 74 |
*/ |
| 75 |
int |
| 76 |
netsnmp_arch_swrun_container_load( netsnmp_container *container, u_int flags) |
| 77 |
{ |
| 78 |
DIR *procdir = NULL; |
| 79 |
struct dirent *procentry_p; |
| 80 |
struct proc *proc_buf; |
| 81 |
int pid, rc, i; |
| 82 |
char *cp; |
| 83 |
netsnmp_swrun_entry *entry; |
| 84 |
|
| 85 |
/* |
| 86 |
* Even if we're using kvm_proc() to retrieve information |
| 87 |
* about a particular process, we seem to use /proc to get |
| 88 |
* the initial list of processes to report on. |
| 89 |
*/ |
| 90 |
procdir = opendir("/proc"); |
| 91 |
if ( NULL == procdir ) { |
| 92 |
snmp_log( LOG_ERR, "Failed to open /proc" ); |
| 93 |
return -1; |
| 94 |
} |
| 95 |
|
| 96 |
/* |
| 97 |
* Walk through the list of processes in the /proc tree |
| 98 |
*/ |
| 99 |
while ( NULL != (procentry_p = readdir( procdir ))) { |
| 100 |
pid = atoi( procentry_p->d_name ); |
| 101 |
if ( 0 == pid ) |
| 102 |
continue; /* Presumably '.' or '..' */ |
| 103 |
|
| 104 |
entry = netsnmp_swrun_entry_create(pid); |
| 105 |
if (NULL == entry) |
| 106 |
continue; /* error already logged by function */ |
| 107 |
if (NULL == (proc_buf = kvm_getproc( kd, pid))) { |
| 108 |
netsnmp_swrun_entry_free(entry); |
| 109 |
continue; |
| 110 |
} |
| 111 |
rc = CONTAINER_INSERT(container, entry); |
| 112 |
|
| 113 |
entry->hrSWRunName_len = sprintf(entry->hrSWRunName, "%.*s", |
| 114 |
(int)sizeof(entry->hrSWRunName)-1, |
| 115 |
proc_buf->p_user.u_comm); |
| 116 |
/* |
| 117 |
* Split u_psargs into two: |
| 118 |
* argv[0] is hrSWRunPath |
| 119 |
* argv[1..] is hrSWRunParameters |
| 120 |
*/ |
| 121 |
cp = strchr(proc_buf->p_user.u_psargs, ' '); |
| 122 |
if (cp) |
| 123 |
*cp = '\0'; /* End of argv[0] */ |
| 124 |
entry->hrSWRunPath_len = sprintf(entry->hrSWRunPath, "%.*s", |
| 125 |
(int)sizeof(entry->hrSWRunPath)-1, |
| 126 |
proc_buf->p_user.u_psargs); |
| 127 |
if (cp) { |
| 128 |
entry->hrSWRunParameters_len = |
| 129 |
sprintf(entry->hrSWRunParameters, "%.*s", |
| 130 |
(int)sizeof(entry->hrSWRunParameters) - 1, cp + 1); |
| 131 |
*cp = ' '; /* Restore u_psargs value */ |
| 132 |
} |
| 133 |
|
| 134 |
/* |
| 135 |
* check for system processes |
| 136 |
*/ |
| 137 |
entry->hrSWRunType = (SSYS & proc_buf->p_flag) |
| 138 |
? 2 /* kernel process */ |
| 139 |
: 4 /* application */ |
| 140 |
; |
| 141 |
|
| 142 |
switch (proc_buf->p_stat) { |
| 143 |
case SRUN: |
| 144 |
case SONPROC: entry->hrSWRunStatus = HRSWRUNSTATUS_RUNNING; |
| 145 |
break; |
| 146 |
case SSLEEP: entry->hrSWRunStatus = HRSWRUNSTATUS_RUNNABLE; |
| 147 |
break; |
| 148 |
case SSTOP: entry->hrSWRunStatus = HRSWRUNSTATUS_NOTRUNNABLE; |
| 149 |
break; |
| 150 |
case SIDL: |
| 151 |
case SZOMB: |
| 152 |
default: entry->hrSWRunStatus = HRSWRUNSTATUS_INVALID; |
| 153 |
break; |
| 154 |
} |
| 155 |
|
| 156 |
entry->hrSWRunPerfCPU = (proc_buf->p_utime * 100); |
| 157 |
entry->hrSWRunPerfCPU += (proc_buf->p_stime * 100); |
| 158 |
entry->hrSWRunPerfMem = proc_buf->p_swrss; |
| 159 |
/* XXX - is this reported in kB? */ |
| 160 |
} |
| 161 |
closedir( procdir ); |
| 162 |
|
| 163 |
DEBUGMSGTL(("swrun:load:arch"," loaded %d entries\n", |
| 164 |
CONTAINER_SIZE(container))); |
| 165 |
|
| 166 |
return 0; |
| 167 |
} |