1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
#include <stdio.h>
#include <stdlib.h>
#include "prfstatwrp.h"
#include <procinfo.h>
#include <sys/proc.h>
#include <nlist.h>
#include <sys/types.h>
#include <sys/times.h>
#define KMEM "/dev/kmem"
int kmem; /* file descriptor */
/* Indices in the nlist array */
#define X_AVENRUN 0
#define X_SYSINFO 1
#define X_VMKER 2
#define X_V 3
static struct nlist nlst[] = {
{ "avenrun", 0, 0, 0, 0, 0 },
{ "sysinfo", 0, 0, 0, 0, 0 },
{ "vmker", 0, 0, 0, 0, 0 },
{ "v", 0, 0, 0, 0, 0 },
{ NULL, 0, 0, 0, 0, 0 }
};
/* offsets in kernel */
static unsigned long avenrun_offset;
static unsigned long sysinfo_offset;
static unsigned long vmker_offset;
static unsigned long proc_offset;
static unsigned long v_offset;
struct proc *p_proc; /* a copy of the process table */
struct procentry64 *p_info;
struct var v_info; /* to determine nprocs */
int nprocs; /* maximum nr of procs in proctab */
int ncpus; /* nr of cpus installed */
int ptsize; /* size of process table in bytes */
void init() {
if ((kmem = open(KMEM, O_RDONLY)) == -1) {
perror(KMEM);
return ;
}
if (knlist(nlst, 1, sizeof(struct nlist)) == -1) {
perror("knlist, proc entry not found");
return;
}
avenrun_offset = nlst[X_AVENRUN].n_value;
sysinfo_offset = nlst[X_SYSINFO].n_value;
vmker_offset = nlst[X_VMKER].n_value;
v_offset = nlst[X_V].n_value;
getkval(v_offset, (caddr_t)&v_info, sizeof v_info, "v");
ncpus = v_info.v_ncpus;
nprocs = 20480;
ptsize = nprocs * sizeof (struct proc);
p_info = (struct procentry64 *)malloc(nprocs * sizeof (struct procentry64));
if (!p_info) {
zbx_error("not enough memory.");
return;
}
}
long get_num_procs()
{
struct procsinfo ps[8192];
pid_t index = 0;
int nprocs;
int i;
char state;
if ((nprocs = getprocs(&ps, sizeof(struct procsinfo), NULL, 0, &index, 8192)) > 0) {
return nprocs;
} else {
return -1;
}
}
long get_running_procs()
{
struct procentry64 *pp;
int running = 0, i, nproc;
pid_t procsindex = 0;
int ptsize_util;
struct proc *p;
init();
if ((nproc = getprocs(p_info, sizeof (struct procsinfo), NULL, 0,
&procsindex, nprocs)) > 0) {
for (pp=p_info, i=0; i < nproc;pp++, i++) {
if (pp->pi_state == SACTIVE && pp->pi_cpu != 0)
running++;
}
return running;
} else {
return -1;
}
}
double get_loadavg(int data_type) {
perfstat_cpu_total_t ub;
if (perfstat_cpu_total ((perfstat_id_t*)NULL, &ub, sizeof(perfstat_cpu_total_t),1) >= 0) {
switch(data_type) {
case CPU_LOADAVG:
return (double) ub.loadavg[0] / 65535;
break;
case CPU_LOADAVG5:
return (double) ub.loadavg[1] / 65535;
break;
case CPU_LOADAVG15:
return (double) ub.loadavg[2] / 65535;
break;
}
} else {
return -1;
}
}
u_longlong_t get_disk_io(int data_type) {
perfstat_disk_total_t ub;
if (perfstat_disk_total ((perfstat_id_t*)NULL, &ub, sizeof(perfstat_disk_total_t),1) >= 0) {
switch(data_type) {
case DISK_IO_RBLKS:
return ub.rblks;
break;
case DISK_IO_WBLKS:
return ub.wblks;
break;
case DISK_IO_TOTAL:
return ub.rblks + ub.wblks;
break;
}
} else {
return -1;
}
}
u_longlong_t get_disk_stat(char diskname[32], int data_type)
{
perfstat_id_t name;
perfstat_disk_t *ub;
int ndisk,i;
ub = malloc(sizeof(perfstat_disk_t)*1);
strcpy(name.name,diskname);
if (perfstat_disk (&name,ub,sizeof(perfstat_disk_t),1) >= 0) {
switch(data_type) {
case DISK_IO_RBLKS:
return ub[0].rblks;
break;
case DISK_IO_WBLKS:
return ub[0].wblks;
break;
}
} else {
return -1;
}
}
int getkval(unsigned long offset, caddr_t ptr, int size, char *refstr)
{
int upper_2gb = 0;
if (offset > 1<<31) {
upper_2gb = 1;
offset &= 0x7fffffff;
}
if (lseek(kmem, offset, SEEK_SET) != offset) {
return -1;
}
if (readx(kmem, ptr, size, upper_2gb) != size) {
if (*refstr == '!')
return 0;
else {
return -1;
}
}
return 1 ;
}
unsigned int get_uptime() {
struct tms tbuf;
time_t uptime;
time_t timeofday;
uptime = (times(&tbuf) / HZ);
return (unsigned int) uptime;
}
|