#!/usr/bin/gawk -f # function nextarg(option, arg) { if (argi >= ARGC) { printf ("%s: missing argument: %s%s\n", program, (length(option) == 1? "-": ""), option); exit (1); } arg = ARGV[argi]; ARGV[argi++] = ""; return (arg); } BEGIN { program = "csl"; STDERR = "/dev/stderr"; IGNORECASE = 1; __bold = "\033[01m"; __red = "\033[31m"; __green = "\033[32m"; __yellow = "\033[33m"; __blue = "\033[34m"; __c5 = "\033[35m"; __c6 = "\033[36m"; __grey = "\033[37m"; __normal = "\033[0m"; argi = 1; while (argi < ARGC && substr(ARGV[argi], 1, 1) == "-") { options = nextarg("option"); if (options == "--") break; for (i = 2; i<=length(options); i++) { c = substr(options, i, 1); if (c == "h") { n = split(nextarg("hostlist"), x, /[ ,]+/); for (i=1; i<=n; i++) colorhost[x[i]] = 1; } else if (c == "p") { n = split(nextarg("proclist"), x, /[ ,]+/); for (i=1; i<=n; i++) colorproc[x[i]] = 1; } else if (c == "g") markpattern = nextarg("pattern"); else { printf ("%s: unkown option: -%s\n", program, c) exit (1); } } } } function parsevalues(string, arr, p, var, val, x) { p = ""; if (match(string, /[A-Z]+=[^ ]/) > 0) { # NAME=VAL while (match(string, /([A-Z][A-Z0-9]*)=([^ ]*)( |$)/, x) > 0) { p = p substr(string, 1, RSTART-1) __grey x[1] __normal "=" \ ((x[1] == "error")? __red: __yellow __bold) x[2] __normal x[3]; string = substr(string, RSTART+RLENGTH); } } else { # name= VAL while (match(string, /( +|^)([a-zA-Z][-\/a-zA-Z0-9]*)= ([^,]*)(, |$)/, x) > 0) { p = p substr(string, 1, RSTART-1) x[1] __grey x[2] __normal "= " \ ((x[2] == "error")? __red: __yellow __bold) x[3] __normal x[4]; string = substr(string, RSTART+RLENGTH); } } p = p string; return (p); } /./ { date = sprintf ("%s %2s %s", $1, $2, $3); host = tolower($4); sub(/\..*$/, "", host); procval = $5; if (procval !~ /:$/) procval = procval ":"; if ((proc = tolower($5)) == "--" || proc == "last") next; sub(/.*\//, "", proc); if (match(proc, /([^\[:]+)(\[([0-9]+)\])?/, x) > 0) { proc = x[1]; pid = x[3]; } line = substr($0, 16); sub(/[^:]*:[ \t]*/, "", line); chost = (host in colorhost)? __c6 __bold $4 __normal: __green __bold $4 __normal; cproc = (proc in colorproc)? __yellow proc __normal: proc; if (pid != "") cproc = cproc "[" pid "]"; if (markpattern != "" && match(host ":" proc, markpattern) != 0) date = __yellow date __normal; if (match(line, /^[+-][a-z]+:?/) > 0) { if (substr(line, 1, 1) == "-") line = __red substr(line, 1, RLENGTH) __normal substr(line, RSTART+RLENGTH); else if (substr(line, 1, RLENGTH) == "+OK:") line = __green __bold substr(line, 1, RLENGTH) __normal substr(line, RSTART+RLENGTH); else line = __blue __bold substr(line, 1, RLENGTH) __normal substr(line, RSTART+RLENGTH); } line = parsevalues(line); print date, chost, cproc ":", line __normal; }