Debian Codegraph @

agent/mibgroup/host/data_access/swrun_procinfo.c

raw
1 /*
2 * swrun_procinfo.c:
3 * hrSWRunTable data access:
4 * getprocs() interface - AIX
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 #include <procinfo.h>
20
21 #include <net-snmp/net-snmp-includes.h>
22 #include <net-snmp/agent/net-snmp-agent-includes.h>
23 #include <net-snmp/library/container.h>
24 #include <net-snmp/library/snmp_debug.h>
25 #include <net-snmp/data_access/swrun.h>
26 #include "swrun_private.h"
27
28 int avail = 1024; /* Size of table to allocate */
29
30 /* ---------------------------------------------------------------------
31 */
32 void
33 netsnmp_arch_swrun_init(void)
34 {
35 /* Nothing to do */
36 return;
37 }
38
39 /* ---------------------------------------------------------------------
40 */
41 int
42 netsnmp_arch_swrun_container_load( netsnmp_container *container, u_int flags)
43 {
44 struct procsinfo *proc_table;
45 pid_t proc_index = 0;
46 int nprocs, i;
47 char pentry[128], *ppentry, fullpath[128];
48 netsnmp_swrun_entry *entry;
49
50 /*
51 * Create a buffer for the process table, based on the size of
52 * the table the last time we loaded this information.
53 * If this isn't big enough, keep increasing the size of the
54 * table until we can retrieve the whole thing.
55 */
56 proc_table = (struct procsinfo *) malloc(avail*(sizeof(struct procsinfo)));
57 while ( avail == (nprocs = getprocs(proc_table, sizeof(struct procsinfo),
58 0, sizeof(struct fdsinfo),
59 &proc_index, avail))) {
60 avail += 1024;
61 free( proc_table );
62 proc_table = (struct procsinfo *) malloc(avail*(sizeof(struct procsinfo)));
63 }
64
65 for ( i=0 ; i<nprocs; i++ ) {
66 if (0 == proc_table[i].pi_state)
67 continue; /* Skip unused entries */
68 entry = netsnmp_swrun_entry_create(proc_table[i].pi_pid);
69 if (NULL == entry)
70 continue; /* error already logged by function */
71 CONTAINER_INSERT(container, entry);
72
73 memset(pentry, 0, sizeof(pentry)); /* Empty each time */
74 if(!(SKPROC & proc_table[i].pi_flags)) { /* Remove kernel processes */
75 getargs(&proc_table[i], sizeof(struct procsinfo), pentry, sizeof(pentry)); /* Call getargs() */
76 for(ppentry = pentry; !((*ppentry == '\0') && (*(ppentry+1) == '\0')); ppentry++) { /* Process until 0x00 0x00 */
77 if((*ppentry == '\0') && (!(*(ppentry+1) == '\0'))) /* if 0x00 !0x00 */
78 *ppentry = ' '; /* change to 0x20 !0x00 */
79 }
80 snprintf(fullpath, sizeof(fullpath)-1, "%.*s", (int)(strchr(pentry, ' ')-pentry), pentry);
81
82 entry->hrSWRunPath_len = snprintf(entry->hrSWRunPath,
83 sizeof(entry->hrSWRunPath), "%s", fullpath);
84 ppentry = strrchr(fullpath, '/');
85 if(ppentry == NULL) {
86 entry->hrSWRunName_len = snprintf(entry->hrSWRunName,
87 sizeof(entry->hrSWRunName), "%s", fullpath);
88 }
89 else {
90 entry->hrSWRunName_len = snprintf(entry->hrSWRunName,
91 sizeof(entry->hrSWRunName), "%s", ppentry + 1);
92 }
93
94 ppentry = strchr(pentry, ' ');
95 entry->hrSWRunParameters_len = snprintf(entry->hrSWRunParameters,
96 sizeof(entry->hrSWRunParameters), "%.*s", (int)(pentry - ppentry), ppentry + 1);
97 }
98
99 entry->hrSWRunType = (SKPROC & proc_table[i].pi_flags)
100 ? 2 /* kernel process */
101 : 4 /* application */
102 ;
103
104 switch (proc_table[i].pi_state) {
105 case SACTIVE:
106 case SRUN: entry->hrSWRunStatus = HRSWRUNSTATUS_RUNNING;
107 break;
108 case SSLEEP: entry->hrSWRunStatus = HRSWRUNSTATUS_RUNNABLE;
109 break;
110 case SSTOP: entry->hrSWRunStatus = HRSWRUNSTATUS_NOTRUNNABLE;
111 break;
112 case SIDL:
113 case SZOMB:
114 default: entry->hrSWRunStatus = HRSWRUNSTATUS_INVALID;
115 break;
116 }
117
118 entry->hrSWRunPerfCPU = (proc_table[i].pi_ru.ru_utime.tv_sec * 100);
119 entry->hrSWRunPerfCPU += (proc_table[i].pi_ru.ru_utime.tv_usec / 10000000);
120 entry->hrSWRunPerfCPU += (proc_table[i].pi_ru.ru_stime.tv_sec * 100);
121 entry->hrSWRunPerfCPU += (proc_table[i].pi_ru.ru_stime.tv_usec / 10000000);
122 entry->hrSWRunPerfMem = proc_table[i].pi_size;
123 entry->hrSWRunPerfMem *= (getpagesize()/1024); /* in kB */
124 }
125 free(proc_table);
126
127 DEBUGMSGTL(("swrun:load:arch"," loaded %d entries\n",
128 (int) CONTAINER_SIZE(container)));
129
130 return 0;
131 }