Mailinglist Archive: opensuse (1470 mails)

< Previous Next >
Re: [opensuse] What is the meaning of these firewall log entries?
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 02/13/2016 05:38 PM, Carlos E. R. wrote:
I found it, but had to modify. In case it is useful for others, here
goes:

program pipetimer2;

{$mode objfpc}{$H+}

uses
SysUtils;

Function StampNow (Fmt: string): AnsiString;
var S: AnsiString;
begin
DateTimeToString (S, Fmt, Now);
StampNow:= S;
end;

var
L: string;

begin
while not eof(input) do begin
readln(L);
writeln(StampNow('yyyy-mm-dd"T"hh:nn:ss.zzz'), ' ', L);
end
end.



Yes, pascal :-P


Sample run:

cer@Telcontar:~/bin/lazarus/mios> echo hello | ./pipetimer2
2016-02-14T00:37:25.890 hello
cer@Telcontar:~/bin/lazarus/mios>

I can't in good conscious let you rely on old pascal for your needs. I event
tweaked the initial code so that localtime/gmtime timestamps are now command
line option configurable. The help:

/bin/tstamp_log -h

tstamplog, version 0.01

usage: tstamplog [-f logfile (default: stdout) -hlv] [logfile]

logs input from stdin to specified logfile.

Options:

-f logfile specifies logfile to which messages are append.
(note: logfile can be specified with or without -f)
-h this help.
-l use localtime for log timestamp.
-v display version information.

example log entry: 'Feb 15 20:12:08 hostname ...message...'

It may provide a bit of flexibility that the current pascal code lacks. Give it
a go and it if works use it, if it doesn't -- delete it.

Compile:

gcc -Wall -Wextra -finline-functions -Ofast -o *tstamp_log tstamp_log.c

If you have less than gcc 4.6, use -O3 optimization.



- --
David C. Rankin, J.D.,P.E.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)

iEYEARECAAYFAlbCxpYACgkQZMpuZ8CyrchllgCeIquqI0pjtbbDPYj1Y4BNEeLD
zsAAn36RCV0MRd+VbJaNLqgWFRSktt5A
=krBU
-----END PGP SIGNATURE-----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#define PACKAGE "tstamplog"
#define VERSION "0.01"

#define xfopen(fn, mode) \
({ FILE *fp = fopen (fn, mode); \
if (!fp) { \
fprintf (stderr, "xfopen() error: file open failed '%s'.\n", fn); \
exit (EXIT_FAILURE); \
} \
fp; \
})

/* constants for
* NOPTS - number of allowed options
* MAXHN - maximum hostname length
* MAXDT - maximum date string size (-1 for length)
* MAXC - maximum characters read for each input
*/
enum { NOPTS = 8, MAXHN = 16, MAXDT = 64, MAXC = 256 };

int tstamplog (const char *s, FILE *fp, int local);
void rmcrlf (char *s);
int processopts (int argc, char **argv, int *opts);
void help (int xcode);

int main (int argc, char **argv) {

int opts[NOPTS] = {0}; /* 0 - argv index of log filename
1 - localtime flag (0 - UTC, 1 - localtime)
2-7 (unused) */
processopts (argc, argv, opts);
FILE *fp = opts[0] ? xfopen (argv[opts[0]], "a") : stdout;
char buf[MAXC] = {0};
#ifdef DEBUG
fprintf (stderr, "\n log file : %s\n use local : %d\n\n",
opts[0] ? argv[opts[0]] : "stdout", opts[1]);
#endif
while (fgets (buf, MAXC, stdin)) {
rmcrlf (buf);
tstamplog (buf, fp, opts[1]);
}
if (fp != stdout) fclose (fp);

return 0;
}

/* log 's' to 'fp' with time stamp in log file format
* mon date time hostname 's'
* local = 0, time in UTC, if 1, use localtime
*/
int tstamplog (const char *s, FILE *fp, int local)
{
time_t now = time (NULL);
int rtn = 0;
char tbuf[MAXDT] = {0};
char hstmn[MAXHN] = {0};

if (local) /* use local time if set */
rtn = strftime (tbuf, MAXDT, "%b %e %T", localtime (&now));
else /* default: time in UTC */
rtn = strftime (tbuf, MAXDT, "%b %e %T", gmtime (&now));

if (!rtn) { /* validate strftime conversion */
fprintf (stderr, "error: zero conversion in strftime.\n");
return 0;
}

if (gethostname (hstmn, MAXHN) == -1) { /* get hostname */
fprintf (stderr, "error: invalid hostname returned.\n");
return 0;
}

/* write log, return number of chars written */
return fprintf (fp, "%s %s %s\n", tbuf, hstmn, s);
}

/* remove trailing carriage return or newlines from 's' */
void rmcrlf (char *s)
{
if (!s) return;
size_t len = strlen (s);
char *p = s + len - 1;

while (p > s && (*p == '\r' || *p == '\n'))
*p = 0;
}

/* process command line options with getopt
* values are made available through the 'opts' array.
* 'optind' is returned for further command line processing.
*/
int processopts (int argc, char **argv, int *opts)
{
int opt;
while ((opt = getopt (argc, argv, "f:hlv")) != -1) {
switch (opt) {
case 'f': /* log filename */
opts[0] = optind - 1;
break;
case 'h':; /* help */
help (0);
case 'l': /* localtime flag */
opts[1] = 1;
break;
case 'v': /* show version information */
printf ("%s, version %s\n", PACKAGE, VERSION);
exit (0);
}
}
/* set argv index for logfile name if arguments remain */
if (!opts[0] && argc > optind) opts[0] = optind++;

return optind; /* return next argument index */
}

/* display help */
void help (int xcode)
{
xcode = xcode ? xcode : 0;

printf ("\n %s, version %s\n\n"
" usage: %s [-f logfile (default: stdout) -hlv] [logfile]\n\n"
" logs input from stdin to specified logfile.\n\n"
" Options:\n\n"
" -f logfile specifies logfile to which messages are
append.\n"
" (note: logfile can be specified with or without
-f)\n"
" -h this help.\n"
" -l use localtime for log timestamp.\n"
" -v display version information.\n\n"
" example log entry: 'Feb 15 20:12:08 hostname ...message...'\n\n",
PACKAGE, VERSION, PACKAGE);

exit (xcode);
}
< Previous Next >
Follow Ups