]> arthur.barton.de Git - netdata.git/blobdiff - src/apps_plugin.c
Merge remote-tracking branch 'fredericopissarra/changes' into registry
[netdata.git] / src / apps_plugin.c
index e1f03c0c87b8d823f454195f86e82966b2d38e0d..8a330ae75e28e997dd7a9f630b9a25397e5339e2 100644 (file)
@@ -223,7 +223,7 @@ long get_system_cpus(void) {
        int processors = 0;
 
        char filename[FILENAME_MAX + 1];
-       snprintf(filename, FILENAME_MAX, "%s/proc/stat", host_prefix);
+       snprintfz(filename, FILENAME_MAX, "%s/proc/stat", host_prefix);
 
        ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
        if(!ff) return 1;
@@ -252,7 +252,7 @@ long get_system_pid_max(void) {
        long mpid = 32768;
 
        char filename[FILENAME_MAX + 1];
-       snprintf(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", host_prefix);
+       snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", host_prefix);
        ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
        if(!ff) return mpid;
 
@@ -377,18 +377,18 @@ struct target *get_users_target(uid_t uid)
                return NULL;
        }
 
-       snprintf(w->compare, MAX_COMPARE_NAME, "%d", uid);
+       snprintfz(w->compare, MAX_COMPARE_NAME, "%d", uid);
        w->comparehash = simple_hash(w->compare);
        w->comparelen = strlen(w->compare);
 
-       snprintf(w->id, MAX_NAME, "%d", uid);
+       snprintfz(w->id, MAX_NAME, "%d", uid);
        w->idhash = simple_hash(w->id);
 
        struct passwd *pw = getpwuid(uid);
        if(!pw)
-               snprintf(w->name, MAX_NAME, "%d", uid);
+               snprintfz(w->name, MAX_NAME, "%d", uid);
        else
-               snprintf(w->name, MAX_NAME, "%s", pw->pw_name);
+               snprintfz(w->name, MAX_NAME, "%s", pw->pw_name);
 
        netdata_fix_chart_name(w->name);
 
@@ -415,18 +415,18 @@ struct target *get_groups_target(gid_t gid)
                return NULL;
        }
 
-       snprintf(w->compare, MAX_COMPARE_NAME, "%d", gid);
+       snprintfz(w->compare, MAX_COMPARE_NAME, "%d", gid);
        w->comparehash = simple_hash(w->compare);
        w->comparelen = strlen(w->compare);
 
-       snprintf(w->id, MAX_NAME, "%d", gid);
+       snprintfz(w->id, MAX_NAME, "%d", gid);
        w->idhash = simple_hash(w->id);
 
        struct group *gr = getgrgid(gid);
        if(!gr)
-               snprintf(w->name, MAX_NAME, "%d", gid);
+               snprintfz(w->name, MAX_NAME, "%d", gid);
        else
-               snprintf(w->name, MAX_NAME, "%s", gr->gr_name);
+               snprintfz(w->name, MAX_NAME, "%s", gr->gr_name);
 
        netdata_fix_chart_name(w->name);
 
@@ -468,12 +468,12 @@ struct target *get_apps_groups_target(const char *id, struct target *target)
                return NULL;
        }
 
-       strncpy(w->id, nid, MAX_NAME);
+       strncpyz(w->id, nid, MAX_NAME);
        w->idhash = simple_hash(w->id);
 
-       strncpy(w->name, nid, MAX_NAME);
+       strncpyz(w->name, nid, MAX_NAME);
 
-       strncpy(w->compare, nid, MAX_COMPARE_NAME);
+       strncpyz(w->compare, nid, MAX_COMPARE_NAME);
        int len = strlen(w->compare);
        if(w->compare[len - 1] == '*') {
                w->compare[len - 1] = '\0';
@@ -511,7 +511,7 @@ int read_apps_groups_conf(const char *name)
 {
        char filename[FILENAME_MAX + 1];
 
-       snprintf(filename, FILENAME_MAX, "%s/apps_%s.conf", config_dir, name);
+       snprintfz(filename, FILENAME_MAX, "%s/apps_%s.conf", config_dir, name);
 
        if(unlikely(debug))
                fprintf(stderr, "apps.plugin: process groups file: '%s'\n", filename);
@@ -563,8 +563,7 @@ int read_apps_groups_conf(const char *name)
                                t++;
                        }
 
-                       strncpy(w->name, t, MAX_NAME);
-                       w->name[MAX_NAME] = '\0';
+                       strncpyz(w->name, t, MAX_NAME);
                        w->hidden = thidden;
                        w->debug = tdebug;
 
@@ -586,7 +585,7 @@ int read_apps_groups_conf(const char *name)
        if(!apps_groups_default_target)
                error("Cannot create default target");
        else
-               strncpy(apps_groups_default_target->name, "other", MAX_NAME);
+               strncpyz(apps_groups_default_target->name, "other", MAX_NAME);
 
        return 0;
 }
