1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
/*
* Copyright (C) 2010 Tildeslash Ltd. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
*
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
#include <config.h>
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include "monitor.h"
#include "socket.h"
#include "event.h"
/**
* Connect to a data collector servlet and send event or status message.
*
* @author Martin Pala, <martinp@tildeslash.com>
*
*
* @file
*/
/* -------------------------------------------------------------- Prototypes */
static int data_send(Socket_T, Mmonit_T, char *);
static int data_check(Socket_T, Mmonit_T);
/* ------------------------------------------------------------------ Public */
/**
* Post event or status data message to mmonit
* @param E An event object or NULL for status data
* @return If failed, return HANDLER_MMONIT flag or HANDLER_SUCCEEDED flag if succeeded
*/
int handle_mmonit(Event_T E) {
char *D = NULL;
Mmonit_T C = Run.mmonits;
int rv = HANDLER_SUCCEEDED;
Socket_T socket;
/* The event is sent to mmonit just once - only in the case that the state changed */
if(!C || (E && !E->state_changed))
return rv;
while(!(socket = socket_create_t(C->url->hostname, C->url->port, SOCKET_TCP, C->ssl, C->timeout))) {
LogError("M/Monit: cannot open a connection to %s -- %s\n", C->url->url, STRERROR);
if((C = C->next)) {
LogInfo("M/Monit: trying next server %s\n", C->url->url);
continue;
} else {
LogError("M/Monit: no server available\n");
rv = HANDLER_MMONIT;
goto exit2;
}
}
D = status_xml(E, LEVEL_FULL);
if(!data_send(socket, C, D)) {
LogError("M/Monit: communication failed\n");
rv = HANDLER_MMONIT;
goto exit1;
}
/* Close write part of socket to indicate to M/Monit that message was sent
and stop M/Monit XML parser from waiting for more data */
socket_shutdown_write(socket);
if(!data_check(socket, C)) {
LogError("M/Monit: communication failed (%s message)\n", E ? "event" : "status");
rv = HANDLER_MMONIT;
goto exit1;
}
DEBUG("M/Monit: %s message sent to %s\n", E ? "event" : "status", C->url->url);
exit1:
FREE(D);
if(socket)
socket_free(&socket);
exit2:
return rv;
}
/* ----------------------------------------------------------------- Private */
/**
* Send message to the server
* @param C An mmonit object
* @param D Data to send
* @return TRUE if the message sending succeeded otherwise FALSE
*/
static int data_send(Socket_T socket, Mmonit_T C, char *D) {
int rv;
char *auth;
auth = Util_getBasicAuthHeader(C->url->user, C->url->password);
rv = socket_print(socket,
"POST %s HTTP/1.1\r\n"
"Host: %s:%d\r\n"
"Content-Type: text/xml\r\n"
"Content-Length: %d\r\n"
"Pragma: no-cache\r\n"
"Accept: */*\r\n"
"User-Agent: %s/%s\r\n"
"Connection: close\r\n"
"%s"
"\r\n"
"%s",
C->url->path,
C->url->hostname, C->url->port,
strlen(D),
prog, VERSION,
auth?auth:"",
D);
FREE(auth);
if(rv <0) {
LogError("M/Monit: error sending data to %s -- %s\n", C->url->url, STRERROR);
return FALSE;
}
return TRUE;
}
/**
* Check that the server returns a valid HTTP response
* @param C An mmonit object
* @return TRUE if the response is valid otherwise FALSE
*/
static int data_check(Socket_T socket, Mmonit_T C) {
int n;
int status;
char buf[STRLEN];
if(!socket_readln(socket, buf, sizeof(buf))) {
LogError("M/Monit: error receiving data from %s -- %s\n", C->url->url, STRERROR);
return FALSE;
}
Util_chomp(buf);
n = sscanf(buf, "%*s %d", &status);
if(n != 1 || (status >= 400)) {
LogError("M/Monit: message sending failed to %s -- %s\n", C->url->url, buf);
return FALSE;
}
return TRUE;
}
|