-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproc_utils.cpp
More file actions
1270 lines (1022 loc) · 30.7 KB
/
proc_utils.cpp
File metadata and controls
1270 lines (1022 loc) · 30.7 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* \file proc_utils.cpp
* This file contains the implementations of the methods for extracting
* information from the proc filesystem.
*/
/*
* ApMon - Application Monitoring Tool
*
* Copyright (C) 2006 California Institute of Technology
*
* Permission is hereby granted, free of charge, to use, copy and modify
* this software and its documentation (the "Software") for any
* purpose, provided that existing copyright notices are retained in
* all copies and that this notice is included verbatim in any distributions
* or substantial portions of the Software.
* This software is a part of the MonALISA framework (http://monalisa.cacr.caltech.edu).
* Users of the Software are asked to feed back problems, benefits,
* and/or suggestions about the software to the MonALISA Development Team
* (developers@monalisa.cern.ch). Support for this software - fixing of bugs,
* incorporation of new features - is done on a best effort basis. All bug
* fixes and enhancements will be made available under the same terms and
* conditions as the original software,
* IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF,
* EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE IS
* PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO
* OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
* MODIFICATIONS.
*/
#include "ApMon.h"
#include "mon_constants.h"
#include "utils.h"
#include "proc_utils.h"
#ifndef WIN32
#include <dirent.h>
#endif
using namespace apmon_utils;
void ProcUtils::getCPUUsage(ApMon& apm, double& cpuUsage,
double& cpuUsr, double& cpuSys,
double& cpuNice, double& cpuIdle,
double& cpuIOWait, double& cpuIRQ,
double& cpuSoftIRQ, double& cpuSteal,
double& cpuGuest,
int numCPUs){
#ifndef WIN32
FILE *fp1;
char line[MAX_STRING_LEN];
char s1[20];
double
usrTime = RET_ERROR,
sysTime = RET_ERROR,
niceTime = RET_ERROR ,
idleTime = RET_ERROR ,
iowaitTime = RET_ERROR ,
irqTime = RET_ERROR ,
softirqTime = RET_ERROR ,
stealTime = RET_ERROR ,
guestTime = RET_ERROR,
totalTime = 0 ;
int indU, indS, indN, indI, indIOWAIT, indIRQ, indSOFTIRQ, indSTEAL, indGUEST;
time_t crtTime = time(NULL);
#ifdef __SUNOS
double tmp = 0;
char extraline[MAX_STRING_LEN];
fp1 = popen("vmstat -s", "r");
while (fgets(line, MAX_STRING_LEN, fp1)){
sscanf(line, "%lf %s", &tmp, extraline);
if (!strcmp(extraline, "user"))
usrTime = tmp;
else
if (!strcmp(extraline, "system"))
sysTime = tmp;
else
if (!strcmp(extraline, "idle"))
idleTime = tmp;
else
if (!strcmp(extraline, "wait"))
iowaitTime = tmp;
}
pclose(fp1);
#else
fp1 = fopen("/proc/stat", "r");
if (fp1 == NULL)
return;
while (fgets(line, MAX_STRING_LEN, fp1)) {
if (strstr(line, "cpu") == line)
break;
}
if (line == 0) {
fclose(fp1);
return;
}
sscanf(line, "%s %lf %lf %lf %lf %lf %lf %lf %lf %lf", s1, &usrTime, &niceTime, &sysTime, &idleTime, &iowaitTime, &irqTime, &softirqTime, &stealTime, &guestTime);
fclose(fp1);
#endif
indU = getVectIndex("cpu_usr" , apm.sysMonitorParams, apm.nSysMonitorParams);
indS = getVectIndex("cpu_sys" , apm.sysMonitorParams, apm.nSysMonitorParams);
indN = getVectIndex("cpu_nice" , apm.sysMonitorParams, apm.nSysMonitorParams);
indI = getVectIndex("cpu_idle" , apm.sysMonitorParams, apm.nSysMonitorParams);
indIOWAIT = getVectIndex("cpu_iowait" , apm.sysMonitorParams, apm.nSysMonitorParams);
indIRQ = getVectIndex("cpu_irq" , apm.sysMonitorParams, apm.nSysMonitorParams);
indSOFTIRQ = getVectIndex("cpu_softirq", apm.sysMonitorParams, apm.nSysMonitorParams);
indSTEAL = getVectIndex("cpu_steal" , apm.sysMonitorParams, apm.nSysMonitorParams);
indGUEST = getVectIndex("cpu_guest" , apm.sysMonitorParams, apm.nSysMonitorParams);
if (idleTime < apm.lastSysVals[indI]) {
apm.lastSysVals[indU] = usrTime;
apm.lastSysVals[indS] = sysTime;
apm.lastSysVals[indI] = idleTime;
apm.lastSysVals[indIOWAIT] = iowaitTime;
#ifndef __SUNOS
apm.lastSysVals[indN] = niceTime;
apm.lastSysVals[indIRQ] = irqTime;
apm.lastSysVals[indSOFTIRQ] = softirqTime;
apm.lastSysVals[indSTEAL] = stealTime;
apm.lastSysVals[indGUEST] = guestTime;
#endif
return;
}
if (numCPUs == 0)
return;
//printf("### crtTime %ld lastSysInfo %ld\n", crtTime, apm.lastSysInfoSend);
if (crtTime <= apm.lastSysInfoSend)
return;
totalTime = (usrTime - apm.lastSysVals[indU]) +
(sysTime -apm.lastSysVals[indS]) +
(idleTime - apm.lastSysVals[indI]) +
(iowaitTime - apm.lastSysVals[indIOWAIT])
#ifndef __SUNOS
+ (niceTime - apm.lastSysVals[indN])
+ (irqTime - apm.lastSysVals[indIRQ])
+ (softirqTime - apm.lastSysVals[indSOFTIRQ])
+ (stealTime - apm.lastSysVals[indSTEAL])
+ (guestTime - apm.lastSysVals[indGUEST])
#endif
;
cpuUsr = 100 * (usrTime - apm.lastSysVals[indU] ) / totalTime;
cpuSys = 100 * (sysTime - apm.lastSysVals[indS] ) / totalTime;
cpuIdle = 100 * (idleTime - apm.lastSysVals[indI] ) / totalTime;
cpuIOWait = 100 * (iowaitTime - apm.lastSysVals[indIOWAIT] ) / totalTime;
#ifndef __SUNOS
cpuNice = 100 * (niceTime - apm.lastSysVals[indN] ) / totalTime;
cpuIRQ = 100 * (irqTime - apm.lastSysVals[indIRQ] ) / totalTime;
cpuSoftIRQ = 100 * (softirqTime - apm.lastSysVals[indSOFTIRQ]) / totalTime;
cpuSteal = 100 * (stealTime - apm.lastSysVals[indSTEAL] ) / totalTime;
cpuGuest = 100 * (guestTime - apm.lastSysVals[indGUEST] ) / totalTime;
#else
cpuNice = cpuIRQ = cpuSoftIRQ = cpuSteal = cpuGuest = RET_ERROR;
#endif
cpuUsage = 100 * (totalTime - (idleTime - apm.lastSysVals[indI])) / totalTime;
apm.lastSysVals[indU] = usrTime;
apm.lastSysVals[indI] = idleTime;
apm.lastSysVals[indS] = sysTime;
apm.lastSysVals[indIOWAIT] = iowaitTime;
#ifndef __SUNOS
apm.lastSysVals[indN] = niceTime;
apm.lastSysVals[indIRQ] = irqTime;
apm.lastSysVals[indSOFTIRQ] = softirqTime;
apm.lastSysVals[indSTEAL] = stealTime;
apm.lastSysVals[indGUEST] = guestTime;
#endif
#endif
}
void ProcUtils::getSwapPages(ApMon& apm, double& pagesIn,
double& pagesOut, double& swapIn,
double& swapOut) {
#ifndef WIN32
FILE *fp1;
char line[MAX_STRING_LEN];
char s1[20];
bool foundPages, foundSwap;
double p_in, p_out, s_in, s_out;
int ind1, ind2;
time_t crtTime = time(NULL);
foundPages = foundSwap = false;
#ifdef __SUNOS
double tmp = 0;
char w1[MAX_STRING_LEN];
char w2[MAX_STRING_LEN];
char w3[MAX_STRING_LEN];
swapIn = swapOut = pagesIn = pagesOut = 0;
fp1 = popen("vmstat -s", "r");
if (fp1==NULL){
return;
}
while (fgets(line, MAX_STRING_LEN, fp1)){
sscanf(line, "%lf %s %s %s", &tmp, w1, w2, w3);
int index = -1;
if (strcmp(w1, "pages")==0){
if (strcmp(w2, "swapped")==0){
if (strcmp(w3, "in")==0){
index = getVectIndex("swap_in", apm.sysMonitorParams, apm.nSysMonitorParams);
foundSwap = true;
if (tmp >= apm.lastSysVals[index])
swapIn = (tmp - apm.lastSysVals[index]) / (crtTime - apm.lastSysInfoSend);
}
else
if (strcmp(w3, "out")==0){
foundSwap = true;
index = getVectIndex("swap_out", apm.sysMonitorParams, apm.nSysMonitorParams);
if (tmp >= apm.lastSysVals[index])
swapOut = (tmp - apm.lastSysVals[index]) / (crtTime - apm.lastSysInfoSend);
}
}
else
if (strcmp(w2, "paged")==0){
if (strcmp(w3, "in")==0){
foundPages = true;
index = getVectIndex("pages_in", apm.sysMonitorParams, apm.nSysMonitorParams);
if (tmp >= apm.lastSysVals[index])
pagesIn = (tmp - apm.lastSysVals[index]) / (crtTime - apm.lastSysInfoSend);
}
else
if (strcmp(w3, "out")==0){
foundPages = true;
index = getVectIndex("pages_out", apm.sysMonitorParams, apm.nSysMonitorParams);
if (tmp >= apm.lastSysVals[index])
pagesOut = (tmp - apm.lastSysVals[index]) / (crtTime - apm.lastSysInfoSend);
}
}
}
if (index>=0)
apm.lastSysVals[index] = tmp;
}
pclose(fp1);
#else
fp1 = fopen("/proc/stat", "r");
if (fp1 == NULL)
return;
if (crtTime <= apm.lastSysInfoSend)
return;
while (fgets(line, MAX_STRING_LEN, fp1)) {
if (strstr(line, "page") == line) {
foundPages = true;
sscanf(line, "%s %lf %lf ", s1, &p_in, &p_out);
ind1 = getVectIndex("pages_in", apm.sysMonitorParams, apm.nSysMonitorParams);
ind2 = getVectIndex("pages_out", apm.sysMonitorParams, apm.nSysMonitorParams);
if (p_in < apm.lastSysVals[ind1] || p_out < apm.lastSysVals[ind2]) {
apm.lastSysVals[ind1] = p_in;
apm.lastSysVals[ind2] = p_out;
return;
}
pagesIn = (p_in - apm.lastSysVals[ind1]) / (crtTime - apm.lastSysInfoSend);
pagesOut = (p_out - apm.lastSysVals[ind2]) / (crtTime - apm.lastSysInfoSend);
apm.lastSysVals[ind1] = p_in;
apm.lastSysVals[ind2] = p_out;
}
if (strstr(line, "swap") == line) {
foundSwap = true;
sscanf(line, "%s %lf %lf ", s1, &s_in, &s_out);
ind1 = getVectIndex("swap_in", apm.sysMonitorParams, apm.nSysMonitorParams);
ind2 = getVectIndex("swap_out", apm.sysMonitorParams, apm.nSysMonitorParams);
if (s_in < apm.lastSysVals[ind1] || s_out < apm.lastSysVals[ind2]) {
apm.lastSysVals[ind1] = s_in;
apm.lastSysVals[ind2] = s_out;
return;
}
swapIn = (s_in - apm.lastSysVals[ind1]) / (crtTime - apm.lastSysInfoSend);
swapOut = (s_out - apm.lastSysVals[ind2]) / (crtTime - apm.lastSysInfoSend);
apm.lastSysVals[ind1] = s_in;
apm.lastSysVals[ind2] = s_out;
}
}
fclose(fp1);
#endif
if (!foundPages || !foundSwap) {
return;
}
#endif
}
void ProcUtils::getLoad(double &load1, double &load5,
double &load15, double &processes) {
#ifndef WIN32
double v1, v5, v15, activeProcs, totalProcs;
FILE *fp1;
#ifdef __SUNOS
char line[MAX_STRING_LEN];
fp1 = popen("uptime", "r");
fgets(line, MAX_STRING_LEN, fp1);
char* ptr = strtok(line, " ,");
while (ptr){
load1 = load5;
load5 = load15;
load15 = atof(ptr);
ptr = strtok(NULL, " ,");
}
// uptime on SunOS doesn't contain the number of processes on the system
processes = RET_ERROR;
pclose(fp1);
#else
fp1 = fopen("/proc/loadavg", "r");
if (fp1 == NULL)
return;
int retScan = fscanf(fp1, "%lf %lf %lf", &v1, &v5, &v15);
if (retScan != 3) {
fclose(fp1);
return;
}
load1 = v1;
load5 = v5;
load15 = v15;
retScan = fscanf(fp1, "%lf/%lf", &activeProcs, &totalProcs);
if (retScan != 2) {
fclose(fp1);
return;
}
processes = totalProcs;
fclose(fp1);
#endif
#endif
}
void ProcUtils::getProcesses(double& processes, double states[]) {
#if defined(WIN32) || defined(__SUNOS)
processes = RET_ERROR;
for (int i = 0; i < NLETTERS; i++)
states[i] = RET_ERROR;
#else
char *argv[4];
char psstat_f[40];
pid_t mypid = getpid();
pid_t cpid;
int status;
char ch, buf[100];
FILE *pf;
snprintf(psstat_f, 39, "/tmp/apmon_psstat%ld", (long)mypid);
switch (cpid = fork()) {
case -1:
return;
case 0:
argv[0] = (char *)"/bin/sh"; argv[1] = (char *)"-c";
snprintf(buf, 99, "ps -A -o state > %s", psstat_f);
argv[2] = buf;
argv[3] = 0;
execv("/bin/sh", argv);
exit(RET_ERROR);
default:
if (waitpid(cpid, &status, 0) == -1) {
snprintf(buf, 99, "[ getProcesses() ] The number of processes could not be determined");
return;
}
}
pf = fopen(psstat_f, "rt");
if (pf == NULL) {
unlink(psstat_f);
snprintf(buf, 99, "[ getProcesses() ] The number of processes could not be determined");
return;
}
processes = 0;
// the states table keeps an entry for each alphabet letter, for efficient
// indexing
for (int i = 0; i < NLETTERS; i++)
states[i] = 0.0;
while (fgets(buf, 10, pf) != 0) {
ch = buf[0];
states[ch - 65]++;
processes++;
}
fclose(pf);
unlink(psstat_f);
#endif
}
void ProcUtils::getSysMem(double &totalMem, double &totalSwap) {
#ifndef WIN32
char s1[20], line[MAX_STRING_LEN];
bool memFound = false, swapFound = false;
double valMem, valSwap;
FILE *fp1;
#ifdef __SUNOS
char tempbuf[MAX_STRING_LEN];
char* pbuf = tempbuf;
fp1 = popen("prtconf", "r");
while (fgets(line, MAX_STRING_LEN, fp1)){
if (!memFound && strstr(line, "Memory size")==line){ // Memory size: 768 Megabytes
memFound = true;
strtok_r(line, " \t\n", &pbuf);
strtok_r(NULL, " \t\n", &pbuf);
valMem = atof(strtok_r(NULL, " \t\n", &pbuf));
switch (strtok_r(NULL, " \t\n", &pbuf)[0]){
case 'T':
case 't': valMem *= 1024;
case 'G':
case 'g': valMem *= 1024;
case 'M':
case 'm': valMem *= 1024;
case 'K':
case 'k': valMem *= 1024;
}
}
}
pclose(fp1);
fp1 = popen("swap -l", "r");
if (fgets(line, MAX_STRING_LEN, fp1)){ //swapfile dev swaplo blocks free
swapFound = true;
}
while (fgets(line, MAX_STRING_LEN, fp1)){
///dev/zvol/dsk/rpool/swap 182,1 8 1048568 1046496
strtok_r(line, " \t\n", &pbuf);
strtok_r(NULL, " \t\n", &pbuf);
strtok_r(NULL, " \t\n", &pbuf);
double swapBlocks = atof(strtok_r(NULL, " \t\n", &pbuf));
valSwap += swapBlocks * 512; // 512-byte blocks
}
pclose(fp1);
#else
fp1 = fopen("/proc/meminfo", "r");
if (fp1 == NULL)
return;
while (fgets(line, MAX_STRING_LEN, fp1)) {
if (strstr(line, "MemTotal:") == line) {
sscanf(line, "%s %lf", s1, &valMem);
memFound = true;
continue;
}
if (strstr(line, "SwapTotal:") == line) {
sscanf(line, "%s %lf", s1, &valSwap);
swapFound = true;
continue;
}
}
fclose(fp1);
#endif
if (!memFound || !swapFound)
return;
totalMem = valMem;
totalSwap = valSwap;
#endif
}
void ProcUtils::getMemUsed(double &usedMem, double& freeMem,
double &usedSwap, double& freeSwap) {
#ifndef WIN32
double mFree = 0, mTotal = 0, sFree = 0, sTotal = 0;
char s1[20], line[MAX_STRING_LEN];
bool mFreeFound = false, mTotalFound = false;
bool sFreeFound = false, sTotalFound = false;
FILE *fp1;
#ifdef __SUNOS
fp1 = popen("vmstat", "r");
fgets(line, MAX_STRING_LEN, fp1); // kthr memory page disk faults cpu
fgets(line, MAX_STRING_LEN, fp1); // r b w swap free re mf pi po fr de sr cd f0 s0 -- in sy cs us sy id
if (fgets(line, MAX_STRING_LEN, fp1)){ // 0 0 0 494204 64652 13 79 0 0 1 0 53 3 -0 1 0 313 673 275 2 10 88
mFreeFound = true;
}
else{
usedMem = freeMem = usedSwap = freeSwap = -1;
pclose(fp1);
return;
}
char tempbuf[MAX_STRING_LEN];
char* pbuf = tempbuf;
strtok_r(line, " \t\n", &pbuf);
strtok_r(NULL, " \t\n", &pbuf);
strtok_r(NULL, " \t\n", &pbuf);
strtok_r(NULL, " \t\n", &pbuf);
mFree = atof(strtok_r(NULL, " \t\n", &pbuf)) * 1024;
pclose(fp1);
fp1 = popen("prtconf", "r");
while (fgets(line, MAX_STRING_LEN, fp1)){
if (!mTotalFound && strstr(line, "Memory size")==line){ // Memory size: 768 Megabytes
mTotalFound = true;
strtok_r(line, " \t\n", &pbuf);
strtok_r(NULL, " \t\n", &pbuf);
mTotal = atof(strtok_r(NULL, " \t\n", &pbuf));
switch (strtok_r(NULL, " \t\n", &pbuf)[0]){
case 'T':
case 't': mTotal *= 1024;
case 'G':
case 'g': mTotal *= 1024;
case 'M':
case 'm': mTotal *= 1024;
case 'K':
case 'k': mTotal *= 1024;
}
}
}
pclose(fp1);
fp1 = popen("swap -l", "r");
if (fgets(line, MAX_STRING_LEN, fp1)){ //swapfile dev swaplo blocks free
sTotalFound = sFreeFound = true;
}
while (fgets(line, MAX_STRING_LEN, fp1)){
///dev/zvol/dsk/rpool/swap 182,1 8 1048568 1046496
strtok_r(line, " \t\n", &pbuf);
strtok_r(NULL, " \t\n", &pbuf);
strtok_r(NULL, " \t\n", &pbuf);
double swapBlocks = atof(strtok_r(NULL, " \t\n", &pbuf));
double freeBlocks = atof(strtok_r(NULL, " \t\n", &pbuf));
sTotal += swapBlocks * 512;
sFree += freeBlocks * 512;
}
pclose(fp1);
#else
fp1 = fopen("/proc/meminfo", "r");
if (fp1 == NULL)
return;
while (fgets(line, MAX_STRING_LEN, fp1)) {
if (strstr(line, "MemTotal:") == line) {
sscanf(line, "%s %lf", s1, &mTotal);
mTotalFound = true;
continue;
}
if (strstr(line, "MemFree:") == line) {
sscanf(line, "%s %lf", s1, &mFree);
mFreeFound = true;
continue;
}
if (strstr(line, "SwapTotal:") == line) {
sscanf(line, "%s %lf", s1, &sTotal);
sTotalFound = true;
continue;
}
if (strstr(line, "SwapFree:") == line) {
sscanf(line, "%s %lf", s1, &sFree);
sFreeFound = true;
continue;
}
}
fclose(fp1);
#endif
if (!mFreeFound || !mTotalFound || !sFreeFound || !sTotalFound)
return;
if (mFree > mTotal)
mTotal = mFree;
if (sFree > sTotal)
sTotal = sFree;
usedMem = (mTotal - mFree) / 1024;
freeMem = mFree / 1024;
usedSwap = (sTotal - sFree) / 1024;
freeSwap = sFree / 1024;
#endif
}
void ProcUtils::getNetworkInterfaces(int &nInterfaces, char names[][20]) {
nInterfaces = 0;
#ifndef WIN32
#ifdef __SUNOS
// on SunOS the TCP stats are global, not per interface, so we account everything under a virtual "eth0" device
//nInterfaces = 1;
//strcpy(names[0], "eth0");
int sockfd, i;
struct lifnum ln;
struct lifconf lc;
sockfd=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
ln.lifn_family=AF_INET;
ln.lifn_flags=ln.lifn_count=0;
if (ioctl(sockfd,SIOCGLIFNUM,&ln) == -1){
perror("Failed to get number of interfaces");
close(sockfd);
return;
}
lc.lifc_family=AF_INET;
lc.lifc_flags=0;
lc.lifc_len=ln.lifn_count * sizeof(struct lifreq);
lc.lifc_req=(struct lifreq *) malloc(lc.lifc_len);
if (ioctl(sockfd,SIOCGLIFCONF,&lc) == -1){
perror("Failed to enumerate interfaces");
close(sockfd);
free(lc.lifc_req);
return;
}
for (i=0; i<ln.lifn_count && i<100; i++){
strncpy(names[nInterfaces], lc.lifc_req[i].lifr_name, 19);
nInterfaces ++;
}
close(sockfd);
free(lc.lifc_req);
#else
char line[MAX_STRING_LEN], *tmp;
// char buf[MAX_STRING_LEN];
// char *pbuf = buf;
FILE *fp1;
fp1 = fopen("/proc/net/dev", "r");
if (fp1 == NULL)
return;
while (fgets(line, MAX_STRING_LEN, fp1) && nInterfaces<100) {
if (strchr(line, ':') == NULL)
continue;
tmp = strtok/*_r*/(line, " :");//, &pbuf);
if (strcmp(tmp, "lo") == 0)
continue;
strncpy(names[nInterfaces], tmp, 19);
nInterfaces++;
}
fclose(fp1);
#endif
#endif
}
void ProcUtils::getNetInfo(ApMon& apm, double **vNetIn,
double **vNetOut, double **vNetErrs) {
#ifndef WIN32
double *netIn, *netOut, *netErrs, bytesReceived = 0, bytesSent = 0;
int errs = 0;
char line[MAX_STRING_LEN], msg[MAX_STRING_LEN];
// char buf[MAX_STRING_LEN];
// char *pbuf = buf;
char *tmp, *tok;
double bootTime = 0;
FILE *fp1;
time_t crtTime = time(NULL);
int ind, i;
if (apm.lastSysInfoSend == 0) {
try {
bootTime = getBootTime();
} catch (procutils_error& err) {
logger(WARNING, "[ getNetInfo() ] Error obtaining boot time. The first system monitoring datagram will contain incorrect data.");
bootTime = 0;
}
}
if (crtTime <= apm.lastSysInfoSend)
return;
netIn = (double *)malloc(apm.nInterfaces * sizeof(double));
netOut = (double *)malloc(apm.nInterfaces * sizeof(double));
netErrs = (double *)malloc(apm.nInterfaces * sizeof(double));
#ifdef __SUNOS
fp1 = popen("netstat -P tcp -s", "r");
// find out the first interface that is not "lo"
// we cannot bind traffic per interface, so we put everything on the first real interface
ind = -1;
for (i=0; i<apm.nInterfaces; i++){
netIn[i] = RET_ERROR;
netOut[i] = RET_ERROR;
netErrs[i] = RET_ERROR;
if ( strncmp(apm.interfaceNames[i], "lo", 2) == 0 )
continue;
if (ind<0)
ind = i;
}
if (ind<0){
pclose(fp1);
return;
}
while (fgets(line, MAX_STRING_LEN, fp1)){
char* ptr = strtok(line, " =\t");
while (ptr){
if (strstr(ptr, "tcpIn")==ptr && strstr(ptr, "Bytes")!=NULL){
ptr = strtok(NULL, " =\t");
bytesReceived += atof(ptr);
}
else
if (strcmp(ptr, "tcpOutDataBytes")==0 || strcmp(ptr, "tcpRetransBytes")==0){
ptr = strtok(NULL, " =\t");
bytesSent += atof(ptr);
}
ptr = strtok(NULL, " =\t");
}
}
pclose(fp1);
errs = -1;
#else
fp1 = fopen("/proc/net/dev", "r");
if (fp1 == NULL)
return;
while (fgets(line, MAX_STRING_LEN, fp1)) {
if (strchr(line, ':') == NULL)
continue;
tmp = strtok/*_r*/(line, " :");//, &pbuf);
/* the loopback interface is not considered */
if (strcmp(tmp, "lo") == 0)
continue;
/* find the index of the interface in the vector */
ind = -1;
for (i = 0; i < apm.nInterfaces; i++)
if (strcmp(apm.interfaceNames[i], tmp) == 0) {
ind = i;
break;
}
if (ind < 0) {
fclose(fp1);
free(netIn); free(netOut); free(netErrs);
snprintf(msg, MAX_STRING_LEN-1, "[ getNetInfo() ] Could not find interface %s in /proc/net/dev", tmp);
return;
}
/* parse the rest of the line */
tok = strtok/*_r*/(NULL, " ");//, &pbuf);
bytesReceived = atof(tok); /* bytes received */
tok = strtok/*_r*/(NULL, " ");//, &pbuf); /* packets received */
tok = strtok/*_r*/(NULL, " ");//, &pbuf); /* input errors */
errs = atoi(tok);
/* some parameters that we are not monitoring */
for (i = 1; i <= 5; i++)
tok = strtok/*_r*/(NULL, " ");//, &pbuf);
tok = strtok/*_r*/(NULL, " ");//, &pbuf); /* bytes transmitted */
bytesSent = atof(tok);
tok = strtok/*_r*/(NULL, " ");//, &pbuf); /* packets transmitted */
tok = strtok/*_r*/(NULL, " ");//, &pbuf); /* output errors */
errs += atoi(tok);
#endif
//printf("### bytesReceived %lf lastRecv %lf\n", bytesReceived,
// apm.lastBytesReceived[ind]);
if (bytesReceived < apm.lastBytesReceived[ind] || bytesSent <
apm.lastBytesSent[ind] || errs < apm.lastNetErrs[ind]) {
apm.lastBytesReceived[ind] = bytesReceived;
apm.lastBytesSent[ind] = bytesSent;
apm.lastNetErrs[ind] = errs;
fclose(fp1);
free(netIn); free(netOut); free(netErrs);
return;
}
if (apm.lastSysInfoSend == 0) {
netIn[ind] = bytesReceived/(crtTime - bootTime);
netOut[ind] = bytesSent/(crtTime - bootTime);
netErrs[ind] = errs;
}
else {
netIn[ind] = (bytesReceived - apm.lastBytesReceived[ind]) / (crtTime -
apm.lastSysInfoSend);
netIn[ind] /= 1024; /* netIn is measured in KBps */
netOut[ind] = (bytesSent - apm.lastBytesSent[ind]) / (crtTime -
apm.lastSysInfoSend);
netOut[ind] /= 1024; /* netOut is measured in KBps */
/* for network errors give the total number */
netErrs[ind] = errs; // - apm.lastNetErrs[ind];
}
apm.lastBytesReceived[ind] = bytesReceived;
apm.lastBytesSent[ind] = bytesSent;
apm.lastNetErrs[ind] = errs;
#ifndef __SUNOS
}
fclose(fp1);
#endif
*vNetIn = netIn;
*vNetOut = netOut;
*vNetErrs = netErrs;
#endif
}
int ProcUtils::getNumCPUs() {
#ifdef WIN32
return 0;
#else
int numCPUs = 0;
char line[MAX_STRING_LEN];
FILE *fp;
#ifdef __SUNOS
fp = popen("psrinfo -p", "r");
fgets(line, MAX_STRING_LEN, fp);
pclose(fp);
numCPUs = atoi(line);
#else
fp = fopen("/proc/stat", "r");
if (fp == NULL)
return -1;
while(fgets(line, MAX_STRING_LEN, fp)) {
if (strstr(line, "cpu") == line && isdigit(line[3]))
numCPUs++;
}
fclose(fp);
#endif
return numCPUs;
#endif
}
void ProcUtils::getCPUInfo(ApMon& apm) {
#ifndef WIN32
#ifndef __SUNOS
double freq = 0;
char line[MAX_STRING_LEN], s1[100], s2[100], s3[100];
// char buf[MAX_STRING_LEN];
// char *pbuf = buf;
char *tmp, *tmp_trim;
bool freqFound = false, bogomipsFound = false;
FILE *fp = fopen("/proc/cpuinfo", "r");
if (fp == NULL)
return;
while (fgets(line, MAX_STRING_LEN, fp)) {
if (strstr(line, "cpu MHz") == line) {
sscanf(line, "%s %s %s %lf", s1, s2, s3, &freq);
apm.currentGenVals[GEN_CPU_MHZ] = freq;
freqFound = true;
continue;
}
if (strstr(line, "bogomips") == line) {
sscanf(line, "%s %s %lf", s1, s2, &(apm.currentGenVals[GEN_BOGOMIPS]));
bogomipsFound = true;
continue;
}
if (strstr(line, "vendor_id") == line) {
tmp = strtok/*_r*/(line, ":");//, &pbuf);
/* take everything that's after the ":" */
tmp = strtok/*_r*/(NULL, ":");//, &pbuf);
tmp_trim = trimString(tmp);
strncpy(apm.cpuVendor, tmp_trim, 99);
free(tmp_trim);
continue;
}
if (strstr(line, "cpu family") == line) {
tmp = strtok/*_r*/(line, ":");//, &pbuf);
tmp = strtok/*_r*/(NULL, ":");//, &pbuf);
tmp_trim = trimString(tmp);
strncpy(apm.cpuFamily, tmp_trim, 99);
free(tmp_trim);
continue;
}
if (strstr(line, "model") == line && strstr(line, "model name") != line) {
tmp = strtok/*_r*/(line, ":");//, &pbuf);
tmp = strtok/*_r*/(NULL, ":");//, &pbuf);
tmp_trim = trimString(tmp);
strncpy(apm.cpuModel, tmp_trim, 99);
free(tmp_trim);
continue;
}
if (strstr(line, "model name") == line) {