@cryptotaxi247 / netdata-1 / commits / 50c97c72c

apps.plugin inherits host prefix from netdata #43

Costa Tsaousis (ktsaou) committed Jan 11, 2016 at 09:00 UTC 50c97c72c55ae211ad43f5cc7debf194e154426f
2 files changed +26 -13
src/apps_plugin.c
+25 -13
@@ -1,7 +1,6 @@
1 // TODO
2 //
3 -// 1. support -ch option to set global /proc and /sys prefix
4 -// 2. disable RESET_OR_OVERFLOW check in charts
3 +// 1. disable RESET_OR_OVERFLOW check in charts
4
5 #ifdef HAVE_CONFIG_H
6 #include <config.h>
@@ -45,6 +44,8 @@ int debug = 0;
44 int update_every = 1;
45 unsigned long long file_counter = 0;
46
47 +char *host_prefix = "";
48 +
49 // ----------------------------------------------------------------------------
50 // memory debugger
51
@@ -204,7 +205,10 @@ procfile *ff = NULL;
205 long get_processors(void) {
206 int processors = 0;
207
207 - ff = procfile_reopen(ff, "/proc/stat", "", PROCFILE_FLAG_DEFAULT);
208 + char filename[FILENAME_MAX + 1];
209 + snprintf(filename, FILENAME_MAX, "%s/proc/stat", host_prefix);
210 +
211 + ff = procfile_reopen(ff, filename, "", PROCFILE_FLAG_DEFAULT);
212 if(!ff) return 1;
213
214 ff = procfile_readall(ff);
@@ -229,7 +233,9 @@ long get_processors(void) {
233 long get_pid_max(void) {
234 long mpid = 32768;
235
232 - ff = procfile_reopen(ff, "/proc/sys/kernel/pid_max", "", PROCFILE_FLAG_DEFAULT);
236 + char filename[FILENAME_MAX + 1];
237 + snprintf(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", host_prefix);
238 + ff = procfile_reopen(ff, filename, "", PROCFILE_FLAG_DEFAULT);
239 if(!ff) return mpid;
240
241 ff = procfile_readall(ff);
@@ -610,7 +616,7 @@ void del_pid_entry(pid_t pid)
616 int read_proc_pid_stat(struct pid_stat *p) {
617 char filename[FILENAME_MAX + 1];
618
613 - snprintf(filename, FILENAME_MAX, "/proc/%d/stat", p->pid);
619 + snprintf(filename, FILENAME_MAX, "%s/proc/%d/stat", host_prefix, p->pid);
620
621 ff = procfile_reopen(ff, filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
622 if(!ff) return 1;
@@ -699,7 +705,7 @@ int read_proc_pid_stat(struct pid_stat *p) {
705 int read_proc_pid_statm(struct pid_stat *p) {
706 char filename[FILENAME_MAX + 1];
707
702 - snprintf(filename, FILENAME_MAX, "/proc/%d/statm", p->pid);
708 + snprintf(filename, FILENAME_MAX, "%s/proc/%d/statm", host_prefix, p->pid);
709
710 ff = procfile_reopen(ff, filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
711 if(!ff) return 1;
@@ -727,7 +733,7 @@ int read_proc_pid_statm(struct pid_stat *p) {
733 int read_proc_pid_io(struct pid_stat *p) {
734 char filename[FILENAME_MAX + 1];
735
730 - snprintf(filename, FILENAME_MAX, "/proc/%d/io", p->pid);
736 + snprintf(filename, FILENAME_MAX, "%s/proc/%d/io", host_prefix, p->pid);
737
738 ff = procfile_reopen(ff, filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
739 if(!ff) return 1;
@@ -1020,7 +1026,10 @@ int update_from_proc(void)
1026 static long count_errors = 0;
1027
1028 char filename[FILENAME_MAX+1];
1023 - DIR *dir = opendir("/proc");
1029 + char dirname[FILENAME_MAX + 1];
1030 +
1031 + snprintf(dirname, FILENAME_MAX, "%s/proc", host_prefix);
1032 + DIR *dir = opendir(dirname);
1033 if(!dir) return 0;
1034
1035 struct dirent *file = NULL;
@@ -1050,7 +1059,7 @@ int update_from_proc(void)
1059
1060 if(read_proc_pid_stat(p)) {
1061 if(!count_errors++ || debug || (p->target && p->target->debug))
1053 - error("Cannot process /proc/%d/stat", pid);
1062 + error("Cannot process %s/proc/%d/stat", host_prefix, pid);
1063
1064 continue;
1065 }
@@ -1062,7 +1071,7 @@ int update_from_proc(void)
1071
1072 if(read_proc_pid_statm(p)) {
1073 if(!count_errors++ || debug || (p->target && p->target->debug))
1065 - error("Cannot process /proc/%d/statm", pid);
1074 + error("Cannot process %s/proc/%d/statm", host_prefix, pid);
1075
1076 continue;
1077 }
@@ -1073,7 +1082,7 @@ int update_from_proc(void)
1082
1083 if(read_proc_pid_io(p)) {
1084 if(!count_errors++ || debug || (p->target && p->target->debug))
1076 - error("Cannot process /proc/%d/io", pid);
1085 + error("Cannot process %s/proc/%d/io", host_prefix, pid);
1086
1087 continue;
1088 }
@@ -1102,7 +1111,7 @@ int update_from_proc(void)
1111 // --------------------------------------------------------------------
1112 // /proc/<pid>/fd
1113
1105 - snprintf(filename, FILENAME_MAX, "/proc/%s/fd", file->d_name);
1114 + snprintf(filename, FILENAME_MAX, "%s/proc/%s/fd", host_prefix, file->d_name);
1115 DIR *fds = opendir(filename);
1116 if(fds) {
1117 int c;
@@ -1136,7 +1145,7 @@ int update_from_proc(void)
1145 if(p->fds[fdid] == 0) {
1146 // we don't know this fd, get it
1147
1139 - sprintf(fdname, "/proc/%s/fd/%s", file->d_name, de->d_name);
1148 + sprintf(fdname, "%s/proc/%s/fd/%s", host_prefix, file->d_name, de->d_name);
1149 ssize_t l = readlink(fdname, linkname, FILENAME_MAX);
1150 if(l == -1) {
1151 if(debug || (p->target && p->target->debug)) {
@@ -1909,6 +1918,9 @@ int main(int argc, char **argv)
1918 // set the name for logging
1919 program_name = "apps.plugin";
1920
1921 + host_prefix = getenv("NETDATA_HOST_PREFIX");
1922 + if(host_prefix == NULL) host_prefix = "";
1923 +
1924 info("starting...");
1925
1926 procfile_adaptive_initial_allocation = 1;
src/main.c
+1
@@ -257,6 +257,7 @@ int main(int argc, char **argv)
257 setenv("NETDATA_WEB_DIR" , config_get("global", "web files directory", WEB_DIR) , 1);
258 setenv("NETDATA_CACHE_DIR" , config_get("global", "cache directory" , CACHE_DIR) , 1);
259 setenv("NETDATA_LOG_DIR" , config_get("global", "log directory" , LOG_DIR) , 1);
260 + setenv("NETDATA_HOST_PREFIX", config_get("global", "host access prefix" , "") , 1);
261
262 // avoid extended to stat(/etc/localtime)
263 // http://stackoverflow.com/questions/4554271/how-to-avoid-excessive-stat-etc-localtime-calls-in-strftime-on-linux