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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
/* -*-mode: c; indent-tabs-mode: nil; c-basic-offset: 2; -*-
*/
/**
* Utilities for the quality of service module mod_qos.
*
* See http://opensource.adnovum.ch/mod_qos/ for further
* details.
*
* Copyright (C) 2007-2012 Pascal Buchbinder
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
static const char revision[] = "$Id: qslogger.c,v 1.9 2012/06/01 16:03:04 pbuchbinder Exp $";
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <regex.h>
#include <syslog.h>
#include <apr.h>
#include <apr_lib.h>
#include "qs_util.h"
// [Wed Mar 28 22:40:41 2012] [warn]
#define QS_DEFAULTPATTERN "^\\[[0-9a-zA-Z :]+\\] \\[([a-z]+)\\] "
// huge (2mb) buffer supporting very long long lines
#define MAX_LINE_BUFFER 2097152
#define QS_MAX_PATTERN_MA 2
static int m_default_severity = LOG_NOTICE;
/**
* ap_strcasestr()
*
* Finds the first occurrence of the substring s2 in the string s1.
*
* @param s1 Text to search the string in.
* @param s2 Search string.
* @return Position of s1 where ths string s2 has been found or NULL if
* s1 does not contain s2.
*/
static char *qs_strcasestr(const char *s1, const char *s2) {
char *p1, *p2;
if(*s2 == '\0') {
/* an empty s2 */
return((char *)s1);
}
while(1) {
for( ; (*s1 != '\0') && (apr_tolower(*s1) != apr_tolower(*s2)); s1++);
if(*s1 == '\0') {
return(NULL);
}
/* found first character of s2, see if the rest matches */
p1 = (char *)s1;
p2 = (char *)s2;
for(++p1, ++p2; apr_tolower(*p1) == apr_tolower(*p2); ++p1, ++p2) {
if(*p1 == '\0') {
/* both strings ended together */
return((char *)s1);
}
}
if(*p2 == '\0') {
/* second string ended, a match */
break;
}
/* didn't find a match here, try starting at next character in s1 */
s1++;
}
return((char *)s1);
}
/**
* Rerurns the priority value
*
* @param priorityname
* @return Priority, LOG_NOTICE (see m_default_severity) if provided name is not recognized.
*/
static int qsgetprio(const char *priorityname) {
int p = m_default_severity;
if(!priorityname) {
return p;
}
if(qs_strcasestr(priorityname, "alert")) {
p = LOG_ALERT;
} else if(qs_strcasestr(priorityname, "crit")) {
p = LOG_CRIT;
} else if(qs_strcasestr(priorityname, "debug")) {
p = LOG_DEBUG;
} else if(qs_strcasestr(priorityname, "emerg")) {
p = LOG_EMERG;
} else if(qs_strcasestr(priorityname, "err")) {
p = LOG_ERR;
} else if(qs_strcasestr(priorityname, "info")) {
p = LOG_INFO;
} else if(qs_strcasestr(priorityname, "notice")) {
p = LOG_NOTICE;
} else if(qs_strcasestr(priorityname, "panic")) {
p = LOG_EMERG;
} else if(qs_strcasestr(priorityname, "warn")) {
p = LOG_WARNING;
}
return p;
}
/**
* Extracts the severity of the message using the provided
* regular expression and determinest the priofity using
* qsgetprio().
*
* @param preg Regular expression to extract the serverity
* @param line Log fline to extract the severity from
* @return Level or LOG_NOTICE (see m_default_severity) if level could not be determined.
*/
static int qsgetlevel(regex_t preg, const char *line) {
int level = m_default_severity;
regmatch_t ma[QS_MAX_PATTERN_MA];
if(regexec(&preg, line, QS_MAX_PATTERN_MA, ma, 0) == 0) {
level = qsgetprio(&line[ma[1].rm_so]);
}
return level;
}
/* entry within the facility table */
typedef struct {
const char* name;
int f;
} qs_f_t;
/**
* Table of known facilities, see sys/syslog.h.
*/
static const qs_f_t qs_facilities[] = {
#ifdef LOG_AUTHPRIV
{ "authpriv", LOG_AUTHPRIV },
#endif
{ "auth", LOG_AUTH },
{ "cron", LOG_CRON },
{ "daemon", LOG_DAEMON },
#ifdef LOG_FTP
{ "ftp", LOG_FTP },
#endif
{ "kern", LOG_KERN },
{ "lpr", LOG_LPR },
{ "mail", LOG_MAIL },
{ "news", LOG_NEWS },
{ "security", LOG_AUTH },
{ "syslog", LOG_SYSLOG },
{ "user", LOG_USER },
{ "uucp", LOG_UUCP },
{ "local0", LOG_LOCAL0 },
{ "local1", LOG_LOCAL1 },
{ "local2", LOG_LOCAL2 },
{ "local3", LOG_LOCAL3 },
{ "local4", LOG_LOCAL4 },
{ "local5", LOG_LOCAL5 },
{ "local6", LOG_LOCAL6 },
{ "local7", LOG_LOCAL7 },
{ NULL, -1 }
};
/**
* Determines the facility (user input).
*
* @param facilityname
* @return The facility id or LOG_DAEMON if the provided
* string is unknown.
*/
static int qsgetfacility(const char *facilityname) {
int f = LOG_DAEMON;
const qs_f_t *facilities = qs_facilities;
if(!facilityname) {
return f;
}
while(facilities->name) {
if(strcasecmp(facilityname, facilities->name) == 0) {
f = facilities->f;
break;
}
facilities++;
}
return f;
}
/**
* Usage message (or man page)
*
* @param cmd
* @param man
*/
static void usage(const char *cmd, int man) {
if(man) {
//.TH [name of program] [section number] [center footer] [left footer] [center header]
printf(".TH %s 1 \"%s\" \"mod_qos utilities %s\" \"%s man page\"\n", qs_CMD(cmd), man_date,
man_version, cmd);
}
printf("\n");
if(man) {
printf(".SH NAME\n");
}
qs_man_print(man, "%s - another shell command interface to the system log module (syslog).\n", cmd);
printf("\n");
if(man) {
printf(".SH SYNOPSIS\n");
}
qs_man_print(man, "%s%s [-r <expression>] [-t <tag>] [-f <facility>] [-l <level>] [-d <level>] [-p]\n", man ? "" : "Usage: ", cmd);
printf("\n");
if(man) {
printf(".SH DESCRIPTION\n");
} else {
printf("Summary\n");
}
qs_man_print(man, "Use this utility to to forward log messages to the systems syslog\n");
qs_man_print(man, "facility, e.g., to forward the messages to a remote host.\n");
qs_man_print(man, "It ready data from stdin.\n");
printf("\n");
if(man) {
printf(".SH OPTIONS\n");
} else {
printf("Options\n");
}
if(man) printf("\n.TP\n");
qs_man_print(man, " -r <expression>\n");
if(man) printf("\n");
qs_man_print(man, " Specifies a regular expression which shall be used to\n");
qs_man_print(man, " determine the severity (syslog level) for each log line.\n");
qs_man_print(man, " The default pattern '"QS_DEFAULTPATTERN"' can\n");
qs_man_print(man, " be used for Apache error log messages but you may configure\n");
qs_man_print(man, " your own pattern matchin and other log format too. Use brackets\n");
qs_man_print(man, " to define the string enclosing the severity string.\n");
qs_man_print(man, " Default level (if severity can't be determined) is defined by the\n");
qs_man_print(man, " option '-d' (see below).\n");
if(man) printf("\n.TP\n");
qs_man_print(man, " -t <tag>\n");
if(man) printf("\n");
qs_man_print(man, " Defines the tag name which shall be used to define the origin\n");
qs_man_print(man, " of the messages, e.g. 'httpd'.\n");
if(man) printf("\n.TP\n");
qs_man_print(man, " -f <facility>\n");
if(man) printf("\n");
qs_man_print(man, " Defines the syslog facility. Default is 'daemon'.\n");
if(man) printf("\n.TP\n");
qs_man_print(man, " -l <level>\n");
if(man) printf("\n");
qs_man_print(man, " Defines the minimal severity a message must have in order to\n");
qs_man_print(man, " be forwarded. Default is 'DEBUG'.\n");
if(man) printf("\n.TP\n");
qs_man_print(man, " -d <level>\n");
if(man) printf("\n");
qs_man_print(man, " The default severity if the specified pattern (-r) does not\n");
qs_man_print(man, " match and the message's serverity can't be determined. Default\n");
qs_man_print(man, " is 'NOTICE'.\n");
if(man) printf("\n.TP\n");
qs_man_print(man, " -p\n");
if(man) printf("\n");
qs_man_print(man, " Writes data also to stdout (for piped logging).\n");
printf("\n");
if(man) {
printf(".SH EXAMPLE\n");
} else {
printf("Example:\n");
}
qs_man_println(man, " ErrorLog \"|./%s -t apache -f local7\"\n", cmd);
printf("\n");
if(man) {
printf(".SH SEE ALSO\n");
printf("qsexec(1), qsfilter2(1), qsgeo(1), qsgrep(1), qslog(1), qspng(1), qsrotate(1), qssign(1), qstail(1)\n");
printf(".SH AUTHOR\n");
printf("Pascal Buchbinder, http://opensource.adnovum.ch/mod_qos/\n");
} else {
printf("See http://opensource.adnovum.ch/mod_qos/ for further details.\n");
}
if(man) {
exit(0);
} else {
exit(1);
}
}
int main(int argc, const char * const argv[]) {
int line_len;
char *line = calloc(1, MAX_LINE_BUFFER+1);
const char *cmd = strrchr(argv[0], '/');
int pass = 0;
const char *tag = NULL;
int facility = LOG_DAEMON;
int severity = LOG_DEBUG;
int level = LOG_INFO;
const char *regexpattern = QS_DEFAULTPATTERN;
regex_t preg;
if(cmd == NULL) {
cmd = (char *)argv[0];
} else {
cmd++;
}
argc--;
argv++;
while(argc >= 1) {
if(strcmp(*argv, "-p") == 0) {
pass = 1;
} else if(strcmp(*argv, "-f") == 0) {
if (--argc >= 1) {
const char *facilityname = *(++argv);
facility = qsgetfacility(facilityname);
}
} else if(strcmp(*argv, "-l") == 0) {
if (--argc >= 1) {
const char *severityname = *(++argv);
severity = qsgetprio(severityname);
}
} else if(strcmp(*argv, "-d") == 0) {
if (--argc >= 1) {
const char *severityname = *(++argv);
m_default_severity = qsgetprio(severityname);
}
} else if(strcmp(*argv, "-t") == 0) {
if (--argc >= 1) {
tag = *(++argv);
}
} else if(strcmp(*argv, "-r") == 0) {
if (--argc >= 1) {
regexpattern = *(++argv);
}
} else if(strcmp(*argv,"-h") == 0) {
usage(cmd, 0);
} else if(strcmp(*argv,"--help") == 0) {
usage(cmd, 0);
} else if(strcmp(*argv,"-?") == 0) {
usage(cmd, 0);
} else if(strcmp(*argv,"--man") == 0) {
usage(cmd, 1);
} else {
usage(cmd, 0);
}
argc--;
argv++;
}
if(regcomp(&preg, regexpattern, REG_EXTENDED)) {
fprintf(stderr, "[%s] failed to compile pattern %s", cmd, regexpattern);
exit(1);
}
openlog(tag ? tag : getlogin(), 0, facility);
// start reading from stdin
while(fgets(line, MAX_LINE_BUFFER, stdin) != NULL) {
line_len = strlen(line) - 1;
while(line_len > 0) { // cut tailing CR/LF
if(line[line_len] >= ' ') {
break;
}
line[line_len] = '\0';
line_len--;
}
// severity is determined using the regular expression provided by the user
level = qsgetlevel(preg, line);
if(level <= severity) {
// send message
syslog(level, "%s", line);
}
if(pass) {
printf("%s\n", line);
fflush(stdout);
}
}
return 0;
}
|