1 |
pajs |
1.1 |
#include <stdio.h> |
2 |
|
|
#include <sys/types.h> |
3 |
|
|
#include <sys/statvfs.h> |
4 |
|
|
#include <sys/mnttab.h> |
5 |
tdb |
1.3 |
#include "ukcprog.h" |
6 |
pajs |
1.2 |
#include <sys/utsname.h> |
7 |
|
|
#include <sys/loadavg.h> |
8 |
pajs |
1.4 |
#include <utmp.h> |
9 |
|
|
#include <kstat.h> |
10 |
|
|
#include <sys/sysinfo.h> |
11 |
|
|
#include <unistd.h> |
12 |
pajs |
1.5 |
#include <sys/stat.h> |
13 |
|
|
#include <sys/swap.h> |
14 |
|
|
#include <stdlib.h> |
15 |
|
|
#include <strings.h> |
16 |
pajs |
1.1 |
|
17 |
pajs |
1.4 |
void die(){ |
18 |
|
|
exit(1); |
19 |
pajs |
1.1 |
} |
20 |
|
|
|
21 |
|
|
|
22 |
pajs |
1.4 |
void diskStats(){ |
23 |
|
|
struct mnttab mp; |
24 |
|
|
struct statvfs df; |
25 |
|
|
FILE *f; |
26 |
|
|
int counter=0; |
27 |
|
|
|
28 |
|
|
if ((f=fopen("/etc/mnttab", "r" ))==NULL){ |
29 |
|
|
errf("Failed to open mounts (%m)"); |
30 |
|
|
die(); |
31 |
|
|
} |
32 |
pajs |
1.1 |
|
33 |
pajs |
1.4 |
while((getmntent(f, &mp)) == 0){ |
34 |
|
|
if ((statvfs(mp.mnt_mountp, &df)) !=0){ |
35 |
|
|
errf("Failed to gets fs stats (%m)"); |
36 |
|
|
die(); |
37 |
|
|
} |
38 |
pajs |
1.1 |
|
39 |
pajs |
1.4 |
printf("packet.disk.p%d.attributes.mount %s\n", counter, mp.mnt_mountp); |
40 |
|
|
printf("packet.disk.p%d.attributes.name %s\n", counter, mp.mnt_special); |
41 |
|
|
printf("packet.disk.p%d.attributes.kbytes %lu\n",counter, ((df.f_frsize/1024) * df.f_blocks)); |
42 |
|
|
printf("packet.disk.p%d.attributes.used %lu\n",counter, (((df.f_frsize/1024) * df.f_blocks) -((df.f_frsize/1024) * df.f_bfree))); |
43 |
|
|
printf("packet.disk.p%d.attributes.avail %lu\n",counter, (df.f_frsize/1024) * df.f_bavail); |
44 |
|
|
printf("packet.disk.p%d.attributes.totalinodes %lu\n", counter, df.f_files); |
45 |
|
|
printf("packet.disk.p%d.attributes.freeinodes %lu\n",counter, df.f_ffree); |
46 |
|
|
|
47 |
|
|
counter++; |
48 |
|
|
} |
49 |
pajs |
1.1 |
} |
50 |
|
|
|
51 |
pajs |
1.4 |
void osStats(){ |
52 |
|
|
struct utsname os; |
53 |
|
|
|
54 |
|
|
if((uname(&os)) == -1){ |
55 |
|
|
errf("Failed to get os stats (%m)"); |
56 |
|
|
die(); |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
printf("packet.os.name %s\n", os.sysname); |
60 |
|
|
printf("packet.os.release %s\n" , os.release); |
61 |
|
|
printf("packet.os.version %s\n", os.version); |
62 |
|
|
printf("packet.os.sysname %s\n" , os.nodename); |
63 |
|
|
printf("packet.os.platform %s\n", os.machine); |
64 |
pajs |
1.2 |
|
65 |
|
|
} |
66 |
|
|
|
67 |
pajs |
1.4 |
void loadStats(){ |
68 |
pajs |
1.2 |
double loadav[3]; |
69 |
|
|
|
70 |
pajs |
1.4 |
if((getloadavg(loadav,3)) == -1){ |
71 |
|
|
errf("Failed to get load averages (%m)"); |
72 |
|
|
die(); |
73 |
pajs |
1.2 |
} |
74 |
|
|
|
75 |
|
|
printf("packet.load.load1 %.2f\n",loadav[0]); |
76 |
|
|
printf("packet.load.load5 %.2f\n",loadav[1]); |
77 |
|
|
printf("packet.load.load15 %.2f\n",loadav[2]); |
78 |
|
|
} |
79 |
pajs |
1.1 |
|
80 |
pajs |
1.4 |
void userStats(){ |
81 |
|
|
int nousers=0; |
82 |
|
|
struct utmp *entry; |
83 |
|
|
|
84 |
|
|
printf("packet.users.list"); |
85 |
|
|
|
86 |
|
|
while((entry=getutent()) != NULL){ |
87 |
|
|
if(entry->ut_type==USER_PROCESS) |
88 |
|
|
{ |
89 |
|
|
printf(" %s",entry->ut_user); |
90 |
|
|
nousers++; |
91 |
|
|
} |
92 |
|
|
} |
93 |
|
|
printf("\npacket.users.count %d\n", nousers); |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
void systemStats(){ |
97 |
|
|
kstat_ctl_t *kc; |
98 |
|
|
kstat_t *ksp; |
99 |
pajs |
1.5 |
kstat_named_t *kn; |
100 |
|
|
int pagesize; |
101 |
pajs |
1.4 |
cpu_stat_t cs; |
102 |
|
|
uint_t cpustats[4][2]; |
103 |
|
|
float usage; |
104 |
|
|
uint_t user, kernel, idle, iowait, total; |
105 |
pajs |
1.5 |
ulong totalmem, freemem; |
106 |
|
|
long swaptotal, swapused; |
107 |
|
|
|
108 |
|
|
struct anoninfo ai; |
109 |
pajs |
1.4 |
|
110 |
pajs |
1.5 |
|
111 |
pajs |
1.4 |
if ((kc = kstat_open()) == NULL) { |
112 |
|
|
errf("kstat_open failure (%m)"); |
113 |
|
|
die(); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
/* Borrowed from gkrellm ;) */ |
117 |
|
|
|
118 |
|
|
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
119 |
|
|
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) |
120 |
|
|
continue; |
121 |
|
|
if (kstat_read(kc, ksp, &cs) == -1) { |
122 |
|
|
perror("kstat_read"); |
123 |
|
|
continue; |
124 |
|
|
} |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
cpustats[0][0]=cs.cpu_sysinfo.cpu[CPU_USER]; |
128 |
|
|
cpustats[1][0]=cs.cpu_sysinfo.cpu[CPU_WAIT]; |
129 |
|
|
cpustats[2][0]=cs.cpu_sysinfo.cpu[CPU_KERNEL]; |
130 |
|
|
cpustats[3][0]=cs.cpu_sysinfo.cpu[CPU_IDLE]; |
131 |
|
|
|
132 |
|
|
sleep(1); |
133 |
|
|
|
134 |
|
|
if (kstat_chain_update(kc) == -1) { |
135 |
|
|
errf("Kstat update failure (%m)"); |
136 |
|
|
die(); |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
for (ksp = kc->kc_chain; ksp!=NULL ; ksp = ksp->ks_next) { |
140 |
|
|
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) |
141 |
|
|
continue; |
142 |
|
|
if (kstat_read(kc, ksp, &cs) == -1) { |
143 |
|
|
perror("kstat_read"); |
144 |
|
|
continue; |
145 |
|
|
} |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
cpustats[0][1]=cs.cpu_sysinfo.cpu[CPU_USER]; |
149 |
|
|
cpustats[1][1]=cs.cpu_sysinfo.cpu[CPU_WAIT]; |
150 |
|
|
cpustats[2][1]=cs.cpu_sysinfo.cpu[CPU_KERNEL]; |
151 |
|
|
cpustats[3][1]=cs.cpu_sysinfo.cpu[CPU_IDLE]; |
152 |
|
|
|
153 |
|
|
user=cpustats[0][1]-cpustats[0][0]; |
154 |
|
|
iowait=cpustats[1][1]-cpustats[1][0]; |
155 |
|
|
kernel=cpustats[2][1]-cpustats[2][0]; |
156 |
|
|
idle=cpustats[3][1]-cpustats[3][0]; |
157 |
|
|
|
158 |
|
|
total=user+kernel+idle+iowait; |
159 |
|
|
|
160 |
|
|
usage=((((float)user)/((float)total))*100.00); |
161 |
|
|
|
162 |
|
|
printf("user : %u\n",user); |
163 |
|
|
printf("total : %u\n",total); |
164 |
|
|
|
165 |
|
|
printf("packet.cpu.user %3.2f\n", usage); |
166 |
|
|
usage=((((float)kernel)/((float)total))*100.00); |
167 |
|
|
printf("packet.cpu.kernel %3.2f\n", usage); |
168 |
|
|
usage=((((float)idle)/((float)total))*100.00); |
169 |
|
|
printf("packet.cpu.idle %3.2f\n", usage); |
170 |
|
|
usage=((((float)iowait)/((float)total))*100.00); |
171 |
|
|
printf("packet.cpu.iowait %3.2f\n", usage); |
172 |
|
|
|
173 |
pajs |
1.5 |
/* MEMORY STATS */ |
174 |
|
|
|
175 |
|
|
if((pagesize=sysconf(_SC_PAGESIZE)) == -1){ |
176 |
|
|
errf("Failed to get pagesize (%m)"); |
177 |
pajs |
1.4 |
die(); |
178 |
|
|
} |
179 |
pajs |
1.2 |
|
180 |
pajs |
1.5 |
if((totalmem=sysconf(_SC_PHYS_PAGES)) == -1){ |
181 |
|
|
errf("Failed to get total memory"); |
182 |
|
|
die(); |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
totalmem=((totalmem*pagesize)/1024)/1024; |
186 |
|
|
|
187 |
|
|
ksp = kstat_lookup(kc, "unix", 0, "system_pages"); |
188 |
|
|
if (kstat_read(kc, ksp, 0) == -1) { |
189 |
|
|
perror("kstat_read"); |
190 |
|
|
die(); |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
if((kn=kstat_data_lookup(ksp, "freemem")) == NULL){ |
194 |
|
|
errf("Failed to get free memory (%m)"); |
195 |
pajs |
1.4 |
die(); |
196 |
|
|
} |
197 |
|
|
|
198 |
pajs |
1.5 |
freemem=(((kn->value.ul)*pagesize)/1024)/1024; |
199 |
|
|
|
200 |
|
|
printf("packet.memory.total %lu\n",totalmem); |
201 |
|
|
printf("packet.memory.free %lu\n",freemem); |
202 |
|
|
printf("packet.memory.used %lu\n",(totalmem-freemem)); |
203 |
|
|
|
204 |
|
|
|
205 |
|
|
if (swapctl(SC_AINFO, &ai) == -1) { |
206 |
|
|
errf("Failed to get swap details (%m)"); |
207 |
|
|
die(); |
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
swaptotal=((ai.ani_max/1024)*pagesize)/1024; |
211 |
|
|
swapused=((ai.ani_resv/1024)*pagesize)/1024; |
212 |
|
|
|
213 |
|
|
/* Ok this is actually for TOP combatability, but this is not |
214 |
|
|
total swap, cos it actually also seems to include memory stats. */ |
215 |
|
|
printf("packet.swap.total %ld\n",swaptotal); |
216 |
|
|
printf("packet.swap.used %ld\n", swapused); |
217 |
|
|
printf("packet.swap.free %lu\n", (swaptotal-swapused)); |
218 |
|
|
|
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
void processStats() { |
222 |
|
|
int sleeping=-1; |
223 |
|
|
int zombie=0; |
224 |
|
|
int stopped=0; |
225 |
|
|
int running=0; |
226 |
|
|
char *line; |
227 |
|
|
char *line_p; |
228 |
|
|
|
229 |
|
|
|
230 |
|
|
FILE *f; |
231 |
|
|
|
232 |
|
|
if((f=popen("/bin/ps -Al" , "r")) == NULL) { |
233 |
|
|
errf("Failed to get process stats (%m)"); |
234 |
|
|
die(); |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
while((line=fpgetline(f)) != NULL) { |
238 |
|
|
line_p=line; |
239 |
|
|
for(; (*line_p == ' ') && (*line_p != '\0'); line_p++); |
240 |
|
|
line_p=strchr(line_p, ' '); |
241 |
|
|
for(; (*line_p == ' ') && (*line_p != '\0'); line_p++); |
242 |
|
|
if (line_p==NULL) abort(); |
243 |
|
|
/* Ok, we should now be at the state :) .. */ |
244 |
|
|
if (*line_p=='S') sleeping++; |
245 |
|
|
if (*line_p=='R') running++; |
246 |
|
|
if (*line_p=='Z') zombie++; |
247 |
|
|
if (*line_p=='T') stopped++; |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
if((pclose(f)) == -1) { |
251 |
|
|
errf("Failed to close process stats (%m)"); |
252 |
|
|
die(); |
253 |
|
|
} |
254 |
|
|
|
255 |
|
|
printf("packet.processes.sleeping %d\n",sleeping); |
256 |
|
|
printf("packet.processes.cpu %d\n",running); |
257 |
|
|
printf("packet.processes.zombie %d\n",zombie); |
258 |
|
|
printf("packet.processes.stopped %d\n", stopped); |
259 |
|
|
printf("packet.processes.total %d\n", (sleeping+running+zombie+stopped)); |
260 |
|
|
|
261 |
pajs |
1.4 |
} |
262 |
pajs |
1.5 |
|
263 |
pajs |
1.4 |
|
264 |
|
|
int main(){ |
265 |
pajs |
1.2 |
diskStats(); |
266 |
|
|
osStats(); |
267 |
|
|
loadStats(); |
268 |
pajs |
1.4 |
userStats(); |
269 |
|
|
systemStats(); |
270 |
pajs |
1.5 |
processStats(); |
271 |
pajs |
1.2 |
return 0; |
272 |
pajs |
1.1 |
} |
273 |
|
|
|