2 * Copyright (c) 2013 Holger Weiss <holger@weiss.in-berlin.de>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
42 static char *escape(const char *);
49 parse_command(const char *line
)
53 debug("Parsing monitoring command");
55 line
= skip_whitespace(line
);
57 command
= xstrdup(line
);
59 xasprintf(&command
, "[%lu] %s", (unsigned long)time(NULL
),
66 parse_check_result(const char *input
, char delimiter
)
68 const char *fields
[4] = { NULL
, NULL
, NULL
, NULL
};
69 char *command
, *escaped
;
71 int n
, pos
, start_pos
;
73 debug("Parsing check result");
75 if (strpbrk(input
, "\\\n") != NULL
)
76 input
= escaped
= escape(input
);
84 for (pos
= 0; n
< 4 && input
[pos
] != '\0'; pos
++)
85 if (input
[pos
] == delimiter
) {
86 lengths
[n
- 1] = pos
- start_pos
;
87 debug("Check result field %d has %d characters (%d-%d)",
88 n
, lengths
[n
- 1], start_pos
, pos
- 1);
90 /* Handle the next field. */
92 fields
[n
] = &input
[start_pos
];
98 debug("Got host check result");
100 "[%lu] PROCESS_HOST_CHECK_RESULT;%.*s;%.*s;%s",
101 (unsigned long)time(NULL
),
102 lengths
[0], fields
[0],
103 lengths
[1], fields
[1],
107 debug("Got service check result");
109 "[%lu] PROCESS_SERVICE_CHECK_RESULT;%.*s;%.*s;%.*s;%s",
110 (unsigned long)time(NULL
),
111 lengths
[0], fields
[0],
112 lengths
[1], fields
[1],
113 lengths
[2], fields
[2],
117 die("Input format incorrect, see the %s(8) man page",
132 escape(const char *input
)
136 size_t size
= strlen(input
) + 1;
138 for (in
= input
; *in
!= '\0'; in
++)
139 if (*in
== '\\' || *in
== '\n')
142 escaped
= xmalloc(size
);
144 for (in
= input
, out
= escaped
; *in
!= '\0'; in
++, out
++)
162 /* vim:set joinspaces noexpandtab textwidth=80 cinoptions=(4,u0: */