summaryrefslogtreecommitdiff
path: root/ar/.local/bin/ylog
blob: 5f7e5569fc7564f191827a93e26339077bb9fdaa (plain)
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
#!/bin/sh

HOST="root@thesiah.xyz"
LOG_DIR="/var/log/nginx"

TARGET="all"          # "all" means no target filter (show all lines)
COUNTRY="all"         # all|kr|us
SCOPE="all"           # all|access|recordings
EXCL_FIREFOX=1        # 1 = exclude Firefox lines by default
EXCLUDES="59.19.56.8" # default exclude pattern
ADD_EXCLUDES=""
LINE_LIMIT=10 # default number of lines when TARGET=all

usage() {
  cat <<'EOF'
Usage: ylog [options]

Options:
  -t TARGET    Search IP or string (default: all → no filter, show all lines)
                e.g. -t 207.96.105.230
                e.g. -t si
                e.g. -t all

  -c COUNTRY   Select country logs (default: all)
                all   : all logs
                kr    : recordings.kr.log + recordings.access.log
                us    : recordings.us.log + recordings.access.log

  -s SCOPE     Select log scope (default: all)
                all        : recordings + access
                recordings : recordings.* logs only
                access     : access.* logs only

  -n           Disable Firefox exclusion (by default, Firefox lines are excluded)

  -x PATTERN   Add extra exclude pattern (can be repeated)
                e.g. -x bot -x '192\.0\.2\.1'

  -l N         Limit number of lines (default: 10)
                Only applies when TARGET=all
                e.g. -l 50 → show last 50 lines per file

  -h           Show this help

Examples:
  ylog                  # All logs, last 10 lines each
  ylog -s recordings    # Recordings logs only, last 10 lines each
  ylog -c kr -t 1.2.3.4 # Search specific IP in Korean logs
  ylog -t all -l 50     # All logs, last 50 lines each
EOF
  exit 0
}

while getopts "t:c:s:nx:l:h" opt; do
  case "$opt" in
  t) TARGET="$OPTARG" ;;
  c) COUNTRY="$OPTARG" ;;
  s) SCOPE="$OPTARG" ;;
  n) EXCL_FIREFOX=0 ;;
  x) ADD_EXCLUDES="${ADD_EXCLUDES}
$OPTARG" ;;
  l) LINE_LIMIT="$OPTARG" ;;
  h) usage ;;
  *) usage ;;
  esac
done
shift $((OPTIND - 1))

# escape for grep -E
esc_target=$(printf '%s' "$TARGET" | sed -E 's/[][^$.*/+?(){}|\\]/\\&/g')

remote_sh='
set -eu
LOG_DIR="'"$LOG_DIR"'"
COUNTRY="'"$COUNTRY"'"
SCOPE="'"$SCOPE"'"
TARGET="'"$TARGET"'"
ESC_TARGET="'"$esc_target"'"
EXCL_FIREFOX='"$EXCL_FIREFOX"'
LINE_LIMIT='"$LINE_LIMIT"'

# collect files
pick_files() {
  # recordings: always include recordings.access.log (old merged logs)
  if [ "$SCOPE" = "recordings" ] || [ "$SCOPE" = "all" ]; then
    for q in "$LOG_DIR/recordings.access.log" "$LOG_DIR/recordings.access.log".*; do
      [ -e "$q" ] && printf "%s\n" "$q"
    done
    case "$COUNTRY" in
      kr) for q in "$LOG_DIR/recordings.kr.log" "$LOG_DIR/recordings.kr.log".*; do [ -e "$q" ] && printf "%s\n" "$q"; done ;;
      us) for q in "$LOG_DIR/recordings.us.log" "$LOG_DIR/recordings.us.log".*; do [ -e "$q" ] && printf "%s\n" "$q"; done ;;
      all)
        for p in recordings.kr.log recordings.us.log; do
          for q in "$LOG_DIR/$p" "$LOG_DIR/$p".*; do [ -e "$q" ] && printf "%s\n" "$q"; done
        done
        ;;
    esac
  fi
  # access logs
  if [ "$SCOPE" = "access" ] || [ "$SCOPE" = "all" ]; then
    for q in "$LOG_DIR/access.log" "$LOG_DIR/access.log".*; do
      [ -e "$q" ] && printf "%s\n" "$q"
    done
  fi
}

# build exclude regex
build_exre() {
  EXRE=""
  TEMP_FILE="/tmp/.ylog_exre_$$"
  rm -f "$TEMP_FILE"

  { printf "%s\n" "${EXCLUDES:-}"; printf "%s\n" "${ADD_EXCLUDES:-}"; } | sed "/^$/d" | while IFS= read -r pat
  do
    esc=$(printf "%s" "$pat" | sed -E "s/[][^$.*/+?(){}|\\]/\\\\&/g")
    if [ -s "$TEMP_FILE" ]; then
      EXRE="$(cat "$TEMP_FILE")|$esc"
    else
      EXRE="$esc"
    fi
    printf "%s" "$EXRE" > "$TEMP_FILE"
  done

  if [ -f "$TEMP_FILE" ]; then
    cat "$TEMP_FILE"
    rm -f "$TEMP_FILE"
  fi
}

FILES_TMP="/tmp/.ylog_files_$$"
pick_files | sed "/^$/d" | sort -u > "$FILES_TMP"

if [ ! -s "$FILES_TMP" ]; then
  echo "[WARN] No log files found for COUNTRY=$COUNTRY SCOPE=$SCOPE." >&2
  exit 0
fi

echo "[SCAN] Target: \"$TARGET\"  Country: $COUNTRY  Scope: $SCOPE"
echo "[FILES]"
cat "$FILES_TMP"

EXRE="$(build_exre || true)"

found=0
for f in $(cat "$FILES_TMP"); do
  [ -e "$f" ] || continue
  case "$f" in *.gz) reader="zcat -f -- \"$f\"" ;; *) reader="cat -- \"$f\"" ;; esac

  if [ "$TARGET" = "all" ]; then
    cmd="$reader"
  else
    cmd="$reader | grep -E -- \"${ESC_TARGET}[[:space:]]\""
  fi

  if [ -n "${EXRE:-}" ]; then
    cmd="$cmd | grep -v -E -- \"$EXRE\""
  fi
  [ "$EXCL_FIREFOX" -eq 1 ] && cmd="$cmd | grep -vi firefox"

  if [ "$TARGET" = "all" ]; then
    if sh -c "$cmd | tail -n $LINE_LIMIT"; then
      found=1
    fi
  else
    if sh -c "$cmd"; then
      found=1
    fi
  fi
done

rm -f "$FILES_TMP"

if [ "$TARGET" != "all" ] && [ "$found" -eq 0 ]; then
  echo "[INFO] No matches found (or filtered out)." >&2
fi
'

# remote execution
ssh "$HOST" \
  EXCLUDES="$(printf '%s' "$EXCLUDES")" \
  ADD_EXCLUDES="$(printf '%s' "$ADD_EXCLUDES")" \
  /bin/sh <<REMOTE_EOF
$remote_sh
REMOTE_EOF