]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
plugin: processes: use a const ref string and strlen i.s.o. magic offset
authorBart De Vos <bart-de-vos@telenet.be>
Mon, 26 Aug 2019 13:38:12 +0000 (15:38 +0200)
committerBart De Vos <bart-de-vos@telenet.be>
Fri, 30 Aug 2019 07:11:34 +0000 (09:11 +0200)
The simple parser is based on 'strstr', whereas the actual data does
include a white-space character to separate the 'id' (procs_running)
from the 'value' (a number). Yet parsing the number can off course only
take place 'after' the white-space. Subsequently, not finding a
'white-space' should be treated as an error condition.

Adding the 'white-space' in const string and defining the offset based
on 'strlen' fixes this properly.

src/processes.c

index f86cd39b1a7bdd71a2ec1067831143dea086dd2d..2cf88a2fe938cc68a9574d0cdd19bb16f808732d 100644 (file)
@@ -1431,7 +1431,7 @@ static int ps_read_process(long pid, process_entry_t *ps, char *state) {
 
 static int procs_running(void) {
   char buffer[4096] = {};
-
+  char id[] = "procs_running "; /* white space terminated */
   char *running;
 
   ssize_t status;
@@ -1441,13 +1441,18 @@ static int procs_running(void) {
     return -1;
   }
 
-  running = strstr(buffer, "procs_running");
+  /* the data contains :
+   * the literal string 'procs_running',
+   * a whitespace
+   * the number of running processes.
+   * The parser does include the white-space character.
+   */
+  running = strstr(buffer, id);
   if (!running) {
     WARNING("procs_running not found, assuming 1");
     return 1;
   }
-
-  running += 14;
+  running += strlen(id);
 
   return atoi(running);
 }