@@ -776,7 +775,7 @@ void del_pid_entry(pid_t pid)
 
 int read_proc_pid_cmdline(struct pid_stat *p) {
        char filename[FILENAME_MAX + 1];
-       snprintf(filename, FILENAME_MAX, "%s/proc/%d/cmdline", host_prefix, p->pid);
+       snprintfz(filename, FILENAME_MAX, "%s/proc/%d/cmdline", host_prefix, p->pid);
 
        int fd = open(filename, O_RDONLY, 0666);
        if(unlikely(fd == -1)) return 1;
@@ -786,8 +785,7 @@ int read_proc_pid_cmdline(struct pid_stat *p) {
 
        if(bytes <= 0) {
                // copy the command to the command line
-               strncpy(p->cmdline, p->comm, MAX_CMDLINE);
-               p->cmdline[MAX_CMDLINE] = '\0';
+               strncpyz(p->cmdline, p->comm, MAX_CMDLINE);
                return 0;
        }
 
@@ -804,7 +802,7 @@ int read_proc_pid_cmdline(struct pid_stat *p) {
 int read_proc_pid_ownership(struct pid_stat *p) {
        char filename[FILENAME_MAX + 1];
 
-       snprintf(filename, FILENAME_MAX, "%s/proc/%d", host_prefix, p->pid);
+       snprintfz(filename, FILENAME_MAX, "%s/proc/%d", host_prefix, p->pid);
 
        // ----------------------------------------
        // read uid and gid
@@ -824,7 +822,7 @@ int read_proc_pid_stat(struct pid_stat *p) {
 
        char filename[FILENAME_MAX + 1];
 
-       snprintf(filename, FILENAME_MAX, "%s/proc/%d/stat", host_prefix, p->pid);
+       snprintfz(filename, FILENAME_MAX, "%s/proc/%d/stat", host_prefix, p->pid);
 
        // ----------------------------------------
 
@@ -846,8 +844,7 @@ int read_proc_pid_stat(struct pid_stat *p) {
 
        // parse the process name
        unsigned int i = 0;
-       strncpy(p->comm, procfile_lineword(ff, 0, 1), MAX_COMPARE_NAME);
-       p->comm[MAX_COMPARE_NAME] = '\0';
+       strncpyz(p->comm, procfile_lineword(ff, 0, 1), MAX_COMPARE_NAME);
 
        // p->pid                       = atol(procfile_lineword(ff, 0, 0+i));
        // comm is at 1
@@ -906,7 +903,7 @@ int read_proc_pid_statm(struct pid_stat *p) {
 
        char filename[FILENAME_MAX + 1];
 
-       snprintf(filename, FILENAME_MAX, "%s/proc/%d/statm", host_prefix, p->pid);
+       snprintfz(filename, FILENAME_MAX, "%s/proc/%d/statm", host_prefix, p->pid);
 
        ff = procfile_reopen(ff, filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
        if(!ff) return 1;
@@ -936,7 +933,7 @@ int read_proc_pid_io(struct pid_stat *p) {
 
        char filename[FILENAME_MAX + 1];
 
-       snprintf(filename, FILENAME_MAX, "%s/proc/%d/io", host_prefix, p->pid);
+       snprintfz(filename, FILENAME_MAX, "%s/proc/%d/io", host_prefix, p->pid);
 
        ff = procfile_reopen(ff, filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
        if(!ff) return 1;
@@ -1184,7 +1181,7 @@ int file_descriptor_find_or_add(const char *name)
 int read_pid_file_descriptors(struct pid_stat *p) {
        char dirname[FILENAME_MAX+1];
 
-       snprintf(dirname, FILENAME_MAX, "%s/proc/%d/fd", host_prefix, p->pid);
+       snprintfz(dirname, FILENAME_MAX, "%s/proc/%d/fd", host_prefix, p->pid);
        DIR *fds = opendir(dirname);
        if(fds) {
                int c;
@@ -1208,7 +1205,7 @@ int read_pid_file_descriptors(struct pid_stat *p) {
                                if(debug) fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + 100);
                                p->fds = realloc(p->fds, (fdid + 100) * sizeof(int));
                                if(!p->fds) {
-                                       error("Cannot re-allocate fds for %s", p->comm);
+                                       fatal("Cannot re-allocate fds for %s", p->comm);
                                        break;
                                }
 
@@ -1277,7 +1274,7 @@ int collect_data_for_all_processes_from_proc(void)
 {
        char dirname[FILENAME_MAX + 1];
 
-       snprintf(dirname, FILENAME_MAX, "%s/proc", host_prefix);
+       snprintfz(dirname, FILENAME_MAX, "%s/proc", host_prefix);
        DIR *dir = opendir(dirname);
        if(!dir) return 0;
 
@@ -2440,16 +2437,14 @@ int main(int argc, char **argv)
                exit(1);
        }
 
-       fprintf(stdout, "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %d\n", update_every);
-       fprintf(stdout, "DIMENSION user '' incremental 1 %d\n", 1000);
-       fprintf(stdout, "DIMENSION system '' incremental 1 %d\n", 1000);
-
-       fprintf(stdout, "CHART netdata.apps_files '' 'Apps Plugin Files' 'files/s' apps.plugin netdata.apps_files line 140001 %d\n", update_every);
-       fprintf(stdout, "DIMENSION files '' incremental 1 1\n");
-       fprintf(stdout, "DIMENSION pids '' absolute 1 1\n");
-       fprintf(stdout, "DIMENSION fds '' absolute 1 1\n");
-       fprintf(stdout, "DIMENSION targets '' absolute 1 1\n");
-
+       fprintf(stdout, "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %1$d\n"
+                       "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 files '' incremental 1 1\n"  
+                  "DIMENSION pids '' absolute 1 1\n"  
+                       "DIMENSION fds '' absolute 1 1\n"  
+                       "DIMENSION targets '' absolute 1 1\n", update_every);
 
 #ifndef PROFILING_MODE
        unsigned long long sunext = (time(NULL) - (time(NULL) % update_every) + update_every) * 1000000ULL;
@@ -2488,7 +2483,6 @@ int main(int argc, char **argv)
                send_collected_data_to_netdata(groups_root_target, "groups", dt);
 
                if(debug) fprintf(stderr, "apps.plugin: done Loop No %llu\n", counter);
-               fflush(NULL);
 
                current_t = time(NULL);