Nagios-Grapher: write_fifo does not process '\n' (diff included)

Heiko Schlittermann hs at schlittermann.de
Wed Nov 21 23:25:00 CET 2007


Hello,

the example for the nagios grapher command is about this way:

    define command{
	...
	command_line .../contrib/fifo_write .../rw/ngraph.pipe '$HOSTNAME$\t...$SERVICEPERFDATA$\n' 3
    }


But the fifo_write doesn't process the '\n'. This brings trailing 'n'
into the fifo output, but no newline. 

Currently I'm not even sure, why the pipe reader (collect2.pl) thinks
there's a line to read..., if the writer doesn't send a newline. But
this is another issue.

Here is the diff to fifo_write.c, which now processes \t and \n is
extensible. (But extension should be done carefully, because the nagios
macros may contain control characters!)

If somebody please could review it. And if it turns out to be useful,
I'd like to see it in the upstream source :)

diff -ruN NagiosGrapher-1.6.1-rc5-0.3.orig/contrib/fifo_write/C/fifo_write.c NagiosGrapher-1.6.1-rc5-0.3/contrib/fifo_write/C/fifo_write.c
--- NagiosGrapher-1.6.1-rc5-0.3.orig/contrib/fifo_write/C/fifo_write.c	2005-12-29 16:20:59.000000000 +0100
+++ NagiosGrapher-1.6.1-rc5-0.3/contrib/fifo_write/C/fifo_write.c	2007-11-21 23:20:06.000000000 +0100
@@ -59,49 +59,31 @@
             hfpath = open(fpath, O_WRONLY | O_NONBLOCK);
             if (hfpath) {
 
-                //allocate memory for the output
-                char *formated_output = (char *) malloc(strlen(output));
+		//inplace edit
+		char *formatted = output;
+		char *raw = output;
+
+		//replace some control chars
+		while (*raw) {
+		    if (*raw == '\\') {
+			switch (*(raw+1)) {
+			    case 't': *(formatted++) = '\t'; raw += 2; continue;
+			    case 'n': *(formatted++) = '\n'; raw += 2; continue;
+			}
+		    }
+
+		    *(formatted++) = *(raw++);
+		}
+		*formatted = '\0';
+
+		// write to pipe
+		alarm(timeout);
+		write(hfpath, output, formatted - output);
+		alarm(0);
+
+		/* Errorhandling below */
+
 
-                //allocation worked?
-                if (formated_output) {
-                    int i, j = 0;
-                    int tab = 0;
-
-                    //replace \t with tabs
-                    for (i = 0; output[i]; i++) {
-                        if (output[i] != '\\') {
-                            if (output[i] != 't') {
-                                formated_output[j++] = output[i];
-                            } else {
-                                if (tab) {
-                                    formated_output[j++] = 9;
-                                } else {
-                                    formated_output[j++] = output[i];
-                                }
-                            }
-                            tab = 0;
-                        } else {
-                            tab = 1;
-                        }
-                    }
-                    formated_output[j] = 0;
-
-                    // write to pipe
-                    alarm(timeout);
-                    write(hfpath, formated_output,
-                          strlen(formated_output));
-                    alarm(0);
-
-                    //free memory
-                    free(formated_output);
-
-                    /* Errorhandling below */
-
-
-                } else {
-                    printf("I was not able to allocate enough memory.\n");
-                    rw = 1;
-                }
                 close(hfpath);
             } else {
                 printf("Could not open %s for writing!\n", fpath);

    Best regards from Dresden
    Viele Grüße aus Dresden
    Heiko Schlittermann
-- 
 SCHLITTERMANN.de ---------------------------- internet & unix support -
 Heiko Schlittermann HS12-RIPE -----------------------------------------
 gnupg encrypted messages are welcome - key ID: 48D0359B ---------------
 gnupg fingerprint: 3061 CFBF 2D88 F034 E8D2  7E92 EE4E AC98 48D0 359B -
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <https://www.monitoring-lists.org/archive/users/attachments/20071121/c3953af7/attachment.sig>
-------------- next part --------------
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
-------------- next part --------------
_______________________________________________
Nagios-users mailing list
Nagios-users at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting any issue. 
::: Messages without supporting info will risk being sent to /dev/null


More information about the Users mailing list