├── article ├── assets │ ├── before.gif │ ├── last_20.png │ ├── count_kc.png │ ├── with_fzf.gif │ ├── command_hist.png │ └── example_logcli.gif └── article.md ├── cfg-template ├── loki-shell.service ├── promtail-logging-config.yaml ├── loki-local-config.yaml ├── loki-docker-local-config.yaml └── loki-docker-s3-config.yaml ├── uninstall ├── shell ├── loki-shell.zsh └── loki-shell.bash ├── README.md ├── LICENSE └── install /article/assets/before.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slim-bean/loki-shell/HEAD/article/assets/before.gif -------------------------------------------------------------------------------- /article/assets/last_20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slim-bean/loki-shell/HEAD/article/assets/last_20.png -------------------------------------------------------------------------------- /article/assets/count_kc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slim-bean/loki-shell/HEAD/article/assets/count_kc.png -------------------------------------------------------------------------------- /article/assets/with_fzf.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slim-bean/loki-shell/HEAD/article/assets/with_fzf.gif -------------------------------------------------------------------------------- /article/assets/command_hist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slim-bean/loki-shell/HEAD/article/assets/command_hist.png -------------------------------------------------------------------------------- /article/assets/example_logcli.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/slim-bean/loki-shell/HEAD/article/assets/example_logcli.gif -------------------------------------------------------------------------------- /cfg-template/loki-shell.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Loki Log Aggregator 3 | After=network.target 4 | 5 | [Service] 6 | Type=simple 7 | User=USER 8 | ExecStart=FIXME/bin/loki -config.file FIXME/config/loki-binary-config.yaml 9 | Restart=always 10 | RestartSec=10 11 | 12 | [Install] 13 | WantedBy=multi-user.target -------------------------------------------------------------------------------- /cfg-template/promtail-logging-config.yaml: -------------------------------------------------------------------------------- 1 | 2 | clients: 3 | - url: HOST/loki/api/v1/push # Make sure this port matches your Loki http port 4 | backoff_config: 5 | max_period: 5s # Keep retries short such that terminal is still usable if Loki is unavailable 6 | max_retries: 3 7 | 8 | scrape_configs: 9 | - job_name: system 10 | static_configs: 11 | - labels: 12 | job: shell # We are using the --stdin flag so logs come from stdin, this flag uses the first defined scrape config 13 | # which is this config, set a label job=shell 14 | -------------------------------------------------------------------------------- /uninstall: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | cat </dev/null; then 2 | _bufcmd(){ 3 | stdbuf -o0 awk '!seen[$0]++' 4 | } 5 | else 6 | _bufcmd(){ 7 | gstdbuf -o0 awk '!seen[$0]++' 8 | } 9 | fi 10 | 11 | _load_all_cmd(){ 12 | END=$(date --iso-8601=seconds) 13 | for i in `seq 720 720 8640` 14 | do 15 | START=$(date -d "-$i hours" --iso-8601=seconds) 16 | $HOME/.loki-shell/bin/logcli query "{job=\"shell\"}" --addr=$LOKI_URL --limit=50000 --batch=1000 --from=$START --to=$END -o raw --quiet 17 | END=$START 18 | done 19 | } 20 | 21 | _load_all() { 22 | _load_all_cmd | _bufcmd 23 | } 24 | 25 | fzf-history-widget() { 26 | local selected 27 | if [[ -v LS_LOCAL ]]; then 28 | # This command is just copied from fzf with the additional header I'll try to keep it updated... 29 | selected=( $(fc -rl 1 | perl -ne 'print if !$seen{(/^\s*[0-9]+\**\s+(.*)/, $1)}++' | 30 | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS -n2..,.. --tiebreak=index --header 'LS_LOCAL is set, querying local history. unset LS_LOCAL to resume.' --bind=ctrl-r:toggle-sort,ctrl-z:ignore $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) ) 31 | else 32 | selected=( $($HOME/.loki-shell/bin/logcli query "{job=\"shell\", host=\"$HOST\"}" --addr=$LOKI_URL --limit=50000 --batch=1000 --since=720h -o raw --quiet | _bufcmd | 33 | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS -n2..,.. --tiebreak=index --header 'ctrl-r to load ALL history, export LS_LOCAL=true for querying builtin history, export PRIVATE=true to not send commands to Loki.' --bind 'ctrl-r:reload(source $HOME/.loki-shell/shell/loki-shell.zsh && _load_all)' $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) ) 34 | fi 35 | local ret=$? 36 | if [ -n "$selected" ]; then 37 | selected=$(echo $selected | tr -d '\n') 38 | zle -U $selected 39 | fi 40 | zle reset-prompt 41 | return $ret 42 | } 43 | 44 | function _send_to_loki() { 45 | if [[ -v PRIVATE ]]; then 46 | echo "PRIVATE set, not sending to loki-shell. 'unset PRIVATE' to resume." 47 | return 0 48 | fi 49 | (HISTTIMEFORMAT= builtin history -1 | 50 | sed 's/^ *\([0-9]*\)\** *//' | 51 | $HOME/.loki-shell/bin/promtail \ 52 | -config.file=$HOME/.loki-shell/config/promtail-logging-config.yaml \ 53 | --stdin -server.disable=true -log.level=error \ 54 | --client.external-labels=host=$HOST 2>&1 | logger -t loki-shell-promtail &) 55 | } 56 | [[ -z $precmd_functions ]] && precmd_functions=() 57 | [[ $precmd_functions =~ _send_to_loki ]] || precmd_functions=($precmd_functions _send_to_loki) 58 | 59 | alias hist="$HOME/.loki-shell/bin/logcli --addr=$LOKI_URL --quiet" 60 | -------------------------------------------------------------------------------- /shell/loki-shell.bash: -------------------------------------------------------------------------------- 1 | if hash stdbuf 2>/dev/null; then 2 | _bufcmd(){ 3 | stdbuf -o0 awk '!seen[$0]++' 4 | } 5 | else 6 | _bufcmd(){ 7 | gstdbuf -o0 awk '!seen[$0]++' 8 | } 9 | fi 10 | 11 | _load_all_cmd(){ 12 | END=$(date --iso-8601=seconds) 13 | for i in `seq 720 720 8640` 14 | do 15 | START=$(date -d "-$i hours" --iso-8601=seconds) 16 | $HOME/.loki-shell/bin/logcli query "{job=\"shell\"}" --addr=$LOKI_URL --limit=50000 --batch=1000 --from=$START --to=$END -o raw --quiet 17 | END=$START 18 | done 19 | } 20 | 21 | _load_all() { 22 | _load_all_cmd | _bufcmd 23 | } 24 | 25 | __fzf_history__() { 26 | local selected 27 | if [[ -v LS_LOCAL ]]; then 28 | # This command is just copied from fzf with the additional header I'll try to keep it updated... 29 | selected=$( 30 | builtin fc -lnr -2147483648 | 31 | last_hist=$(HISTTIMEFORMAT='' builtin history 1) perl -n -l0 -e 'BEGIN { getc; $/ = "\n\t"; $HISTCMD = $ENV{last_hist} + 1 } s/^[ *]//; print $HISTCMD - $. . "\t$_" if !$seen{$_}++' | 32 | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS -n2..,.. --tiebreak=index --header 'LS_LOCAL is set, querying local history. unset LS_LOCAL to resume.' --bind=ctrl-r:toggle-sort,ctrl-z:ignore $FZF_CTRL_R_OPTS +m --read0" $(__fzfcmd) --query "$READLINE_LINE" 33 | ) || return 34 | else 35 | selected=$( 36 | $HOME/.loki-shell/bin/logcli query "{job=\"shell\", host=\"$HOSTNAME\"}" --addr=$LOKI_URL --limit=50000 --batch=1000 --since=720h -o raw --quiet | _bufcmd | 37 | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS -n2..,.. --tiebreak=index --header 'ctrl-r to load ALL history, export LS_LOCAL=true for querying builtin history, export PRIVATE=true to not send commands to Loki.' --bind 'ctrl-r:reload(source $HOME/.loki-shell/shell/loki-shell.bash && _load_all)' $FZF_CTRL_R_OPTS +m " $(__fzfcmd) --query "$READLINE_LINE" 38 | ) || return 39 | fi 40 | READLINE_LINE=${selected#*$'\t'} 41 | if [ -z "$READLINE_POINT" ]; then 42 | echo "$READLINE_LINE" 43 | else 44 | READLINE_POINT=0x7fffffff 45 | fi 46 | } 47 | 48 | function _send_to_loki { 49 | if [[ -v PRIVATE ]]; then 50 | echo "PRIVATE set, not sending to loki-shell. 'unset PRIVATE' to resume." 51 | return 0 52 | fi 53 | (HISTTIMEFORMAT= builtin history 1 | sed 's/^ *\([0-9]*\)\** *//' | 54 | $HOME/.loki-shell/bin/promtail \ 55 | -config.file=$HOME/.loki-shell/config/promtail-logging-config.yaml \ 56 | --stdin -server.disable=true -log.level=error \ 57 | --client.external-labels=host=$HOSTNAME 2>&1 | logger -t loki-shell-promtail &) 58 | } 59 | [[ $PROMPT_COMMAND =~ _send_to_loki ]] || PROMPT_COMMAND="_send_to_loki;${PROMPT_COMMAND:-:}" 60 | 61 | alias hist="$HOME/.loki-shell/bin/logcli --addr=$LOKI_URL --quiet" -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # loki-shell 2 | 3 | This project is all about how to use Loki to store your shell history! 4 | 5 | This README picks up where this [article](article/article.md) left off, which covers getting started. 6 | 7 | ## What's New 8 | 9 | ### 2022/01/09 10 | 11 | * Hitting `ctrl-r` again when the search window is open will search for ALL the shell history (removes the host label from the query) searching up to the last 12 months 12 | * `export PRIVATE=true` will stop sending anything to Loki until you remove the environment variable with `unset PRIVATE` 13 | * `export LS_LOCAL=true` will query the local history instead of Loki until you remove the environment variable with `unset LS_LOCAL` 14 | 15 | NOTE: I moved from Wasabi to Google Cloud Storage because of some performance issues with Wasabi. I _think_ they were rate limiting me because the configuration I was using was loading a years worth of shell history and keeping the index files cached locally, every 5mins each cached index would be checked for changes resulting in 300+ list operations. I think Wasabi may have become tired with me running 300+ sync List operations + 300+ compactor list operations every 5 mins on a stored total of something like 1G of data. I don't blame them but there was nothing in logs/returns etc to help me understand why compaction had suddenly become very slow and so I was frustrated so I moved things to GCS and was surprised when I discovered I was looking at $50 a month in list operations. [There is an issue for improving list operations in Loki](https://github.com/grafana/loki/issues/5018) 16 | 17 | ## Installation 18 | 19 | Here are some instructions to get you set up and run Loki yourself, integrated with your shell history. 20 | 21 | This guide is meant to keep things simple, so we will run Loki locally on your computer and store all the files on the filesystem. 22 | 23 | **Note:** We will not be changing any existing behaviors around history, so **your existing shell history command and history settings will be untouched.** We are hooking command history to duplicate it to Loki via `$PROMPT_COMMAND` in Bash and `precmd` in Zsh, and on the `ctrl-r` side of things we are overloading the function fzf uses to hook the `ctrl-r` command. It is safe to try this, and if you decide you don't like it, follow the steps in the Uninstall section on the [git repo](https://github.com/slim-bean/loki-shell) to remove all traces. Your shell history will be untouched. 24 | 25 | 26 | ### Step 1: Install fzf 27 | 28 | There are several ways to install fzf, but I prefer [the git instructions](https://github.com/junegunn/fzf#using-git), which are: 29 | 30 | ```bash 31 | git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf 32 | ~/.fzf/install 33 | ``` 34 | 35 | _Say yes to all the prompted questions._ 36 | 37 | **NOTE** If you previously had fzf installed, make sure you have the key bindings enabled (make sure when you type ctrl-r, fzf pops up). You can re-run the fzf install to enable key bindings if necessary. 38 | 39 | 40 | ### Step 2: Install loki-shell 41 | 42 | Using the same model of installation as fzf, loki shell also has a git repo and install script: 43 | 44 | ```bash 45 | git clone --depth 1 https://github.com/slim-bean/loki-shell.git ~/.loki-shell 46 | ~/.loki-shell/install 47 | ``` 48 | 49 | The first thing the script is going to do is create the `~/.loki-shell` directory where all files will be kept (including Loki data). 50 | 51 | Next it will download binaries for Promtail, Logcli, and Loki. 52 | 53 | Then you will get the first question: 54 | 55 | ```none 56 | Do you want to install Loki? ([y]/n) 57 | ``` 58 | 59 | **For the best use of this tool, I highly recommend setting up Loki on a server somewhere with a cloud storage providers object storage as the backend** 60 | 61 | This way your shell history can be saved from multiple computers and object storage can help protect it for all of time. Personally I run a Raspberry Pi on my home network with Loki running (just make sure it's a 64bit OS!!) and I send my history to Google Cloud Storage (S3 would work fine too!) 62 | 63 | If you have a centralized Loki running already for loki-shell, answer `n` here. 64 | 65 | If you don't have a central Loki, the script will help you setup Loki locally, it still can be nice to run Loki locally, and you could choose to use a remote object storage to save your shell history for increased durability! 66 | 67 | There are more detailed instructutions for the prompts around installing Loki in the [original article](article/article.md#step-2-install-loki-shell) 68 | 69 | 70 | #### Shell integration 71 | 72 | Regardless of how you installed Loki, you should now see a prompt: 73 | 74 | ```none 75 | Enter the URL for your Loki server or press enter for default (http://localhost:4100) 76 | ``` 77 | 78 | If you had set up a centralized Loki, you would enter that URL here. However, for this demo we are going to use the default; you can just press enter. 79 | 80 | A lot of text will spit out explaining all the entries added to your `~.bashrc` or `~.zshrc` (or both!). 81 | 82 | That's it! 83 | 84 | ```none 85 | Finished. Restart your shell or reload config file. 86 | source ~/.bashrc # bash 87 | source ~/.zshrc # zsh 88 | ``` 89 | 90 | ## Good stuff to know 91 | 92 | When you hit `ctrl-r` the default configuration will query Loki for the last 30 days of logs for the host you are on and pass them to fzf, the line limit is 50,000 lines. 93 | 94 | If your shell history for this machine is longer than 50k lines you won't get all the results for 30 days, you will get the 50k most recent. 95 | 96 | If you hit `ctrl-r` Loki will be queried for all {job="shell"} logs with no `host` label for the past 12 months. 97 | 98 | An alias called `hist` is created which configures `logcli` to connect to loki-shell and allow you to run custom queries. 99 | 100 | If you don't need 30 days of shell history every time you hit `ctrl-r` in `~/.loki-shell/shell/loki-shell.xxsh` files change `--since=720h` to something shorter(or longer) and source the file or restart your shell. 101 | 102 | If you are using the hist alias or grafana you can get all your shell history with the label `{job="shell"}`, to get a specific host `{job="shell",host="host1"}`, it's possible to use a regex to match multiple hosts too `{job="shell",host=~"host1|host2"}` 103 | 104 | For a much more detailed list of query possibilities check out the [LogQL Guide](https://grafana.com/docs/loki/latest/logql/) 105 | 106 | ## Performance notes 107 | 108 | Fastest performance will be using a filesystem and having Loki run locally, however this is probably the least durable. 109 | 110 | I run Loki on a Raspberry Pi so I can connect to it from many machines, and the storage is in S3. This combination is not the best for performance but there are optimizations in place to help with this. 111 | 112 | In the Loki config we setup an in memory cache for chunks at 50MB, and we also set the ttl for index queries to 30 days. 113 | What this means is that once Loki has fetched the data for any query subsequent calls will not need to hit the object store and will be processed very quickly. 114 | 115 | This does mean that after a restart of Loki or if you haven't queried it in a while there might be a longer pause if it has to fetch index or chunk files from the object store. 116 | 117 | In practice this is usually manageable because logcli batches the requests and streams them to fzf, so the most recent 1000 results are available very quickly for searching while batches are fetched in the background. 118 | 119 | ## Storage Options 120 | 121 | You most likely will want to upgrade from the filesystem config to an object store in the cloud for better durability of your data and easier access. 122 | 123 | These are essentialy the same instructions for running with docker but they use a different config file with an `s3` compatible store instead. 124 | 125 | I'm using [Wasabi](https://wasabi.com/) because it was cheaper and something new to try, I don't know how good it is yet but so far it's been no problems. 126 | 127 | NOTE: This will replace the default local config created by the startup script, but you can always recreate that file by deleting `config/loki-docker-config.yaml` and re-running the install. 128 | 129 | ```bash 130 | cd ~/.loki-shell/ 131 | cp cfg-template/loki-docker-s3-config.yaml config/loki-docker-config.yaml 132 | ``` 133 | 134 | Open the file in your favorite editor and you will need these two lines with your bucket info: 135 | 136 | ```bash 137 | s3: https://ACCESS_KEY_ID:SECRET_ACCESS_KEY@s3.wasabisys.com/BUCKET_NAME 138 | region: REGION 139 | ``` 140 | 141 | Save your changes and restart Loki! 142 | 143 | ```bash 144 | docker restart loki-shell 145 | ``` 146 | 147 | Migrating existing data is possible but I need to make available a tool to do this which is currently a bit hacked together, more to come here. 148 | 149 | ## Durability 150 | 151 | Loki does not have a Write Ahead Log for in memory data yet, it's coming but it's not here yet. 152 | What this means is: if you shutdown or kill log without sending a SIGTERM first and letting it shutdown on it's own, **YOU WILL LOSE UP TO 1H OF SHELL COMMANDS** 153 | 154 | Always safely shutdown the process. 155 | 156 | Or you can `curl http://localhost:4100/flush` to manually force a flush of all streams in memory before shutting down. 157 | 158 | If you want an even more durable setup consider running two Loki instances against the same s3 bucket and configuring promtail to send to both: 159 | 160 | ```yaml 161 | clients: 162 | - url: http://localhost:4100/loki/api/v1/push # Make sure this port matches your Loki http port 163 | backoff_config: 164 | max_period: 5s 165 | max_retries: 3 166 | - url: https://some.other.host:4100/loki/api/v1/push 167 | backoff_config: 168 | max_period: 5s 169 | max_retries: 3 170 | ``` 171 | 172 | Please note the short retry times and period, this is to keep the promtail processes running for a short time in the background. If your network or Loki instances are down promtail will give up rather quickly, 15s at most, before abandoning your shell commands. 173 | You can increase these timeouts just be aware if a remote endpoint is slow or unavailable the promtail process will stay running in the background trying to send logs until it times out, you could end up with a lot of them if you keep entering commands. 174 | 175 | This _does_ result in double the data in the object store however Loki will handle and de-duplicate this data at query time. All of this increases processing time, storage, costs etc but is how I run my setup. 176 | 177 | ## Troubleshooting 178 | 179 | Failures to send to loki via the promtail instances are sent to the system log via the `logger` command, search your system log for the tag `loki-shell-promtail`. 180 | 181 | Loki failures and issues should be visible in the loki log file either with `docker logs loki-shell` or wherever systemd logs depending on how you ran Loki. 182 | 183 | If `ctrl-r` doesn't produce any results, you can test the command used in the history function manually: 184 | 185 | ``` 186 | $HOME/.loki-shell/bin/logcli query "{job=\"shell\", host=\"$HOSTNAME\"}" --addr=$LOKI_URL --limit=50000 --batch=1000 --since=720h -o raw --quiet 187 | ``` 188 | 189 | ## Uninstalling 190 | 191 | An uninstall script is included: 192 | 193 | ``` 194 | ~/.loki-shell/uninstall 195 | ``` 196 | 197 | 198 | ## Updates 199 | 200 | If you installed fzf the git install method always gets you the most recent version and also makes updates as simple as: 201 | 202 | ```bash 203 | cd ~/.fzf 204 | git pull 205 | ./install 206 | ``` 207 | 208 | Similarly loki-shell can be updated: 209 | 210 | ```bash 211 | cd ~/.loki-shell 212 | git pull 213 | ./install 214 | ``` 215 | 216 | Note, config files for Loki and Promtail are in the `~/.loki-shell/config` directory, the initial install copies them from `~/.loki-shell/cfg-template` 217 | 218 | Running install again to update will not replace these files once copied so you may want to manually diff any changes in `cfg-template` against the files in `config` 219 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2020 slim-bean 190 | 191 | Copyright 2013-2020 Junegunn Choi install 192 | 193 | This project is licensed under the Apache License, Version 2.0 with the 194 | exception of `index` file which is licesned under the MIT license. 195 | 196 | 197 | Licensed under the Apache License, Version 2.0 (the "License"); 198 | you may not use this file except in compliance with the License. 199 | You may obtain a copy of the License at 200 | 201 | http://www.apache.org/licenses/LICENSE-2.0 202 | 203 | Unless required by applicable law or agreed to in writing, software 204 | distributed under the License is distributed on an "AS IS" BASIS, 205 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 206 | See the License for the specific language governing permissions and 207 | limitations under the License. 208 | -------------------------------------------------------------------------------- /article/article.md: -------------------------------------------------------------------------------- 1 | # Level up your shell history with Loki and fzf 2 | 3 | [Loki](https://github.com/grafana/loki) is an Apache 2.0 licensed open source log aggregation framework designed by Grafana Labs and built with tremendous support from a growing community -- as well as the project I work on every day. My goal with this article is to provide an introduction to Loki in a hands-on way. I thought rather than just talking about how Loki works, a concrete use case that solves some real problems would be much more engaging! A special thanks to my colleague Jack Baldry for planting the seed for this idea. I had the Loki knowledge to make this happen, but if it weren’t for his suggestion, I don’t think we ever would have made it here. 4 | 5 | ## The problem: Durable centralized shell history 6 | 7 | I love my shell history and have always been a fanatical _ctrl-r_ user. About a year ago my terminal life was forever changed when my peer Dieter Plaetinck introduced me to the command line fuzzy finder, fzf. 8 | 9 | Suddenly my searching through commands went from this: 10 | 11 | ![before](assets/before.gif) 12 | 13 | To this: 14 | 15 | ![after](assets/with_fzf.gif) 16 | 17 | fzf has significantly improved my quality of life, but there are still some missing pieces around my shell history: 18 | 19 | * Losing shell history when terminals close abruptly, computers crash, computers die, whole disk encryption keys are forgotten... 20 | * Access to my shell history _from_ all my computers _on_ all my computers. 21 | 22 | I think of my shell history as documentation: It's an important story I don't want to lose. Combining Loki with my shell history will help solve these problems and more! 23 | 24 | ## Background on Loki 25 | 26 | Loki is built to take the very successful and intuitive label model used by the open source [Prometheus](https://prometheus.io/) project and expand it into the world of log aggregation. This enables developers and operators to seamlessly pivot between their metrics and logs by using the same set of labels. Not using Prometheus? No worries, there are still plenty of reasons why Loki might be a good fit for your log storage needs: 27 | 28 | * Low overhead: Loki does NOT do full text log indexing; it only creates an index of the labels you put on your logs. Keeping a small index substantially reduces the operating requirements of Loki. I'm running my loki-shell project on a Raspberry Pi using just a little over 50MB of memory. 29 | * Low cost: The log content is compressed and stored in object stores like S3, GCS, Azure Blob, or even directly on a filesystem. The goal is to use storage that is inexpensive and durable. 30 | * Flexibility: Loki is available in a single binary to be downloaded and run directly. It is also provided as a Docker image to run in any container environment. A Helm chart is available to get started quickly in Kubernetes. If you really demand a lot from your logging tools, look at the [production setup running at Grafana Labs](https://grafana.com/docs/loki/latest/installation/tanka/). [Jsonnet](https://jsonnet.org) and the open source [Tanka](https://tanka.dev/) are used to deploy that same Loki image as discrete building blocks enabling massive horizontal scaling, high availability, replication, separate scaling of read and write paths, highly parallelizable querying, and more. 31 | 32 | In summary, Loki takes the approach of keeping a small index of metadata about your logs (labels) and storing log content itself unindexed and compressed in inexpensive object stores to make operating easier and cheaper. The application is built to run as a single process and easily evolve into a highly available distributed system. High query performance can be obtained on larger logging workloads through parallelization and sharding of queries -- a bit like MapReduce for your logs. 33 | 34 | The best part? All of this functionality is available for anyone to use, for free, today. Following in the footsteps and success of Grafana, Grafana Labs is committed to making Loki a fully featured, fully open log aggregation software anyone can use. 35 | 36 | ## The solution 37 | 38 | I'm now running Loki on a Raspberry Pi on my home network, which is storing my shell history offsite in an S3 bucket. 39 | 40 | When I hit `ctrl-r` [Logcli] is used to make several batching requests that are streamed into fzf. Here is an example with the top showing the logs of the Loki server on the Pi. 41 | 42 | ![example](assets/example_logcli.gif) 43 | 44 | Ready to give it a try? 45 | 46 | Here are some instructions to get you set up and run Loki yourself, integrated with your shell history. 47 | 48 | This guide is meant to keep things simple, so we will run Loki locally on your computer and store all the files on the filesystem. 49 | 50 | All of this information as well as ways you can set up a more elaborate installation can be found here: https://github.com/slim-bean/loki-shell 51 | 52 | **Note:** We will not be changing any existing behaviors around history, so **your existing shell history command and history settings will be untouched.** We are hooking command history to duplicate it to Loki via `$PROMPT_COMMAND` in Bash and `precmd` in Zsh, and on the `ctrl-r` side of things we are overloading the function fzf uses to hook the `ctrl-r` command. It is safe to try this, and if you decide you don't like it, follow the steps in the Uninstall section on the [git repo](https://github.com/slim-bean/loki-shell) to remove all traces. Your shell history will be untouched. 53 | 54 | 55 | ### Step 1: Install fzf 56 | 57 | There are several ways to install fzf, but I prefer [the git instructions](https://github.com/junegunn/fzf#using-git), which are: 58 | 59 | ```bash 60 | git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf 61 | ~/.fzf/install 62 | ``` 63 | 64 | _Say yes to all the prompted questions._ 65 | 66 | **NOTE** If you previously had fzf installed, make sure you have the key bindings enabled (make sure when you type ctrl-r, fzf pops up). You can re-run the fzf install to enable key bindings if necessary. 67 | 68 | 69 | ### Step 2: Install loki-shell 70 | 71 | Using the same model of installation as fzf, loki shell also has a git repo and install script: 72 | 73 | ```bash 74 | git clone --depth 1 https://github.com/slim-bean/loki-shell.git ~/.loki-shell 75 | ~/.loki-shell/install 76 | ``` 77 | 78 | The first thing the script is going to do is create the `~/.loki-shell` directory where all files will be kept (including Loki data). 79 | 80 | Next it will download binaries for Promtail, Logcli, and Loki. 81 | 82 | Then you will get the first question: 83 | 84 | ```none 85 | Do you want to install Loki? ([y]/n) 86 | ``` 87 | 88 | If you have a centralized Loki running already for loki-shell, you could answer `n` here; however, we want to answer `y` or press enter. 89 | 90 | There are two options available for running Loki locally, as a docker image (recommended) or as the single binary (with support for adding a systemd service). 91 | 92 | I recommend using Docker if it's available, as I think it simplifies operations a bit, but both work just fine! 93 | 94 | #### Docker 95 | 96 | ```none 97 | [y] to run Loki in Docker, [n] to run Loki as a binary ([y]/n) y 98 | Error: No such object: loki-shell 99 | Error response from daemon: No such container: loki-shell 100 | Error: No such container: loki-shell 101 | 54843ff3392f198f5cac51a6a5071036f67842bbc23452de8c3efa392c0c2e1e 102 | ``` 103 | 104 | If this is the first time running the install, you can disregard the Error messages. This script will stop and replace a running Loki container if the version does not match, allowing you to re-run this script to upgrade Loki. 105 | 106 | This is it! Loki is now running as a Docker container. 107 | 108 | Data from Loki will be stored in `~/.loki-shell/data`. 109 | 110 | The image is run with `--restart=unless-stopped` so it will restart at reboot but will stay stopped if you run `docker stop loki-shell`. 111 | 112 | You can skip to _Shell integration_. 113 | 114 | #### Binary 115 | 116 | There are many ways to run a binary on a Linux system. This script can install a systemd service. If you don't have systemd you can still use the binary install: 117 | 118 | ```none 119 | [y] to run Loki in Docker, [n] to run Loki as a binary ([y]/n) n 120 | 121 | Run Loki with systemd? ([y]/n) n 122 | 123 | This is as far as this script can take you 124 | You will need to setup an auto-start for Loki 125 | It can be run with this command: /home/username/.loki-shell/bin/loki -config.file=/home/username/.loki-shell/config/loki-binary-config.yaml 126 | ``` 127 | 128 | The script will spit out the command you need to use to run Loki, and you will be on your own to set up an init script or another method of auto-starting it. 129 | 130 | You can just run the command directly if you want, and run Loki from your current shell! 131 | 132 | If you _DO_ have systemd, you have the option of letting the script install the systemd service or showing you the commands to run yourself: 133 | 134 | ```none 135 | Run Loki with systemd? ([y]/n) y 136 | 137 | Installing the systemd service requires root permissions. 138 | [y] to run these commands with sudo [n] to print out the commands and you can run them yourself. ([y]/n) n 139 | sudo cp /home/ed/.loki-shell/config/loki-shell.service /etc/systemd/system/loki-shell.service 140 | sudo systemctl daemon-reload 141 | sudo systemctl enable loki-shell 142 | sudo systemctl start loki-shell 143 | Copy these commands and run them when the script finishes. (press enter to continue) 144 | ``` 145 | 146 | #### Shell integration 147 | 148 | Regardless of how you installed Loki, you should now see a prompt: 149 | 150 | ```none 151 | Enter the URL for your Loki server or press enter for default (http://localhost:4100) 152 | ``` 153 | 154 | If you had set up a centralized Loki, you would enter that URL here. However, for this demo we are going to use the default; you can just press enter. 155 | 156 | A lot of text will spit out explaining all the entries added to your `~.bashrc` or `~.zshrc` (or both!). 157 | 158 | That's it! 159 | 160 | ```none 161 | Finished. Restart your shell or reload config file. 162 | source ~/.bashrc # bash 163 | source ~/.zshrc # zsh 164 | ``` 165 | 166 | ### Step 3: Try it out! 167 | 168 | Start using your shell and use `ctrl-r` to see your commands show up. 169 | 170 | Open multiple terminal windows, type a command in one and `ctrl-r` in another, and see your commands available immediately. 171 | 172 | Also notice that when you switch between terminals and enter commands, they are available immediately by `ctrl-r`, but the operation of the up-arrow is not affected between terminals! (This may not be true with oh-my-zsh installed, which automatically appends all commands to the history.) 173 | 174 | Use `ctrl-r` multiple times to toggle between sorting by time and relevance. 175 | 176 | **Note:** The configuration applied here will only show the query history for the current host even if you are sending shell data from multiple hosts to Loki. I think by default this makes the most sense. There is a lot you can tweak here if you would like this behavior to change; see the [repo](https://github.com/slim-bean/loki-shell) to learn more. 177 | 178 | 179 | Also installed is an alias called `hist`: 180 | 181 | ```bash 182 | alias hist="$HOME/.loki-shell/bin/logcli --addr=$LOKI_URL" 183 | ``` 184 | 185 | LogCLI can be used to query and search your history directly in Loki, allowing you to search other hosts. 186 | 187 | Check out the [getting started guide for LogCLI](https://grafana.com/docs/loki/latest/getting-started/logcli/) to learn more about querying. 188 | 189 | LogQL metric queries can let you do some interesting things like see how many times I issued the `kc` command (my alias for kubectl) in the last 30 days: 190 | 191 | ![count](assets/count_kc.png) 192 | 193 | ## Extra credit 194 | 195 | Install Grafana and play around with your shell history. 196 | 197 | ```bash 198 | docker run -d -p 3000:3000 --name=grafana grafana/grafana 199 | ``` 200 | 201 | Open up a web browser at `http://localhost:3000` login using the default admin/admin user and password. 202 | 203 | On the left, navigate to `Configuration -> Datasources`, click the `Add Datasource` button, and select `Loki`. 204 | 205 | For the url you should be able to use `http://localhost:4100` (however on my WSL2 machine I had to use the IP of the computer itself). 206 | 207 | Click `Save and Test`. You should see `Data source connected and labels found.` 208 | 209 | Click on the `Explore` icon on the left, make sure the `Loki` datasource is selected, and try out a query: `{job="shell"}`. 210 | 211 | When you have more hosts sending shell commands, you can limit the results to a certain host using the `hostname` label that is being added: `{job="shell", hostname="myhost"}`. 212 | 213 | You can also look for specific commands with filter expressions `{job="shell"} |= "docker"`. 214 | 215 | Or you can start exploring the world of metrics from logs `rate({job="shell"}[1m])` to see how often you are using your shell: 216 | 217 | ![last_20_days](assets/last_20.png) 218 | 219 | Want to reconstruct a timeline from an incident? Filter by a specific command and see when it was run: 220 | 221 | ![command_history](assets/command_hist.png) 222 | 223 | To see what else you can do and learn more about Loki's query language, [check out this LogQL guide](https://grafana.com/docs/loki/latest/logql/). 224 | 225 | ## Final thoughts 226 | 227 | For more ideas, troubleshooting, and updates, follow the [git repo](https://github.com/slim-bean/loki-shell). This is still a work in progress, so please report any issues there as well. 228 | 229 | To learn more about Loki, check out [the documentation](https://grafana.com/docs/loki/latest/), [blog posts](https://grafana.com/categories/loki/), and [git repo](https://github.com/grafana/loki), or try it in [Grafana Cloud](https://grafana.com/products/cloud/). 230 | 231 | -------------------------------------------------------------------------------- /install: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Note this file was copied from https://github.com/junegunn/fzf/blob/master/install 4 | # and modified for this project but it's original license and copyright 5 | # are still credited to the original author. 6 | 7 | # The MIT License (MIT) 8 | # 9 | # Copyright (c) 2013-2020 Junegunn Choi 10 | # Copyright (c) 2020 slim-bean 11 | # 12 | # Permission is hereby granted, free of charge, to any person obtaining a copy 13 | # of this software and associated documentation files (the "Software"), to deal 14 | # in the Software without restriction, including without limitation the rights 15 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | # copies of the Software, and to permit persons to whom the Software is 17 | # furnished to do so, subject to the following conditions: 18 | # 19 | # The above copyright notice and this permission notice shall be included in 20 | # all copies or substantial portions of the Software. 21 | # 22 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 28 | # THE SOFTWARE. 29 | 30 | set -u 31 | 32 | version=2.6.1 33 | auto_completion= 34 | update_config=2 35 | binary_arch= 36 | shells="bash zsh" 37 | prefix='~/.loki-shell' 38 | prefix_expand=~/.loki-shell 39 | fish_dir=${XDG_CONFIG_HOME:-$HOME/.config}/fish 40 | docker_tag="grafana/loki:$version" 41 | binary_only=0 42 | 43 | help() { 44 | cat << EOF 45 | usage: $0 [OPTIONS] 46 | 47 | --help Show this message 48 | --bin Download binaries only 49 | --[no-]key-bindings Enable/disable key bindings (CTRL-T, CTRL-R, ALT-C) 50 | --[no-]completion Enable/disable fuzzy completion (bash & zsh) 51 | --[no-]update-rc Whether or not to update shell configuration files 52 | 53 | --no-bash Do not set up bash configuration 54 | --no-zsh Do not set up zsh configuration 55 | --no-fish Do not set up fish configuration 56 | 57 | --docker-tag Specify an exact docker image to use for Loki e.g. --docker-tag=grafana/loki:1.6.1 58 | EOF 59 | } 60 | 61 | while test $# -gt 0; do 62 | case $1 in 63 | --help) 64 | help 65 | exit 0 66 | ;; 67 | --update-rc) 68 | update_config=1 69 | shift 70 | ;; 71 | --no-update-rc) 72 | update_config=0 73 | shift 74 | ;; 75 | --bin) 76 | binary_only=1 77 | shift 78 | ;; 79 | --no-bash) 80 | shells=${shells/bash/} 81 | shift 82 | ;; 83 | --no-zsh) 84 | shells=${shells/zsh/} 85 | shift 86 | ;; 87 | --no-fish) 88 | shells=${shells/fish/} 89 | shift 90 | ;; 91 | --docker-tag*) 92 | docker_tag=$(echo $1 | sed -e 's/^[^=]*=//g') 93 | shift 94 | ;; 95 | *) 96 | echo "unknown option: $opt" 97 | help 98 | exit 1 99 | ;; 100 | esac 101 | done 102 | 103 | cd "$(dirname "${BASH_SOURCE[0]}")" 104 | loki_shell_base=$(pwd) 105 | loki_shell_base_esc=$(printf %q "$loki_shell_base") 106 | 107 | # make some directories 108 | mkdir -p "$loki_shell_base"/data 109 | mkdir -p "$loki_shell_base"/config 110 | mkdir -p "$loki_shell_base"/bin 111 | 112 | # check for stdbuf 113 | if ! hash stdbuf 2>/dev/null; then 114 | if ! hash gstdbuf 2>/dev/null; then 115 | echo 'loki-shell requires either stdbuf or gstdbuf to be installed and on the path' 116 | echo 'install the coreutils package and re-run this install script (brew install coreutils)' 117 | exit 1 118 | fi 119 | fi 120 | 121 | ask() { 122 | while true; do 123 | read -p "$1 ([y]/n) " -r 124 | REPLY=${REPLY:-"y"} 125 | if [[ $REPLY =~ ^[Yy]$ ]]; then 126 | return 1 127 | elif [[ $REPLY =~ ^[Nn]$ ]]; then 128 | return 0 129 | fi 130 | done 131 | } 132 | 133 | prompt() { 134 | read -p "$1 (press enter to continue) " -r 135 | } 136 | 137 | prompt_string() { 138 | read -p "$1 ($2) " -r 139 | REPLY=${REPLY:-"$2"} 140 | echo $REPLY 141 | } 142 | 143 | check_binary() { 144 | echo -n " - Checking ${1} executable ... " 145 | local output 146 | output=$("$loki_shell_base"/bin/${1} --version 2>&1) 147 | if [ $? -ne 0 ]; then 148 | echo "Error: $output" 149 | binary_error="Invalid binary" 150 | else 151 | if [[ $output != *"$version"* ]]; then 152 | echo "output did not contain correct version $version: $output" 153 | binary_error="Invalid version" 154 | else 155 | echo "$output" 156 | binary_error="" 157 | return 0 158 | fi 159 | fi 160 | rm -f "$loki_shell_base"/bin/${1} 161 | return 1 162 | } 163 | 164 | try_curl() { 165 | command -v curl > /dev/null && 166 | if [[ $1 =~ tgz$ ]]; then 167 | curl -fL $1 | tar -xzf - 168 | else 169 | local temp=${TMPDIR:-/tmp}/temp.zip 170 | curl -fLo "$temp" $1 && unzip -o "$temp" && rm -f "$temp" 171 | fi 172 | } 173 | 174 | try_wget() { 175 | command -v wget > /dev/null && 176 | if [[ $1 =~ tgz$ ]]; then 177 | wget -O - $1 | tar -xzf - 178 | else 179 | local temp=${TMPDIR:-/tmp}/${1}.zip 180 | wget -O "$temp" $1 && unzip -o "$temp" && rm -f "$temp" 181 | fi 182 | } 183 | 184 | download() { 185 | echo "Downloading ${1}" 186 | 187 | if [ -x "$loki_shell_base"/bin/"${1}" ]; then 188 | echo " - Already exists" 189 | check_binary ${1} && return 190 | fi 191 | 192 | mkdir -p "$loki_shell_base"/bin && cd "$loki_shell_base"/bin 193 | if [ $? -ne 0 ]; then 194 | binary_error="Failed to create bin directory" 195 | return 196 | fi 197 | 198 | local url 199 | url=https://github.com/grafana/loki/releases/download/v$version/${2} 200 | 201 | set -o pipefail 202 | if ! (try_curl $url || try_wget $url); then 203 | set +o pipefail 204 | binary_error="Failed to download with curl and wget" 205 | return 206 | fi 207 | set +o pipefail 208 | 209 | mv *${1}* ${1} 210 | 211 | if [ ! -f ${1} ]; then 212 | binary_error="Failed to download ${1}" 213 | return 214 | fi 215 | 216 | chmod +x ${1} && check_binary ${1} 217 | } 218 | 219 | # Try to download binary executable 220 | archi=$(uname -sm) 221 | binary_available=1 222 | binary_error="" 223 | case "$archi" in 224 | Darwin\ *64) 225 | download promtail promtail-darwin-${binary_arch:-amd64}.zip 226 | download logcli logcli-darwin-${binary_arch:-amd64}.zip 227 | download loki loki-darwin-${binary_arch:-amd64}.zip 228 | ;; 229 | Linux\ armv7*) 230 | download promtail promtail-linux-${binary_arch:-arm}.zip 231 | download logcli logcli-linux-${binary_arch:-arm}.zip 232 | download loki loki-linux-${binary_arch:-arm}.zip 233 | ;; 234 | Linux\ armv8*) 235 | download promtail promtail-linux-${binary_arch:-arm64}.zip 236 | download logcli logcli-linux-${binary_arch:-arm64}.zip 237 | download loki loki-linux-${binary_arch:-arm64}.zip 238 | ;; 239 | Linux\ aarch64*) 240 | download promtail promtail-linux-${binary_arch:-arm64}.zip 241 | download logcli logcli-linux-${binary_arch:-arm64}.zip 242 | download loki loki-linux-${binary_arch:-arm64}.zip 243 | ;; 244 | Linux\ *64) 245 | download promtail promtail-linux-${binary_arch:-amd64}.zip 246 | download logcli logcli-linux-${binary_arch:-amd64}.zip 247 | download loki loki-linux-${binary_arch:-amd64}.zip 248 | ;; 249 | OpenBSD\ *64) 250 | download promtail promtail-freebsd-${binary_arch:-amd64}.zip 251 | download logcli logcli-freebsd-${binary_arch:-amd64}.zip 252 | download loki loki-freebsd-${binary_arch:-amd64}.zip 253 | ;; 254 | *) binary_available=0 binary_error=1 ;; 255 | esac 256 | 257 | [[ ${binary_only} -eq 1 ]] && exit 0 258 | 259 | for s in $shells; do 260 | if ! command -v "$s" > /dev/null; then 261 | shells=${shells/$s/} 262 | fi 263 | done 264 | 265 | if [[ ${#shells} -lt 3 ]]; then 266 | echo "No shell configuration to be updated." 267 | exit 0 268 | fi 269 | 270 | append_line() { 271 | set -e 272 | 273 | local update line file pat lno 274 | update="$1" 275 | line="$2" 276 | file="$3" 277 | pat="${4:-}" 278 | lno="" 279 | 280 | echo "Update $file:" 281 | echo " - $line" 282 | if [ -f "$file" ]; then 283 | if [ $# -lt 4 ]; then 284 | lno=$(\grep -nF "$line" "$file" | sed 's/:.*//' | tr '\n' ' ') 285 | else 286 | lno=$(\grep -nF "$pat" "$file" | sed 's/:.*//' | tr '\n' ' ') 287 | fi 288 | fi 289 | if [ -n "$lno" ]; then 290 | echo " - Already exists: line #$lno" 291 | else 292 | if [ $update -eq 1 ]; then 293 | [ -f "$file" ] && echo >> "$file" 294 | echo "$line" >> "$file" 295 | echo " + Added" 296 | else 297 | echo " ~ Skipped" 298 | fi 299 | fi 300 | echo 301 | set +e 302 | } 303 | 304 | # Install Loki 305 | 306 | echo 307 | ask "Do you want to install Loki?" 308 | install_loki=$? 309 | 310 | if [ $install_loki -eq 1 ]; then 311 | ## Ask docker or binary 312 | echo 313 | ask "[y] to run Loki in Docker, [n] to run Loki as a binary" 314 | docker=$? 315 | if [ $docker -eq 1 ]; then 316 | if [ -f "$loki_shell_base"/config/loki-docker-config.yaml ]; then 317 | echo 318 | echo "Existing config file found at $loki_shell_base/.loki-shell/config/loki-docker-config.yaml" 319 | prompt "File will not be modified" 320 | else 321 | cp "$loki_shell_base"/cfg-template/loki-docker-local-config.yaml "$loki_shell_base"/config/loki-docker-config.yaml 322 | fi 323 | 324 | docker inspect loki-shell | grep -q "$docker_tag" 325 | if [ $? -ne 0 ]; then 326 | docker stop loki-shell || true 327 | docker rm loki-shell || true 328 | docker run -d --restart=unless-stopped --name=loki-shell --user "$(id -u)":"$(id -g)" --mount type=bind,source="$loki_shell_base"/config/loki-docker-config.yaml,target=/etc/loki/local-config.yaml --mount type=bind,source="$loki_shell_base"/data,target=/loki -p 4100:4100 "$docker_tag" 329 | else 330 | echo "loki-shell docker image already running at correct version" 331 | fi 332 | else 333 | if [ -f "$loki_shell_base"/config/loki-binary-config.yaml ]; then 334 | echo 335 | echo "Existing config file found at $loki_shell_base/.loki-shell/config/loki-binary-config.yaml" 336 | prompt "File will not be modified" 337 | else 338 | cp "$loki_shell_base"/cfg-template/loki-local-config.yaml "$loki_shell_base"/config/loki-binary-config.yaml 339 | sed -i.bak "s|FIXME|$loki_shell_base|g" "$loki_shell_base"/config/loki-binary-config.yaml 340 | fi 341 | echo 342 | ask "Run Loki with systemd?" 343 | systemd=$? 344 | if [ $systemd -eq 1 ]; then 345 | cp "$loki_shell_base"/cfg-template/loki-shell.service "$loki_shell_base"/config/loki-shell.service 346 | sed -i.bak "s|FIXME|$loki_shell_base|g" "$loki_shell_base"/config/loki-shell.service 347 | sed -i.bak "s|USER|$USER|g" "$loki_shell_base"/config/loki-shell.service 348 | echo 349 | echo "Installing the systemd service requires root permissions." 350 | ask "[y] to run these commands with sudo [n] to print out the commands and you can run them yourself." 351 | run_sudo_cmds=$? 352 | if [ $run_sudo_cmds -eq 1 ]; then 353 | sudo cp $loki_shell_base/config/loki-shell.service /etc/systemd/system/loki-shell.service 354 | sudo systemctl daemon-reload 355 | sudo systemctl enable loki-shell 356 | sudo systemctl start loki-shell 357 | else 358 | echo "sudo cp $loki_shell_base/config/loki-shell.service /etc/systemd/system/loki-shell.service" 359 | echo "sudo systemctl daemon-reload" 360 | echo "sudo systemctl enable loki-shell" 361 | echo "sudo systemctl start loki-shell" 362 | prompt "Copy these commands and run them when the script finishes." 363 | fi 364 | else 365 | echo 366 | echo "This is as far as this script can take you" 367 | echo "You will need to setup an auto-start for Loki" 368 | echo "It can be run with this command: $loki_shell_base/bin/loki -config.file=$loki_shell_base/config/loki-binary-config.yaml" 369 | prompt "" 370 | fi 371 | fi 372 | fi 373 | 374 | 375 | 376 | if [ $update_config -eq 2 ]; then 377 | # The script used to ask if you wanted to update shell files, I removed this so set this var to the value of "yes" 378 | update_config=1 379 | 380 | default_host="http://localhost:4100" 381 | echo 382 | loki_host=$(prompt_string "Enter the URL for your Loki server or press enter for default" "$default_host") 383 | if [ -f "$loki_shell_base"/config/promtail-logging-config.yaml ]; then 384 | echo "Existing promtail config file found at $loki_shell_base/.loki-shell/config/promtail-logging-config.yaml" 385 | prompt "File will not be modified" 386 | else 387 | cp "$loki_shell_base"/cfg-template/promtail-logging-config.yaml "$loki_shell_base"/config/promtail-logging-config.yaml 388 | sed -i.bak "s|HOST|$loki_host|g" "$loki_shell_base"/config/promtail-logging-config.yaml 389 | fi 390 | fi 391 | echo 392 | for shell in $shells; do 393 | [ $shell = zsh ] && dest=${ZDOTDIR:-~}/.zshrc || dest=~/.bashrc 394 | append_line $update_config "############## BEGIN LOKI-SHELL #####################" "$dest" 395 | append_line $update_config "# NOTE when changing the Loki URL, also remember to change the promtail config: ~/.loki-shell/config/promtail-logging-config.yaml" "$dest" 396 | append_line $update_config "export LOKI_URL=\"$loki_host\"" "$dest" "export LOKI_URL" 397 | append_line $update_config "[ -f ${prefix}/shell/loki-shell.${shell} ] && source ${prefix}/shell/loki-shell.${shell}" "$dest" "${prefix}/shell/loki-shell.${shell}" 398 | append_line $update_config "############## END LOKI-SHELL #####################" "$dest" 399 | done 400 | 401 | 402 | if [ $update_config -eq 1 ]; then 403 | echo 'Finished. Restart your shell or reload config file.' 404 | if [[ "$shells" =~ bash ]]; then 405 | echo -n ' source ~/.bashrc # bash' 406 | [[ "$archi" =~ Darwin ]] && echo -n ' (.bashrc should be loaded from .bash_profile)' 407 | echo 408 | fi 409 | [[ "$shells" =~ zsh ]] && echo " source ${ZDOTDIR:-~}/.zshrc # zsh" 410 | echo 411 | echo 'Use uninstall script to remove loki-shell.' 412 | echo 413 | fi 414 | --------------------------------------------------------------------------------