-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmonitor_utils.cpp
More file actions
1321 lines (1155 loc) · 41.8 KB
/
monitor_utils.cpp
File metadata and controls
1321 lines (1155 loc) · 41.8 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 monitor_utils.cpp
* This file contains the implementations of some functions used for
* obtaining monitoring information.
*/
/*
* 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 "monitor_utils.h"
#include "proc_utils.h"
#include "utils.h"
#include "mon_constants.h"
using namespace apmon_utils;
using namespace apmon_mon_utils;
void ApMon::sendJobInfo() {
#ifndef WIN32
int i;
long crtTime;
/* the apMon_free() function calls sendJobInfo() from another thread and
we need mutual exclusion */
pthread_mutex_lock(&mutexBack);
if (nMonJobs == 0) {
logger(WARNING, "There are no jobs to be monitored, not sending job monitoring information.");
pthread_mutex_unlock(&mutexBack);
return;
}
crtTime = time(NULL);
logger(INFO, "Sending job monitoring information...");
lastJobInfoSend = (time_t)crtTime;
/* send monitoring information for all the jobs specified by the user */
for (i = 0; i < nMonJobs; i++)
sendOneJobInfo(monJobs[i]);
pthread_mutex_unlock(&mutexBack);
#endif
}
void ApMon::updateJobInfo(MonitoredJob job) {
bool needJobInfo, needDiskInfo;
bool jobExists = true;
char err_msg[200];
PsInfo jobInfo;
JobDirInfo dirInfo;
/**** runtime, CPU & memory usage information ****/
needJobInfo = actJobMonitorParams[JOB_RUN_TIME] ||
actJobMonitorParams[JOB_CPU_TIME] || actJobMonitorParams[JOB_CPU_USAGE] ||
actJobMonitorParams[JOB_MEM_USAGE] || actJobMonitorParams[JOB_VIRTUALMEM]
|| actJobMonitorParams[JOB_RSS] || actJobMonitorParams[JOB_OPEN_FILES];
if (needJobInfo) {
try {
readJobInfo(job.pid, jobInfo);
currentJobVals[JOB_RUN_TIME] = jobInfo.etime;
currentJobVals[JOB_CPU_TIME] = jobInfo.cputime;
currentJobVals[JOB_CPU_USAGE] = jobInfo.pcpu;
currentJobVals[JOB_MEM_USAGE] = jobInfo.pmem;
currentJobVals[JOB_VIRTUALMEM] = jobInfo.vsz;
currentJobVals[JOB_RSS] = jobInfo.rsz;
if (jobInfo.open_fd < 0)
jobRetResults[JOB_OPEN_FILES] = RET_ERROR;
currentJobVals[JOB_OPEN_FILES] = jobInfo.open_fd;
} catch (runtime_error &err) {
logger(WARNING, err.what());
jobRetResults[JOB_RUN_TIME] = jobRetResults[JOB_CPU_TIME] =
jobRetResults[JOB_CPU_USAGE] = jobRetResults[JOB_MEM_USAGE] =
jobRetResults[JOB_VIRTUALMEM] = jobRetResults[JOB_RSS] =
jobRetResults[JOB_OPEN_FILES] = RET_ERROR;
strncpy(err_msg, err.what(), 199);
if (strstr(err_msg, "does not exist") != NULL)
jobExists = false;
}
}
/* if the monitored job has terminated, remove it */
if (!jobExists) {
try {
removeJobToMonitor(job.pid);
} catch (runtime_error &err) {
logger(WARNING, err.what());
}
return;
}
/* disk usage information */
needDiskInfo = actJobMonitorParams[JOB_DISK_TOTAL] ||
actJobMonitorParams[JOB_DISK_USED] || actJobMonitorParams[JOB_DISK_FREE] ||
actJobMonitorParams[JOB_DISK_USAGE] || actJobMonitorParams[JOB_WORKDIR_SIZE];
if (needDiskInfo) {
try {
readJobDiskUsage(job, dirInfo);
currentJobVals[JOB_WORKDIR_SIZE] = dirInfo.workdir_size;
currentJobVals[JOB_DISK_TOTAL] = dirInfo.disk_total;
currentJobVals[JOB_DISK_USED] = dirInfo.disk_used;
currentJobVals[JOB_DISK_USAGE] = dirInfo.disk_usage;
currentJobVals[JOB_DISK_FREE] = dirInfo.disk_free;
} catch (runtime_error& err) {
logger(WARNING, err.what());
jobRetResults[JOB_WORKDIR_SIZE] = jobRetResults[JOB_DISK_TOTAL] =
jobRetResults[JOB_DISK_USED] = jobRetResults[JOB_DISK_USAGE] =
jobRetResults[JOB_DISK_FREE] = RET_ERROR;
}
}
}
void ApMon::sendOneJobInfo(MonitoredJob job) {
int i;
int nParams = 0;
char **paramNames, **paramValues;
int *valueTypes;
valueTypes = (int *)malloc(nJobMonitorParams * sizeof(int));
paramNames = (char **)malloc(nJobMonitorParams * sizeof(char *));
paramValues = (char **)malloc(nJobMonitorParams * sizeof(char *));
for (i = 0; i < nJobMonitorParams; i++) {
jobRetResults[i] = RET_SUCCESS;
currentJobVals[i] = 0;
}
updateJobInfo(job);
for (i = 0; i < nJobMonitorParams; i++) {
if (actJobMonitorParams[i] && jobRetResults[i] != RET_ERROR) {
paramNames[nParams] = jobMonitorParams[i];
paramValues[nParams] = (char *)¤tJobVals[i];
valueTypes[nParams] = XDR_REAL64;
nParams++;
}
/* don't disable the parameter (maybe for another job it can be
obtained) */
/*
else
if (autoDisableMonitoring)
actJobMonitorParams[ind] = 0;
*/
}
if (nParams == 0) {
free(paramNames); free(valueTypes);
free(paramValues);
return;
}
try {
if (nParams > 0)
sendParameters(job.clusterName, job.nodeName, nParams,
paramNames, valueTypes, paramValues);
} catch (runtime_error& err) {
logger(WARNING, err.what());
}
free(paramNames);
free(valueTypes);
free(paramValues);
}
void ApMon::updateSysInfo() {
int needCPUInfo, needSwapPagesInfo, needLoadInfo, needMemInfo,
needNetInfo, needUptime, needProcessesInfo, needNetstatInfo;
/**** CPU usage information ****/
needCPUInfo = actSysMonitorParams[SYS_CPU_USAGE] ||
actSysMonitorParams[SYS_CPU_USR] || actSysMonitorParams[SYS_CPU_SYS] ||
actSysMonitorParams[SYS_CPU_NICE] || actSysMonitorParams[SYS_CPU_IDLE] ||
actSysMonitorParams[SYS_CPU_IOWAIT] || actSysMonitorParams[SYS_CPU_IRQ] ||
actSysMonitorParams[SYS_CPU_SOFTIRQ] || actSysMonitorParams[SYS_CPU_STEAL] ||
actSysMonitorParams[SYS_CPU_GUEST];
if (needCPUInfo) {
try {
ProcUtils::getCPUUsage(*this, currentSysVals[SYS_CPU_USAGE],
currentSysVals[SYS_CPU_USR],
currentSysVals[SYS_CPU_SYS],
currentSysVals[SYS_CPU_NICE],
currentSysVals[SYS_CPU_IDLE],
currentSysVals[SYS_CPU_IOWAIT],
currentSysVals[SYS_CPU_IRQ],
currentSysVals[SYS_CPU_SOFTIRQ],
currentSysVals[SYS_CPU_STEAL],
currentSysVals[SYS_CPU_GUEST],
numCPUs);
} catch (procutils_error &perr) {
/* "permanent" error (the parameters could not be obtained) */
logger(WARNING, perr.what());
sysRetResults[SYS_CPU_USAGE] = sysRetResults[SYS_CPU_SYS] =
sysRetResults[SYS_CPU_USR] = sysRetResults[SYS_CPU_NICE] =
sysRetResults[SYS_CPU_IDLE] = sysRetResults[SYS_CPU_USAGE] =
sysRetResults[SYS_CPU_IOWAIT] = sysRetResults[SYS_CPU_IRQ] =
sysRetResults[SYS_CPU_SOFTIRQ] = sysRetResults[SYS_CPU_STEAL] =
sysRetResults[SYS_CPU_GUEST] =
PROCUTILS_ERROR;
} catch (runtime_error &err) {
/* temporary error (next time we might be able to get the paramerers) */
logger(WARNING, err.what());
sysRetResults[SYS_CPU_USAGE] = sysRetResults[SYS_CPU_SYS] =
sysRetResults[SYS_CPU_USR] = sysRetResults[SYS_CPU_NICE] =
sysRetResults[SYS_CPU_IDLE] = sysRetResults[SYS_CPU_USAGE] =
sysRetResults[SYS_CPU_IOWAIT] = sysRetResults[SYS_CPU_IRQ] =
sysRetResults[SYS_CPU_SOFTIRQ] = sysRetResults[SYS_CPU_STEAL] =
sysRetResults[SYS_CPU_GUEST] =
RET_ERROR;
}
}
needSwapPagesInfo = actSysMonitorParams[SYS_PAGES_IN] ||
actSysMonitorParams[SYS_PAGES_OUT] || actSysMonitorParams[SYS_SWAP_IN] ||
actSysMonitorParams[SYS_SWAP_OUT];
if (needSwapPagesInfo) {
try {
ProcUtils::getSwapPages(*this, currentSysVals[SYS_PAGES_IN],
currentSysVals[SYS_PAGES_OUT],
currentSysVals[SYS_SWAP_IN],
currentSysVals[SYS_SWAP_OUT]);
} catch (procutils_error &perr) {
/* "permanent" error (the parameters could not be obtained) */
logger(WARNING, perr.what());
sysRetResults[SYS_PAGES_IN] = sysRetResults[SYS_PAGES_OUT] =
sysRetResults[SYS_SWAP_OUT] = sysRetResults[SYS_SWAP_IN] = PROCUTILS_ERROR;
} catch (runtime_error &err) {
/* temporary error (next time we might be able to get the paramerers) */
logger(WARNING, err.what());
sysRetResults[SYS_PAGES_IN] = sysRetResults[SYS_PAGES_OUT] =
sysRetResults[SYS_SWAP_IN] = sysRetResults[SYS_SWAP_OUT] = RET_ERROR;
}
}
needLoadInfo = actSysMonitorParams[SYS_LOAD1] ||
actSysMonitorParams[SYS_LOAD5] || actSysMonitorParams[SYS_LOAD15];
if (needLoadInfo) {
double dummyVal;
try {
/* the number of processes is now obtained with the getProcesses()
function, not with getLoad() */
ProcUtils::getLoad(currentSysVals[SYS_LOAD1], currentSysVals[SYS_LOAD5],
currentSysVals[SYS_LOAD15],dummyVal);
} catch (procutils_error& perr) {
/* "permanent" error (the parameters could not be obtained) */
logger(WARNING, perr.what());
sysRetResults[SYS_LOAD1] = sysRetResults[SYS_LOAD5] =
sysRetResults[SYS_LOAD15] = PROCUTILS_ERROR;
}
}
/**** get statistics about the current processes ****/
needProcessesInfo = actSysMonitorParams[SYS_PROCESSES];
if (needProcessesInfo) {
try {
ProcUtils::getProcesses(currentSysVals[SYS_PROCESSES],
currentProcessStates);
} catch (runtime_error& err) {
logger(WARNING, err.what());
sysRetResults[SYS_PROCESSES] = RET_ERROR;
}
}
/**** get the amount of memory currently in use ****/
needMemInfo = actSysMonitorParams[SYS_MEM_USED] ||
actSysMonitorParams[SYS_MEM_FREE] || actSysMonitorParams[SYS_SWAP_USED] ||
actSysMonitorParams[SYS_SWAP_FREE] || actSysMonitorParams[SYS_MEM_USAGE] ||
actSysMonitorParams[SYS_SWAP_USAGE];
if (needMemInfo) {
try {
ProcUtils::getMemUsed(currentSysVals[SYS_MEM_USED],
currentSysVals[SYS_MEM_FREE],
currentSysVals[SYS_SWAP_USED],
currentSysVals[SYS_SWAP_FREE]);
currentSysVals[SYS_MEM_USAGE] = 100 * currentSysVals[SYS_MEM_USED] /
(currentSysVals[SYS_MEM_USED] + currentSysVals[SYS_MEM_FREE]);
currentSysVals[SYS_SWAP_USAGE] = 100 * currentSysVals[SYS_SWAP_USED] /
(currentSysVals[SYS_SWAP_USED] + currentSysVals[SYS_SWAP_FREE]);
} catch (procutils_error &perr) {
logger(WARNING, perr.what());
sysRetResults[SYS_MEM_USED] = sysRetResults[SYS_MEM_FREE] =
sysRetResults[SYS_SWAP_USED] = sysRetResults[SYS_SWAP_FREE] =
sysRetResults[SYS_MEM_USAGE] = sysRetResults[SYS_SWAP_USAGE] =
PROCUTILS_ERROR;
}
}
/**** network monitoring information ****/
needNetInfo = actSysMonitorParams[SYS_NET_IN] ||
actSysMonitorParams[SYS_NET_OUT] || actSysMonitorParams[SYS_NET_ERRS];
if (needNetInfo && this -> nInterfaces > 0) {
try {
ProcUtils::getNetInfo(*this, ¤tNetIn, ¤tNetOut,
¤tNetErrs);
} catch (procutils_error &perr) {
logger(WARNING, perr.what());
sysRetResults[SYS_NET_IN] = sysRetResults[SYS_NET_OUT] =
sysRetResults[SYS_NET_ERRS] = PROCUTILS_ERROR;
} catch (runtime_error &err) {
logger(WARNING, err.what());
sysRetResults[SYS_NET_IN] = sysRetResults[SYS_NET_OUT] =
sysRetResults[SYS_NET_ERRS] = RET_ERROR;
}
}
needNetstatInfo = actSysMonitorParams[SYS_NET_SOCKETS] ||
actSysMonitorParams[SYS_NET_TCP_DETAILS];
if (needNetstatInfo) {
try {
ProcUtils::getNetstatInfo(*this, this -> currentNSockets,
this -> currentSocketsTCP);
} catch (runtime_error &err) {
logger(WARNING, err.what());
sysRetResults[SYS_NET_SOCKETS] = sysRetResults[SYS_NET_TCP_DETAILS] =
RET_ERROR;
}
}
needUptime = actSysMonitorParams[SYS_UPTIME];
if (needUptime) {
try {
currentSysVals[SYS_UPTIME] = ProcUtils::getUpTime();
} catch (procutils_error &perr) {
logger(WARNING, perr.what());
sysRetResults[SYS_UPTIME] = PROCUTILS_ERROR;
}
}
}
void ApMon::sendSysInfo() {
#ifndef WIN32
int nParams = 0, maxNParams;
int i;
long crtTime;
int *valueTypes;
char **paramNames, **paramValues;
crtTime = time(NULL);
logger(INFO, "Sending system monitoring information...");
/* make some initializations only the first time this
function is called */
if (this -> sysInfo_first) {
for (i = 0; i < this -> nInterfaces; i++) {
this -> lastBytesSent[i] = this -> lastBytesReceived[i] = 0.0;
this -> lastNetErrs[i] = 0;
}
this -> sysInfo_first = FALSE;
}
/* the maximum number of parameters that can be included in a datagram */
/* (the last three terms are for: parameters corresponding to each possible
state of the processes, parameters corresponding to the types of open
sockets, parameters corresponding to each possible state of the TCP
sockets.) */
maxNParams = nSysMonitorParams + (2 * nInterfaces - 1) + 15 + 4 +
N_TCP_STATES;
valueTypes = (int *)malloc(maxNParams * sizeof(int));
paramNames = (char **)malloc(maxNParams * sizeof(char *));
paramValues = (char **)malloc(maxNParams * sizeof(char *));
for (i = 0; i < nSysMonitorParams; i++) {
if (actSysMonitorParams[i] > 0) /* if the parameter is enabled */
sysRetResults[i] = RET_SUCCESS;
else /* mark it with RET_ERROR so that it will be not included in the
datagram */
sysRetResults[i] = RET_ERROR;
}
updateSysInfo();
for (i = 0; i < nSysMonitorParams; i++) {
if (i == SYS_NET_IN || i == SYS_NET_OUT || i == SYS_NET_ERRS ||
i == SYS_NET_SOCKETS || i == SYS_NET_TCP_DETAILS || i == SYS_PROCESSES)
continue;
if (sysRetResults[i] == PROCUTILS_ERROR) {
/* could not read the requested information from /proc, disable this
parameter */
if (autoDisableMonitoring)
actSysMonitorParams[i] = 0;
}
else
if (sysRetResults[i] != RET_ERROR && currentSysVals[i] != RET_ERROR) {
/* the parameter is enabled and there were no errors obtaining it */
paramNames[nParams] = strdup(sysMonitorParams[i]);
paramValues[nParams] = (char *)¤tSysVals[i];
valueTypes[nParams] = XDR_REAL64;
nParams++;
}
}
if (actSysMonitorParams[SYS_NET_IN] == 1) {
if (sysRetResults[SYS_NET_IN] == PROCUTILS_ERROR) {
if (autoDisableMonitoring)
actSysMonitorParams[SYS_NET_IN] = 0;
} else if (sysRetResults[SYS_NET_IN] != RET_ERROR) {
for (i = 0; i < nInterfaces; i++) {
if (currentNetIn[i] != RET_ERROR){
paramNames[nParams] = (char *)malloc(20 * sizeof(char));
strncpy(paramNames[nParams], interfaceNames[i], 16);
strcat(paramNames[nParams], "_in");
paramValues[nParams] = (char *)¤tNetIn[i];
valueTypes[nParams] = XDR_REAL64;
nParams++;
}
}
}
}
if (actSysMonitorParams[SYS_NET_OUT] == 1) {
if (sysRetResults[SYS_NET_IN] == PROCUTILS_ERROR) {
if (autoDisableMonitoring)
actSysMonitorParams[SYS_NET_OUT] = 0;
} else if (sysRetResults[SYS_NET_OUT] != RET_ERROR) {
for (i = 0; i < nInterfaces; i++) {
if (currentNetOut[i] != RET_ERROR){
paramNames[nParams] = (char *)malloc(20 * sizeof(char));
strncpy(paramNames[nParams], interfaceNames[i], 15);
strcat(paramNames[nParams], "_out");
paramValues[nParams] = (char *)¤tNetOut[i];
valueTypes[nParams] = XDR_REAL64;
nParams++;
}
}
}
}
if (actSysMonitorParams[SYS_NET_ERRS] == 1) {
if (sysRetResults[SYS_NET_ERRS] == PROCUTILS_ERROR) {
if (autoDisableMonitoring)
actSysMonitorParams[SYS_NET_ERRS] = 0;
} else if (sysRetResults[SYS_NET_ERRS] != RET_ERROR) {
for (i = 0; i < nInterfaces; i++) {
if (currentNetErrs[i] != RET_ERROR ){
paramNames[nParams] = (char *)malloc(20 * sizeof(char));
strncpy(paramNames[nParams], interfaceNames[i], 14);
strcat(paramNames[nParams], "_errs");
paramValues[nParams] = (char *)¤tNetErrs[i];
valueTypes[nParams] = XDR_REAL64;
nParams++;
}
}
}
}
if (actSysMonitorParams[SYS_PROCESSES] == 1) {
if (sysRetResults[SYS_PROCESSES] != RET_ERROR) {
char act_states[] = {'D', 'R', 'S', 'T', 'Z'};
for (i = 0; i < 5; i++) {
if (currentProcessStates[act_states[i] - 65] != RET_ERROR){
paramNames[nParams] = (char *)malloc(20 * sizeof(char));
snprintf(paramNames[nParams], 19, "processes_%c", act_states[i]);
paramValues[nParams] = (char *)¤tProcessStates[act_states[i] - 65];
valueTypes[nParams] = XDR_REAL64;
nParams++;
}
}
}
}
if (actSysMonitorParams[SYS_NET_SOCKETS] == 1) {
if (sysRetResults[SYS_NET_SOCKETS] != RET_ERROR) {
const char * const socket_types[] = {"tcp", "udp", "icm", "unix"};
for (i = 0; i < 4; i++) {
if (currentNSockets[i] != RET_ERROR) {
paramNames[nParams] = (char *)malloc(30 * sizeof(char));
snprintf(paramNames[nParams], 29, "sockets_%s", socket_types[i]);
paramValues[nParams] = (char *)¤tNSockets[i];
valueTypes[nParams] = XDR_REAL64;
nParams++;
}
}
}
}
if (actSysMonitorParams[SYS_NET_TCP_DETAILS] == 1) {
if (sysRetResults[SYS_NET_TCP_DETAILS] != RET_ERROR) {
for (i = 0; i < N_TCP_STATES; i++) {
if (currentSocketsTCP[i] != RET_ERROR){
paramNames[nParams] = (char *)malloc(30 * sizeof(char));
snprintf(paramNames[nParams], 29, "sockets_tcp_%s", socketStatesMapTCP[i]);
paramValues[nParams] = (char *)¤tSocketsTCP[i];
valueTypes[nParams] = XDR_REAL64;
nParams++;
}
}
}
}
try {
if (nParams > 0)
sendParameters(sysMonCluster, sysMonNode, nParams,
paramNames, valueTypes, paramValues);
} catch (runtime_error& err) {
logger(WARNING, err.what());
}
this -> lastSysInfoSend = crtTime;
if (sysRetResults[SYS_NET_IN] == RET_SUCCESS) {
free(currentNetIn);
free(currentNetOut);
free(currentNetErrs);
}
for (i = 0; i < nParams; i++)
free(paramNames[i]);
free(paramNames);
free(valueTypes);
free(paramValues);
#endif
}
void ApMon::updateGeneralInfo() {
strcpy(cpuVendor, ""); strcpy(cpuFamily, "");
strcpy(cpuModel, ""); strcpy(cpuModelName, "");
if (actGenMonitorParams[GEN_CPU_MHZ] == 1 ||
actGenMonitorParams[GEN_BOGOMIPS] == 1 ||
actGenMonitorParams[GEN_CPU_VENDOR_ID] == 1 ||
actGenMonitorParams[GEN_CPU_FAMILY] == 1 ||
actGenMonitorParams[GEN_CPU_MODEL] == 1 ||
actGenMonitorParams[GEN_CPU_MODEL_NAME] == 1) {
try {
ProcUtils::getCPUInfo(*this);
} catch (procutils_error& err) {
logger(WARNING, err.what());
genRetResults[GEN_CPU_MHZ] = genRetResults[GEN_BOGOMIPS] = PROCUTILS_ERROR;
}
}
if (actGenMonitorParams[GEN_TOTAL_MEM] == 1 ||
actGenMonitorParams[GEN_TOTAL_SWAP] == 1) {
try {
ProcUtils::getSysMem(currentGenVals[GEN_TOTAL_MEM],
currentGenVals[GEN_TOTAL_SWAP]);
} catch (procutils_error& perr) {
logger(WARNING, perr.what());
genRetResults[GEN_TOTAL_MEM] = genRetResults[GEN_TOTAL_SWAP] = PROCUTILS_ERROR;
}
}
if (this -> numCPUs > 0)
currentGenVals[GEN_NO_CPUS] = this -> numCPUs;
else
genRetResults[GEN_NO_CPUS] = PROCUTILS_ERROR;
}
void ApMon::sendGeneralInfo() {
#ifndef WIN32
int nParams, maxNParams, i;
long crtTime;
char tmp_s[50];
char **paramNames, **paramValues;
int *valueTypes;
crtTime = time(NULL);
logger(INFO, "Sending general monitoring information...");
maxNParams = nGenMonitorParams + numIPs;
valueTypes = (int *)malloc(maxNParams * sizeof(int));
paramNames = (char **)malloc(maxNParams * sizeof(char *));
paramValues = (char **)malloc(maxNParams * sizeof(char *));
nParams = 0;
updateGeneralInfo();
if (actGenMonitorParams[GEN_HOSTNAME]) {
paramNames[nParams] = strdup(genMonitorParams[GEN_HOSTNAME]);
valueTypes[nParams] = XDR_STRING;
paramValues[nParams] = myHostname;
nParams++;
}
if (actGenMonitorParams[GEN_IP]) {
for (i = 0; i < this -> numIPs; i++) {
strcpy(tmp_s, "ip_");
strncat(tmp_s, interfaceNames[i], 46);
paramNames[nParams] = strdup(tmp_s);
valueTypes[nParams] = XDR_STRING;
paramValues[nParams] = this -> allMyIPs[i];
nParams++;
}
}
if (actGenMonitorParams[GEN_CPU_VENDOR_ID] && strlen(cpuVendor) != 0) {
paramNames[nParams] = strdup(genMonitorParams[GEN_CPU_VENDOR_ID]);
valueTypes[nParams] = XDR_STRING;
paramValues[nParams] = cpuVendor;
nParams++;
}
if (actGenMonitorParams[GEN_CPU_FAMILY] && strlen(cpuFamily) != 0) {
paramNames[nParams] = strdup(genMonitorParams[GEN_CPU_FAMILY]);
valueTypes[nParams] = XDR_STRING;
paramValues[nParams] = cpuFamily;
nParams++;
}
if (actGenMonitorParams[GEN_CPU_MODEL] && strlen(cpuModel) != 0) {
paramNames[nParams] = strdup(genMonitorParams[GEN_CPU_MODEL]);
valueTypes[nParams] = XDR_STRING;
paramValues[nParams] = cpuModel;
nParams++;
}
if (actGenMonitorParams[GEN_CPU_MODEL_NAME] && strlen(cpuModelName) != 0) {
paramNames[nParams] = strdup(genMonitorParams[GEN_CPU_MODEL_NAME]);
valueTypes[nParams] = XDR_STRING;
paramValues[nParams] = cpuModelName;
nParams++;
}
for (i = 0; i < nGenMonitorParams; i++) {
if (actGenMonitorParams[i] != 1 || i == GEN_IP || i == GEN_HOSTNAME ||
i == GEN_CPU_VENDOR_ID || i == GEN_CPU_FAMILY || i == GEN_CPU_MODEL
|| i == GEN_CPU_MODEL_NAME)
continue;
if (genRetResults[i] == PROCUTILS_ERROR) {
/* could not read the requested information from /proc, disable this
parameter */
if (autoDisableMonitoring)
actGenMonitorParams[i] = 0;
} else if (genRetResults[i] != RET_ERROR) {
paramNames[nParams] = strdup(genMonitorParams[i]);
paramValues[nParams] = (char *)¤tGenVals[i];
valueTypes[nParams] = XDR_REAL64;
nParams++;
}
}
try {
if (nParams > 0)
sendParameters(sysMonCluster, sysMonNode, nParams,
paramNames, valueTypes, paramValues);
} catch (runtime_error& err) {
logger(WARNING, err.what());
}
for (i = 0; i < nParams; i++)
free(paramNames[i]);
free(paramNames);
free(valueTypes);
free(paramValues);
#endif
}
void ApMon::initMonitoring() {
int i;
this -> autoDisableMonitoring = true;
this -> sysMonitoring = false;
this -> jobMonitoring = false;
this -> genMonitoring = false;
this -> confCheck = false;
#ifndef WIN32
pthread_mutex_init(&this -> mutex, NULL);
pthread_mutex_init(&this -> mutexBack, NULL);
pthread_mutex_init(&this -> mutexCond, NULL);
pthread_cond_init(&this -> confChangedCond, NULL);
#else
logger(INFO, "init mutexes...");
this -> mutex = CreateMutex(NULL, FALSE, NULL);
this -> mutexBack = CreateMutex(NULL, FALSE, NULL);
this -> mutexCond = CreateMutex(NULL, FALSE, NULL);
this -> confChangedCond = CreateEvent(NULL, FALSE, FALSE, NULL);
// Initialize the Windows Sockets library
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
logger(FATAL, "Could not initialize the Windows Sockets library (WS2_32.dll)");
}
#endif
this -> haveBkThread = false;
this -> bkThreadStarted = false;
this -> stopBkThread = false;
this -> recheckChanged = false;
this -> jobMonChanged = false;
this -> sysMonChanged = false;
this -> recheckInterval = RECHECK_INTERVAL;
this -> crtRecheckInterval = RECHECK_INTERVAL;
this -> jobMonitorInterval = JOB_MONITOR_INTERVAL;
this -> sysMonitorInterval = SYS_MONITOR_INTERVAL;
this -> nSysMonitorParams = initSysParams(this -> sysMonitorParams);
this -> nGenMonitorParams = initGenParams(this -> genMonitorParams);
this -> nJobMonitorParams = initJobParams(this -> jobMonitorParams);
initSocketStatesMapTCP(this -> socketStatesMapTCP);
this -> sysInfo_first = true;
try {
this -> lastSysInfoSend = ProcUtils::getBootTime();
} catch (procutils_error& perr) {
logger(WARNING, perr.what());
logger(WARNING, "The first system monitoring values may be inaccurate");
this -> lastSysInfoSend = 0;
}
for (i = 0; i < nSysMonitorParams; i++)
this -> lastSysVals[i] = 0;
//this -> lastUsrTime = this -> lastSysTime = 0;
//this -> lastNiceTime = this -> lastIdleTime = 0;
for (i = 0; i < nSysMonitorParams; i++) {
actSysMonitorParams[i] = 1;
sysRetResults[i] = RET_SUCCESS;
}
for (i = 0; i < nGenMonitorParams; i++) {
actGenMonitorParams[i] = 1;
genRetResults[i] = RET_SUCCESS;
}
for (i = 0; i < nJobMonitorParams; i++) {
actJobMonitorParams[i] = 1;
jobRetResults[i] = RET_SUCCESS;
}
this -> maxMsgRate = MAX_MSG_RATE;
}
void ApMon::parseXApMonLine(char *line) {
bool flag, found;
int ind;
char tmp[MAX_STRING_LEN], logmsg[200];
char *param, *value;
// char sbuf[MAX_STRING_LEN];
// char *pbuf = sbuf;
char *sep = (char *)" =";
strncpy(tmp, line, MAX_STRING_LEN-1);
char *tmp2 = tmp + strlen("xApMon_");
param = strtok/*_r*/(tmp2, sep);//, &pbuf);
value = strtok/*_r*/(NULL, sep);//, &pbuf);
/* if it is an on/off parameter, assign its value to flag */
if (strcmp(value, "on") == 0)
flag = true;
else /* if it is not an on/off paramenter the value of flag doesn't matter */
flag = false;
pthread_mutex_lock(&mutexBack);
found = false;
if (strcmp(param, "job_monitoring") == 0) {
this -> jobMonitoring = flag; found = true;
}
if (strcmp(param, "sys_monitoring") == 0) {
this -> sysMonitoring = flag; found = true;
}
if (strcmp(param, "job_interval") == 0) {
this -> jobMonitorInterval = atol(value); found = true;
}
if (strcmp(param, "sys_interval") == 0) {
this -> sysMonitorInterval = atol(value); found = true;
}
if (strcmp(param, "general_info") == 0) {
this -> genMonitoring = flag; found = true;
}
if (strcmp(param, "conf_recheck") == 0) {
this -> confCheck = flag; found = true;
}
if (strcmp(param, "recheck_interval") == 0) {
this -> recheckInterval = this -> crtRecheckInterval = atol(value);
found = true;
}
if (strcmp(param, "auto_disable") == 0) {
this -> autoDisableMonitoring = flag;
found = true;
}
if (strcmp(param, "maxMsgRate") == 0) {
this -> maxMsgRate = atoi(value);
found = true;
}
if (found) {
pthread_mutex_unlock(&mutexBack);
return;
}
if (strstr(param, "sys_") == param) {
ind = getVectIndex(param + strlen("sys_"), sysMonitorParams,
nSysMonitorParams);
if (ind < 0) {
pthread_mutex_unlock(&mutexBack);
snprintf(logmsg, 199, "Invalid parameter name in the configuration file: %s", param);
logger(WARNING, logmsg);
return;
}
found = true;
this -> actSysMonitorParams[ind] = (int)flag;
}
if (strstr(param, "job_") == param) {
ind = getVectIndex(param + strlen("job_"), jobMonitorParams,
nJobMonitorParams);
if (ind < 0) {
pthread_mutex_unlock(&mutexBack);
snprintf(logmsg, 199, "Invalid parameter name in the configuration file: %s", param);
logger(WARNING, logmsg);
return;
}
found = true;
this -> actJobMonitorParams[ind] = (int)flag;
}
if (!found) {
ind = getVectIndex(param, genMonitorParams,
nGenMonitorParams);
if (ind < 0) {
pthread_mutex_unlock(&mutexBack);
snprintf(logmsg, 199, "Invalid parameter name in the configuration file: %s", param);
logger(WARNING, logmsg);
return;
} else {
found = true;
this -> actGenMonitorParams[ind] = (int)flag;
}
}
if (!found) {
snprintf(logmsg, 199, "Invalid parameter name in the configuration file: %s", param);
logger(WARNING, logmsg);
}
pthread_mutex_unlock(&mutexBack);
}
long *apmon_mon_utils::getChildren(long pid, int& nChildren){
#ifdef WIN32
return 0;
#else
FILE *pf;
long *pids, *ppids, *children;
int nProcesses;
int i, j, status;
pid_t cpid;
char *argv[4], msg[MAX_STRING_LEN], sval[20];
bool processFound;
long mypid = getpid();
char children_f[50], np_f[50], cmd[200];
/* generate the names of the temporary files in which we have the output
of some commands */
snprintf(children_f, 49, "/tmp/apmon_children%ld", mypid);
snprintf(np_f, 49, "/tmp/apmon_np%ld", mypid);
switch (cpid = fork()) {
case -1:
return 0;
case 0:
argv[0] = (char *)"/bin/sh"; argv[1] = (char *)"-c";
snprintf(cmd, 199, "ps --no-headers -A -o ppid,pid > %s && wc -l %s > %s", children_f, children_f, np_f);
argv[2] = cmd;
/*
argv[2] = "ps --no-headers -eo ppid,pid > /tmp/apmon_children.txt && wc -l /tmp/out_children.txt > /tmp/out_np.txt";
*/
argv[3] = 0;
execv("/bin/sh", argv);
exit(RET_ERROR);
default:
if (waitpid(cpid, &status, 0) == -1) {
snprintf(msg, MAX_STRING_LEN-1, "[ getChildren() ] The number of sub-processes for %ld could not be determined", pid);
unlink(children_f); unlink(np_f);
return 0;
}
}
/* find the number of processes */
pf = fopen(np_f, "rt");
if (pf == NULL) {
unlink(np_f); unlink(children_f);
snprintf(msg, MAX_STRING_LEN-1, "[ getChildren() ] The number of sub-processes for %ld could not be determined",
pid);
return 0;
}
int retScan = fscanf(pf, "%d", &nProcesses);
if (retScan < 1)
nProcesses = 1;
fclose(pf);
unlink(np_f);
pids = (long *)malloc(nProcesses * sizeof(long));
ppids = (long *)malloc(nProcesses * sizeof(long));
/* estimated maximum size for the returned vector; it will be realloc'ed */
children = (long *)malloc(nProcesses * sizeof(long));
pf = fopen(children_f, "rt");
if (pf == NULL) {
free(pids); free(ppids); free(children);
unlink(children_f);
snprintf(msg, MAX_STRING_LEN-1, "[ getChildren() ] The sub-processes for %ld could not be determined", pid);
return 0;
}
/* scan the output of the ps command and find the children of the process,
and also check if the process is still running */
children[0] = pid; nChildren = 1;
processFound = false;
for (i = 0; i < nProcesses; i++) {
retScan = fscanf(pf, "%ld %ld", &ppids[i], &pids[i]);
if (retScan < 2)
continue;
/* look for the given process */
if (pids[i] == children[0] || ppids[i] == children[0])
processFound = true;
if (ppids[i] == children[0]) {
children[nChildren++] = pids[i];
}
}
fclose(pf);
unlink(children_f);
if (processFound == false) {
free(pids); free(ppids); free(children);
nChildren = 0;
snprintf(msg, MAX_STRING_LEN-1, "[ getChildren() ] The process %ld does not exist", pid);
return 0;
}
/* find the PIDs of all the descendant processes */
i = 1;
while (i < nChildren) {
/* find the children of the i-th child */
for (j = 0; j < nProcesses; j++) {
if (ppids[j] == children[i]) {
children[nChildren++] = pids[j];
}
}
i++;
}
snprintf(msg, MAX_STRING_LEN-1, "Sub-processes for process %ld: ", pid);
for (i = 0; i < nChildren; i++) {
snprintf(sval, 19, "%ld ", children[i]);