forked from netdata/netdata
-
Notifications
You must be signed in to change notification settings - Fork 2
/
apps_plugin.c
3850 lines (3126 loc) · 134 KB
/
apps_plugin.c
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
// SPDX-License-Identifier: GPL-3.0-or-later
/*
* netdata apps.plugin
* (C) Copyright 2016-2017 Costa Tsaousis <[email protected]>
* Released under GPL v3+
*/
#include "../../libnetdata/libnetdata.h"
// ----------------------------------------------------------------------------
// callback required by fatal()
void netdata_cleanup_and_exit(int ret) {
exit(ret);
}
void send_statistics( const char *action, const char *action_result, const char *action_data) {
(void) action;
(void) action_result;
(void) action_data;
return;
}
// callbacks required by popen()
void signals_block(void) {};
void signals_unblock(void) {};
void signals_reset(void) {};
// callback required by eval()
int health_variable_lookup(const char *variable, uint32_t hash, struct rrdcalc *rc, calculated_number *result) {
(void)variable;
(void)hash;
(void)rc;
(void)result;
return 0;
};
// required by get_system_cpus()
char *netdata_configured_host_prefix = "";
// ----------------------------------------------------------------------------
// debugging
static int debug_enabled = 0;
static inline void debug_log_int(const char *fmt, ... ) {
va_list args;
fprintf( stderr, "apps.plugin: ");
va_start( args, fmt );
vfprintf( stderr, fmt, args );
va_end( args );
fputc('\n', stderr);
}
#ifdef NETDATA_INTERNAL_CHECKS
#define debug_log(fmt, args...) do { if(unlikely(debug_enabled)) debug_log_int(fmt, ##args); } while(0)
#else
static inline void debug_log_dummy(void) {}
#define debug_log(fmt, args...) debug_log_dummy()
#endif
// ----------------------------------------------------------------------------
#ifdef __FreeBSD__
#include <sys/user.h>
#endif
// ----------------------------------------------------------------------------
// per O/S configuration
// the minimum PID of the system
// this is also the pid of the init process
#define INIT_PID 1
// if the way apps.plugin will work, will read the entire process list,
// including the resource utilization of each process, instantly
// set this to 1
// when set to 0, apps.plugin builds a sort list of processes, in order
// to process children processes, before parent processes
#ifdef __FreeBSD__
#define ALL_PIDS_ARE_READ_INSTANTLY 1
#else
#define ALL_PIDS_ARE_READ_INSTANTLY 0
#endif
// ----------------------------------------------------------------------------
// string lengths
#define MAX_COMPARE_NAME 100
#define MAX_NAME 100
#define MAX_CMDLINE 16384
// ----------------------------------------------------------------------------
// the rates we are going to send to netdata will have this detail a value of:
// - 1 will send just integer parts to netdata
// - 100 will send 2 decimal points
// - 1000 will send 3 decimal points
// etc.
#define RATES_DETAIL 10000ULL
// ----------------------------------------------------------------------------
// factor for calculating correct CPU time values depending on units of raw data
static unsigned int time_factor = 0;
// ----------------------------------------------------------------------------
// to avoid reallocating too frequently, we can increase the number of spare
// file descriptors used by processes.
// IMPORTANT:
// having a lot of spares, increases the CPU utilization of the plugin.
#define MAX_SPARE_FDS 1
// ----------------------------------------------------------------------------
// command line options
static int
update_every = 1,
enable_guest_charts = 0,
#ifdef __FreeBSD__
enable_file_charts = 0,
#else
enable_file_charts = 1,
max_fds_cache_seconds = 60,
#endif
enable_users_charts = 1,
enable_groups_charts = 1,
include_exited_childs = 1;
// will be changed to getenv(NETDATA_USER_CONFIG_DIR) if it exists
static char *user_config_dir = CONFIG_DIR;
static char *stock_config_dir = LIBCONFIG_DIR;
// ----------------------------------------------------------------------------
// internal flags
// handled in code (automatically set)
static int
show_guest_time = 0, // 1 when guest values are collected
show_guest_time_old = 0,
proc_pid_cmdline_is_needed = 0; // 1 when we need to read /proc/cmdline
// ----------------------------------------------------------------------------
// internal counters
static size_t
global_iterations_counter = 1,
calls_counter = 0,
file_counter = 0,
filenames_allocated_counter = 0,
inodes_changed_counter = 0,
links_changed_counter = 0,
targets_assignment_counter = 0;
// ----------------------------------------------------------------------------
// Normalization
//
// With normalization we lower the collected metrics by a factor to make them
// match the total utilization of the system.
// The discrepancy exists because apps.plugin needs some time to collect all
// the metrics. This results in utilization that exceeds the total utilization
// of the system.
//
// With normalization we align the per-process utilization, to the total of
// the system. We first consume the exited children utilization and it the
// collected values is above the total, we proportionally scale each reported
// metric.
// the total system time, as reported by /proc/stat
static kernel_uint_t
global_utime = 0,
global_stime = 0,
global_gtime = 0;
// the normalization ratios, as calculated by normalize_utilization()
double utime_fix_ratio = 1.0,
stime_fix_ratio = 1.0,
gtime_fix_ratio = 1.0,
minflt_fix_ratio = 1.0,
majflt_fix_ratio = 1.0,
cutime_fix_ratio = 1.0,
cstime_fix_ratio = 1.0,
cgtime_fix_ratio = 1.0,
cminflt_fix_ratio = 1.0,
cmajflt_fix_ratio = 1.0;
// ----------------------------------------------------------------------------
// target
//
// target is the structure that processes are aggregated to be reported
// to netdata.
//
// - Each entry in /etc/apps_groups.conf creates a target.
// - Each user and group used by a process in the system, creates a target.
struct target {
char compare[MAX_COMPARE_NAME + 1];
uint32_t comparehash;
size_t comparelen;
char id[MAX_NAME + 1];
uint32_t idhash;
char name[MAX_NAME + 1];
uid_t uid;
gid_t gid;
kernel_uint_t minflt;
kernel_uint_t cminflt;
kernel_uint_t majflt;
kernel_uint_t cmajflt;
kernel_uint_t utime;
kernel_uint_t stime;
kernel_uint_t gtime;
kernel_uint_t cutime;
kernel_uint_t cstime;
kernel_uint_t cgtime;
kernel_uint_t num_threads;
// kernel_uint_t rss;
kernel_uint_t status_vmsize;
kernel_uint_t status_vmrss;
kernel_uint_t status_vmshared;
kernel_uint_t status_rssfile;
kernel_uint_t status_rssshmem;
kernel_uint_t status_vmswap;
kernel_uint_t io_logical_bytes_read;
kernel_uint_t io_logical_bytes_written;
// kernel_uint_t io_read_calls;
// kernel_uint_t io_write_calls;
kernel_uint_t io_storage_bytes_read;
kernel_uint_t io_storage_bytes_written;
// kernel_uint_t io_cancelled_write_bytes;
int *target_fds;
int target_fds_size;
kernel_uint_t openfiles;
kernel_uint_t openpipes;
kernel_uint_t opensockets;
kernel_uint_t openinotifies;
kernel_uint_t openeventfds;
kernel_uint_t opentimerfds;
kernel_uint_t opensignalfds;
kernel_uint_t openeventpolls;
kernel_uint_t openother;
unsigned int processes; // how many processes have been merged to this
int exposed; // if set, we have sent this to netdata
int hidden; // if set, we set the hidden flag on the dimension
int debug_enabled;
int ends_with;
int starts_with; // if set, the compare string matches only the
// beginning of the command
struct target *target; // the one that will be reported to netdata
struct target *next;
};
struct target
*apps_groups_default_target = NULL, // the default target
*apps_groups_root_target = NULL, // apps_groups.conf defined
*users_root_target = NULL, // users
*groups_root_target = NULL; // user groups
size_t
apps_groups_targets_count = 0; // # of apps_groups.conf targets
// ----------------------------------------------------------------------------
// pid_stat
//
// structure to store data for each process running
// see: man proc for the description of the fields
struct pid_fd {
int fd;
#ifndef __FreeBSD__
ino_t inode;
char *filename;
uint32_t link_hash;
size_t cache_iterations_counter;
size_t cache_iterations_reset;
#endif
};
struct pid_stat {
int32_t pid;
char comm[MAX_COMPARE_NAME + 1];
char *cmdline;
uint32_t log_thrown;
// char state;
int32_t ppid;
// int32_t pgrp;
// int32_t session;
// int32_t tty_nr;
// int32_t tpgid;
// uint64_t flags;
// these are raw values collected
kernel_uint_t minflt_raw;
kernel_uint_t cminflt_raw;
kernel_uint_t majflt_raw;
kernel_uint_t cmajflt_raw;
kernel_uint_t utime_raw;
kernel_uint_t stime_raw;
kernel_uint_t gtime_raw; // guest_time
kernel_uint_t cutime_raw;
kernel_uint_t cstime_raw;
kernel_uint_t cgtime_raw; // cguest_time
// these are rates
kernel_uint_t minflt;
kernel_uint_t cminflt;
kernel_uint_t majflt;
kernel_uint_t cmajflt;
kernel_uint_t utime;
kernel_uint_t stime;
kernel_uint_t gtime;
kernel_uint_t cutime;
kernel_uint_t cstime;
kernel_uint_t cgtime;
// int64_t priority;
// int64_t nice;
int32_t num_threads;
// int64_t itrealvalue;
// kernel_uint_t starttime;
// kernel_uint_t vsize;
// kernel_uint_t rss;
// kernel_uint_t rsslim;
// kernel_uint_t starcode;
// kernel_uint_t endcode;
// kernel_uint_t startstack;
// kernel_uint_t kstkesp;
// kernel_uint_t kstkeip;
// uint64_t signal;
// uint64_t blocked;
// uint64_t sigignore;
// uint64_t sigcatch;
// uint64_t wchan;
// uint64_t nswap;
// uint64_t cnswap;
// int32_t exit_signal;
// int32_t processor;
// uint32_t rt_priority;
// uint32_t policy;
// kernel_uint_t delayacct_blkio_ticks;
uid_t uid;
gid_t gid;
kernel_uint_t status_vmsize;
kernel_uint_t status_vmrss;
kernel_uint_t status_vmshared;
kernel_uint_t status_rssfile;
kernel_uint_t status_rssshmem;
kernel_uint_t status_vmswap;
#ifndef __FreeBSD__
ARL_BASE *status_arl;
#endif
kernel_uint_t io_logical_bytes_read_raw;
kernel_uint_t io_logical_bytes_written_raw;
// kernel_uint_t io_read_calls_raw;
// kernel_uint_t io_write_calls_raw;
kernel_uint_t io_storage_bytes_read_raw;
kernel_uint_t io_storage_bytes_written_raw;
// kernel_uint_t io_cancelled_write_bytes_raw;
kernel_uint_t io_logical_bytes_read;
kernel_uint_t io_logical_bytes_written;
// kernel_uint_t io_read_calls;
// kernel_uint_t io_write_calls;
kernel_uint_t io_storage_bytes_read;
kernel_uint_t io_storage_bytes_written;
// kernel_uint_t io_cancelled_write_bytes;
struct pid_fd *fds; // array of fds it uses
size_t fds_size; // the size of the fds array
int children_count; // number of processes directly referencing this
unsigned char keep:1; // 1 when we need to keep this process in memory even after it exited
int keeploops; // increases by 1 every time keep is 1 and updated 0
unsigned char updated:1; // 1 when the process is currently running
unsigned char merged:1; // 1 when it has been merged to its parent
unsigned char read:1; // 1 when we have already read this process for this iteration
int sortlist; // higher numbers = top on the process tree
// each process gets a unique number
struct target *target; // app_groups.conf targets
struct target *user_target; // uid based targets
struct target *group_target; // gid based targets
usec_t stat_collected_usec;
usec_t last_stat_collected_usec;
usec_t io_collected_usec;
usec_t last_io_collected_usec;
char *fds_dirname; // the full directory name in /proc/PID/fd
char *stat_filename;
char *status_filename;
char *io_filename;
char *cmdline_filename;
struct pid_stat *parent;
struct pid_stat *prev;
struct pid_stat *next;
};
size_t pagesize;
// log each problem once per process
// log flood protection flags (log_thrown)
#define PID_LOG_IO 0x00000001
#define PID_LOG_STATUS 0x00000002
#define PID_LOG_CMDLINE 0x00000004
#define PID_LOG_FDS 0x00000008
#define PID_LOG_STAT 0x00000010
static struct pid_stat
*root_of_pids = NULL, // global list of all processes running
**all_pids = NULL; // to avoid allocations, we pre-allocate the
// the entire pid space.
static size_t
all_pids_count = 0; // the number of processes running
#if (ALL_PIDS_ARE_READ_INSTANTLY == 0)
// Another pre-allocated list of all possible pids.
// We need it to pids and assign them a unique sortlist id, so that we
// read parents before children. This is needed to prevent a situation where
// a child is found running, but until we read its parent, it has exited and
// its parent has accumulated its resources.
static pid_t
*all_pids_sortlist = NULL;
#endif
// ----------------------------------------------------------------------------
// file descriptor
//
// this is used to keep a global list of all open files of the system.
// it is needed in order to calculate the unique files processes have open.
#define FILE_DESCRIPTORS_INCREASE_STEP 100
// types for struct file_descriptor->type
typedef enum fd_filetype {
FILETYPE_OTHER,
FILETYPE_FILE,
FILETYPE_PIPE,
FILETYPE_SOCKET,
FILETYPE_INOTIFY,
FILETYPE_EVENTFD,
FILETYPE_EVENTPOLL,
FILETYPE_TIMERFD,
FILETYPE_SIGNALFD
} FD_FILETYPE;
struct file_descriptor {
avl avl;
#ifdef NETDATA_INTERNAL_CHECKS
uint32_t magic;
#endif /* NETDATA_INTERNAL_CHECKS */
const char *name;
uint32_t hash;
FD_FILETYPE type;
int count;
int pos;
} *all_files = NULL;
static int
all_files_len = 0,
all_files_size = 0;
// ----------------------------------------------------------------------------
// apps_groups.conf
// aggregate all processes in groups, to have a limited number of dimensions
static struct target *get_users_target(uid_t uid) {
struct target *w;
for(w = users_root_target ; w ; w = w->next)
if(w->uid == uid) return w;
w = callocz(sizeof(struct target), 1);
snprintfz(w->compare, MAX_COMPARE_NAME, "%u", uid);
w->comparehash = simple_hash(w->compare);
w->comparelen = strlen(w->compare);
snprintfz(w->id, MAX_NAME, "%u", uid);
w->idhash = simple_hash(w->id);
struct passwd *pw = getpwuid(uid);
if(!pw || !pw->pw_name || !*pw->pw_name)
snprintfz(w->name, MAX_NAME, "%u", uid);
else
snprintfz(w->name, MAX_NAME, "%s", pw->pw_name);
netdata_fix_chart_name(w->name);
w->uid = uid;
w->next = users_root_target;
users_root_target = w;
debug_log("added uid %u ('%s') target", w->uid, w->name);
return w;
}
struct target *get_groups_target(gid_t gid)
{
struct target *w;
for(w = groups_root_target ; w ; w = w->next)
if(w->gid == gid) return w;
w = callocz(sizeof(struct target), 1);
snprintfz(w->compare, MAX_COMPARE_NAME, "%u", gid);
w->comparehash = simple_hash(w->compare);
w->comparelen = strlen(w->compare);
snprintfz(w->id, MAX_NAME, "%u", gid);
w->idhash = simple_hash(w->id);
struct group *gr = getgrgid(gid);
if(!gr || !gr->gr_name || !*gr->gr_name)
snprintfz(w->name, MAX_NAME, "%u", gid);
else
snprintfz(w->name, MAX_NAME, "%s", gr->gr_name);
netdata_fix_chart_name(w->name);
w->gid = gid;
w->next = groups_root_target;
groups_root_target = w;
debug_log("added gid %u ('%s') target", w->gid, w->name);
return w;
}
// find or create a new target
// there are targets that are just aggregated to other target (the second argument)
static struct target *get_apps_groups_target(const char *id, struct target *target, const char *name) {
int tdebug = 0, thidden = target?target->hidden:0, ends_with = 0;
const char *nid = id;
// extract the options
while(nid[0] == '-' || nid[0] == '+' || nid[0] == '*') {
if(nid[0] == '-') thidden = 1;
if(nid[0] == '+') tdebug = 1;
if(nid[0] == '*') ends_with = 1;
nid++;
}
uint32_t hash = simple_hash(id);
// find if it already exists
struct target *w, *last = apps_groups_root_target;
for(w = apps_groups_root_target ; w ; w = w->next) {
if(w->idhash == hash && strncmp(nid, w->id, MAX_NAME) == 0)
return w;
last = w;
}
// find an existing target
if(unlikely(!target)) {
while(*name == '-') {
if(*name == '-') thidden = 1;
name++;
}
for(target = apps_groups_root_target ; target != NULL ; target = target->next) {
if(!target->target && strcmp(name, target->name) == 0)
break;
}
if(unlikely(debug_enabled)) {
if(unlikely(target))
debug_log("REUSING TARGET NAME '%s' on ID '%s'", target->name, target->id);
else
debug_log("NEW TARGET NAME '%s' on ID '%s'", name, id);
}
}
if(target && target->target)
fatal("Internal Error: request to link process '%s' to target '%s' which is linked to target '%s'", id, target->id, target->target->id);
w = callocz(sizeof(struct target), 1);
strncpyz(w->id, nid, MAX_NAME);
w->idhash = simple_hash(w->id);
if(unlikely(!target))
// copy the name
strncpyz(w->name, name, MAX_NAME);
else
// copy the id
strncpyz(w->name, nid, MAX_NAME);
strncpyz(w->compare, nid, MAX_COMPARE_NAME);
size_t len = strlen(w->compare);
if(w->compare[len - 1] == '*') {
w->compare[len - 1] = '\0';
w->starts_with = 1;
}
w->ends_with = ends_with;
if(w->starts_with && w->ends_with)
proc_pid_cmdline_is_needed = 1;
w->comparehash = simple_hash(w->compare);
w->comparelen = strlen(w->compare);
w->hidden = thidden;
#ifdef NETDATA_INTERNAL_CHECKS
w->debug_enabled = tdebug;
#else
if(tdebug)
fprintf(stderr, "apps.plugin has been compiled without debugging\n");
#endif
w->target = target;
// append it, to maintain the order in apps_groups.conf
if(last) last->next = w;
else apps_groups_root_target = w;
debug_log("ADDING TARGET ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s"
, w->id
, w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
, w->target?w->target->name:w->name
, (w->hidden)?"hidden":"-"
, (w->debug_enabled)?"debug":"-"
);
return w;
}
// read the apps_groups.conf file
static int read_apps_groups_conf(const char *path, const char *file)
{
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/apps_%s.conf", path, file);
debug_log("process groups file: '%s'", filename);
// ----------------------------------------
procfile *ff = procfile_open(filename, " :\t", PROCFILE_FLAG_DEFAULT);
if(!ff) return 1;
procfile_set_quotes(ff, "'\"");
ff = procfile_readall(ff);
if(!ff)
return 1;
size_t line, lines = procfile_lines(ff);
for(line = 0; line < lines ;line++) {
size_t word, words = procfile_linewords(ff, line);
if(!words) continue;
char *name = procfile_lineword(ff, line, 0);
if(!name || !*name) continue;
// find a possibly existing target
struct target *w = NULL;
// loop through all words, skipping the first one (the name)
for(word = 0; word < words ;word++) {
char *s = procfile_lineword(ff, line, word);
if(!s || !*s) continue;
if(*s == '#') break;
// is this the first word? skip it
if(s == name) continue;
// add this target
struct target *n = get_apps_groups_target(s, w, name);
if(!n) {
error("Cannot create target '%s' (line %zu, word %zu)", s, line, word);
continue;
}
// just some optimization
// to avoid searching for a target for each process
if(!w) w = n->target?n->target:n;
}
}
procfile_close(ff);
apps_groups_default_target = get_apps_groups_target("p+!o@w#e$i^r&7*5(-i)l-o_", NULL, "other"); // match nothing
if(!apps_groups_default_target)
fatal("Cannot create default target");
// allow the user to override group 'other'
if(apps_groups_default_target->target)
apps_groups_default_target = apps_groups_default_target->target;
return 0;
}
// ----------------------------------------------------------------------------
// struct pid_stat management
static inline void init_pid_fds(struct pid_stat *p, size_t first, size_t size);
static inline struct pid_stat *get_pid_entry(pid_t pid) {
if(unlikely(all_pids[pid]))
return all_pids[pid];
struct pid_stat *p = callocz(sizeof(struct pid_stat), 1);
p->fds = mallocz(sizeof(struct pid_fd) * MAX_SPARE_FDS);
p->fds_size = MAX_SPARE_FDS;
init_pid_fds(p, 0, p->fds_size);
if(likely(root_of_pids))
root_of_pids->prev = p;
p->next = root_of_pids;
root_of_pids = p;
p->pid = pid;
all_pids[pid] = p;
all_pids_count++;
return p;
}
static inline void del_pid_entry(pid_t pid) {
struct pid_stat *p = all_pids[pid];
if(unlikely(!p)) {
error("attempted to free pid %d that is not allocated.", pid);
return;
}
debug_log("process %d %s exited, deleting it.", pid, p->comm);
if(root_of_pids == p)
root_of_pids = p->next;
if(p->next) p->next->prev = p->prev;
if(p->prev) p->prev->next = p->next;
// free the filename
#ifndef __FreeBSD__
{
size_t i;
for(i = 0; i < p->fds_size; i++)
if(p->fds[i].filename)
freez(p->fds[i].filename);
}
#endif
freez(p->fds);
freez(p->fds_dirname);
freez(p->stat_filename);
freez(p->status_filename);
#ifndef __FreeBSD__
arl_free(p->status_arl);
#endif
freez(p->io_filename);
freez(p->cmdline_filename);
freez(p->cmdline);
freez(p);
all_pids[pid] = NULL;
all_pids_count--;
}
// ----------------------------------------------------------------------------
static inline int managed_log(struct pid_stat *p, uint32_t log, int status) {
if(unlikely(!status)) {
// error("command failed log %u, errno %d", log, errno);
if(unlikely(debug_enabled || errno != ENOENT)) {
if(unlikely(debug_enabled || !(p->log_thrown & log))) {
p->log_thrown |= log;
switch(log) {
case PID_LOG_IO:
#ifdef __FreeBSD__
error("Cannot fetch process %d I/O info (command '%s')", p->pid, p->comm);
#else
error("Cannot process %s/proc/%d/io (command '%s')", netdata_configured_host_prefix, p->pid, p->comm);
#endif
break;
case PID_LOG_STATUS:
#ifdef __FreeBSD__
error("Cannot fetch process %d status info (command '%s')", p->pid, p->comm);
#else
error("Cannot process %s/proc/%d/status (command '%s')", netdata_configured_host_prefix, p->pid, p->comm);
#endif
break;
case PID_LOG_CMDLINE:
#ifdef __FreeBSD__
error("Cannot fetch process %d command line (command '%s')", p->pid, p->comm);
#else
error("Cannot process %s/proc/%d/cmdline (command '%s')", netdata_configured_host_prefix, p->pid, p->comm);
#endif
break;
case PID_LOG_FDS:
#ifdef __FreeBSD__
error("Cannot fetch process %d files (command '%s')", p->pid, p->comm);
#else
error("Cannot process entries in %s/proc/%d/fd (command '%s')", netdata_configured_host_prefix, p->pid, p->comm);
#endif
break;
case PID_LOG_STAT:
break;
default:
error("unhandled error for pid %d, command '%s'", p->pid, p->comm);
break;
}
}
}
errno = 0;
}
else if(unlikely(p->log_thrown & log)) {
// error("unsetting log %u on pid %d", log, p->pid);
p->log_thrown &= ~log;
}
return status;
}
static inline void assign_target_to_pid(struct pid_stat *p) {
targets_assignment_counter++;
uint32_t hash = simple_hash(p->comm);
size_t pclen = strlen(p->comm);
struct target *w;
for(w = apps_groups_root_target; w ; w = w->next) {
// if(debug_enabled || (p->target && p->target->debug_enabled)) debug_log_int("\t\tcomparing '%s' with '%s'", w->compare, p->comm);
// find it - 4 cases:
// 1. the target is not a pattern
// 2. the target has the prefix
// 3. the target has the suffix
// 4. the target is something inside cmdline
if(unlikely(( (!w->starts_with && !w->ends_with && w->comparehash == hash && !strcmp(w->compare, p->comm))
|| (w->starts_with && !w->ends_with && !strncmp(w->compare, p->comm, w->comparelen))
|| (!w->starts_with && w->ends_with && pclen >= w->comparelen && !strcmp(w->compare, &p->comm[pclen - w->comparelen]))
|| (proc_pid_cmdline_is_needed && w->starts_with && w->ends_with && p->cmdline && strstr(p->cmdline, w->compare))
))) {
if(w->target) p->target = w->target;
else p->target = w;
if(debug_enabled || (p->target && p->target->debug_enabled))
debug_log_int("%s linked to target %s", p->comm, p->target->name);
break;
}
}
}
// ----------------------------------------------------------------------------
// update pids from proc
static inline int read_proc_pid_cmdline(struct pid_stat *p) {
static char cmdline[MAX_CMDLINE + 1];
#ifdef __FreeBSD__
size_t i, bytes = MAX_CMDLINE;
int mib[4];
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_ARGS;
mib[3] = p->pid;
if (unlikely(sysctl(mib, 4, cmdline, &bytes, NULL, 0)))
goto cleanup;
#else
if(unlikely(!p->cmdline_filename)) {
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/proc/%d/cmdline", netdata_configured_host_prefix, p->pid);
p->cmdline_filename = strdupz(filename);
}
int fd = open(p->cmdline_filename, procfile_open_flags, 0666);
if(unlikely(fd == -1)) goto cleanup;
ssize_t i, bytes = read(fd, cmdline, MAX_CMDLINE);
close(fd);
if(unlikely(bytes < 0)) goto cleanup;
#endif
cmdline[bytes] = '\0';
for(i = 0; i < bytes ; i++) {
if(unlikely(!cmdline[i])) cmdline[i] = ' ';
}
if(p->cmdline) freez(p->cmdline);
p->cmdline = strdupz(cmdline);
debug_log("Read file '%s' contents: %s", p->cmdline_filename, p->cmdline);
return 1;
cleanup:
// copy the command to the command line
if(p->cmdline) freez(p->cmdline);
p->cmdline = strdupz(p->comm);
return 0;
}
// ----------------------------------------------------------------------------
// macro to calculate the incremental rate of a value
// each parameter is accessed only ONCE - so it is safe to pass function calls
// or other macros as parameters
#define incremental_rate(rate_variable, last_kernel_variable, new_kernel_value, collected_usec, last_collected_usec) { \
kernel_uint_t _new_tmp = new_kernel_value; \
(rate_variable) = (_new_tmp - (last_kernel_variable)) * (USEC_PER_SEC * RATES_DETAIL) / ((collected_usec) - (last_collected_usec)); \
(last_kernel_variable) = _new_tmp; \
}
// the same macro for struct pid members
#define pid_incremental_rate(type, var, value) \
incremental_rate(var, var##_raw, value, p->type##_collected_usec, p->last_##type##_collected_usec)
// ----------------------------------------------------------------------------
#ifndef __FreeBSD__
struct arl_callback_ptr {
struct pid_stat *p;
procfile *ff;
size_t line;
};
void arl_callback_status_uid(const char *name, uint32_t hash, const char *value, void *dst) {
(void)name; (void)hash; (void)value;
struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 5)) return;
//const char *real_uid = procfile_lineword(aptr->ff, aptr->line, 1);
const char *effective_uid = procfile_lineword(aptr->ff, aptr->line, 2);
//const char *saved_uid = procfile_lineword(aptr->ff, aptr->line, 3);
//const char *filesystem_uid = procfile_lineword(aptr->ff, aptr->line, 4);
if(likely(effective_uid && *effective_uid))
aptr->p->uid = (uid_t)str2l(effective_uid);
}
void arl_callback_status_gid(const char *name, uint32_t hash, const char *value, void *dst) {
(void)name; (void)hash; (void)value;
struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 5)) return;
//const char *real_gid = procfile_lineword(aptr->ff, aptr->line, 1);
const char *effective_gid = procfile_lineword(aptr->ff, aptr->line, 2);
//const char *saved_gid = procfile_lineword(aptr->ff, aptr->line, 3);
//const char *filesystem_gid = procfile_lineword(aptr->ff, aptr->line, 4);
if(likely(effective_gid && *effective_gid))
aptr->p->gid = (uid_t)str2l(effective_gid);
}
void arl_callback_status_vmsize(const char *name, uint32_t hash, const char *value, void *dst) {
(void)name; (void)hash; (void)value;
struct arl_callback_ptr *aptr = (struct arl_callback_ptr *)dst;
if(unlikely(procfile_linewords(aptr->ff, aptr->line) < 3)) return;
aptr->p->status_vmsize = str2kernel_uint_t(procfile_lineword(aptr->ff, aptr->line, 1));
}