]> arthur.barton.de Git - netdata.git/blobdiff - src/apps_plugin.c
Merge pull request #1854 from ktsaou/master
[netdata.git] / src / apps_plugin.c
index 6996b38069a52a32cbd4a672792ef887df169e1f..fcfe92a56ecfc737cab25cb81e45c57b388ee878 100644 (file)
@@ -64,6 +64,7 @@ static int
 
 static size_t
         global_iterations_counter = 1,
+        calls_counter = 0,
         file_counter = 0;
 
 
@@ -687,19 +688,19 @@ static inline int managed_log(struct pid_stat *p, uint32_t log, int status) {
                 p->log_thrown |= log;
                 switch(log) {
                     case PID_LOG_IO:
-                        error("Cannot process %s/proc/%d/io (command '%s')", global_host_prefix, p->pid, p->comm);
+                        error("Cannot process %s/proc/%d/io (command '%s')", netdata_configured_host_prefix, p->pid, p->comm);
                         break;
 
                     case PID_LOG_STATM:
-                        error("Cannot process %s/proc/%d/statm (command '%s')", global_host_prefix, p->pid, p->comm);
+                        error("Cannot process %s/proc/%d/statm (command '%s')", netdata_configured_host_prefix, p->pid, p->comm);
                         break;
 
                     case PID_LOG_CMDLINE:
-                        error("Cannot process %s/proc/%d/cmdline (command '%s')", global_host_prefix, p->pid, p->comm);
+                        error("Cannot process %s/proc/%d/cmdline (command '%s')", netdata_configured_host_prefix, p->pid, p->comm);
                         break;
 
                     case PID_LOG_FDS:
-                        error("Cannot process entries in %s/proc/%d/fd (command '%s')", global_host_prefix, p->pid, p->comm);
+                        error("Cannot process entries in %s/proc/%d/fd (command '%s')", netdata_configured_host_prefix, p->pid, p->comm);
                         break;
 
                     case PID_LOG_STAT:
@@ -760,7 +761,7 @@ static inline int read_proc_pid_cmdline(struct pid_stat *p) {
 
     if(unlikely(!p->cmdline_filename)) {
         char filename[FILENAME_MAX + 1];
-        snprintfz(filename, FILENAME_MAX, "%s/proc/%d/cmdline", global_host_prefix, p->pid);
+        snprintfz(filename, FILENAME_MAX, "%s/proc/%d/cmdline", netdata_configured_host_prefix, p->pid);
         p->cmdline_filename = strdupz(filename);
     }
 
@@ -813,7 +814,7 @@ static inline int read_proc_pid_stat(struct pid_stat *p) {
 
     if(unlikely(!p->stat_filename)) {
         char filename[FILENAME_MAX + 1];
-        snprintfz(filename, FILENAME_MAX, "%s/proc/%d/stat", global_host_prefix, p->pid);
+        snprintfz(filename, FILENAME_MAX, "%s/proc/%d/stat", netdata_configured_host_prefix, p->pid);
         p->stat_filename = strdupz(filename);
     }
 
@@ -831,7 +832,7 @@ static inline int read_proc_pid_stat(struct pid_stat *p) {
 
     p->last_stat_collected_usec = p->stat_collected_usec;
     p->stat_collected_usec = now_monotonic_usec();
-    file_counter++;
+    calls_counter++;
 
     // p->pid           = str2pid_t(procfile_lineword(ff, 0, 0));
     char *comm          = procfile_lineword(ff, 0, 1);
@@ -935,7 +936,7 @@ static inline int read_proc_pid_stat(struct pid_stat *p) {
     }
 
     if(unlikely(debug || (p->target && p->target->debug)))
-        fprintf(stderr, "apps.plugin: READ PROC/PID/STAT: %s/proc/%d/stat, process: '%s' on target '%s' (dt=%llu) VALUES: utime=" KERNEL_UINT_FORMAT ", stime=" KERNEL_UINT_FORMAT ", cutime=" KERNEL_UINT_FORMAT ", cstime=" KERNEL_UINT_FORMAT ", minflt=" KERNEL_UINT_FORMAT ", majflt=" KERNEL_UINT_FORMAT ", cminflt=" KERNEL_UINT_FORMAT ", cmajflt=" KERNEL_UINT_FORMAT ", threads=%d\n", global_host_prefix, p->pid, p->comm, (p->target)?p->target->name:"UNSET", p->stat_collected_usec - p->last_stat_collected_usec, p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt, p->num_threads);
+        fprintf(stderr, "apps.plugin: READ PROC/PID/STAT: %s/proc/%d/stat, process: '%s' on target '%s' (dt=%llu) VALUES: utime=" KERNEL_UINT_FORMAT ", stime=" KERNEL_UINT_FORMAT ", cutime=" KERNEL_UINT_FORMAT ", cstime=" KERNEL_UINT_FORMAT ", minflt=" KERNEL_UINT_FORMAT ", majflt=" KERNEL_UINT_FORMAT ", cminflt=" KERNEL_UINT_FORMAT ", cmajflt=" KERNEL_UINT_FORMAT ", threads=%d\n", netdata_configured_host_prefix, p->pid, p->comm, (p->target)?p->target->name:"UNSET", p->stat_collected_usec - p->last_stat_collected_usec, p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt, p->num_threads);
 
     if(unlikely(global_iterations_counter == 1)) {
         p->minflt           = 0;
@@ -973,7 +974,7 @@ static inline int read_proc_pid_statm(struct pid_stat *p) {
 
     if(unlikely(!p->statm_filename)) {
         char filename[FILENAME_MAX + 1];
-        snprintfz(filename, FILENAME_MAX, "%s/proc/%d/statm", global_host_prefix, p->pid);
+        snprintfz(filename, FILENAME_MAX, "%s/proc/%d/statm", netdata_configured_host_prefix, p->pid);
         p->statm_filename = strdupz(filename);
     }
 
@@ -983,7 +984,7 @@ static inline int read_proc_pid_statm(struct pid_stat *p) {
     ff = procfile_readall(ff);
     if(unlikely(!ff)) goto cleanup;
 
-    file_counter++;
+    calls_counter++;
 
     p->statm_size           = str2kernel_uint_t(procfile_lineword(ff, 0, 0));
     p->statm_resident       = str2kernel_uint_t(procfile_lineword(ff, 0, 1));
@@ -1011,7 +1012,7 @@ static inline int read_proc_pid_io(struct pid_stat *p) {
 
     if(unlikely(!p->io_filename)) {
         char filename[FILENAME_MAX + 1];
-        snprintfz(filename, FILENAME_MAX, "%s/proc/%d/io", global_host_prefix, p->pid);
+        snprintfz(filename, FILENAME_MAX, "%s/proc/%d/io", netdata_configured_host_prefix, p->pid);
         p->io_filename = strdupz(filename);
     }
 
@@ -1022,7 +1023,7 @@ static inline int read_proc_pid_io(struct pid_stat *p) {
     ff = procfile_readall(ff);
     if(unlikely(!ff)) goto cleanup;
 
-    file_counter++;
+    calls_counter++;
 
     p->last_io_collected_usec = p->io_collected_usec;
     p->io_collected_usec = now_monotonic_usec();
@@ -1087,7 +1088,7 @@ static inline int read_proc_stat() {
     static usec_t collected_usec = 0, last_collected_usec = 0;
 
     if(unlikely(!ff)) {
-        snprintfz(filename, FILENAME_MAX, "%s/proc/stat", global_host_prefix);
+        snprintfz(filename, FILENAME_MAX, "%s/proc/stat", netdata_configured_host_prefix);
         ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
         if(unlikely(!ff)) goto cleanup;
     }
@@ -1098,7 +1099,7 @@ static inline int read_proc_stat() {
     last_collected_usec = collected_usec;
     collected_usec = now_monotonic_usec();
 
-    file_counter++;
+    calls_counter++;
 
     kernel_uint_t last;
 
@@ -1387,8 +1388,9 @@ static inline void make_all_pid_fds_negative(struct pid_stat *p) {
 }
 
 static inline void cleanup_negative_pid_fds(struct pid_stat *p) {
-    int *fd = p->fds, *end = &p->fds[p->fds_size];
-    while(fd < end) {
+    int *fd = p->fds, *fdend = &p->fds[p->fds_size];
+
+    while(fd < fdend) {
         if(unlikely(*fd < 0)) {
             file_descriptor_not_used(-(*fd));
             *fd++ = 0;
@@ -1406,7 +1408,7 @@ static inline void zero_pid_fds(struct pid_stat *p, int first, int size) {
 static inline int read_pid_file_descriptors(struct pid_stat *p) {
     if(unlikely(!p->fds_dirname)) {
         char dirname[FILENAME_MAX+1];
-        snprintfz(dirname, FILENAME_MAX, "%s/proc/%d/fd", global_host_prefix, p->pid);
+        snprintfz(dirname, FILENAME_MAX, "%s/proc/%d/fd", netdata_configured_host_prefix, p->pid);
         p->fds_dirname = strdupz(dirname);
     }
 
@@ -1429,7 +1431,7 @@ static inline int read_pid_file_descriptors(struct pid_stat *p) {
             continue;
 
         // get its number
-        int fdid = (int)str2l(de->d_name);
+        int fdid = (int) str2l(de->d_name);
         if(unlikely(fdid < 0)) continue;
 
         // check if the fds array is small
@@ -1437,7 +1439,12 @@ static inline int read_pid_file_descriptors(struct pid_stat *p) {
             // it is small, extend it
 
             if(unlikely(debug))
-                fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + MAX_SPARE_FDS);
+                fprintf(stderr
+                        , "apps.plugin: extending fd memory slots for %s from %d to %d\n"
+                        , p->comm
+                        , p->fds_size
+                        , fdid + MAX_SPARE_FDS
+                );
 
             p->fds = reallocz(p->fds, (fdid + MAX_SPARE_FDS) * sizeof(int));
 
@@ -1449,7 +1456,7 @@ static inline int read_pid_file_descriptors(struct pid_stat *p) {
         if(unlikely(p->fds[fdid] == 0)) {
             // we don't know this fd, get it
 
-            sprintf(fdname, "%s/proc/%d/fd/%s", global_host_prefix, p->pid, de->d_name);
+            sprintf(fdname, "%s/proc/%d/fd/%s", netdata_configured_host_prefix, p->pid, de->d_name);
             ssize_t l = readlink(fdname, linkname, FILENAME_MAX);
             if(unlikely(l == -1)) {
                 if(debug || (p->target && p->target->debug)) {
@@ -1468,9 +1475,10 @@ static inline int read_pid_file_descriptors(struct pid_stat *p) {
             p->fds[fdid] = file_descriptor_find_or_add(linkname);
         }
 
-        // else make it positive again, we need it
-        // of course, the actual file may have changed, but we don't care so much
-        // FIXME: we could compare the inode as returned by readdir dirent structure
+            // else make it positive again, we need it
+            // of course, the actual file may have changed, but we don't care so much
+            // FIXME: we could compare the inode as returned by readdir dirent structure
+            // UPDATE: no we cannot use inodes - under /proc inodes don't change when the link is changed
 
         else
             p->fds[fdid] = -p->fds[fdid];
@@ -1886,7 +1894,7 @@ static int collect_data_for_all_processes(void) {
 
     char dirname[FILENAME_MAX + 1];
 
-    snprintfz(dirname, FILENAME_MAX, "%s/proc", global_host_prefix);
+    snprintfz(dirname, FILENAME_MAX, "%s/proc", netdata_configured_host_prefix);
     DIR *dir = opendir(dirname);
     if(!dir) return 0;
 
@@ -1952,10 +1960,11 @@ static void cleanup_exited_pids(void) {
             if(unlikely(debug && (p->keep || p->keeploops)))
                 fprintf(stderr, " > CLEANUP cannot keep exited process %d (%s) anymore - removing it.\n", p->pid, p->comm);
 
-            for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] > 0) {
-                file_descriptor_not_used(p->fds[c]);
-                p->fds[c] = 0;
-            }
+            for(c = 0; c < p->fds_size; c++)
+                if(p->fds[c] > 0) {
+                    file_descriptor_not_used(p->fds[c]);
+                    p->fds[c] = 0;
+                }
 
             pid_t r = p->pid;
             p = p->next;
@@ -2398,6 +2407,7 @@ static usec_t send_resource_usage_to_netdata() {
                         "DIMENSION user '' incremental 1 1000\n"
                         "DIMENSION system '' incremental 1 1000\n"
                         "CHART netdata.apps_files '' 'Apps Plugin Files' 'files/s' apps.plugin netdata.apps_files line 140001 %1$d\n"
+                        "DIMENSION calls '' incremental 1 1\n"
                         "DIMENSION files '' incremental 1 1\n"
                         "DIMENSION pids '' absolute 1 1\n"
                         "DIMENSION fds '' absolute 1 1\n"
@@ -2431,6 +2441,7 @@ static usec_t send_resource_usage_to_netdata() {
         "SET system = %llu\n"
         "END\n"
         "BEGIN netdata.apps_files %llu\n"
+        "SET calls = %zu\n"
         "SET files = %zu\n"
         "SET pids = %zu\n"
         "SET fds = %d\n"
@@ -2447,6 +2458,7 @@ static usec_t send_resource_usage_to_netdata() {
         , cpuuser
         , cpusyst
         , usec
+        , calls_counter
         , file_counter
         , all_pids_count
         , all_files_len
@@ -2960,7 +2972,7 @@ static void parse_args(int argc, char **argv)
                     "\n"
                     " netdata apps.plugin %s\n"
                     " Copyright (C) 2016-2017 Costa Tsaousis <costa@tsaousis.gr>\n"
-                    " Released under GNU Public License v3 or later.\n"
+                    " Released under GNU General Public License v3 or later.\n"
                     " All rights reserved.\n"
                     "\n"
                     " This program is a data collector plugin for netdata.\n"
@@ -3090,12 +3102,12 @@ int main(int argc, char **argv) {
     error_log_errors_per_period = 100;
     error_log_throttle_period = 3600;
 
-    global_host_prefix = getenv("NETDATA_HOST_PREFIX");
-    if(global_host_prefix == NULL) {
+    netdata_configured_host_prefix = getenv("NETDATA_HOST_PREFIX");
+    if(netdata_configured_host_prefix == NULL) {
         // info("NETDATA_HOST_PREFIX is not passed from netdata");
-        global_host_prefix = "";
+        netdata_configured_host_prefix = "";
     }
-    // else info("Found NETDATA_HOST_PREFIX='%s'", global_host_prefix);
+    // else info("Found NETDATA_HOST_PREFIX='%s'", netdata_configured_host_prefix);
 
     config_dir = getenv("NETDATA_CONFIG_DIR");
     if(config_dir == NULL) {