Debian Codegraph @

agent/mibgroup/host/data_access/swrun_pstat.c

raw
1 /*
2 * swrun_pstat.c:
3 * hrSWRunTable data access:
4 * pstat_getdynamic() interface - HPUX
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
19 #ifdef HAVE_SYS_PARAM_H
20 #include <sys/param.h>
21 #endif
22 #ifdef HAVE_SYS_PSTAT_H
23 #define _PSTAT64
24 #include <sys/pstat.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 /* ---------------------------------------------------------------------
35 */
36 void
37 netsnmp_arch_swrun_init(void)
38 {
39 extern int _swrun_max;
40 struct pst_static pst_buf;
41
42 pstat_getstatic( &pst_buf, sizeof(struct pst_static), 1, 0);
43 _swrun_max = pst_buf.max_proc;
44 return;
45 }
46
47 /* ---------------------------------------------------------------------
48 */
49 int
50 netsnmp_arch_swrun_container_load( netsnmp_container *container, u_int flags)
51 {
52 struct pst_status *proc_table;
53 struct pst_dynamic pst_dyn;
54 int nproc, i, rc;
55 char *cp;
56 netsnmp_swrun_entry *entry;
57
58 pstat_getdynamic( &pst_dyn, sizeof(struct pst_dynamic), 1, 0);
59 nproc = pst_dyn.psd_activeprocs;
60 proc_table = (struct pst_status *) malloc(nproc*(sizeof(struct pst_status)));
61 pstat_getproc(proc_table, sizeof(struct pst_status), nproc, 0);
62
63 for ( i=0 ; i<nproc; i++ ) {
64 entry = netsnmp_swrun_entry_create(proc_table[i].pst_pid);
65 if (NULL == entry)
66 continue; /* error already logged by function */
67 rc = CONTAINER_INSERT(container, entry);
68
69 entry->hrSWRunName_len = snprintf(entry->hrSWRunName,
70 sizeof(entry->hrSWRunName)-1,
71 "%s", proc_table[i].pst_ucomm);
72 /*
73 * Split pst_cmd into two:
74 * argv[0] is hrSWRunPath
75 * argv[1..] is hrSWRunParameters
76 */
77 cp = strchr(proc_table[i].pst_cmd, ' ');
78 if (cp)
79 *cp = '\0'; /* End of argv[0] */
80 entry->hrSWRunPath_len = snprintf(entry->hrSWRunPath,
81 sizeof(entry->hrSWRunPath)-1,
82 "%s", proc_table[i].pst_cmd);
83 if (cp) {
84 entry->hrSWRunParameters_len =
85 snprintf(entry->hrSWRunParameters,
86 sizeof(entry->hrSWRunParameters)-1, "%s", cp+1);
87 *cp = ' '; /* Restore pst_cmd value */
88 }
89
90 entry->hrSWRunType = (PS_SYS & proc_table[i].pst_flag)
91 ? 2 /* kernel process */
92 : 4 /* application */
93 ;
94
95 switch (proc_table[i].pst_stat) {
96 case PS_RUN: entry->hrSWRunStatus = HRSWRUNSTATUS_RUNNING;
97 break;
98 case PS_SLEEP: entry->hrSWRunStatus = HRSWRUNSTATUS_RUNNABLE;
99 break;
100 case PS_STOP: entry->hrSWRunStatus = HRSWRUNSTATUS_NOTRUNNABLE;
101 break;
102 case PS_IDLE:
103 case PS_ZOMBIE:
104 case PS_OTHER:
105 default: entry->hrSWRunStatus = HRSWRUNSTATUS_INVALID;
106 break;
107 }
108
109 entry->hrSWRunPerfCPU = proc_table[i].pst_cptickstotal;
110 entry->hrSWRunPerfMem = proc_table[i].pst_rssize;
111 entry->hrSWRunPerfMem *= getpagesize() / 1024; /* in kB */
112 /* XXX - Check this last calculation */
113 }
114 free(proc_table);
115
116 DEBUGMSGTL(("swrun:load:arch"," loaded %d entries\n",
117 CONTAINER_SIZE(container)));
118
119 return 0;
120 }