summaryrefslogtreecommitdiffstats
path: root/example-scripts
diff options
context:
space:
mode:
Diffstat (limited to 'example-scripts')
-rwxr-xr-xexample-scripts/clipboard.sh69
-rwxr-xr-xexample-scripts/log_to_csv.sh66
-rw-r--r--example-scripts/report-demo.lnav83
-rw-r--r--example-scripts/tag-ssh-msgs.lnav10
4 files changed, 228 insertions, 0 deletions
diff --git a/example-scripts/clipboard.sh b/example-scripts/clipboard.sh
new file mode 100755
index 0000000..2cdd016
--- /dev/null
+++ b/example-scripts/clipboard.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+# Wrapper for various clipboard I/O on Linux desktop environments and Windows emulations thereof
+
+if [ -z "$STDIN_COPY_COMMAND" ] || [ -z "$STDOUT_PASTE_COMMAND" ]
+then
+ if [ -n "$WAYLAND_DISPLAY" ]
+ then
+ STDIN_COPY_COMMAND="wl-copy --foreground --type text/plain"
+ STDOUT_PASTE_COMMAND="wl-paste --no-newline"
+ elif [ -n "$DISPLAY" ]
+ then
+ if command -v xclip
+ then
+ STDIN_COPY_COMMAND="xclip -quiet -i -selection clipboard"
+ STDOUT_PASTE_COMMAND="xclip -o -selection clipboard"
+ elif command -v xsel
+ then
+ STDIN_COPY_COMMAND="xsel --nodetach -i --clipboard"
+ STDOUT_PASTE_COMMAND="xsel -o --clipboard"
+ fi
+ elif command -v lemonade
+ then
+ STDIN_COPY_COMMAND="lemonade copy"
+ STDOUT_PASTE_COMMAND="lemonade paste"
+ elif command -v doitclient
+ then
+ STDIN_COPY_COMMAND="doitclient wclip"
+ STDOUT_PASTE_COMMAND="doitclient wclip -r"
+ elif command -v win32yank.exe
+ then
+ STDIN_COPY_COMMAND="win32yank.exe -i --crlf"
+ STDOUT_PASTE_COMMAND="win32yank.exe -o --lf"
+ elif command -v clip.exe
+ then
+ STDIN_COPY_COMMAND="clip.exe"
+ STDOUT_PASTE_COMMAND=":"
+ elif [ -n "$TMUX" ]
+ then
+ STDIN_COPY_COMMAND="tmux load-buffer -"
+ STDOUT_PASTE_COMMAND="tmux save-buffer -"
+ else
+ echo 'No clipboard command' >&2
+ exit 10
+ fi > /dev/null
+fi
+
+case $1 in
+ copy) exec $STDIN_COPY_COMMAND > /dev/null 2>/dev/null ;;
+ paste) exec $STDOUT_PASTE_COMMAND < /dev/null 2>/dev/null ;;
+ "") # Try to guess intention
+ if ! [ -t 0 ] # stdin is piped
+ then
+ exec $STDIN_COPY_COMMAND > /dev/null 2>/dev/null
+ elif ! [ -t 1 ] # stdout is piped
+ then
+ exec $STDOUT_PASTE_COMMAND < /dev/null 2>/dev/null
+ else
+ export STDIN_COPY_COMMAND STDOUT_PASTE_COMMAND
+ fi
+ ;;
+ *) cat << EOF
+Usage:
+ clipboard copy
+ clipboard paste
+ . clipboard
+EOF
+ exit 10
+ ;;
+esac
diff --git a/example-scripts/log_to_csv.sh b/example-scripts/log_to_csv.sh
new file mode 100755
index 0000000..70117dd
--- /dev/null
+++ b/example-scripts/log_to_csv.sh
@@ -0,0 +1,66 @@
+#!/usr/bin/env bash
+
+#
+# An example script that converts messages in a syslog file into a
+# CSV-formatted file. The CSV file is written to the current directory
+# with the same base name as the source file. If the script is run on
+# the same file multiple times, it will only convert newly added lines.
+#
+# NOTE: lnav is going to save some state in $HOME; you might want to change
+# $HOME to something else...
+#
+# NOTE 2: Unfortunately, this is pretty inefficient right now since lnav
+# is going to store the entire log file in memory when processing the
+# result of the SQL SELECT.
+#
+
+if test $# -lt 1; then
+ echo "usage: $0 <path>"
+ echo "Convert a syslog file into CSV format."
+ exit 1
+fi
+
+if test ! -f "$1"; then
+ echo "error: expecting a log file as the first argument"
+ exit 1
+fi
+
+# Figure out a unique file name.
+out_file_base=$(basename "$1")
+counter=0
+while test -e "${out_file_base}.${counter}.csv"; do
+ counter=$((counter + 1))
+done
+export OUT_FILE="${out_file_base}.${counter}.csv"
+
+# Here's a quick summary of what this is doing:
+#
+# 1. ':load-session' will load the session data which stores which lines
+# are bookmarked in a file. We're using bookmarks to keep track of the
+# last line that we converted in a previous run of this script.
+# 2. ';CREATE TABLE helper' creates a temporary table that we use to store
+# the range of messages that we'll be converting.
+# 3. ';INSERT INTO helper' will figure out the range of lines in syslog file
+# to convert.
+# 4. ';UPDATE syslog_log' will set a bookmark on the last line of the range
+# we computed in the previous step.
+# 5. ';SELECT *' will pull all of the log messages in the computed range.
+# 6. ':write-csv-to' will write out the log messages SELECTed in step #5.
+# 7. ':save-session' will save the bookmark we set so it can be loaded on
+# future runs of this script.
+
+lnav -nq -d /tmp/lnav.err \
+ -c ":load-session" \
+ -c ";CREATE TABLE helper ( start_line int, max_line int )" \
+ -c ";INSERT INTO helper ( start_line, max_line ) VALUES (\
+ (SELECT coalesce(\
+ (SELECT max(log_line) FROM syslog_log where log_mark = 1) + 1,\
+ 0)),\
+ (SELECT max(log_line) FROM syslog_log ))" \
+ -c ";UPDATE syslog_log SET log_mark = 1 where log_line = (\
+ SELECT max_line FROM helper)" \
+ -c ";SELECT *,log_text FROM syslog_log where log_line between (\
+ SELECT start_line FROM helper) and (SELECT max_line FROM helper)" \
+ -c ':write-csv-to $OUT_FILE' \
+ -c ":save-session" \
+ "$1"
diff --git a/example-scripts/report-demo.lnav b/example-scripts/report-demo.lnav
new file mode 100644
index 0000000..aeb0040
--- /dev/null
+++ b/example-scripts/report-demo.lnav
@@ -0,0 +1,83 @@
+#
+# @synopsis: report-demo [<output-path>]
+# @description: Generate a report for requests in access_log files
+#
+
+# Figure out the file path where the report should be written to, default is
+# stdout
+;SELECT CASE
+ WHEN $1 IS NULL THEN '-'
+ ELSE $1
+ END AS out_path
+
+# Redirect output from commands to $out_path
+:redirect-to $out_path
+
+# Print an introductory message
+;SELECT printf('\n%d total requests', count(1)) AS msg FROM access_log
+:echo $msg
+
+;WITH top_paths AS (
+ SELECT
+ cs_uri_stem,
+ count(1) AS total_hits,
+ sum(sc_bytes) as bytes,
+ count(distinct c_ip) as visitors
+ FROM access_log
+ WHERE sc_status BETWEEN 200 AND 300
+ GROUP BY cs_uri_stem
+ ORDER BY total_hits DESC
+ LIMIT 50),
+ weekly_hits_with_gaps AS (
+ SELECT timeslice(log_time_msecs, '1w') AS week,
+ cs_uri_stem,
+ count(1) AS weekly_hits
+ FROM access_log
+ WHERE cs_uri_stem IN (SELECT cs_uri_stem FROM top_paths) AND
+ sc_status BETWEEN 200 AND 300
+ GROUP BY week, cs_uri_stem),
+ all_weeks AS (
+ SELECT week
+ FROM weekly_hits_with_gaps
+ GROUP BY week
+ ORDER BY week ASC),
+ weekly_hits AS (
+ SELECT all_weeks.week,
+ top_paths.cs_uri_stem,
+ ifnull(weekly_hits, 0) AS hits
+ FROM all_weeks
+ CROSS JOIN top_paths
+ LEFT JOIN weekly_hits_with_gaps
+ ON all_weeks.week = weekly_hits_with_gaps.week AND
+ top_paths.cs_uri_stem = weekly_hits_with_gaps.cs_uri_stem)
+ SELECT weekly_hits.cs_uri_stem AS Path,
+ printf('%,9d', total_hits) AS Hits,
+ printf('%,9d', visitors) AS Visitors,
+ printf('%9s', humanize_file_size(bytes)) as Amount,
+ sparkline(hits) AS Weeks
+ FROM weekly_hits
+ LEFT JOIN top_paths ON top_paths.cs_uri_stem = weekly_hits.cs_uri_stem
+ GROUP BY weekly_hits.cs_uri_stem
+ ORDER BY Hits DESC
+ LIMIT 10
+
+:write-table-to -
+
+:echo
+:echo Failed Requests
+:echo
+
+;SELECT printf('%,9d', count(1)) AS Hits,
+ printf('%,9d', count(distinct c_ip)) AS Visitors,
+ sc_status AS Status,
+ cs_method AS Method,
+ group_concat(distinct cs_version) AS Versions,
+ cs_uri_stem AS Path,
+ replicate('|', (cast(count(1) AS REAL) / $total_requests) * 100.0) AS "% of Requests"
+ FROM access_log
+ WHERE sc_status >= 400
+ GROUP BY cs_method, cs_uri_stem
+ ORDER BY Hits DESC
+ LIMIT 10
+
+:write-table-to -
diff --git a/example-scripts/tag-ssh-msgs.lnav b/example-scripts/tag-ssh-msgs.lnav
new file mode 100644
index 0000000..9ffad6a
--- /dev/null
+++ b/example-scripts/tag-ssh-msgs.lnav
@@ -0,0 +1,10 @@
+#
+# @synopsis: tag-ssh-msgs
+# @description: Tag interesting SSH log messages
+#
+
+;UPDATE all_logs
+ SET log_tags = json_concat(log_tags, '#ssh.invalid-user')
+ WHERE log_text LIKE '%Invalid user from%'
+
+;SELECT 'Tagged ' || changes() || ' messages';