2 * Mausezahn - A fast versatile traffic generator
3 * Copyright (C) 2008,2009 Herbert Haas
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License version 2 as published by the
7 * Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, see http://www.gnu.org/licenses/gpl-2.0.html
23 #define MZ_SYSLOG_HELP \
24 "| Syslog type: Send (traditional) Syslog packets via UDP.\n" \
28 "| severity, sev 0-7 .... Severity level from Emergency (0) to Debug (7)\n" \
29 "| facility, fac 0-23 .... Facility number\n" \
31 "| time hh:mm:ss .... Local time, 24-hour format\n" \
32 "| month, mon Mmm .... Current month, 1-12\n" \
33 "| day dd .... Current day, 0-31\n" \
35 "| host max 314 bytes .... Name or IP Address of sending host\n" \
39 "| Per default the severity \"Warning\" (4), the facility \"Security\" (4), and the\n" \
40 "| current time stamp is used. If no host is given, host is set to \"MZ\"\n" \
42 "| You can define the Syslog message itself using the -P flag. For example:\n" \
44 "| mz eth0 -t syslog sev=3 -P \"You have been mausezahned.\"\n" \
46 "| By the way, mz (by intention) does not check if your timestamp is valid according\n" \
47 "| calendar rules. It is generally recommended to follow the Darwin Era Calendar ;-)\n" \
52 // RFC 3164 states that a Syslog message consists of three parts: PRI, HEADER, and MSG.
54 // 1) PRI: contains facility(f) and severity(s), using the syntax "<N>" where N = f * 8 + s
56 // 2) HEADER: contains a timestamp and a sender-ID (name or IP), for example "May 25 23:42:42 Mausezahnhost"
57 // Note that instead of leading zeroes a space must be used for the day e. g. "May 5".
58 // However leading zeroes are required for hour, minutes, seconds, e. g. "01:05:09"
60 // 3) MSG: consists of TAG and CONTENT field. The TAG identifies the program or process and
61 // must not exceed 32 characters. Typically the TAG and the CONTENT fields are delimited
62 // via either a "[", or a colon (:) or a space. The CONTENT field is a simple text.
64 // EXAMPLE from RFC 3164:
66 // <34>Oct 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8
68 // EXAMPLE from Cisco Router:
70 // *Mar 23 13:45:08.727: %ENVMON-3-FAN_FAILED: Fan 2 not rotating
74 int create_syslog_packet(void)
76 unsigned int pri
, sev
, fac
, day
, curday
, mon
, curmon
;
77 char lt
[8], host
[314];
79 { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
83 struct tm curtime_broken
;
84 char argval
[MAX_PAYLOAD_SIZE
];
87 aa
=number_of_args(tx
.arg_string
);
89 if ( (getarg(tx
.arg_string
,"help", NULL
)==1) && (mode
==SYSLOG
) )
91 ca
++; // counts each argument
94 cli_print(gcli
, "%s", MZ_SYSLOG_HELP
);
101 "\n%s", MZ_SYSLOG_HELP
);
108 if ( (getarg(tx
.arg_string
,"severity", argval
)==1) ||
109 (getarg(tx
.arg_string
,"sev", argval
)==1) )
111 ca
++; // counts each argument
112 sev
= (unsigned int) str2int(argval
);
119 if ( (getarg(tx
.arg_string
,"facility", argval
)==1) ||
120 (getarg(tx
.arg_string
,"fac", argval
)==1) )
122 ca
++; // counts each argument
123 fac
= (unsigned int) str2int(argval
);
132 localtime_r (&curtime
, &curtime_broken
);
136 if (getarg(tx
.arg_string
,"time", argval
)==1)
138 ca
++; // counts each argument
139 strncpy(lt
,argval
,8);
140 // TODO: check if specified timestamp has valid format, e. g. 15:03:22
149 curmon
= curtime_broken
.tm_mon
; // Note that Jan = 0, ..., Dec = 11 !!!
151 if ( (getarg(tx
.arg_string
,"month", argval
)==1) ||
152 (getarg(tx
.arg_string
,"mon", argval
)==1) )
154 ca
++; // counts each argument
155 mon
= (unsigned int) str2int(argval
);
156 if ( (mon
<1) || (mon
>12) )
158 fprintf(stderr
, " mz/syslog: Invalid month; will use current month (%i)!\n", curmon
+1);
167 curday
= curtime_broken
.tm_mday
;
169 if (getarg(tx
.arg_string
,"day", argval
)==1)
171 ca
++; // counts each argument
172 day
= (unsigned int) str2int(argval
);
173 if ( (day
<1) || (day
>31) )
175 fprintf(stderr
, " mz/syslog: Invalid day; will use current day(%i)!\n", curday
);
185 if (getarg(tx
.arg_string
,"host", argval
)==1)
187 ca
++; // counts each argument
188 strncpy(host
,argval
,314); // 314 is just an arbitrary number ;-)
192 strcpy(host
, "MZ42");
196 // CHECK SURPLUS ARGUMENTS
198 fprintf(stderr
, "WARNING: %i unmatched arguments within argument string!\n", aa
-ca
);
202 // Now put everything together:
204 // Again the EXAMPLE from RFC 3164:
206 // <34>Oct 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8
212 sprintf((char*) tx
.udp_payload
, "<%d>%s %2i %s %s ",
219 if (tx
.ascii
) // ASCII PAYLOAD overrides hex payload
221 strncat((char *)tx
.udp_payload
, (char *)tx
.ascii_payload
, 2048);
222 tx
.ascii
=0; // avoid that 'create_udp_packet' overwrites this!
226 strcat((char *)tx
.udp_payload
, "%MZSYS-42-CRN: Main reactor exceeded critical temperature!");
230 tx
.udp_payload_s
= strlen((char *)tx
.udp_payload
);
235 tx
.udp_len
= 8 + tx
.udp_payload_s
;
239 fprintf(stderr
, "Syslog: %s\n", tx
.udp_payload
);